feat(utils): hash_map_free_ast
This commit is contained in:
parent
5eed5fa65f
commit
45d97fcc3f
1 changed files with 33 additions and 0 deletions
|
|
@ -7,6 +7,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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 *))
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue