Hello,
I have reduced my problem to the following question :
I have this code in C, these are the wrappers corresponding to the Ocaml functions. I call the three following functions from C code. They correspond respectively to functions of type : int -> unit, int -> unit and int -> float array -> float.
My concern is about the good (w.r.t gc) way of calling Ocaml functions with return type unit from C.
Thus where to put Camlparam, and Camlreturn and how etc... I have tried this but it fails too :
------------------------------------------------------------
void init_tree_c(int m) {
CAMLparam0();
CAMLlocal2(val_m,res);
val_m = Val_int(m);
res=caml_callback(*closure_init_tree, val_m);
CAMLreturn0;
}
void set_depth_c(int max_depth) {
CAMLparam0();
CAMLlocal2(val_max_depth,res);
val_max_depth = Val_int(max_depth);
res=caml_callback(*closure_set_depth, val_max_depth);
CAMLreturn0;
}
double psi_c(int m, int dim_x,double * x){
int d;
CAMLparam0();
CAMLlocal3(val_m,val_x,res);
val_m = Val_int(m);
val_x = caml_alloc(dim_x * Double_wosize, Double_array_tag);
for(d=0;d<dim_x;d++){
// printf("Stored %f",x[d]);
Store_double_field(val_x, d,x[d]);
}
res = caml_callback2( *closure_psi, val_m, val_x);
CAMLreturn(Double_val(res));
}
------------------------------------------------------------
Here is the corresponding OCaml Code (with an additionnal get_tree function):
(I have added some printers for debug purpose).
------------------------------------------------------------
let (init_tree, get_tree, set_depth, psi) =
let random_tree = ref [|Empty|] and depth = ref 3 in
(
(function m -> random_tree := Array.make m Empty;print_int m;print_string " trees initialized.";print_newline();),
(function m -> Array.get !random_tree m;),
(function d -> depth := d;print_string "Maximum depth initialized to ";print_int d;print_newline();),
(function m -> (function x ->
let t,v = aux (Array.get !random_tree m) !depth 0 1 (Array.length x) x in
Array.set !random_tree m t;
v))
);;
------------------------------------------------------------
Note that : random_tree contains an arrray of m trees, and that this variable is shared by the different functions.
Of course there is the register code etc...
Callback.register "init_tree_Ocaml" init_tree;;
Callback.register "set_depth_Ocaml" set_depth;;
Callback.register "psi_Ocaml" psi;;
...