garbage collection

This commit is contained in:
biglyderv 2025-03-26 01:20:38 -04:00
parent c02e8aa59a
commit e6165f3af8
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
6 changed files with 44 additions and 21 deletions

View file

@ -5,6 +5,7 @@
//
#include "map.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@ -167,7 +168,8 @@ static struct bucket *find_entry(hashmap *m, const void *key, size_t ksize,
}
}
int hashmap_set(hashmap *m, const void *key, size_t ksize, uintptr_t val) {
int hashmap_set(hashmap *m, const void *key, size_t ksize, uintptr_t val,
int free_old) {
if (m->count + 1 > HASHMAP_MAX_LOAD * m->capacity) {
if (hashmap_resize(m) == -1)
return -1;
@ -186,6 +188,12 @@ int hashmap_set(hashmap *m, const void *key, size_t ksize, uintptr_t val) {
entry->ksize = ksize;
entry->hash = hash;
}
if (free_old && entry->value != 0 && entry->value != val &&
strcmp((char *)entry->value, (char *)val) == 0) {
free((char *)val);
return 0;
}
entry->value = val;
return 0;
}