From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 9C80FBCAE for ; Fri, 24 Jun 2005 12:01:25 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j5OA1PJm006842 for ; Fri, 24 Jun 2005 12:01:25 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA17367 for ; Fri, 24 Jun 2005 12:01:24 +0200 (MET DST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j5OA1Nvr006832 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Fri, 24 Jun 2005 12:01:24 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1DlZXp-00082r-00 for ; Thu, 23 Jun 2005 22:47:33 +0100 Date: Thu, 23 Jun 2005 22:47:33 +0100 To: caml-list@inria.fr Subject: Wrapping a callback to OCaml code from C Message-ID: <20050623214733.GA30897@furbychan.cocan.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.28i From: Richard Jones X-Miltered: at nez-perce with ID 42BBD9F5.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 42BBD9F4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ocaml:01 ocaml:01 bindings:01 typedef:01 val:01 val:01 camlprim:01 camlparam:01 camlreturn:01 segfaults:01 behaves:01 notepad:01 rewrite:01 passing:01 data:02 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Hi: I'm currently making some OCaml bindings for some C code. The C code which is causing me difficulty provides a callback interface. The interface, in C, looks like: typedef void callback_t (void *data, obj *o1, obj *o2); void run (void *data, callback_t *callback); When 'run' function is called, it will call the callback function passed several times, passing 'data' as the first parameter. I want to provide an equivalent function in OCaml. My current best attempt is this, which uses the 'data' parameter to hold the address of the OCaml closure: static void callback_wrapper (void *fvpv, obj *o1, obj *o2) { value *fvp = (value *) fvpv; value fv = *fvp; value o1v, o2v; o1v = Val_obj (o1); o2v = Val_obj (o2); caml_callback2 (fv, o1v, o2v); } CAMLprim value run_wrapper (value fv) { CAMLparam1 (fv); value *fvp = &fv; caml_register_global_root (fvp); run (fvp, callback_wrapper); caml_remove_global_root (fvp); CAMLreturn (Val_unit); } I suspect that this code is wrong or GC-unsafe in some way. The reason is that my program sometimes segfaults, and behaves differently if I rewrite callback_wrapper in inconsequential ways. Can someone tell me if I'm doing the right thing here? Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com