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 SAA21434; Tue, 20 Jul 2004 18:44:24 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA21779 for ; Tue, 20 Jul 2004 18:44:23 +0200 (MET DST) Received: from postfix4-1.free.fr (postfix4-1.free.fr [213.228.0.62]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i6KGiMEV017904 for ; Tue, 20 Jul 2004 18:44:22 +0200 Received: from univ-savoie.fr (grenoble-1-62-147-1-228.dial.proxad.net [62.147.1.228]) by postfix4-1.free.fr (Postfix) with ESMTP id DC58D165DD4; Tue, 20 Jul 2004 18:44:20 +0200 (CEST) Message-ID: <40FDA063.9080408@univ-savoie.fr> Date: Tue, 20 Jul 2004 18:44:51 -0400 From: Christophe Raffalli User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040115 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Christophe Raffalli , caml-list Subject: Re: [Caml-list] Calback References: <40FC0BEF.4080909@univ-savoie.fr> <3B84CC2A-D97E-11D8-A7B0-00039310CAE8@inria.fr> <40FD79F9.6040309@univ-savoie.fr> <20040720.164053.104046716.andrieu@ijm.jussieu.fr> <40FD9FDE.3070000@univ-savoie.fr> In-Reply-To: <40FD9FDE.3070000@univ-savoie.fr> X-Enigmail-Version: 0.83.3.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Miltered: at nez-perce with ID 40FD4BE6.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; raffalli:01 raffalli:01 univ-savoie:01 caml-list:01 andrieu:01 2004:99 passing:01 callback:01 callback:01 hashtable:01 passing:01 hashtable:01 int-:01 'a-:01 api:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Olivier Andrieu wrote: > 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). > This is even worth ! > > ----- 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. This is to follow the same api than GLUT (generalized a bit because in glut the argument is always int. > > // 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)'. > It was just a first try. You are right. Do you have such a platform ? Could check in that case the prototype of glutTimerFunc in GL/glut.h ? If it is still int, I could do a test in the C code and go through a C array if (sizeof(int) != sizeof(void*)) -- Christophe Raffalli Université de Savoie Batiment Le Chablais, bureau 21 73376 Le Bourget-du-Lac Cedex tél: (33) 4 79 75 81 03 fax: (33) 4 79 75 87 42 mail: Christophe.Raffalli@univ-savoie.fr www: http://www.lama.univ-savoie.fr/~RAFFALLI --------------------------------------------- IMPORTANT: this mail is signed using PGP/MIME At least Enigmail/Mozilla, mutt or evolution can check this signature --------------------------------------------- ------------------- 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