Add HashMapGetKey() so we can free bucket keys before deleting them.

This commit is contained in:
Jordan Bancino 2023-02-17 03:14:43 +00:00
parent feb11de6b0
commit 46fe667988
3 changed files with 39 additions and 0 deletions

View file

@ -293,6 +293,7 @@ DbCacheEvict(Db * db)
pthread_mutex_destroy(&ref->lock); pthread_mutex_destroy(&ref->lock);
hash = DbHashKey(ref->name); hash = DbHashKey(ref->name);
Free(HashMapGetKey(db->cache, hash));
HashMapDelete(db->cache, hash); HashMapDelete(db->cache, hash);
Free(hash); Free(hash);
@ -621,6 +622,7 @@ DbDelete(Db * db, size_t nArgs,...)
{ {
pthread_mutex_lock(&ref->lock); pthread_mutex_lock(&ref->lock);
Free(HashMapGetKey(db->cache, hash));
HashMapDelete(db->cache, hash); HashMapDelete(db->cache, hash);
JsonFree(ref->json); JsonFree(ref->json);
StringArrayFree(ref->name); StringArrayFree(ref->name);

View file

@ -244,6 +244,40 @@ HashMapGet(HashMap * map, const char *key)
return NULL; return NULL;
} }
void *
HashMapGetKey(HashMap * map, const char *key)
{
unsigned long hash;
size_t index;
if (!map || !key)
{
return NULL;
}
hash = map->hashFunc(key);
index = hash % map->capacity;
for (;;)
{
HashMapBucket *bucket = map->entries[index];
if (!bucket)
{
break;
}
if (bucket->hash == hash)
{
return bucket->key;
}
index = (index + 1) % map->capacity;
}
return NULL;
}
int int
HashMapIterate(HashMap * map, char **key, void **value) HashMapIterate(HashMap * map, char **key, void **value)
{ {

View file

@ -42,6 +42,9 @@ extern void *
extern void * extern void *
HashMapGet(HashMap *, const char *); HashMapGet(HashMap *, const char *);
extern void *
HashMapGetKey(HashMap *, const char *);
extern void * extern void *
HashMapDelete(HashMap *, const char *); HashMapDelete(HashMap *, const char *);