Version française
Home     About     Download     Resources     Contact us    
Browse thread
embedding ocaml from C++ -- PODs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] embedding ocaml from C++ -- PODs
On Mon, Jul 02, 2007 at 01:06:03PM +0200, Oliver Kania wrote:
> Hello,
> the OCAML documentation gives good explanation about how call OCAML
> functions from C.
> However , what I want to do is the following. I have a C structure with many
> fields.
> I want to be able to initialize this structure both in C and in Ocaml and
> want access to the same instance from both languages.
> The structure could be a global object in OCAML and C. The important point
> is that I want to have access to the very same
> data and not to any copies, which means I want to pass a reference from
> OCAML to C or vice versa.
> More generally speaking, I would like to create objects in C and pass
> references to these objects to the OCAML side and vice versa.
> Its all about data flow between the two sides.

This is possible, although you need to understand the OCaml runtime
fully to be able to do it.  Here are a few questions and comments to
get the ball rolling for you:

What sort of data is going to be stored in this structure?  (ints,
floats, arrays, structures linked with pointers, bitfields, ...?)

How are the structure elements aligned?  Can you choose the alignment?

Can you change the C fields to suit OCaml?  For example, if you want
to store native ints directly in the structure, then it's going to be
a lot simpler if you store them as OCaml ints (shift left one place
with the bottom bit set) instead of C ints, but if you do this you'll
have (a) slightly more work to do on the C side, and (b) one less bit
to play with.

You can share C pointers with OCaml directly, but beware of this
problem which may bite you:
http://caml.inria.fr/pub/old_caml_site/caml-list/1415.html

Rich.

-- 
Richard Jones
Red Hat