summaryrefslogtreecommitdiff
path: root/src/hash_table.c
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2023-01-08 14:44:25 -0800
committerPreston Pan <preston@nullring.xyz>2023-01-08 14:44:25 -0800
commit87d82ead963c24d84a4f6e417b96b9bf73d132bb (patch)
tree096e60118f6e62508db653d5102d85e77b8d73e9 /src/hash_table.c
parentaa1dd020edb82f26dd5bc29378177cfcaa4c53ed (diff)
fix memory problem
Diffstat (limited to 'src/hash_table.c')
-rw-r--r--src/hash_table.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/hash_table.c b/src/hash_table.c
index 6648bd1..29946b1 100644
--- a/src/hash_table.c
+++ b/src/hash_table.c
@@ -78,6 +78,15 @@ void sl_list_add(sl_list_t *l, char *key, ast_t *value) {
}
}
+void sl_list_modify(sl_list_t *l, char *key, ast_t *value) {
+ sl_node_t *cur = l->head;
+ while (cur != NULL) {
+ if (strcmp(cur->value->key, key) == 0)
+ cur->value->value = value;
+ cur = cur->next;
+ }
+}
+
ast_t *sl_list_get(sl_list_t *l, char *key) {
sl_node_t *cur = l->head;
for (int i = 0; i < l->size; i++) {
@@ -123,6 +132,8 @@ hash_table_t *init_hash_table(int size) {
void hash_table_add(hash_table_t *h, char *key, ast_t *value) {
sl_list_t *l = h->buckets[hash(key, h->size)];
+ if (sl_list_exists(l, key))
+ sl_list_modify(l, key, value);
sl_list_add(l, key, value);
}