Fixed leaks in hashmap
This commit is contained in:
parent
544ed8b045
commit
f75685a6c7
12 changed files with 99 additions and 75 deletions
1
includes/aq
Symbolic link
1
includes/aq
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
../allocator/include/aq
|
||||||
|
|
@ -73,7 +73,7 @@ typedef struct s_hashmap_env
|
||||||
/// @param cmp The comparison function
|
/// @param cmp The comparison function
|
||||||
/// @param drop The drop function
|
/// @param drop The drop function
|
||||||
/// @return A new hashmap
|
/// @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
|
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions
|
||||||
/// @param hash The hash function
|
/// @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 drop The drop function
|
||||||
/// @param cap The number of buckets
|
/// @param cap The number of buckets
|
||||||
/// @return A new hashmap
|
/// @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
|
/// @brief Free the hashmap and all of its entries
|
||||||
/// @param hmap The hashmap to drop
|
/// @param hmap The hashmap to free
|
||||||
void hmap_free_env(t_hashmap_env *hmap);
|
void hmap_env_free(t_hashmap_env *hmap);
|
||||||
|
|
||||||
/// @brief Inserts a key-value pair into the hashmap
|
/// @brief Inserts a key-value pair into the hashmap
|
||||||
/// @param hmap The hashmap
|
/// @param hmap The hashmap
|
||||||
/// @param key The key
|
/// @param key The key
|
||||||
/// @param value The value
|
/// @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
|
/// @brief Gets the value associated with the key
|
||||||
/// @param hmap The hashmap
|
/// @param hmap The hashmap
|
||||||
/// @param key The key
|
/// @param key The key
|
||||||
/// @return The value associated with the key, or NULL if the key is not in the hashmap
|
/// @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
|
/// @brief Removes the key-value pair from the hashmap
|
||||||
/// @param hmap The hashmap
|
/// @param hmap The hashmap
|
||||||
/// @param key The key
|
/// @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
|
/// @brief Get an entry from the hashmap
|
||||||
/// @param hmap 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
|
/// @param prev The previous entry in the bucket
|
||||||
/// @return The entry, or NULL if the key is not in the hashmap
|
/// @return The entry, or NULL if the key is not in the hashmap
|
||||||
/// @note this is an internal function
|
/// @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
|
/// @brief Iterates over the hashmap and calls the given function for each key-value pair
|
||||||
/// @param self The hashmap
|
/// @param self The hashmap
|
||||||
|
|
|
||||||
30
output/src/hashmap/env/env.c
vendored
30
output/src/hashmap/env/env.c
vendored
|
|
@ -14,19 +14,18 @@
|
||||||
#include "me/hash/sip.h"
|
#include "me/hash/sip.h"
|
||||||
#include "me/hashmap/hashmap_env.h"
|
#include "me/hashmap/hashmap_env.h"
|
||||||
#include "me/mem/mem.h"
|
#include "me/mem/mem.h"
|
||||||
#include "me/mem/mem.h"
|
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_hashmap_env *hmap_new_env(t_hash_env_fn hfunc,
|
t_hashmap_env *hmap_env_new(t_hash_env_fn hfunc,
|
||||||
t_eq_env_fn cfunc,
|
t_eq_env_fn cfunc,
|
||||||
t_drop_env_fn drop)
|
t_drop_env_fn drop)
|
||||||
{
|
{
|
||||||
return (hmap_new_with_buckets_env(hfunc, cfunc, drop,
|
return (
|
||||||
DEFAULT_BUCKETS));
|
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_hash_env_fn hfunc, t_eq_env_fn cfunc,
|
||||||
t_drop_env_fn drop, t_usize buckets)
|
t_drop_env_fn drop, t_usize buckets)
|
||||||
{
|
{
|
||||||
|
|
@ -46,17 +45,22 @@ t_hashmap_env *hmap_new_with_buckets_env(
|
||||||
return (hmap);
|
return (hmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hmap_free_env(t_hashmap_env *hmap)
|
void hmap_env_free(t_hashmap_env *hmap)
|
||||||
{
|
{
|
||||||
t_usize index;
|
t_usize index;
|
||||||
|
t_entry_env *entry;
|
||||||
|
t_entry_env *tmp;
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
while (index < hmap->num_buckets)
|
while (index < hmap->num_buckets)
|
||||||
{
|
{
|
||||||
if (hmap->buckets[index])
|
entry = hmap->buckets[index];
|
||||||
|
while (entry != NULL)
|
||||||
{
|
{
|
||||||
hmap->drop(hmap->buckets[index]->kv);
|
hmap->drop(entry->kv);
|
||||||
mem_free(hmap->buckets[index]);
|
tmp = entry->next;
|
||||||
|
mem_free(entry);
|
||||||
|
entry = tmp;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
@ -65,7 +69,7 @@ void hmap_free_env(t_hashmap_env *hmap)
|
||||||
mem_free(hmap);
|
mem_free(hmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_entry_env *hmap_get_entry_env(t_hashmap_env *hmap,
|
t_entry_env *hmap_env_get_entry(t_hashmap_env *hmap,
|
||||||
t_usize hashed_key,
|
t_usize hashed_key,
|
||||||
t_str *key,
|
t_str *key,
|
||||||
t_entry_env **prev)
|
t_entry_env **prev)
|
||||||
|
|
@ -88,7 +92,7 @@ t_entry_env *hmap_get_entry_env(t_hashmap_env *hmap,
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hmap_insert_env(t_hashmap_env *hmap, t_str key,
|
bool hmap_env_insert(t_hashmap_env *hmap, t_str key,
|
||||||
t_str value)
|
t_str value)
|
||||||
{
|
{
|
||||||
t_usize hashed_key;
|
t_usize hashed_key;
|
||||||
|
|
@ -98,7 +102,7 @@ void hmap_insert_env(t_hashmap_env *hmap, t_str key,
|
||||||
hmap->hfunc(&hmap->hasher, &key);
|
hmap->hfunc(&hmap->hasher, &key);
|
||||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||||
prev = NULL;
|
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)
|
if (entry == NULL)
|
||||||
{
|
{
|
||||||
entry = mem_alloc(sizeof(t_entry_env));
|
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;
|
hmap->buckets[hashed_key % hmap->num_buckets] = entry;
|
||||||
else
|
else
|
||||||
prev->next = entry;
|
prev->next = entry;
|
||||||
|
return (false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
hmap->drop(entry->kv);
|
hmap->drop(entry->kv);
|
||||||
entry->kv.key = key;
|
entry->kv.key = key;
|
||||||
entry->kv.val = value;
|
entry->kv.val = value;
|
||||||
|
return (true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
output/src/hashmap/env/env_utils.c
vendored
9
output/src/hashmap/env/env_utils.c
vendored
|
|
@ -13,11 +13,10 @@
|
||||||
#include "me/hash/sip.h"
|
#include "me/hash/sip.h"
|
||||||
#include "me/hashmap/hashmap_env.h"
|
#include "me/hashmap/hashmap_env.h"
|
||||||
#include "me/mem/mem.h"
|
#include "me/mem/mem.h"
|
||||||
#include "me/mem/mem.h"
|
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_str *hmap_get_env(t_hashmap_env *hmap,
|
t_str *hmap_env_get(t_hashmap_env *hmap,
|
||||||
t_str *key)
|
t_str *key)
|
||||||
{
|
{
|
||||||
t_usize hashed_key;
|
t_usize hashed_key;
|
||||||
|
|
@ -26,13 +25,13 @@ t_str *hmap_get_env(t_hashmap_env *hmap,
|
||||||
|
|
||||||
hmap->hfunc(&hmap->hasher, key);
|
hmap->hfunc(&hmap->hasher, key);
|
||||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
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)
|
if (entry == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (&entry->kv.val);
|
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_usize hashed_key;
|
||||||
t_entry_env *prev;
|
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);
|
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||||
hmap->hasher = hasher_sip13_new();
|
hmap->hasher = hasher_sip13_new();
|
||||||
prev = NULL;
|
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)
|
if (entry == NULL)
|
||||||
return;
|
return;
|
||||||
if (prev == NULL)
|
if (prev == NULL)
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@
|
||||||
#define STACK_VERSION_NONE ((t_stack_version)-1)
|
#define STACK_VERSION_NONE ((t_stack_version)-1)
|
||||||
#define TS_DECODE_ERROR (-1)
|
#define TS_DECODE_ERROR (-1)
|
||||||
|
|
||||||
#if true
|
#if false
|
||||||
# undef malloc
|
# undef malloc
|
||||||
# undef calloc
|
# undef calloc
|
||||||
# undef realloc
|
# undef realloc
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */
|
/* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/05/19 14:55:56 by maiboyer ### ########.fr */
|
/* Updated: 2024/05/21 14:55:11 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -47,7 +47,7 @@ static void _free_env(t_kv_env kv)
|
||||||
|
|
||||||
t_hashmap_env *create_env_map(void)
|
t_hashmap_env *create_env_map(void)
|
||||||
{
|
{
|
||||||
return (hmap_new_env(_hash_str, _cmp_str, _free_env));
|
return (hmap_env_new(_hash_str, _cmp_str, _free_env));
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error _build_envp_iterator(t_usize idx, const t_str *key, t_str *val,
|
t_error _build_envp_iterator(t_usize idx, const t_str *key, t_str *val,
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/05/09 15:00:53 by rparodi #+# #+# */
|
/* Created: 2024/05/09 15:00:53 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/05/19 14:56:22 by maiboyer ### ########.fr */
|
/* Updated: 2024/05/21 14:57:28 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
#include "app/node.h"
|
#include "app/node.h"
|
||||||
#include "app/state.h"
|
#include "app/state.h"
|
||||||
#include "gmr/symbols.h"
|
#include "gmr/symbols.h"
|
||||||
|
#include "me/mem/mem.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include "me/vec/vec_str.h"
|
#include "me/vec/vec_str.h"
|
||||||
// #include "app/node/handle_program.h"
|
// #include "app/node/handle_program.h"
|
||||||
|
|
@ -22,6 +23,14 @@
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
void handle_command_free_infork(void *vshcat)
|
||||||
|
{
|
||||||
|
t_utils *shcat;
|
||||||
|
|
||||||
|
shcat = vshcat;
|
||||||
|
ft_exit(shcat, 255);
|
||||||
|
}
|
||||||
|
|
||||||
t_error handle_command(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
|
t_error handle_command(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
|
||||||
{
|
{
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
|
@ -55,11 +64,10 @@ t_error handle_command(t_node *self, t_utils *shcat, t_i32 *out_exit_code)
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
// vec_str_push(&spawn_info.arguments, NULL);
|
|
||||||
spawn_info.stdin = inherited();
|
spawn_info.stdin = inherited();
|
||||||
spawn_info.stdout = inherited();
|
spawn_info.stdout = inherited();
|
||||||
spawn_info.stderr = inherited();
|
spawn_info.stderr = inherited();
|
||||||
spawn_info.forked_free = NULL;
|
spawn_info.forked_free = handle_command_free_infork;
|
||||||
if (build_envp(shcat->env, &spawn_info.environement))
|
if (build_envp(shcat->env, &spawn_info.environement))
|
||||||
return (vec_str_free(spawn_info.arguments), ERROR);
|
return (vec_str_free(spawn_info.arguments), ERROR);
|
||||||
if (spawn_process(spawn_info, &shcat->ret))
|
if (spawn_process(spawn_info, &shcat->ret))
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ void ft_free_utils(t_utils *s)
|
||||||
if (s->path)
|
if (s->path)
|
||||||
ft_free_strs(s->path);
|
ft_free_strs(s->path);
|
||||||
if (s->env)
|
if (s->env)
|
||||||
hmap_free_env(s->env);
|
hmap_env_free(s->env);
|
||||||
ts_parser_delete(s->parser.parser);
|
ts_parser_delete(s->parser.parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/05/19 14:55:28 by maiboyer ### ########.fr */
|
/* Updated: 2024/05/21 14:53:26 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -75,7 +75,8 @@ t_error populate_env(t_hashmap_env *env, t_str envp[])
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (temp[0] == NULL || temp[1] == NULL)
|
if (temp[0] == NULL || temp[1] == NULL)
|
||||||
return (printf("TEMP NULL\n"), ERROR);
|
return (printf("TEMP NULL\n"), ERROR);
|
||||||
hmap_insert_env(env, temp[0], temp[1]);
|
if (hmap_env_insert(env, temp[0], temp[1]))
|
||||||
|
printf("'%s' was already in the hmap ?????\n", temp[0]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ typedef struct s_hashmap_C__PREFIX__
|
||||||
/// @param cmp The comparison function
|
/// @param cmp The comparison function
|
||||||
/// @param drop The drop function
|
/// @param drop The drop function
|
||||||
/// @return A new hashmap
|
/// @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
|
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions
|
||||||
/// @param hash The hash function
|
/// @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 drop The drop function
|
||||||
/// @param cap The number of buckets
|
/// @param cap The number of buckets
|
||||||
/// @return A new hashmap
|
/// @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
|
/// @brief Free the hashmap and all of its entries
|
||||||
/// @param hmap The hashmap to drop
|
/// @param hmap The hashmap to free
|
||||||
void hmap_free_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap);
|
void hmap_C__PREFIX___free(t_hashmap_C__PREFIX__ *hmap);
|
||||||
|
|
||||||
/// @brief Inserts a key-value pair into the hashmap
|
/// @brief Inserts a key-value pair into the hashmap
|
||||||
/// @param hmap The hashmap
|
/// @param hmap The hashmap
|
||||||
/// @param key The key
|
/// @param key The key
|
||||||
/// @param value The value
|
/// @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
|
/// @brief Gets the value associated with the key
|
||||||
/// @param hmap The hashmap
|
/// @param hmap The hashmap
|
||||||
/// @param key The key
|
/// @param key The key
|
||||||
/// @return The value associated with the key, or NULL if the key is not in the hashmap
|
/// @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
|
/// @brief Removes the key-value pair from the hashmap
|
||||||
/// @param hmap The hashmap
|
/// @param hmap The hashmap
|
||||||
/// @param key The key
|
/// @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
|
/// @brief Get an entry from the hashmap
|
||||||
/// @param hmap 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
|
/// @param prev The previous entry in the bucket
|
||||||
/// @return The entry, or NULL if the key is not in the hashmap
|
/// @return The entry, or NULL if the key is not in the hashmap
|
||||||
/// @note this is an internal function
|
/// @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
|
/// @brief Iterates over the hashmap and calls the given function for each key-value pair
|
||||||
/// @param self The hashmap
|
/// @param self The hashmap
|
||||||
|
|
|
||||||
|
|
@ -14,19 +14,18 @@
|
||||||
#include "me/hash/sip.h"
|
#include "me/hash/sip.h"
|
||||||
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
||||||
#include "me/mem/mem.h"
|
#include "me/mem/mem.h"
|
||||||
#include "me/mem/mem.h"
|
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_hashmap_C__PREFIX__ *hmap_new_C__PREFIX__(t_hash_C__PREFIX___fn hfunc,
|
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hfunc,
|
||||||
t_eq_C__PREFIX___fn cfunc,
|
t_eq_C__PREFIX___fn cfunc,
|
||||||
t_drop_C__PREFIX___fn drop)
|
t_drop_C__PREFIX___fn drop)
|
||||||
{
|
{
|
||||||
return (hmap_new_with_buckets_C__PREFIX__(hfunc, cfunc, drop,
|
return (
|
||||||
DEFAULT_BUCKETS));
|
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_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc,
|
||||||
t_drop_C__PREFIX___fn drop, t_usize buckets)
|
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);
|
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_usize index;
|
||||||
|
t_entry_C__PREFIX__ *entry;
|
||||||
|
t_entry_C__PREFIX__ *tmp;
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
while (index < hmap->num_buckets)
|
while (index < hmap->num_buckets)
|
||||||
{
|
{
|
||||||
if (hmap->buckets[index])
|
entry = hmap->buckets[index];
|
||||||
|
while (entry != NULL)
|
||||||
{
|
{
|
||||||
hmap->drop(hmap->buckets[index]->kv);
|
hmap->drop(entry->kv);
|
||||||
mem_free(hmap->buckets[index]);
|
tmp = entry->next;
|
||||||
|
mem_free(entry);
|
||||||
|
entry = tmp;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
@ -65,7 +69,7 @@ void hmap_free_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
|
||||||
mem_free(hmap);
|
mem_free(hmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_entry_C__PREFIX__ *hmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
t_entry_C__PREFIX__ *hmap_C__PREFIX___get_entry(t_hashmap_C__PREFIX__ *hmap,
|
||||||
t_usize hashed_key,
|
t_usize hashed_key,
|
||||||
C__KEYTYPE__ *key,
|
C__KEYTYPE__ *key,
|
||||||
t_entry_C__PREFIX__ **prev)
|
t_entry_C__PREFIX__ **prev)
|
||||||
|
|
@ -88,7 +92,7 @@ t_entry_C__PREFIX__ *hmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hmap_insert_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
bool hmap_C__PREFIX___insert(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
||||||
C__VALTYPE__ value)
|
C__VALTYPE__ value)
|
||||||
{
|
{
|
||||||
t_usize hashed_key;
|
t_usize hashed_key;
|
||||||
|
|
@ -98,7 +102,7 @@ void hmap_insert_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
||||||
hmap->hfunc(&hmap->hasher, &key);
|
hmap->hfunc(&hmap->hasher, &key);
|
||||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||||
prev = NULL;
|
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)
|
if (entry == NULL)
|
||||||
{
|
{
|
||||||
entry = mem_alloc(sizeof(t_entry_C__PREFIX__));
|
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;
|
hmap->buckets[hashed_key % hmap->num_buckets] = entry;
|
||||||
else
|
else
|
||||||
prev->next = entry;
|
prev->next = entry;
|
||||||
|
return (false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
hmap->drop(entry->kv);
|
hmap->drop(entry->kv);
|
||||||
entry->kv.key = key;
|
entry->kv.key = key;
|
||||||
entry->kv.val = value;
|
entry->kv.val = value;
|
||||||
|
return (true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,10 @@
|
||||||
#include "me/hash/sip.h"
|
#include "me/hash/sip.h"
|
||||||
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
#include "me/hashmap/hashmap_C__PREFIX__.h"
|
||||||
#include "me/mem/mem.h"
|
#include "me/mem/mem.h"
|
||||||
#include "me/mem/mem.h"
|
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
C__VALTYPE__ *hmap_get_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
C__VALTYPE__ *hmap_C__PREFIX___get(t_hashmap_C__PREFIX__ *hmap,
|
||||||
C__KEYTYPE__ *key)
|
C__KEYTYPE__ *key)
|
||||||
{
|
{
|
||||||
t_usize hashed_key;
|
t_usize hashed_key;
|
||||||
|
|
@ -26,13 +25,13 @@ C__VALTYPE__ *hmap_get_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
|
||||||
|
|
||||||
hmap->hfunc(&hmap->hasher, key);
|
hmap->hfunc(&hmap->hasher, key);
|
||||||
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
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)
|
if (entry == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (&entry->kv.val);
|
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_usize hashed_key;
|
||||||
t_entry_C__PREFIX__ *prev;
|
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);
|
hashed_key = hasher_reset_and_finish(&hmap->hasher);
|
||||||
hmap->hasher = hasher_sip13_new();
|
hmap->hasher = hasher_sip13_new();
|
||||||
prev = NULL;
|
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)
|
if (entry == NULL)
|
||||||
return;
|
return;
|
||||||
if (prev == NULL)
|
if (prev == NULL)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue