summaryrefslogtreecommitdiff
path: root/src/hashtable.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-17 16:17:44 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-17 16:17:44 -0800
commit4c7d2719a27d3c2fc81cb9264fac1332bfb8848e (patch)
tree7614bcede5be2246be6b67d390f437a31aa537ca /src/hashtable.c
parent5bb8f72816e7bbcffdc5053f9e7dd3d819fcd840 (diff)
added more frees
Diffstat (limited to 'src/hashtable.c')
-rw-r--r--src/hashtable.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/hashtable.c b/src/hashtable.c
index 9a8484d..3a83861 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -8,7 +8,14 @@ const unsigned int fnvOffset = 2166136261U;
pair_t init_pair(char* key, void* value) {
pair_t p;
- p.key = key;
+ if (key != NULL) {
+ char* kp = malloc((strlen(key) + 1) * sizeof(char));
+ strcpy(kp, key);
+ p.key = kp;
+ }
+ else {
+ p.key = NULL;
+ }
p.value = value;
return p;
}
@@ -20,6 +27,12 @@ hashtable_t* init_hashtable(unsigned int capacity, hashtable_t* parent) {
return ht;
}
void destroy_hashtable(hashtable_t* hashtable) {
+ /* TODO: optimize this */
+ for (int i = 0; i < hashtable->capacity; i++) {
+ if (hashtable->pairs[i].key != NULL) {
+ free(hashtable->pairs[i].key);
+ }
+ }
free(hashtable->pairs);
free(hashtable);
}