From: Odalric-Ambrym Maillard <odalricambrym.maillard@gmail.com>
To: caml-list@yquem.inria.fr, Sylvain Le Gall <sylvain@le-gall.net>
Subject: Re: OCaml, C, Garbage Collector etc
Date: Mon, 18 Jan 2010 10:06:06 +0100 [thread overview]
Message-ID: <406c33611001180106k678d21e8i34d2df8801da32c8@mail.gmail.com> (raw)
In-Reply-To: <406c33611001150943o2aeb2429qefa22f1102699730@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2474 bytes --]
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;;
...
[-- Attachment #2: Type: text/html, Size: 2800 bytes --]
prev parent reply other threads:[~2010-01-18 9:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-15 17:43 Odalric-Ambrym Maillard
2010-01-18 9:06 ` Odalric-Ambrym Maillard [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=406c33611001180106k678d21e8i34d2df8801da32c8@mail.gmail.com \
--to=odalricambrym.maillard@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=odalric.maillard@inria.fr \
--cc=sylvain@le-gall.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox