caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Joel Stanley <jstanley@galois.com>
To: damien.doligez@inria.fr
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Using the C FFI to wrap an OCaml library
Date: Fri, 08 Feb 2008 13:53:34 -0800 (PST)	[thread overview]
Message-ID: <20080208.135334.126881887.jstanley@galois.com> (raw)
In-Reply-To: <24097E85-C08E-4547-986B-4EF91B7692C3@inria.fr>

>>>>> Damien Doligez writes:

>> Looking at the macro expansions, I'm suspicious about the safety between
>> C functions,

> Could you elaborate on what makes you say that?  The macros carefully
> implement a stack discipline designed for nested calls.

Well, much of my curiosity stems from the fact that I know little about
the OCaml runtime or how the OCaml GC is invoked.  If the GC can ever be
invoked in its own thread, it seems like while the values (of type
'value') are being passed back on the stack (but *after* the
caml__local_roots pointer has been reverted) there could be problems?

If this can't ever happen, great! Why not?

>> 2. As a follow-up question to #1, the OCaml values may need to be
>> carried across yet another FFI, in this case, C <-> Poly/ML .  Even if
>> using the macros was a safe way to carry values between C functions, I"m
>> not sure I can easily replicate the macros on the other side of the FFI,
>> so am wondering if an explicit memory management approach using
>> caml_register_global_root will work.  E.g.,
>> 
>> value* alloc_value() {
>> value* p = malloc(sizeof(value));
>> caml_register_global_root(p);
>> return p;
>> }

> In order to make this work, you have to explain to Poly/ML that every
> access to the value must be done through the value*...I think it's
> more reasonable to just copy the data between worlds instead of trying
> to share pointers between OCaml and Poly/ML.

So let's talk about a concrete example here.  Let's say that I have some
dynamically-allocated object on the OCaml heap (for discussion purposes,
say it's an instance of some simple 'stack' class) and that I've
returned a reference to that object to a C function.  Are you saying
that the stack contents (e.g., a deep copy of my object) themselves are
marshaled across this boundary every time?

What about, perhaps, having an object registration mechanism on the
OCaml so that a newly-allocated object's reference goes into a (global)
hash table, and an opaque handle (e.g., some fresh, unique integer
value) is yielded back to C land?  This would require an explicit
deallocation (just passing the handle back) to come from the C side, but
should keep the heap object around until such a deallocation call is
made.  Right?

Basically, the problem is that I have a large OCaml library that
constructs a variety of different types of objects and I would like a
1-1 correspondence between those objects and Poly/ML values that wrap
corresponding C volatiles.  I'd like to do this in the most
straightforward and safe way possible.

Thanks for your help,
Joel


  reply	other threads:[~2008-02-08 21:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-06 23:54 Joel Stanley
2008-02-07 22:26 ` [Caml-list] " Damien Doligez
2008-02-08 21:53   ` Joel Stanley [this message]
2008-02-09  4:07     ` Jonathan Bryant
2008-02-12 21:58       ` Damien Doligez

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=20080208.135334.126881887.jstanley@galois.com \
    --to=jstanley@galois.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=damien.doligez@inria.fr \
    /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).