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_C__PREFIX__
|
|||
/// @param cmp The comparison function
|
||||
/// @param drop The drop function
|
||||
/// @return A new hashmap
|
||||
t_hashmap_C__PREFIX__ *hmap_new_C__PREFIX__(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop);
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___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_C__PREFIX__ *hmap_new_C__PREFIX__(t_hash_C__PREFIX___fn hash, t_eq_C__
|
|||
/// @param drop The drop function
|
||||
/// @param cap The number of buckets
|
||||
/// @return A new hashmap
|
||||
t_hashmap_C__PREFIX__ *hmap_new_with_buckets_C__PREFIX__(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop, size_t cap);
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop, size_t cap);
|
||||
|
||||
/// @brief Drops the hashmap and all of its entries
|
||||
/// @param hmap The hashmap to drop
|
||||
void hmap_free_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap);
|
||||
/// @brief Free the hashmap and all of its entries
|
||||
/// @param hmap The hashmap to free
|
||||
void hmap_C__PREFIX___free(t_hashmap_C__PREFIX__ *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_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key, C__VALTYPE__ value);
|
||||
/// @return true if the key already existed before, false otherwise
|
||||
bool hmap_C__PREFIX___insert(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key, C__VALTYPE__ 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
|
||||
C__VALTYPE__ *hmap_get_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
|
||||
C__VALTYPE__ *hmap_C__PREFIX___get(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
|
||||
|
||||
/// @brief Removes the key-value pair from the hashmap
|
||||
/// @param hmap The hashmap
|
||||
/// @param key The key
|
||||
void hmap_remove_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
|
||||
void hmap_C__PREFIX___remove(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
|
||||
|
||||
/// @brief Get an entry from the hashmap
|
||||
/// @param hmap The hashmap
|
||||
|
|
@ -110,7 +112,7 @@ void hmap_remove_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *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_C__PREFIX__ *hmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, t_usize hash, C__KEYTYPE__ *key, t_entry_C__PREFIX__ **prev);
|
||||
t_entry_C__PREFIX__ *hmap_C__PREFIX___get_entry(t_hashmap_C__PREFIX__ *hmap, t_usize hash, C__KEYTYPE__ *key, t_entry_C__PREFIX__ **prev);
|
||||
|
||||
/// @brief Iterates over the hashmap and calls the given function for each key-value pair
|
||||
/// @param self The hashmap
|
||||
|
|
|
|||
|
|
@ -14,19 +14,18 @@
|
|||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_hashmap_C__PREFIX__ *hmap_new_C__PREFIX__(t_hash_C__PREFIX___fn hfunc,
|
||||
t_eq_C__PREFIX___fn cfunc,
|
||||
t_drop_C__PREFIX___fn drop)
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hfunc,
|
||||
t_eq_C__PREFIX___fn cfunc,
|
||||
t_drop_C__PREFIX___fn drop)
|
||||
{
|
||||
return (hmap_new_with_buckets_C__PREFIX__(hfunc, cfunc, drop,
|
||||
DEFAULT_BUCKETS));
|
||||
return (
|
||||
hmap_C__PREFIX___new_with_buckets(hfunc, cfunc, drop, DEFAULT_BUCKETS));
|
||||
}
|
||||
|
||||
t_hashmap_C__PREFIX__ *hmap_new_with_buckets_C__PREFIX__(
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(
|
||||
t_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc,
|
||||
t_drop_C__PREFIX___fn drop, t_usize buckets)
|
||||
{
|
||||
|
|
@ -46,17 +45,22 @@ t_hashmap_C__PREFIX__ *hmap_new_with_buckets_C__PREFIX__(
|
|||
return (hmap);
|
||||
}
|
||||
|
||||
void hmap_free_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
|
||||
void hmap_C__PREFIX___free(t_hashmap_C__PREFIX__ *hmap)
|
||||
{
|
||||
t_usize index;
|
||||
t_entry_C__PREFIX__ *entry;
|
||||
t_entry_C__PREFIX__ *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_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
|
|||
mem_free(hmap);
|
||||
}
|
||||
|
||||
t_entry_C__PREFIX__ *hmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
||||
t_usize hashed_key,
|
||||
C__KEYTYPE__ *key,
|
||||
t_entry_C__PREFIX__ **prev)
|
||||
t_entry_C__PREFIX__ *hmap_C__PREFIX___get_entry(t_hashmap_C__PREFIX__ *hmap,
|
||||
t_usize hashed_key,
|
||||
C__KEYTYPE__ *key,
|
||||
t_entry_C__PREFIX__ **prev)
|
||||
{
|
||||
t_entry_C__PREFIX__ *entry;
|
||||
|
||||
|
|
@ -88,8 +92,8 @@ t_entry_C__PREFIX__ *hmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
|||
return (NULL);
|
||||
}
|
||||
|
||||
void hmap_insert_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
||||
C__VALTYPE__ value)
|
||||
bool hmap_C__PREFIX___insert(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
||||
C__VALTYPE__ value)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_C__PREFIX__ *prev;
|
||||
|
|
@ -98,7 +102,7 @@ void hmap_insert_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
|||
hmap->hfunc(&hmap->hasher, &key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
prev = NULL;
|
||||
entry = hmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev);
|
||||
entry = hmap_C__PREFIX___get_entry(hmap, hashed_key, &key, &prev);
|
||||
if (entry == NULL)
|
||||
{
|
||||
entry = mem_alloc(sizeof(t_entry_C__PREFIX__));
|
||||
|
|
@ -109,11 +113,13 @@ void hmap_insert_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,12 +13,11 @@
|
|||
#include "me/hash/sip.h"
|
||||
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
C__VALTYPE__ *hmap_get_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
||||
C__KEYTYPE__ *key)
|
||||
C__VALTYPE__ *hmap_C__PREFIX___get(t_hashmap_C__PREFIX__ *hmap,
|
||||
C__KEYTYPE__ *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_C__PREFIX__ *entry;
|
||||
|
|
@ -26,13 +25,13 @@ C__VALTYPE__ *hmap_get_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
|||
|
||||
hmap->hfunc(&hmap->hasher, key);
|
||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
entry = hmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
|
||||
entry = hmap_C__PREFIX___get_entry(hmap, hashed_key, key, &prev);
|
||||
if (entry == NULL)
|
||||
return (NULL);
|
||||
return (&entry->kv.val);
|
||||
}
|
||||
|
||||
void hmap_remove_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
|
||||
void hmap_C__PREFIX___remove(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
|
||||
{
|
||||
t_usize hashed_key;
|
||||
t_entry_C__PREFIX__ *prev;
|
||||
|
|
@ -42,7 +41,7 @@ void hmap_remove_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
|
|||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||
hmap->hasher = hasher_sip13_new();
|
||||
prev = NULL;
|
||||
entry = hmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
|
||||
entry = hmap_C__PREFIX___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