caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Joel Stanley <jstanley@galois.com>
To: caml-list@yquem.inria.fr
Subject: Using the C FFI to wrap an OCaml library
Date: Wed, 6 Feb 2008 15:54:56 -0800	[thread overview]
Message-ID: <85FB0157-5721-45F1-9440-A3021913BA1F@galois.com> (raw)

Hi everyone,

I have a couple of questions about the OCaml FFI.  I'm attempting to
wrap up an existing OCaml library and provide a C API for it, as opposed
to the other way around, which seems to be much more common.

1. Can the CAMLparam*/CAMLlocal*/CAMLreturn macros be used to safely
carry values of type 'value' between C functions (even C functions that
have the appropriate CAMLparam/CAMLreturn invocations present)?  I
thought this worked, but more extensive testing has yielded some hangs
with the stack trace looking like:

     #0  0x00002b98 in caml_oldify_local_roots ()
     #1  0x00004dd7 in caml_empty_minor_heap ()
     #2  0x00004f28 in caml_minor_collection ()
     #3  0x00003501 in caml_garbage_collection ()
     #4  0x00011888 in caml_call_gc ()
     #5  0x00013577 in run_solver ()
     #6  0x00013cb4 in main ()

where run_solver here is the C function that is passed an opaque object
reference (elided by a value of type 'value') from another C function,
and is calling methods repeatedly on the provided object via  
caml_callback.

Looking at the macro expansions, I'm suspicious about the safety between
C functions, and wonder if the only way to carry data is use
caml_register_global_root (and manage my own memory if I need dynamic
allocation).

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;
   }

Any help is greatly appreciated.

--
Joel Stanley






             reply	other threads:[~2008-02-06 23:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-06 23:54 Joel Stanley [this message]
2008-02-07 22:26 ` [Caml-list] " Damien Doligez
2008-02-08 21:53   ` Joel Stanley
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=85FB0157-5721-45F1-9440-A3021913BA1F@galois.com \
    --to=jstanley@galois.com \
    --cc=caml-list@yquem.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).