caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Fabrice Le Fessant <fabrice.le_fessant@inria.fr>
To: Chris Hecker <checker@d6.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] dynamically loading C functions
Date: Fri, 9 Mar 2001 11:54:17 +0100 (CET)	[thread overview]
Message-ID: <15016.46681.967782.380481@cremant.inria.fr> (raw)
In-Reply-To: <4.3.2.7.2.20010308010305.00cd4140@shell16.ba.best.com>


I've updated the dlopen ocaml port:
http://pauillac.inria.fr/~lefessan/src/dlopen/

It now works on 4 systems: x86, alpha, sparc and PPC.

It can use either printf formats or type combinators to specify
returned functions types. 

Type combinators must be use if you want to pass structs to C
functions (encoded by tuples in Caml), or get side-effects.

For example, it test.c defines:

struct tt {
  char field1;
  int  field2;
  char field3;
  double field4;
};

int set_tuple(struct tt* t)
{
  printf("%c - %d - %c - %f\n", t->field1, t->field2, t->field3, t->field4);
  t->field2 = 24;
  return 0;
}

You can use the following code:

# open Dlopen;; 
# let dll = dlopen "./test.so"  [RTLD_NOW];;
val dll : Dlopen.dll = <abstr>

# let struct_t =  record4_t (char_t, mutable_t int_t, char_t, float_t);;
val struct_t : (char * int ref * char * float) Dlopen.meta = <abstr>

# let set_tuple = dlsym_t dll "set_tuple" (
    star_t (mutable_t struct_t) @-> int_t);;
val set_tuple : (char * int ref * char * float) ref option -> int = <fun>

# let x =ref 1;;
val x : int ref = {contents=1}
# let y = ref ('a',x,'b',2.0);;
val y : (char * int ref * char * float) ref =
  {contents='a', {contents=1}, 'b', 2}

# set_tuple (Some y);;
a - 1 - b - 2.000000
- : int = 0

# x,y;;
- : int ref * (char * int ref * char * float) ref =
{contents=24}, {contents='a', {contents=24}, 'b', 2}

where both the references x and y capture the side-effect of the
function on the record (note that sharing is not preserved, ie in the
final result, 
   {contents=24}, {contents='a', {contents=24}, 'b', 2}
       ^                                ^
       ^-------------- != --------------^

Be careful to exactly encode the C arguments format in the Caml type
combinators (in particular, pointers are encoded by star_t as in the
example).

Regards,

- Fabrice
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


      reply	other threads:[~2001-03-09 10:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-05 20:28 Chris Hecker
2001-03-05 21:24 ` Marcin 'Qrczak' Kowalczyk
2001-03-06  0:10   ` Chris Hecker
2001-03-06  0:55     ` Chris Hecker
2001-03-06 16:48       ` Marcin 'Qrczak' Kowalczyk
2001-03-06 18:02         ` Chris Hecker
2001-03-08  8:22           ` Fabrice Le Fessant
2001-03-08  9:34             ` Chris Hecker
2001-03-09 10:54               ` Fabrice Le Fessant [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15016.46681.967782.380481@cremant.inria.fr \
    --to=fabrice.le_fessant@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).