From: Alain Frisch <Alain.Frisch@inria.fr>
To: Thomas Fischbacher <Thomas.Fischbacher@Physik.Uni-Muenchen.DE>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] C Callback issues
Date: Wed, 23 Nov 2005 14:41:59 +0100 [thread overview]
Message-ID: <438471A7.9050503@inria.fr> (raw)
In-Reply-To: <Pine.LNX.4.61.0511231319580.19523@eiger.cip.physik.uni-muenchen.de>
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
prev parent reply other threads:[~2005-11-23 13:42 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-11-23 12:59 Thomas Fischbacher
2005-11-23 13:40 ` [Caml-list] " Olivier Andrieu
2005-11-23 14:11 ` Thomas Fischbacher
2005-11-23 13:41 ` Alain Frisch [this message]
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=438471A7.9050503@inria.fr \
--to=alain.frisch@inria.fr \
--cc=Thomas.Fischbacher@Physik.Uni-Muenchen.DE \
--cc=caml-list@inria.fr \
/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