1 #include <unordered_map>
4 #include "common_stub.hpp"
8 #include <sys/resource.h>
15 using std::unordered_map;
18 typedef unordered_map<string, pair<struct custom_operations*, size_t>> type_map_t;
19 static type_map_t * type_map = 0;
20 static value * cpp_exception = 0;
22 static void init_error()
24 caml_failwith("C++: initialization error");
27 static void init_exception()
29 cpp_exception = caml_named_value("CPlusPlusError");
30 if (cpp_exception == 0)
34 static void init_type_map()
37 type_map = new type_map_t();
42 void register_custom_(char* name,
44 void (*finalize)(value v))
46 if (type_map == 0) init_error();
47 struct custom_operations * ops =
48 (struct custom_operations*) calloc(1, sizeof(struct custom_operations));
49 ops->identifier = name;
50 ops->finalize = finalize;
51 ops->compare = custom_compare_default;
52 ops->hash = custom_hash_default;
53 ops->serialize = custom_serialize_default;
54 ops->deserialize = custom_deserialize_default;
55 type_map->insert(make_pair(string(name), make_pair(ops, size)));
58 value alloc_custom_(char* name)
62 if (type_map == 0) init_error();
63 string key = string(name);
64 type_map_t::iterator it = type_map->find(key);
65 if (it == type_map->end())
68 result = caml_alloc_custom(it->second.first, it->second.second, 1, 1);
74 ML_BINDING value sxsi_cpp_init(value unit)
80 /* Set the stack space to unlimited */
81 getrlimit(RLIMIT_STACK, &rlim);
82 if (rlim.rlim_max == RLIM_INFINITY && rlim.rlim_cur != RLIM_INFINITY) {
83 rlim.rlim_cur = RLIM_INFINITY;
84 setrlimit(RLIMIT_STACK, &rlim);
86 // mallopt(M_MMAP_THRESHOLD, 0);
91 void sxsi_raise_msg(const char * msg)
93 if (cpp_exception == 0) init_error();
94 caml_raise_with_string(*cpp_exception, msg);