From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q45Ce9tx000926 for ; Sat, 5 May 2012 14:40:09 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AskBAP4epU/ZSMDjgGdsb2JhbABFsnQiAQELCwsFFgMkggwBAQQBJxM/BQsLGAklDwEEDRshE4d/AQMGCbAPHysNFYk+ihdohiAEm2GFXIdu X-IronPort-AV: E=Sophos;i="4.75,536,1330902000"; d="scan'208";a="142678240" Received: from fmmailgate02.web.de ([217.72.192.227]) by mail4-smtp-sop.national.inria.fr with ESMTP; 05 May 2012 14:40:03 +0200 Received: from moweb002.kundenserver.de (moweb002.kundenserver.de [172.19.20.108]) by fmmailgate02.web.de (Postfix) with ESMTP id 4F0031C443254 for ; Sat, 5 May 2012 14:40:03 +0200 (CEST) Received: from frosties.localnet ([95.208.118.96]) by smtp.web.de (mrweb002) with ESMTPA (Nemesis) id 0Lilcj-1RsOnH2jKH-00cvef; Sat, 05 May 2012 14:40:03 +0200 Received: from mrvn by frosties.localnet with local (Exim 4.77) (envelope-from ) id 1SQeHE-0004We-EK; Sat, 05 May 2012 14:39:56 +0200 From: Goswin von Brederlow To: Joel Reymont Cc: Goswin von Brederlow , Gabriel Scherer , caml-list@inria.fr References: <871un1z8sq.fsf@frosties.localnet> <8762ccqqt0.fsf@frosties.localnet> Date: Sat, 05 May 2012 14:39:56 +0200 In-Reply-To: (Joel Reymont's message of "Fri, 4 May 2012 18:23:47 +0100") Message-ID: <87bom26btv.fsf@frosties.localnet> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Provags-ID: V02:K0:+NuL/tDTAEWODTZ8FV9Q32k1nY6zNIqwS00yB/3w9Us saUCLw52WnB7rzdXAZEXi0cvgGzO0XClpsCs4wpCEQsQx7fcUN P4xWsZCL2YpmaHfWTCQ7ubgYbnW59V00DNyro3STOqukK6WC7q UhPeCHt7CjnDZ3fSOecFjUWIJ8lcxAiT5fv/RG2H2BGNHaBByJ 4X2I59hSwcnzyZZfVahkQ== Subject: Re: [Caml-list] using modules to wrap c++ classes Joel Reymont writes: > On Fri, May 4, 2012 at 9:43 AM, Goswin von Brederlow wrote: > >> As discussed on irc you need to create your callbacks[] array as ocaml >> block and register that itself as root. That also has the benefit that >> you only have to register a single root instead of 50. > > Assuming that I have a class named MyCallbacks with a public member > callbacks of type value, is this correct? No. The callbacks[] can be public, protect, private or static. You could also register the individual callbacks but then you need to register and deregister all 50 of them seperate. Having just one makes things easier. So the "need" above might be to strong. It is just simpler that way. And I think the GC had some performance issues with too many roots. > Also, can I use Is_block to check if there's a closure value stored in > the callbacks block before I dispatch to the closure? Sure. > Thanks, Joel > > --- > > // finalizer, stored in custom_operations > > void Callbacks_delete(value v) > { > CAMLparam1(v); > MyCallbacks* o = Callbacks_val(v); > caml_remove_global_root(&o->callbacks); > delete o; > } > > CAMLprim value > Callbacks_new() > { > CAMLparam0(); > CAMLlocal2(v, cbks); > v = caml_alloc_custom(&ops, sizeof(MyCallbacks*), 0, 1); > MyCallbacks* o = new MyCallbacks(); > Callbacks_val(v) = o; > cbks = caml_alloc(NUM_CBKS, 0); > o->callbacks = cbks; > caml_register_global_root(&o->callbacks); > CAMLreturn(v); > } > > extern "C" CAMLprim value > Callbacks_set(value self, value cbk) > { > CAMLparam2(self, cbk); > MyCallbacks* o = Callbacks_val(self); > Store_field(o->callbacks, Tag_val(cbk), Field(0, cbk)); > CAMLreturn(Val_unit); > } Don't you have to use caml_modify() here instead of Store_field()? I think Store_field is only alowed in freshly allocated blocks. MfG Goswin