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_lookup_from_c (lookup_info *table, int data)
60 for (i = table[0].data; i > 0; i--)
61 if (table[i].data == data) return table[i].key;
62 invalid_argument ("ml_lookup_from_c");
65 int ml_lookup_to_c (lookup_info *table, value key)
67 int first = 1, last = table[0].data, current;
69 while (first < last) {
70 current = (first+last)/2;
71 if (table[current].key >= key) last = current;
72 else first = current + 1;
74 if (table[first].key == key) return table[first].data;
75 invalid_argument ("ml_lookup_to_c");