Joel Reymont <joe...@gmail.com> writes: > On Fri, May 4, 2012 at 9:43 AM, Goswin von Brederlow <goswin-...@web.de> > 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 -- Caml-list mailing list. Subscription management and archives: https://sympa-roc.inria.fr/wws/info/caml-list Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs