From 45d97fcc3f852437f56dd21f8aaf3641c61c5c1f Mon Sep 17 00:00:00 2001 From: "william.valenduc" Date: Sat, 31 Jan 2026 18:30:15 +0000 Subject: [PATCH] feat(utils): hash_map_free_ast --- src/utils/hash_map/hash_map.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/utils/hash_map/hash_map.c b/src/utils/hash_map/hash_map.c index b07b63d..0da4fc4 100644 --- a/src/utils/hash_map/hash_map.c +++ b/src/utils/hash_map/hash_map.c @@ -7,6 +7,8 @@ #include #include +#include "../ast/ast.h" + /* ** Hash the key using FNV-1a 32 bits hash algorithm. */ @@ -36,6 +38,14 @@ static void destroy_pair_list(struct pair_list **p) *p = NULL; } +static void destroy_pair_list_ast(struct pair_list **p) +{ + free((char *)(*p)->key); + ast_free((*p)->value); + free((*p)); + *p = NULL; +} + struct hash_map *hash_map_init(size_t size) { struct hash_map *p = malloc(sizeof(struct hash_map)); @@ -120,6 +130,29 @@ void hash_map_free(struct hash_map **hash_map) } } +void hash_map_free_ast(struct hash_map **hash_map) +{ + struct pair_list *l; + struct pair_list *prev; + + if (hash_map != NULL && *hash_map != NULL) + { + for (size_t i = 0; i < (*hash_map)->size; i++) + { + l = (*hash_map)->data[i]; + while (l != NULL) + { + prev = l; + l = l->next; + destroy_pair_list_ast(&prev); + } + } + free((*hash_map)->data); + free(*hash_map); + *hash_map = NULL; + } +} + void hash_map_foreach(struct hash_map *hash_map, void (*fn)(const char *, const void *)) {