From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr 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 44975BB9C for ; Wed, 23 Nov 2005 14:42:03 +0100 (CET) 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 jANDg2E1004318 for ; Wed, 23 Nov 2005 14:42:02 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA04102 for ; Wed, 23 Nov 2005 14:42:02 +0100 (MET) Received: from [128.93.11.101] (buzet.inria.fr [128.93.11.101]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jANDg1n0015035 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 23 Nov 2005 14:42:02 +0100 Message-ID: <438471A7.9050503@inria.fr> Date: Wed, 23 Nov 2005 14:41:59 +0100 From: Alain Frisch User-Agent: Debian Thunderbird 1.0.6 (X11/20050802) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Thomas Fischbacher Cc: caml-list@inria.fr Subject: Re: [Caml-list] C Callback issues References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 438471AA.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 438471A9.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; frisch:01 frisch:01 caml-list:01 foo:01 ocaml:01 ocaml:01 pointer:01 pointer:01 hash:01 hash:01 hashing:01 ata:98 callbacks:01 callbacks:01 wrote:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Thomas Fischbacher wrote: > Hello all, > > suppose I have a C library which provides functionality via registering C > callbacks that take a closure parameter. For the sake of this example, > let's say via set_foo_callback(void (*f)(void *),void *closure_param). > > Now, if I want to lift this to the OCaml level by writing a C > callback-wrapper which will use the closure arg to pass the OCaml > function to be called, I encounter a slight problem: > > Callbacks have to be "announced globally" to C by means of > Callback.register. I guessed I missed something here. Why do you need to use Callback.register? The problem I see is that you need to copy the pointer to the OCaml closure in a C data structure and register it as a global root. Now, if I understand correctly, you'd like to share a global root if the same function closure is used several times, and you want to implement this sharing efficiently. 1. If you think you'll never have more than a few hundreds closures, you can simply do a linear lookup. 2. You can also use the data code pointer as a hash key. It never changes, but especially in native code, this might not be a very good idea (because of the way curryfication and partial applications are treated). You can also hash (with something similar to polymorphic hashing) the content of the closure environment. 3. You can use directly the closure pointer as a hash key, but you must update the hash table when the GC is triggered. -- Alain