Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
OCaml, C, Garbage Collector etc
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Odalric-Ambrym Maillard <odalricambrym.maillard@g...>
Subject: Re: OCaml, C, Garbage Collector etc

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) {
  val_m = Val_int(m);
  res=caml_callback(*closure_init_tree, val_m);

void set_depth_c(int max_depth) {
  val_max_depth = Val_int(max_depth);
  res=caml_callback(*closure_set_depth, val_max_depth);

double psi_c(int m, int dim_x,double * x){
  int d;
  val_m = Val_int(m);
  val_x = caml_alloc(dim_x * Double_wosize, Double_array_tag);
    //    printf("Stored %f",x[d]);
    Store_double_field(val_x, d,x[d]);
  res = caml_callback2( *closure_psi, val_m, val_x);

Here is the corresponding OCaml Code (with an additionnal get_tree
(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;
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;;