4 #include <caml/mlvalues.h>
5 #include <caml/alloc.h>
6 #include <caml/memory.h>
7 #include <caml/callback.h>
12 value copy_memblock_indirected (void *src, asize_t size)
14 value ret = alloc (Wosize_asize(size)+2, Abstract_tag);
15 if (!src) ml_raise_null_pointer ();
18 memcpy (&Field(ret,2), src, size);
22 value ml_some (value v)
25 value ret = alloc_small(1,0);
30 void ml_raise_null_pointer ()
32 static value * exn = NULL;
34 exn = caml_named_value ("null_pointer");
35 raise_constant (*exn);
38 value Val_pointer (void *ptr)
40 value ret = alloc_small (2, Abstract_tag);
41 if (!ptr) ml_raise_null_pointer ();
42 Field(ret,1) = (value)ptr;
46 value copy_string_check (const char*str)
48 if (!str) ml_raise_null_pointer ();
49 return copy_string ((char*) str);
52 value copy_string_or_null (const char*str)
54 return copy_string (str ? (char*) str : "");
57 value *ml_global_root_new (value v)
59 value *p = stat_alloc(sizeof(value));
61 register_global_root (p);
65 void ml_global_root_destroy (void *data)
67 remove_global_root ((value *)data);
71 value ml_lookup_from_c (lookup_info *table, int data)
74 for (i = table[0].data; i > 0; i--)
75 if (table[i].data == data) return table[i].key;
76 invalid_argument ("ml_lookup_from_c");
79 int ml_lookup_to_c (lookup_info *table, value key)
81 int first = 1, last = table[0].data, current;
83 while (first < last) {
84 current = (first+last)/2;
85 if (table[current].key >= key) last = current;
86 else first = current + 1;
88 if (table[first].key == key) return table[first].data;
89 invalid_argument ("ml_lookup_to_c");