garbage collection
This commit is contained in:
parent
c02e8aa59a
commit
e6165f3af8
6 changed files with 44 additions and 21 deletions
10
src/map.c
10
src/map.c
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue