* Wrapping a callback to OCaml code from C
@ 2005-06-23 21:47 Richard Jones
2005-06-24 10:40 ` [Caml-list] " Richard Jones
2005-06-24 11:38 ` Olivier Andrieu
0 siblings, 2 replies; 5+ messages in thread
From: Richard Jones @ 2005-06-23 21:47 UTC (permalink / raw)
To: caml-list
Hi:
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:
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);
}
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);
}
I suspect that this code is wrong or GC-unsafe in some way. The
reason is that my program sometimes segfaults, and behaves differently
if I rewrite callback_wrapper in inconsequential ways.
Can someone tell me if I'm doing the right thing here?
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Wrapping a callback to OCaml code from C
2005-06-23 21:47 Wrapping a callback to OCaml code from C Richard Jones
@ 2005-06-24 10:40 ` Richard Jones
2005-06-24 11:15 ` Daniel Bünzli
2005-06-24 11:38 ` Olivier Andrieu
1 sibling, 1 reply; 5+ messages in thread
From: Richard Jones @ 2005-06-24 10:40 UTC (permalink / raw)
To: caml-list
On Thu, Jun 23, 2005 at 10:47:33PM +0100, Richard Jones wrote:
[...]
There was quite a delay in this posting appearing. In the meantime I
think I've fixed the problem by adding CAMLparam0 .. CAMLlocal3 ..
CAMLreturn0 around the callback wrapper function.
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Wrapping a callback to OCaml code from C
2005-06-24 10:40 ` [Caml-list] " Richard Jones
@ 2005-06-24 11:15 ` Daniel Bünzli
2005-06-24 11:23 ` Richard Jones
0 siblings, 1 reply; 5+ messages in thread
From: Daniel Bünzli @ 2005-06-24 11:15 UTC (permalink / raw)
To: Richard Jones; +Cc: caml-list
Le 24 juin 05 à 12:40, Richard Jones a écrit :
> On Thu, Jun 23, 2005 at 10:47:33PM +0100, Richard Jones wrote:
> [...]
>
> There was quite a delay in this posting appearing. In the meantime I
> think I've fixed the problem by adding CAMLparam0 .. CAMLlocal3 ..
> CAMLreturn0 around the callback wrapper function.
I would like to understand why this is needed. Did you allocate
something with the gc in your Val_obj functions ?
Daniel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Wrapping a callback to OCaml code from C
2005-06-24 11:15 ` Daniel Bünzli
@ 2005-06-24 11:23 ` Richard Jones
0 siblings, 0 replies; 5+ messages in thread
From: Richard Jones @ 2005-06-24 11:23 UTC (permalink / raw)
To: Daniel Bünzli; +Cc: caml-list
On Fri, Jun 24, 2005 at 01:15:16PM +0200, Daniel Bünzli wrote:
>
> Le 24 juin 05 à 12:40, Richard Jones a écrit :
>
> >On Thu, Jun 23, 2005 at 10:47:33PM +0100, Richard Jones wrote:
> >[...]
> >
> >There was quite a delay in this posting appearing. In the meantime I
> >think I've fixed the problem by adding CAMLparam0 .. CAMLlocal3 ..
> >CAMLreturn0 around the callback wrapper function.
>
> I would like to understand why this is needed. Did you allocate
> something with the gc in your Val_obj functions ?
Yes. Val_obj is in fact a thin wrapper around this function:
static inline value
Val_voidptr (void *ptr, const char *)
{
CAMLparam0 ();
CAMLlocal1 (rv);
rv = caml_alloc (1, Abstract_tag);
Field (rv, 0) = (value) ptr;
CAMLreturn (rv);
}
and of course callback_wrapper calls callback2 which calls into OCaml
code - rather a lot of allocation possibilities there!
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Wrapping a callback to OCaml code from C
2005-06-23 21:47 Wrapping a callback to OCaml code from C Richard Jones
2005-06-24 10:40 ` [Caml-list] " Richard Jones
@ 2005-06-24 11:38 ` Olivier Andrieu
1 sibling, 0 replies; 5+ messages in thread
From: Olivier Andrieu @ 2005-06-24 11:38 UTC (permalink / raw)
To: Richard Jones
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-06-24 14:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-23 21:47 Wrapping a callback to OCaml code from C 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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox