From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 7EB22BC69 for ; Mon, 2 Jul 2007 13:48:11 +0200 (CEST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l62BmArb002035 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Mon, 2 Jul 2007 13:48:11 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1I5KO1-0002L8-00; Mon, 02 Jul 2007 12:48:09 +0100 Date: Mon, 2 Jul 2007 12:48:09 +0100 To: Oliver Kania Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] embedding ocaml from C++ -- PODs Message-ID: <20070702114809.GA8908@furbychan.cocan.org> References: <1262c4ee0707020406s7da99e37mc6706db8d35342a0@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1262c4ee0707020406s7da99e37mc6706db8d35342a0@mail.gmail.com> User-Agent: Mutt/1.5.9i From: Richard Jones X-Miltered: at discorde with ID 4688E5FA.002 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 0200,:01 ocaml:01 runtime:01 arrays:01 pointers:01 bitfields:01 aligned:01 alignment:01 pointers:01 wrote:01 oliver:01 caml-list:01 caml-list:01 ints:01 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