From 427a35b7f1403c216ce73c68438f0081db1fd0d4 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Tue, 31 Jan 2012 16:34:39 +0000 Subject: ocaml: add helpers for Some/None option types. Signed-off-by: Ian Campbell Acked-by: Ian Jackson Committed-by: Ian Jackson --- tools/ocaml/libs/xl/xenlight_stubs.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'tools/ocaml') diff --git a/tools/ocaml/libs/xl/xenlight_stubs.c b/tools/ocaml/libs/xl/xenlight_stubs.c index 4b19e9a858..d3e4751a36 100644 --- a/tools/ocaml/libs/xl/xenlight_stubs.c +++ b/tools/ocaml/libs/xl/xenlight_stubs.c @@ -130,6 +130,19 @@ static int string_string_tuple_array_val (caml_gc *gc, char ***c_val, value v) #endif +/* Option type support as per http://www.linux-nantes.org/~fmonnier/ocaml/ocaml-wrapping-c.php */ +#define Val_none Val_int(0) +#define Some_val(v) Field(v,0) + +static value Val_some(value v) +{ + CAMLparam1(v); + CAMLlocal1(some); + some = caml_alloc(1, 0); + Store_field(some, 0, v); + CAMLreturn(some); +} + static value Val_mac (libxl_mac *c_val) { CAMLparam0(); @@ -205,14 +218,13 @@ static value Val_topologyinfo(libxl_topologyinfo *c_val) topologyinfo = caml_alloc_tuple(c_val->coremap.entries); for (i = 0; i < c_val->coremap.entries; i++) { - v = Val_int(0); /* None */ + v = Val_none; if (c_val->coremap.array[i] != LIBXL_CPUARRAY_INVALID_ENTRY) { topology = caml_alloc_tuple(3); Store_field(topology, 0, Val_int(c_val->coremap.array[i])); Store_field(topology, 1, Val_int(c_val->socketmap.array[i])); Store_field(topology, 2, Val_int(c_val->nodemap.array[i])); - v = caml_alloc(1, 0); /* Some */ - Store_field(v, 0, topology); + v = Val_some(topology); } Store_field(topologyinfo, i, v); } -- cgit v1.2.3