aboutsummaryrefslogtreecommitdiff
path: root/src/hashtable.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hashtable.c')
-rw-r--r--src/hashtable.c28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/hashtable.c b/src/hashtable.c
index 9f5d2e1..955a3bf 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -4,8 +4,16 @@
#include "common.h"
#include "hashtable.h"
+#include "mempool.h"
+
+#define MEMPOOL_CAP 32
+#define MEMPOOL_OBJ_TYPE struct hashtable
+MEMPOOL_GENERATE(ht)
+#define MEMPOOL_CAP 128
+#define MEMPOOL_OBJ_TYPE struct hashtable_item
+MEMPOOL_GENERATE(hi)
+
// TODO:
-// - automatic growing
// - insertion options
#define HASH(ht, key) (ht->hash_func(key) % ht->cap)
@@ -17,7 +25,8 @@ static void hashtable_table_append_item(struct hashtable_item ** table, size_t i
hashtable_t hashtable_create(size_t cap, hashtable_hash_func hash_func, hashtable_equal_func equal_func)
{
hashtable_t ht;
- ERR_Z(ht = malloc(sizeof(*ht)), goto fail);
+ // ERR_Z(ht = malloc(sizeof(*ht)), goto fail);
+ ht = ht_mempool_allocate();
ht->hash_func = hash_func;
ht->equal_func = equal_func;
@@ -38,13 +47,15 @@ void hashtable_destroy(hashtable_t ht)
if(ht->table) {
hashtable_for_each_item_safe(ht, item, i) {
- free(item);
+ // free(item);
+ hi_mempool_free(item);
}
free(ht->table);
}
- free(ht);
+ // free(ht);
+ ht_mempool_free(ht);
}
void hashtable_reset(hashtable_t ht)
@@ -53,7 +64,8 @@ void hashtable_reset(hashtable_t ht)
if(ht->table) {
hashtable_for_each_item_safe(ht, item, i) {
- free(item);
+ // free(item);
+ hi_mempool_free(item);
}
}
@@ -75,7 +87,8 @@ int hashtable_insert(hashtable_t ht, void *key, void *data, void **prevkey, void
return 0;
}
- ERR_Z(item = malloc(sizeof(*item)), return -ENOMEM);
+ // ERR_Z(item = malloc(sizeof(*item)), return -ENOMEM);
+ item = hi_mempool_allocate();
item->key = key;
item->data = data;
@@ -85,7 +98,7 @@ int hashtable_insert(hashtable_t ht, void *key, void *data, void **prevkey, void
ht->size++;
if(ht->size > (ht->cap * 3/4)) {
- return hashtable_grow(ht, 1 << ht->cap);
+ return hashtable_grow(ht, ht->cap << 1);
}
return 0;
@@ -124,7 +137,6 @@ static int hashtable_grow(hashtable_t ht, size_t cap)
{
struct hashtable_item **new_table;
ERR_Z(new_table = calloc(cap, sizeof(*new_table)), return -ENOMEM);
-
if(ht->cap > 0) {
hashtable_for_each_item_safe(ht, item, i) {
// hash but with the new cap