Fixed leaks in hashmap
This commit is contained in:
parent
544ed8b045
commit
f75685a6c7
12 changed files with 99 additions and 75 deletions
|
|
@ -73,7 +73,7 @@ typedef struct s_hashmap_env
|
|||
/// @param cmp The comparison function
|
||||
/// @param drop The drop function
|
||||
/// @return A new hashmap
|
||||
t_hashmap_env *hmap_new_env(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop);
|
||||
t_hashmap_env *hmap_env_new(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop);
|
||||
|
||||
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions
|
||||
/// @param hash The hash function
|
||||
|
|
@ -81,27 +81,29 @@ t_hashmap_env *hmap_new_env(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn d
|
|||
/// @param drop The drop function
|
||||
/// @param cap The number of buckets
|
||||
/// @return A new hashmap
|
||||
t_hashmap_env *hmap_new_with_buckets_env(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop, size_t cap);
|
||||
t_hashmap_env *hmap_env_new_with_buckets(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop, size_t cap);
|
||||
|
||||
/// @brief Drops the hashmap and all of its entries
|
||||
/// @param hmap The hashmap to drop
|
||||
void hmap_free_env(t_hashmap_env *hmap);
|
||||
/// @brief Free the hashmap and all of its entries
|
||||
/// @param hmap The hashmap to free
|
||||
void hmap_env_free(t_hashmap_env *hmap);
|
||||
|
||||
/// @brief Inserts a key-value pair into the hashmap
|
||||
/// @param hmap The hashmap
|
||||
/// @param key The key
|
||||
/// @param value The value
|
||||
void hmap_insert_env(t_hashmap_env *hmap, t_str key, t_str value);
|
||||
/// @return true if the key already existed before, false otherwise
|
||||
bool hmap_env_insert(t_hashmap_env *hmap, t_str key, t_str value);
|
||||
|
||||
/// @brief Gets the value associated with the key
|
||||
/// @param hmap The hashmap
|
||||
/// @param key The key
|
||||
/// @return The value associated with the key, or NULL if the key is not in the hashmap
|
||||
t_str *hmap_get_env(t_hashmap_env *hmap, t_str *key);
|
||||
t_str *hmap_env_get(t_hashmap_env *hmap, t_str *key);
|
||||
|
||||
/// @brief Removes the key-value pair from the hashmap
|
||||
/// @param hmap The hashmap
|
||||
/// @param key The key
|
||||
void hmap_remove_env(t_hashmap_env *hmap, t_str *key);
|
||||
void hmap_env_remove(t_hashmap_env *hmap, t_str *key);
|
||||
|
||||
/// @brief Get an entry from the hashmap
|
||||
/// @param hmap The hashmap
|
||||
|
|
@ -110,7 +112,7 @@ void hmap_remove_env(t_hashmap_env *hmap, t_str *key);
|
|||
/// @param prev The previous entry in the bucket
|
||||
/// @return The entry, or NULL if the key is not in the hashmap
|
||||
/// @note this is an internal function
|
||||
t_entry_env *hmap_get_entry_env(t_hashmap_env *hmap, t_usize hash, t_str *key, t_entry_env **prev);
|
||||
t_entry_env *hmap_env_get_entry(t_hashmap_env *hmap, t_usize hash, t_str *key, t_entry_env **prev);
|
||||
|
||||
/// @brief Iterates over the hashmap and calls the given function for each key-value pair
|
||||
/// @param self The hashmap
|
||||
|
|
|
|||
42
output/src/hashmap/env/env.c
vendored
42
output/src/hashmap/env/env.c
vendored
|
|
@ -14,19 +14,18 @@
|
|||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_env.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_hashmap_env *hmap_new_env(t_hash_env_fn hfunc,
|
||||
t_eq_env_fn cfunc,
|
||||
t_drop_env_fn drop)
|
||||
t_hashmap_env *hmap_env_new(t_hash_env_fn hfunc,
|
||||
t_eq_env_fn cfunc,
|
||||
t_drop_env_fn drop)
|
||||
{
|
||||
return (hmap_new_with_buckets_env(hfunc, cfunc, drop,
|
||||
DEFAULT_BUCKETS));
|
||||
return (
|
||||
hmap_env_new_with_buckets(hfunc, cfunc, drop, DEFAULT_BUCKETS));
|
||||
}
|
||||
|
||||
t_hashmap_env *hmap_new_with_buckets_env(
|
||||
t_hashmap_env *hmap_env_new_with_buckets(
|
||||
t_hash_env_fn hfunc, t_eq_env_fn cfunc,
|
||||
t_drop_env_fn drop, t_usize buckets)
|
||||
{
|
||||
|
|
@ -46,17 +45,22 @@ t_hashmap_env *hmap_new_with_buckets_env(
|
|||
return (hmap);
|
||||
}
|
||||
|
||||
void hmap_free_env(t_hashmap_env *hmap)
|
||||
void hmap_env_free(t_hashmap_env *hmap)
|
||||
{
|
||||
t_usize index;
|
||||
t_entry_env *entry;
|
||||
t_entry_env *tmp;
|
||||
|
||||
index = 0;
|
||||
while (index < hmap->num_buckets)
|
||||
{
|
||||
if (hmap->buckets[index])
|
||||
entry = hmap->buckets[index];
|
||||
while (entry != NULL)
|
||||
{
|
||||
hmap->drop(hmap->buckets[index]->kv);
|
||||
mem_free(hmap->buckets[index]);
|
||||
hmap->drop(entry->kv);
|
||||
tmp = entry->next;
|
||||
mem_free(entry);
|
||||
entry = tmp;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
|
@ -65,10 +69,10 @@ void hmap_free_env(t_hashmap_env *hmap)
|
|||
mem_free(hmap);
|
||||
}
|
||||
|
||||
t_entry_env *hmap_get_entry_env(t_hashmap_env *hmap,
|
||||
t_usize hashed_key,
|
||||
t_str *key,
|
||||
t_entry_env **prev)
|
||||
t_entry_env *hmap_env_get_entry(t_hashmap_env *hmap,
|
||||
t_usize hashed_key,
|
||||
t_str *key,
|
||||
t_entry_env **prev)
|
||||
{
|
||||
t_entry_env *entry;
|
||||
|
||||
|
|
@ -88,8 +92,8 @@ t_entry_env *hmap_get_entry_env(t_hashmap_env *hmap,
|
|||
return (NULL);
|
||||
}
|
||||
|
||||
void hmap_insert_env(t_hashmap_env *hmap, t_str key,
|
||||
t_str value)
|
||||
bool hmap_env_insert(t_hashmap_env *hmap, t_str key,
|
||||
t_str value)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_env *prev;
|
||||
|
|
@ -98,7 +102,7 @@ void hmap_insert_env(t_hashmap_env *hmap, t_str key,
|
|||
hmap->hfunc(&hmap->hasher, &key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
prev = NULL;
|
||||
entry = hmap_get_entry_env(hmap, hashed_key, &key, &prev);
|
||||
entry = hmap_env_get_entry(hmap, hashed_key, &key, &prev);
|
||||
if (entry == NULL)
|
||||
{
|
||||
entry = mem_alloc(sizeof(t_entry_env));
|
||||
|
|
@ -109,11 +113,13 @@ void hmap_insert_env(t_hashmap_env *hmap, t_str key,
|
|||
hmap->buckets[hashed_key % hmap->num_buckets] = entry;
|
||||
else
|
||||
prev->next = entry;
|
||||
return (false);
|
||||
}
|
||||
else
|
||||
{
|
||||
hmap->drop(entry->kv);
|
||||
entry->kv.key = key;
|
||||
entry->kv.val = value;
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
output/src/hashmap/env/env_utils.c
vendored
11
output/src/hashmap/env/env_utils.c
vendored
|
|
@ -13,12 +13,11 @@
|
|||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_env.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_str *hmap_get_env(t_hashmap_env *hmap,
|
||||
t_str *key)
|
||||
t_str *hmap_env_get(t_hashmap_env *hmap,
|
||||
t_str *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_env *entry;
|
||||
|
|
@ -26,13 +25,13 @@ t_str *hmap_get_env(t_hashmap_env *hmap,
|
|||
|
||||
hmap->hfunc(&hmap->hasher, key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
entry = hmap_get_entry_env(hmap, hashed_key, key, &prev);
|
||||
entry = hmap_env_get_entry(hmap, hashed_key, key, &prev);
|
||||
if (entry == NULL)
|
||||
return (NULL);
|
||||
return (&entry->kv.val);
|
||||
}
|
||||
|
||||
void hmap_remove_env(t_hashmap_env *hmap, t_str *key)
|
||||
void hmap_env_remove(t_hashmap_env *hmap, t_str *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_env *prev;
|
||||
|
|
@ -42,7 +41,7 @@ void hmap_remove_env(t_hashmap_env *hmap, t_str *key)
|
|||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
hmap->hasher = hasher_sip13_new();
|
||||
prev = NULL;
|
||||
entry = hmap_get_entry_env(hmap, hashed_key, key, &prev);
|
||||
entry = hmap_env_get_entry(hmap, hashed_key, key, &prev);
|
||||
if (entry == NULL)
|
||||
return;
|
||||
if (prev == NULL)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue