caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* C wrappers and callbacks from Ocaml to C and back to Ocaml
@ 2009-08-06 21:17 Erik de Castro Lopo
  2009-08-06 23:38 ` [Caml-list] " Goswin von Brederlow
  0 siblings, 1 reply; 3+ messages in thread
From: Erik de Castro Lopo @ 2009-08-06 21:17 UTC (permalink / raw)
  To: caml-list

Hi all,

I'm wrapping some C code that requires me to define a struct in Ocaml
like this:

    type xt = { f : xt -> a }

and then pass the struct to C which stores it, and later the C needs
to find function f, call it from C, passing it the Ocaml xt struct.

Anyone have any clues on how to do this? I have most of the rest of
the C wrapper sorted out,  its just this thats causing me any difficulty.

Cheers,
Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] C wrappers and callbacks from Ocaml to C and back to Ocaml
  2009-08-06 21:17 C wrappers and callbacks from Ocaml to C and back to Ocaml Erik de Castro Lopo
@ 2009-08-06 23:38 ` Goswin von Brederlow
  2009-08-07  4:23   ` Erik de Castro Lopo
  0 siblings, 1 reply; 3+ messages in thread
From: Goswin von Brederlow @ 2009-08-06 23:38 UTC (permalink / raw)
  To: caml-list

Erik de Castro Lopo <mle+ocaml@mega-nerd.com> writes:

> Hi all,
>
> I'm wrapping some C code that requires me to define a struct in Ocaml
> like this:
>
>     type xt = { f : xt -> a }
>
> and then pass the struct to C which stores it, and later the C needs
> to find function f, call it from C, passing it the Ocaml xt struct.
>
> Anyone have any clues on how to do this? I have most of the rest of
> the C wrapper sorted out,  its just this thats causing me any difficulty.
>
> Cheers,
> Erik

Well, you just do. A structure is a value like anything else.

What you have to wath out for is the GC. If you store the value
somewhere then you have to tell the GC about it by registering it as a
new root. And when you are done with it you need to unregister it.

As for access the docs tell you about how to access fields of a record
and also how to invoke a ocaml closure from C. Just read the relevant
sections from the chapter about interfacing ocaml and C.

MfG
        Goswin


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] C wrappers and callbacks from Ocaml to C and back to Ocaml
  2009-08-06 23:38 ` [Caml-list] " Goswin von Brederlow
@ 2009-08-07  4:23   ` Erik de Castro Lopo
  0 siblings, 0 replies; 3+ messages in thread
From: Erik de Castro Lopo @ 2009-08-07  4:23 UTC (permalink / raw)
  To: caml-list

Goswin von Brederlow wrote:

> Well, you just do. A structure is a value like anything else.

Yes, with a bit of careful debugging I managed to get a bit further
and even managed to extract (in the C code) the Ocaml function from
the Ocaml struct and I think I can call it, but I haven't test that
yet.

> What you have to watch out for is the GC.

This is what I was worried about :-).

> If you store the value
> somewhere then you have to tell the GC about it by registering it as a
> new root. And when you are done with it you need to unregister it.

Ah yes, the following two functions in <caml/memory.h>:

    CAMLextern void caml_register_global_root (value *);
    CAMLextern void caml_remove_global_root (value *);

Cheers,
Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-08-07  4:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-06 21:17 C wrappers and callbacks from Ocaml to C and back to Ocaml Erik de Castro Lopo
2009-08-06 23:38 ` [Caml-list] " Goswin von Brederlow
2009-08-07  4:23   ` Erik de Castro Lopo

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).