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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 14F3DBC6C for ; Thu, 7 Feb 2008 23:26:54 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.25,318,1199660400"; d="scan'208";a="22376954" Received: from bne75-9-88-168-234-79.fbx.proxad.net (HELO [192.168.0.62]) ([88.168.234.79]) by mail4-relais-sop.national.inria.fr with ESMTP/TLS/AES128-SHA; 07 Feb 2008 23:26:53 +0100 Cc: Joel Stanley Message-Id: <24097E85-C08E-4547-986B-4EF91B7692C3@inria.fr> From: Damien Doligez To: caml-list In-Reply-To: <85FB0157-5721-45F1-9440-A3021913BA1F@galois.com> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v915) Subject: Re: [Caml-list] Using the C FFI to wrap an OCaml library Date: Thu, 7 Feb 2008 23:26:54 +0100 References: <85FB0157-5721-45F1-9440-A3021913BA1F@galois.com> X-Mailer: Apple Mail (2.915) X-Spam: no; 0.00; damien:01 damien:01 ffi:01 ocaml:01 camlparam:01 camlreturn:01 camlparam:01 camlreturn:01 oldify:01 solver:01 solver:01 elided:01 expansions:01 ocaml:01 ffi:01 On 2008-02-07, at 00:54, Joel Stanley wrote: > 1. Can the CAMLparam*/CAMLlocal*/CAMLreturn macros be used to safely > carry values of type 'value' between C functions (even C functions > that > have the appropriate CAMLparam/CAMLreturn invocations present)? Yes, they are designed for that. > I > thought this worked, but more extensive testing has yielded some hangs > with the stack trace looking like: > > #0 0x00002b98 in caml_oldify_local_roots () > #1 0x00004dd7 in caml_empty_minor_heap () > #2 0x00004f28 in caml_minor_collection () > #3 0x00003501 in caml_garbage_collection () > #4 0x00011888 in caml_call_gc () > #5 0x00013577 in run_solver () > #6 0x00013cb4 in main () > > where run_solver here is the C function that is passed an opaque > object > reference (elided by a value of type 'value') from another C function, > and is calling methods repeatedly on the provided object via > caml_callback. This could be anything, the most likely is that you used "return" somewhere instead of "CAMLreturn". > Looking at the macro expansions, I'm suspicious about the safety > between > C functions, Could you elaborate on what makes you say that? The macros carefully implement a stack discipline designed for nested calls. > and wonder if the only way to carry data is use > caml_register_global_root (and manage my own memory if I need dynamic > allocation). You don't want to do that, it would be too inefficient. > 2. As a follow-up question to #1, the OCaml values may need to be > carried across yet another FFI, in this case, C <-> Poly/ML . Even if > using the macros was a safe way to carry values between C functions, > I"m > not sure I can easily replicate the macros on the other side of the > FFI, > so am wondering if an explicit memory management approach using > caml_register_global_root will work. E.g., > > value* alloc_value() { > value* p = malloc(sizeof(value)); > caml_register_global_root(p); > return p; > } In order to make this work, you have to explain to Poly/ML that every access to the value must be done through the value*. Or you need to make sure that Poly/ML code never allocates in the OCaml heap, and never calls back to a C function that does. I think it's more reasonable to just copy the data between worlds instead of trying to share pointers between OCaml and Poly/ML. -- Damien