Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] GC and caml_oldify_local_roots taking too much time
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christophe Raffalli <christophe.raffalli@u...>
Subject: [Caml-list] Calback (was caml_oldify_local_roots taking too much time)
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 ?

For instance Glut.idleFunc will do a search in the hashtable of caml 
names at each callback and if the callback was transmitted  through a C 
global variable there vould only be one indirection ?

I am about to change all the callback mechnism used in Glut. So if 
anyone knows a good reason to use names and hashtables ?

----- 8< ----- here is the code

----- First the caml code:

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

----- Then the C code:

// 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;
   leave_blocking_section ();
   value fun = Field(*v, 0);
   value arg = Field(*v, 1);
   callback (fun, arg);
   enter_blocking_section ();

CAMLprim value ml_glutTimerFunc(value millis_val, value fun_arg) // set 
Timer callback
   // fun_arg is a caml pair with a function and its argument we 
register a root on this pair and set the callback.
     unsigned int millis;
     value *v = (value*) malloc(sizeof(value));
     *v = fun_arg;
     millis = Int_val(millis_val);
     glutTimerFunc(millis, &glutTimerFunc_cb, (int) v); // register with 
     return Val_unit;

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
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: