caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
To: Richard Jones <rich@annexia.org>
Subject: Re: [Caml-list] Wrapping a callback to OCaml code from C
Date: Fri, 24 Jun 2005 13:38:37 +0200	[thread overview]
Message-ID: <17083.61629.766378.319685@karryall.dnsalias.org> (raw)
In-Reply-To: <20050623214733.GA30897@furbychan.cocan.org>

Hi,

 Richard Jones [Thursday 23 June 2005] :
 > I'm currently making some OCaml bindings for some C code.  The C
 > code which is causing me difficulty provides a callback interface.
 > 
 > The interface, in C, looks like:
 > 
 >   typedef void callback_t (void *data, obj *o1, obj *o2);
 >   void run (void *data, callback_t *callback);
 > 
 > When 'run' function is called, it will call the callback function
 > passed several times, passing 'data' as the first parameter.  I want
 > to provide an equivalent function in OCaml.
 > 
 > My current best attempt is this, which uses the 'data' parameter to
 > hold the address of the OCaml closure:
 > 
 >   CAMLprim value
 >   run_wrapper (value fv)
 >   {
 >     CAMLparam1 (fv);
 >     value *fvp = &fv;
 >     caml_register_global_root (fvp);
 >     run (fvp, callback_wrapper);
 >     caml_remove_global_root (fvp);
 >     CAMLreturn (Val_unit);
 >   }

you don't need caml_remove_global_root / caml_remove_global_root if
the callback is only called during the run() invocation. A local root
as registered by the CAMLparam macro is enough.

 >   static void
 >   callback_wrapper (void *fvpv, obj *o1, obj *o2)
 >   {
 >     value *fvp = (value *) fvpv;
 >     value fv = *fvp;
 >     value o1v, o2v;
 >     o1v = Val_obj (o1);
 >     o2v = Val_obj (o2);
 >     caml_callback2 (fv, o1v, o2v);
 >   }

you should dereference fvp at the last possible time, after the
Val_obj calls if theses calls are allocating in the caml heap (eg
caml_copy_double, caml_copy_string).

Also you probably should use caml_callback2_exn, otherwise if your
caml callback raises an exception, control will jump back straight to
the ocaml code and skip the end of the C caller code (run), which will
usually result in resource leaks (for instance in your version it would
leak the global root).

-- 
   Olivier


      parent reply	other threads:[~2005-06-24 14:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-23 21:47 Richard Jones
2005-06-24 10:40 ` [Caml-list] " Richard Jones
2005-06-24 11:15   ` Daniel Bünzli
2005-06-24 11:23     ` Richard Jones
2005-06-24 11:38 ` Olivier Andrieu [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=17083.61629.766378.319685@karryall.dnsalias.org \
    --to=andrieu@ijm.jussieu.fr \
    --cc=rich@annexia.org \
    /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).