#include #include #include #include #include "ht.h" struct hashtable { value v; }; static struct hashtable * ht_alloc (value v) { struct hashtable * wrapper = malloc(sizeof(struct hashtable)); if (wrapper == NULL) return NULL; wrapper->v = v; caml_register_global_root(&(wrapper->v)); return wrapper; } void ht_free (struct hashtable * ht) { caml_remove_global_root(&(ht->v)); free(ht); } struct hashtable * ht_create (void) { static value *create_closure = NULL; if (create_closure == NULL) create_closure = caml_named_value("create"); return ht_alloc(caml_callback(*create_closure, Val_unit)); } void ht_add (struct hashtable * ht, char *key, int val) { static value *add_closure = NULL; if (add_closure == NULL) add_closure = caml_named_value("add"); caml_callback3(*add_closure, ht->v, caml_copy_string(key), Val_int(val)); } void ht_mem (struct hashtable * ht, char *key) { static value *mem_closure = NULL; if (mem_closure == NULL) mem_closure = caml_named_value("mem"); caml_callback2(*mem_closure, ht->v, caml_copy_string(key)); } void ht_remove (struct hashtable * ht, char *key) { static value *remove_closure = NULL; if (remove_closure == NULL) remove_closure = caml_named_value("remove"); caml_callback2(*remove_closure, ht->v, caml_copy_string(key)); }