caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Joel Reymont <joelr1@gmail.com>
Cc: Goswin von Brederlow <goswin-v-b@web.de>,
	Gabriel Scherer <gabriel.scherer@gmail.com>,
	caml-list@inria.fr
Subject: Re: [Caml-list] using modules to wrap c++ classes
Date: Sat, 05 May 2012 14:39:56 +0200	[thread overview]
Message-ID: <87bom26btv.fsf@frosties.localnet> (raw)
In-Reply-To: <CAOQwdX1PUPz3E76c9QcOf61Ak4c5+83ANYvM21jA9dQ_6RyvnA@mail.gmail.com> (Joel Reymont's message of "Fri, 4 May 2012 18:23:47 +0100")

Joel Reymont <joelr1@gmail.com> writes:

> On Fri, May 4, 2012 at 9:43 AM, Goswin von Brederlow <goswin-v-b@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

  reply	other threads:[~2012-05-05 12:40 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-03 12:22 Joel Reymont
2012-05-03 13:35 ` Goswin von Brederlow
2012-05-03 13:56   ` Joel Reymont
2012-05-03 14:27     ` Gabriel Scherer
2012-05-03 15:02       ` Joel Reymont
2012-05-03 18:41         ` Joel Reymont
2012-05-03 19:20           ` Joel Reymont
2012-05-04  8:43           ` Goswin von Brederlow
2012-05-04 17:23             ` Joel Reymont
2012-05-05 12:39               ` Goswin von Brederlow [this message]
2012-05-05 14:11                 ` Joel Reymont

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bom26btv.fsf@frosties.localnet \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    --cc=joelr1@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).