caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: Oliver Kania <kania.oliver@googlemail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] embedding ocaml from C++ -- PODs
Date: Mon, 2 Jul 2007 12:48:09 +0100	[thread overview]
Message-ID: <20070702114809.GA8908@furbychan.cocan.org> (raw)
In-Reply-To: <1262c4ee0707020406s7da99e37mc6706db8d35342a0@mail.gmail.com>

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


  reply	other threads:[~2007-07-02 11:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-02 11:06 Oliver Kania
2007-07-02 11:48 ` Richard Jones [this message]
     [not found]   ` <1262c4ee0707020509h48d43e24s5e8902810cae7985@mail.gmail.com>
2007-07-03  8:59     ` [Caml-list] " Richard Jones
2007-07-05 20:43       ` Oliver Kania
2007-07-02 11:49 ` Thomas Fischbacher

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=20070702114809.GA8908@furbychan.cocan.org \
    --to=rich@annexia.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=kania.oliver@googlemail.com \
    /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).