* C wrappers for Ocaml functions
@ 2009-04-14 11:51 Tim Leek
2009-04-14 14:06 ` [Caml-list] " Dmitry Bely
2009-04-14 14:40 ` Xavier Leroy
0 siblings, 2 replies; 3+ messages in thread
From: Tim Leek @ 2009-04-14 11:51 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 6349 bytes --]
Hello. Not sure if this should have gone to beginners or the regular
list. Also not sure if its been answered before. If so, please
redirect me! I did a bunch of searching both on google and in the lists
and couldn't find the answers I needed. So here goes.
I have written a blob of code in Ocaml that I like very much. I'd love
to keep it in ocaml and not have to worry about things like memory leaks
and so on. However, much of what I do is in C and can't be in Ocaml.
So I am investigating packing my nice Ocaml code into a library and
writing C bindings so that I can talk to it.
Virtually all of the tutorials out there and documentation cover how to
create Ocaml bindings to a C library. I don't want to do that. The few
examples I have found that are relevant are toy ones. How to write C
bindings to an Ocaml "fib" function, e.g. In particular, I have found
none that cover how to obtain pointers to OCaml function return values
that are not strings or ints, how to store them in C-land, and how to pass
them back to Ocaml as parameters.
Let's take as a concrete example the following: creating C bindings for
a simple hash table mapping string keys to integer values. If I can
generate bindings for this that work I should be able to do so for the
library I really care about.
Here's what I put together for the hash table slightly-less-than-toy
example. It doesn't even compile. [Oddly, with very similar incantations
my own code does compiles but then segfaults inside one of the Ocaml fns.]
Any help much appreciated!
-Tim
1. The implementation.
% cat ht.ml
type ht = (string,int) Hashtbl.t
let create () : ht = Hashtbl.create 100
let add (table:ht) key valu = Hashtbl.add table key valu
let mem (table:ht) key = Hashtbl.mem table key
let remove (table:ht) key = if (Hashtbl.mem table key) then
Hashtbl.remove table key
let _ = Callback.register "create" create
let _ = Callback.register "add" add
let _ = Callback.register "mem" mem
let _ = Callback.register "remove" remove
2. The wrappers
% cat ht_wrap.c
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/alloc.h>
void *ht_create (void) {
static value *create_closure = NULL;
if (create_closure == NULL)
create_closure = caml_named_value("create");
return ((void *) (caml_callback(*create_closure, Val_unit)));
}
void ht_add (void *ht, char *key, int val) {
static value *add_closure = NULL;
if (add_closure == NULL)
add_closure = caml_named_value("add");
caml_callback3(*add_closure, (value) ht, caml_copy_string(key),
Val_int(val));
}
void ht_mem (void *ht, char *key) {
static value *mem_closure = NULL;
if (mem_closure == NULL)
mem_closure = caml_named_value("mem");
caml_callback2(*mem_closure, (value) ht, caml_copy_string(key));
}
void ht_remove (void *ht, char *key) {
static value *remove_closure = NULL;
if (remove_closure == NULL)
remove_closure = caml_named_value("remove");
caml_callback2(*remove_closure, (value) ht, caml_copy_string(key));
}
3. Header file for those wrappers
% cat ht.h
void *ht_create (void);
void ht_add (void *ht, char *key, int val);
void ht_mem (void *ht, char *key);
void ht_remove (void *ht, char *key);
4. The test program, with main() function.
% cat ht_test.c
#include <stdio.h>
#include "ht.h"
int main (int argc, char **argv) {
void *ht;
caml_startup(argv);
ht = ht_create();
ht_add(ht, "foo", 1);
ht_add(ht, "foo", 1);
ht_add(ht, "bar", 1);
ht_remove(ht, "foo");
}
5. And this is how I am attempting to compile it. Note that error I'm
getting here is in link. If I add "-lm" it gets less noisy but still is
mad at me. Why am I having to add math library? Again, help!!
% ocamlopt -output-obj -o ht.o ht.ml
% ocamlopt -c ht_wrap.c
% cp /usr/lib/ocaml/3.10.0/libasmrun.a ./ht.a
% ar r ht.a ht_wrap.o ht.o
% gcc -o htt ht_test.c ht.a -lcurses
ht.a(floats.o): In function `caml_ceil_float':
(.text+0x193): undefined reference to `ceil'
ht.a(floats.o): In function `caml_atan2_float':
(.text+0x1ae): undefined reference to `atan2'
ht.a(floats.o): In function `caml_atan_float':
(.text+0x1c5): undefined reference to `atan'
ht.a(floats.o): In function `caml_acos_float':
(.text+0x1dc): undefined reference to `acos'
ht.a(floats.o): In function `caml_asin_float':
(.text+0x1f3): undefined reference to `asin'
ht.a(floats.o): In function `caml_tanh_float':
(.text+0x20a): undefined reference to `tanh'
ht.a(floats.o): In function `caml_tan_float':
(.text+0x221): undefined reference to `tan'
ht.a(floats.o): In function `caml_cosh_float':
(.text+0x238): undefined reference to `cosh'
ht.a(floats.o): In function `caml_cos_float':
(.text+0x24f): undefined reference to `cos'
ht.a(floats.o): In function `caml_sinh_float':
(.text+0x266): undefined reference to `sinh'
ht.a(floats.o): In function `caml_sin_float':
(.text+0x27d): undefined reference to `sin'
ht.a(floats.o): In function `caml_power_float':
(.text+0x298): undefined reference to `pow'
ht.a(floats.o): In function `caml_sqrt_float':
(.text+0x2bf): undefined reference to `sqrt'
ht.a(floats.o): In function `caml_log10_float':
(.text+0x400): undefined reference to `log10'
ht.a(floats.o): In function `caml_log_float':
(.text+0x417): undefined reference to `log'
ht.a(floats.o): In function `caml_fmod_float':
(.text+0x551): undefined reference to `fmod'
ht.a(floats.o): In function `caml_floor_float':
(.text+0x568): undefined reference to `floor'
ht.a(floats.o): In function `caml_exp_float':
(.text+0x57f): undefined reference to `exp'
ht.a(ht.o): In function `caml_program':
(.text+0x46): undefined reference to `camlHt__entry'
ht.a(ht.o): In function `caml_globals':
(.data+0x2e0): undefined reference to `camlHt'
ht.a(ht.o): In function `caml_data_segments':
(.data+0x410): undefined reference to `camlHt__data_begin'
ht.a(ht.o): In function `caml_data_segments':
(.data+0x418): undefined reference to `camlHt__data_end'
ht.a(ht.o): In function `caml_code_segments':
(.data+0x488): undefined reference to `camlHt__code_begin'
ht.a(ht.o): In function `caml_code_segments':
(.data+0x490): undefined reference to `camlHt__code_end'
ht.a(ht.o): In function `caml_frametable':
(.data+0x4d8): undefined reference to `camlHt__frametable'
collect2: ld returned 1 exit status
--
When I see an adult on a bicycle, I do not despair for the future of the
human race. - H. G.Wells
[-- Attachment #2: Type: text/html, Size: 7844 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] C wrappers for Ocaml functions
2009-04-14 11:51 C wrappers for Ocaml functions Tim Leek
@ 2009-04-14 14:06 ` Dmitry Bely
2009-04-14 14:40 ` Xavier Leroy
1 sibling, 0 replies; 3+ messages in thread
From: Dmitry Bely @ 2009-04-14 14:06 UTC (permalink / raw)
To: Caml List
On Tue, Apr 14, 2009 at 3:51 PM, Tim Leek <trleek@gmail.com> wrote:
> Hello. Not sure if this should have gone to beginners or the regular
> list. Also not sure if its been answered before. If so, please
> redirect me! I did a bunch of searching both on google and in the lists
> and couldn't find the answers I needed. So here goes.
>
> I have written a blob of code in Ocaml that I like very much. I'd love
> to keep it in ocaml and not have to worry about things like memory leaks
> and so on. However, much of what I do is in C and can't be in Ocaml.
> So I am investigating packing my nice Ocaml code into a library and
> writing C bindings so that I can talk to it.
>
> Virtually all of the tutorials out there and documentation cover how to
> create Ocaml bindings to a C library. I don't want to do that. The few
> examples I have found that are relevant are toy ones. How to write C
> bindings to an Ocaml "fib" function, e.g. In particular, I have found
> none that cover how to obtain pointers to OCaml function return values
> that are not strings or ints, how to store them in C-land, and how to pass
> them back to Ocaml as parameters.
>
> Let's take as a concrete example the following: creating C bindings for
> a simple hash table mapping string keys to integer values. If I can
> generate bindings for this that work I should be able to do so for the
> library I really care about.
>
> Here's what I put together for the hash table slightly-less-than-toy
> example. It doesn't even compile. [Oddly, with very similar incantations
> my own code does compiles but then segfaults inside one of the Ocaml fns.]
> Any help much appreciated!
>
> -Tim
>
>
> 1. The implementation.
>
> % cat ht.ml
>
> type ht = (string,int) Hashtbl.t
>
> let create () : ht = Hashtbl.create 100
>
> let add (table:ht) key valu = Hashtbl.add table key valu
>
> let mem (table:ht) key = Hashtbl.mem table key
>
> let remove (table:ht) key = if (Hashtbl.mem table key) then
> Hashtbl.remove table key
>
> let _ = Callback.register "create" create
>
> let _ = Callback.register "add" add
>
> let _ = Callback.register "mem" mem
>
> let _ = Callback.register "remove" remove
>
> 2. The wrappers
>
> % cat ht_wrap.c
> #include <caml/mlvalues.h>
> #include <caml/callback.h>
> #include <caml/alloc.h>
>
> void *ht_create (void) {
> static value *create_closure = NULL;
> if (create_closure == NULL)
> create_closure = caml_named_value("create");
> return ((void *) (caml_callback(*create_
> closure, Val_unit)));
> }
>
> void ht_add (void *ht, char *key, int val) {
> static value *add_closure = NULL;
> if (add_closure == NULL)
> add_closure = caml_named_value("add");
> caml_callback3(*add_closure, (value) ht, caml_copy_string(key),
> Val_int(val));
> }
>
> void ht_mem (void *ht, char *key) {
> static value *mem_closure = NULL;
> if (mem_closure == NULL)
> mem_closure = caml_named_value("mem");
> caml_callback2(*mem_closure, (value) ht, caml_copy_string(key));
> }
>
> void ht_remove (void *ht, char *key) {
> static value *remove_closure = NULL;
> if (remove_closure == NULL)
> remove_closure = caml_named_value("remove");
> caml_callback2(*remove_closure, (value) ht, caml_copy_string(key));
> }
>
>
> 3. Header file for those wrappers
>
> % cat ht.h
>
> void *ht_create (void);
> void ht_add (void *ht, char *key, int val);
> void ht_mem (void *ht, char *key);
> void ht_remove (void *ht, char *key);
>
> 4. The test program, with main() function.
>
> % cat ht_test.c
> #include <stdio.h>
> #include "ht.h"
>
> int main (int argc, char **argv) {
> void *ht;
>
> caml_startup(argv);
>
> ht = ht_create();
> ht_add(ht, "foo", 1);
> ht_add(ht, "foo", 1);
> ht_add(ht, "bar", 1);
> ht_remove(ht, "foo");
> }
>
>
> 5. And this is how I am attempting to compile it. Note that error I'm
> getting here is in link. If I add "-lm" it gets less noisy but still is
> mad at me. Why am I having to add math library? Again, help!!
>
> % ocamlopt -output-obj -o ht.o ht.ml
Wrong! You cannot specify ht.o as an output as ht.o is an intermediate
result of ht.ml compilation. Activate -verbose flag and you'll see.
Replace it with something like
ocamlopt -output-obj -o ht_out.o ht.ml
> % ocamlopt -c ht_wrap.c
> % cp /usr/lib/ocaml/3.10.0/libasmrun.a ./ht.a
> % ar r ht.a ht_wrap.o ht.o
> % gcc -o htt ht_test.c ht.a -lcurses
> ht.a(floats.o): In function `caml_ceil_float':
> (.text+0x193): undefined reference to `ceil'
> ht.a(floats.o): In function `caml_atan2_float':
> (.text+0x1ae): undefined reference to `atan2'
(...)
Why simply not to use
ocamlopt -c ht_wrap.c
gcc -o htt ht_test.c ht_out.o ht_wrap.o -L${OCAMLLIB} -lasmrun -lcurses -lm
?
- Dmitry Bely
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] C wrappers for Ocaml functions
2009-04-14 11:51 C wrappers for Ocaml functions Tim Leek
2009-04-14 14:06 ` [Caml-list] " Dmitry Bely
@ 2009-04-14 14:40 ` Xavier Leroy
1 sibling, 0 replies; 3+ messages in thread
From: Xavier Leroy @ 2009-04-14 14:40 UTC (permalink / raw)
To: Tim Leek; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 1628 bytes --]
> So I am investigating packing my nice Ocaml code into a library and
> writing C bindings so that I can talk to it. [...]
> In particular, I have found
> none that cover how to obtain pointers to OCaml function return values
> that are not strings or ints, how to store them in C-land, and how to pass
> them back to Ocaml as parameters.
You have two choices: copy the contents of the OCaml data structure to
a C data structure, e.g. an OCaml tuple or record of ints can be
copied to a C struct, field by field; or, treat the OCaml value as an
opaque type on C's side.
The code you posted attempts to do the second approach, but you cannot
just take a Caml "value" and give it to arbitrary C code: the Caml GC
will lose track of the value and reclaim it (or copy it elsewhere) at
the first opportunity.
Instead, you need to wrap the Caml value in a C memory block and
register it with the GC using caml_register_global_root. Then, an
explicit free function must be provided in the C interface to
unregister with the GC and to free the wrapper. See the
attached modification of your ht_wrap.c example. It should put you on
the right tracks.
> 5. And this is how I am attempting to compile it. Note that error I'm
> getting here is in link. If I add "-lm" it gets less noisy but still is
> mad at me. Why am I having to add math library?
Because the Caml runtime system needs it. As Dmitry wrote, the
simplest way to build your example is:
ocamlopt -output-obj -o ht_lib.o ht.ml
ocamlopt -c ht_wrap.c
gcc -o htt ht_test.c ht_lib.o ht_wrap.o -L`ocamlopt -where` -lasmrun -lcurses -lm
Hope this helps,
- Xavier Leroy
[-- Attachment #2: ht_wrap.c --]
[-- Type: text/x-c, Size: 1381 bytes --]
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include "ht.h"
struct hashtable {
value v;
};
static struct hashtable * ht_alloc (value v)
{
struct hashtable * wrapper = malloc(sizeof(struct hashtable));
if (wrapper == NULL) return NULL;
wrapper->v = v;
caml_register_global_root(&(wrapper->v));
return wrapper;
}
void ht_free (struct hashtable * ht)
{
caml_remove_global_root(&(ht->v));
free(ht);
}
struct hashtable * ht_create (void) {
static value *create_closure = NULL;
if (create_closure == NULL)
create_closure = caml_named_value("create");
return ht_alloc(caml_callback(*create_closure, Val_unit));
}
void ht_add (struct hashtable * ht, char *key, int val) {
static value *add_closure = NULL;
if (add_closure == NULL)
add_closure = caml_named_value("add");
caml_callback3(*add_closure, ht->v, caml_copy_string(key), Val_int(val));
}
void ht_mem (struct hashtable * ht, char *key) {
static value *mem_closure = NULL;
if (mem_closure == NULL)
mem_closure = caml_named_value("mem");
caml_callback2(*mem_closure, ht->v, caml_copy_string(key));
}
void ht_remove (struct hashtable * ht, char *key) {
static value *remove_closure = NULL;
if (remove_closure == NULL)
remove_closure = caml_named_value("remove");
caml_callback2(*remove_closure, ht->v, caml_copy_string(key));
}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-04-14 14:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-14 11:51 C wrappers for Ocaml functions Tim Leek
2009-04-14 14:06 ` [Caml-list] " Dmitry Bely
2009-04-14 14:40 ` Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox