caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
To: christophe.raffalli@univ-savoie.fr
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Calback
Date: Tue, 20 Jul 2004 16:40:53 +0200 (CEST)	[thread overview]
Message-ID: <20040720.164053.104046716.andrieu@ijm.jussieu.fr> (raw)
In-Reply-To: <40FD79F9.6040309@univ-savoie.fr>

 Christophe Raffalli [Tue, 20 Jul 2004]:
 > Thanks for the help on the list. I decided to change Glut.timerFunc
 > for the following code, passing the Caml function and its argument
 > using a C int (transmitted to the times) which is in fact a pointer
 > to a Caml pair. This pointer is registered as a global root and
 > removed by the callback itself. I give the code bellow if someone
 > wants to write something similar.
 > 
 > A small question: why do people use callback trough a name and a
 > hashtable instead of passing the callback inside C variables
 > (global or local) ? It could save a lot of time ?

(caml_named_value uses a linked list, not a hashtable).

 > ----- First the caml code:
 > 
 > 
 > (* timerFunc is non-window-dependent *)
 > external _glutTimerFunc : int->((value:'a->'b) * 'a)->unit = 
 > "ml_glutTimerFunc"
 > let timerFunc ~ms ~cb ~value:v : (unit) =
 >      _glutTimerFunc ms (cb,v);; (* register the callback with GLUT *)

It's just easier not to bother with the argument and have the callback
be of type `unit -> unit' (or `unit -> 'a'), since you can include any
argument you like in the closure.

 > // for timer we can not use Register.callback because it grows 
 > infinitely the
 > // number of global root
 > 
 > static void glutTimerFunc_cb(int fun_arg)
 > {
 >    // fun_arg is a pointer on a caml pair hidden in an integer.
 >    // Moreover we remove the global root because each callback is used once
 >    // by the timer.
 >    value *v = (value) fun_arg;

I'm afraid this will break on plaforms where `sizeof (int) != sizeof (long)'.

-- 
   Olivier

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-07-20 14:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-19 17:59 [Caml-list] GC and caml_oldify_local_roots taking too much time Christophe Raffalli
2004-07-19 12:21 ` Damien Doligez
2004-07-19 19:33   ` Christophe Raffalli
2004-07-19 20:17   ` Christophe Raffalli
2004-07-19 14:28     ` Richard Jones
2004-07-19 14:38       ` Richard Jones
2004-07-19 15:12     ` Olivier Andrieu
2004-07-19 20:36   ` Christophe Raffalli
2004-07-20 20:00   ` [Caml-list] Calback (was caml_oldify_local_roots taking too much time) Christophe Raffalli
2004-07-20 14:40     ` Olivier Andrieu [this message]
     [not found]       ` <40FD9FDE.3070000@univ-savoie.fr>
2004-07-20 22:44         ` [Caml-list] Calback Christophe Raffalli
2004-07-21  0:54           ` Jere Sanisalo
2004-07-20 23:09       ` Christophe Raffalli

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=20040720.164053.104046716.andrieu@ijm.jussieu.fr \
    --to=andrieu@ijm.jussieu.fr \
    --cc=caml-list@inria.fr \
    --cc=christophe.raffalli@univ-savoie.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;
as well as URLs for NNTP newsgroup(s).