1 #include <unordered_map>
4 #include "common_stub.hpp"
8 #include <sys/resource.h>
13 using std::unordered_map;
16 typedef unordered_map<string, pair<struct custom_operations*, size_t>> type_map_t;
17 static type_map_t * type_map = 0;
18 static value * cpp_exception = 0;
20 static void init_error()
22 caml_failwith("C++: initialization error");
25 static void init_exception()
27 cpp_exception = caml_named_value("CPlusPlusError");
28 if (cpp_exception == 0)
32 static void init_type_map()
35 type_map = new type_map_t();
40 void register_custom_(char* name,
42 void (*finalize)(value v))
44 if (type_map == 0) init_error();
45 struct custom_operations * ops =
46 (struct custom_operations*) calloc(1, sizeof(struct custom_operations));
47 ops->identifier = name;
48 ops->finalize = finalize;
49 ops->compare = custom_compare_default;
50 ops->hash = custom_hash_default;
51 ops->serialize = custom_serialize_default;
52 ops->deserialize = custom_deserialize_default;
53 type_map->insert(make_pair(string(name), make_pair(ops, size)));
56 value alloc_custom_(char* name)
60 if (type_map == 0) init_error();
61 string key = string(name);
62 type_map_t::iterator it = type_map->find(key);
63 if (it == type_map->end())
66 result = caml_alloc_custom(it->second.first, it->second.second, 1, 1);
72 extern "C" value sxsi_cpp_init(value unit)
78 /* Set the stack space to unlimited */
79 getrlimit(RLIMIT_STACK, &rlim);
80 if (rlim.rlim_max == RLIM_INFINITY && rlim.rlim_cur != RLIM_INFINITY) {
81 rlim.rlim_cur = RLIM_INFINITY;
82 setrlimit(RLIMIT_STACK, &rlim);
88 void sxsi_raise_msg(char * msg)
90 if (cpp_exception == 0) init_error();
91 caml_raise_with_string(*cpp_exception, msg);