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 = nullptr;
37 m = 2 * d->allocated + i;
38 p = mem_r(d, s * m + sizeof(vector_t));
41 p = mem_a(s * m + sizeof(vector_t));
42 ((vector_t*)p)->used = 0;
50 void _util_vec_delete(void *data) {
51 mem_d(vec_meta(data));
55 * Hash table for generic data, based on dynamic memory allocations
56 * all around. This is the internal interface, please look for
57 * EXPOSED INTERFACE comment below
60 char *key; /* the key for this node in table */
61 void *value; /* pointer to the data as void* */
62 hash_node_t *next; /* next node (linked list) */
65 size_t hash(const char *key);
67 size_t util_hthash(hash_table_t *ht, const char *key) {
68 return hash(key) % ht->size;
71 static hash_node_t *_util_htnewpair(const char *key, void *value) {
73 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
76 if (!(node->key = util_strdupe(key))) {
88 * EXPOSED INTERFACE for the hashtable implementation
89 * util_htnew(size) -- to make a new hashtable
90 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
91 * util_htget(table, key) -- to get something from the table
92 * util_htdel(table) -- to delete the table
94 hash_table_t *util_htnew(size_t size) {
95 hash_table_t *hashtable = nullptr;
100 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
103 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
108 hashtable->size = size;
109 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
114 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
115 hash_node_t *newnode = nullptr;
116 hash_node_t *next = nullptr;
117 hash_node_t *last = nullptr;
119 next = ht->table[bin];
121 while (next && next->key && strcmp(key, next->key) > 0)
122 last = next, next = next->next;
124 /* already in table, do a replace */
125 if (next && next->key && strcmp(key, next->key) == 0) {
128 /* not found, grow a pair man :P */
129 newnode = _util_htnewpair(key, value);
130 if (next == ht->table[bin]) {
131 newnode->next = next;
132 ht->table[bin] = newnode;
134 last->next = newnode;
136 newnode->next = next;
137 last->next = newnode;
142 void util_htset(hash_table_t *ht, const char *key, void *value) {
143 util_htseth(ht, key, util_hthash(ht, key), value);
146 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
147 hash_node_t *pair = ht->table[bin];
149 while (pair && pair->key && strcmp(key, pair->key) > 0)
152 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
158 void *util_htget(hash_table_t *ht, const char *key) {
159 return util_htgeth(ht, key, util_hthash(ht, key));
162 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
163 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
168 keylen = strlen(key);
170 pair = ht->table[bin];
171 while (pair && pair->key) {
172 len = strlen(pair->key);
178 cmp = strcmp(key, pair->key);
186 cmp = strcmp(key, pair->key + len - keylen);
188 uintptr_t up = (uintptr_t)pair->value;
198 * Free all allocated data in a hashtable, this is quite the amount
201 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
204 for (; i < ht->size; ++i) {
205 hash_node_t *n = ht->table[i];
225 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
226 hash_node_t **pair = &ht->table[bin];
229 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
230 pair = &(*pair)->next;
233 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
244 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
245 util_htrmh(ht, key, util_hthash(ht, key), cb);
248 void util_htdel(hash_table_t *ht) {
249 util_htrem(ht, nullptr);