From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA16237; Tue, 20 Jul 2004 16:40:57 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA16360 for ; Tue, 20 Jul 2004 16:40:56 +0200 (MET DST) Received: from shiva.jussieu.fr (shiva.jussieu.fr [134.157.0.129]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i6KEetSH031221 for ; Tue, 20 Jul 2004 16:40:55 +0200 Received: from localhost (akasha.ijm.jussieu.fr [134.157.173.57]) by shiva.jussieu.fr (8.12.11/jtpda-5.4) with ESMTP id i6KEepBB029423 ; Tue, 20 Jul 2004 16:40:51 +0200 (CEST) X-Ids: 167 Date: Tue, 20 Jul 2004 16:40:53 +0200 (CEST) Message-Id: <20040720.164053.104046716.andrieu@ijm.jussieu.fr> To: christophe.raffalli@univ-savoie.fr Cc: caml-list@inria.fr Subject: Re: [Caml-list] Calback From: Olivier Andrieu In-Reply-To: <40FD79F9.6040309@univ-savoie.fr> References: <40FC0BEF.4080909@univ-savoie.fr> <3B84CC2A-D97E-11D8-A7B0-00039310CAE8@inria.fr> <40FD79F9.6040309@univ-savoie.fr> X-Mailer: Mew version 4.0.64 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 40FD2EF7.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at shiva.jussieu.fr with ID 40FD2EF3.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Antivirus: scanned by sophie at shiva.jussieu.fr X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 andrieu:01 andrieu:01 raffalli:01 2004:99 passing:01 callback:01 callback:01 hashtable:01 passing:01 hashtable:01 int-:01 'a-:01 sizeof:01 sizeof:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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