7 * strdup does it's own malloc, we need to track malloc. We don't want
8 * to overwrite malloc though, infact, we can't really hook it at all
9 * without library specific assumptions. So we re implement strdup.
11 char *stat_mem_strdup(const char *src, bool empty) {
19 if ((!empty ? len : true) && (ptr = (char*)mem_a(len + 1))) {
20 memcpy(ptr, src, len);
28 * The reallocate function for resizing vectors.
30 void _util_vec_grow(void **a, size_t i, size_t s) {
31 vector_t *d = vec_meta(*a);
36 m = 2 * d->allocated + i;
37 p = mem_r(d, s * m + sizeof(vector_t));
40 p = mem_a(s * m + sizeof(vector_t));
41 ((vector_t*)p)->used = 0;
49 void _util_vec_delete(void *data) {
50 mem_d(vec_meta(data));
54 * Hash table for generic data, based on dynamic memory allocations
55 * all around. This is the internal interface, please look for
56 * EXPOSED INTERFACE comment below
58 typedef struct hash_node_t {
59 char *key; /* the key for this node in table */
60 void *value; /* pointer to the data as void* */
61 struct hash_node_t *next; /* next node (linked list) */
64 size_t hash(const char *key);
66 size_t util_hthash(hash_table_t *ht, const char *key) {
67 return hash(key) % ht->size;
70 static hash_node_t *_util_htnewpair(const char *key, void *value) {
72 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
75 if (!(node->key = util_strdupe(key))) {
87 * EXPOSED INTERFACE for the hashtable implementation
88 * util_htnew(size) -- to make a new hashtable
89 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
90 * util_htget(table, key) -- to get something from the table
91 * util_htdel(table) -- to delete the table
93 hash_table_t *util_htnew(size_t size) {
94 hash_table_t *hashtable = NULL;
99 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
102 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
107 hashtable->size = size;
108 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
113 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
114 hash_node_t *newnode = NULL;
115 hash_node_t *next = NULL;
116 hash_node_t *last = NULL;
118 next = ht->table[bin];
120 while (next && next->key && strcmp(key, next->key) > 0)
121 last = next, next = next->next;
123 /* already in table, do a replace */
124 if (next && next->key && strcmp(key, next->key) == 0) {
127 /* not found, grow a pair man :P */
128 newnode = _util_htnewpair(key, value);
129 if (next == ht->table[bin]) {
130 newnode->next = next;
131 ht->table[bin] = newnode;
133 last->next = newnode;
135 newnode->next = next;
136 last->next = newnode;
141 void util_htset(hash_table_t *ht, const char *key, void *value) {
142 util_htseth(ht, key, util_hthash(ht, key), value);
145 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
146 hash_node_t *pair = ht->table[bin];
148 while (pair && pair->key && strcmp(key, pair->key) > 0)
151 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
157 void *util_htget(hash_table_t *ht, const char *key) {
158 return util_htgeth(ht, key, util_hthash(ht, key));
161 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
162 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
167 keylen = strlen(key);
169 pair = ht->table[bin];
170 while (pair && pair->key) {
171 len = strlen(pair->key);
177 cmp = strcmp(key, pair->key);
185 cmp = strcmp(key, pair->key + len - keylen);
187 uintptr_t up = (uintptr_t)pair->value;
197 * Free all allocated data in a hashtable, this is quite the amount
200 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
203 for (; i < ht->size; ++i) {
204 hash_node_t *n = ht->table[i];
224 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
225 hash_node_t **pair = &ht->table[bin];
228 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
229 pair = &(*pair)->next;
232 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
243 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
244 util_htrmh(ht, key, util_hthash(ht, key), cb);
247 void util_htdel(hash_table_t *ht) {
248 util_htrem(ht, NULL);