refactor(hashmap): use atomic size

This commit is contained in:
2025-12-02 02:19:23 +01:00
parent d93eef112a
commit 19c461f089
2 changed files with 110 additions and 112 deletions

View File

@@ -5,23 +5,28 @@
#include <string.h> #include <string.h>
/* /*
* @brief Returns the mutex ID. * @brief Returns the mutex ID based on hash value.
*/ */
static size_t get_mutex(hashmap_s *hashmap, size_t hash) { static inline size_t get_mutex(hashmap_s *hashmap, size_t hash) {
return hash % hashmap->num_locks; return hash % hashmap->num_locks;
} }
static size_t get_bucket_index(hashmap_s *hashmap, void *key) { /*
* @brief Returns the bucket index for a given key.
*/
static inline size_t get_bucket_index(hashmap_s *hashmap, void *key) {
unsigned int hash = hashmap->hash(key); unsigned int hash = hashmap->hash(key);
return (size_t)(hash % MYCLIB_HASHMAP_SIZE); return (size_t)(hash % MYCLIB_HASHMAP_SIZE);
} }
/*
* @brief Free the contents of a bucket (key and value).
*/
static void free_bucket_content(hashmap_s *hashmap, bucket_s *bucket) { static void free_bucket_content(hashmap_s *hashmap, bucket_s *bucket) {
if (bucket == NULL) { if (bucket == NULL) {
return; return;
} }
/* Free key if free function is provided, otherwise free raw pointer if allocated */
if (bucket->key != NULL) { if (bucket->key != NULL) {
if (hashmap->free_key != NULL) { if (hashmap->free_key != NULL) {
hashmap->free_key(bucket->key); hashmap->free_key(bucket->key);
@@ -31,7 +36,6 @@ static void free_bucket_content(hashmap_s *hashmap, bucket_s *bucket) {
bucket->key = NULL; bucket->key = NULL;
} }
/* Free value if free function is provided, otherwise free raw pointer if allocated */
if (bucket->value != NULL) { if (bucket->value != NULL) {
if (hashmap->free_value != NULL) { if (hashmap->free_value != NULL) {
hashmap->free_value(bucket->value); hashmap->free_value(bucket->value);
@@ -42,17 +46,19 @@ static void free_bucket_content(hashmap_s *hashmap, bucket_s *bucket) {
} }
} }
/*
* @brief Find a bucket by key in the chain.
* @param[out] prev Set to the previous bucket in the chain (or NULL if first).
*/
static bucket_s *find_bucket(hashmap_s *hashmap, void *key, bucket_s **prev) { static bucket_s *find_bucket(hashmap_s *hashmap, void *key, bucket_s **prev) {
size_t index = get_bucket_index(hashmap, key); size_t index = get_bucket_index(hashmap, key);
bucket_s *bucket = &hashmap->map[index]; bucket_s *bucket = &hashmap->map[index];
*prev = NULL; *prev = NULL;
/* If primary bucket has no key, nothing stored here */
if (bucket->key == NULL) { if (bucket->key == NULL) {
return NULL; return NULL;
} }
/* Search through chain */
while (bucket != NULL) { while (bucket != NULL) {
if (hashmap->equal(bucket->key, key)) { if (hashmap->equal(bucket->key, key)) {
return bucket; return bucket;
@@ -75,8 +81,6 @@ hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn,
return NULL; return NULL;
} }
memset(hashmap, 0, sizeof *hashmap);
hashmap->hash = hash_fn; hashmap->hash = hash_fn;
hashmap->equal = equal_fn; hashmap->equal = equal_fn;
hashmap->free_key = free_key_fn; hashmap->free_key = free_key_fn;
@@ -84,7 +88,7 @@ hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn,
hashmap->key_size = key_size; hashmap->key_size = key_size;
hashmap->value_size = value_size; hashmap->value_size = value_size;
hashmap->size = 0; atomic_init(&hashmap->size, 0);
hashmap->num_locks = 64; hashmap->num_locks = 64;
hashmap->locks = malloc(sizeof(mtx_t) * hashmap->num_locks); hashmap->locks = malloc(sizeof(mtx_t) * hashmap->num_locks);
@@ -93,22 +97,17 @@ hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn,
return NULL; return NULL;
} }
int ret;
for (size_t i = 0; i < hashmap->num_locks; ++i) { for (size_t i = 0; i < hashmap->num_locks; ++i) {
ret = mtx_init(&(hashmap->locks[i]), mtx_recursive); if (mtx_init(&(hashmap->locks[i]), mtx_plain) != thrd_success) {
if (ret != thrd_success) {
/* Mutex failed: cleanup previous inits and return NULL */
for (size_t j = 0; j < i; ++j) { for (size_t j = 0; j < i; ++j) {
mtx_destroy(&(hashmap->locks[j])); mtx_destroy(&(hashmap->locks[j]));
} }
free(hashmap->locks); free(hashmap->locks);
free(hashmap); free(hashmap);
return NULL; return NULL;
} }
} }
/* Initialize buckets to zero */
memset(hashmap->map, 0, sizeof(hashmap->map)); memset(hashmap->map, 0, sizeof(hashmap->map));
return hashmap; return hashmap;
@@ -119,7 +118,6 @@ void hm_free(hashmap_s *hashmap) {
return; return;
} }
/* Iterate through all buckets in the hash map */
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) { for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) {
bucket_s *bucket = &hashmap->map[i]; bucket_s *bucket = &hashmap->map[i];
@@ -127,35 +125,20 @@ void hm_free(hashmap_s *hashmap) {
free_bucket_content(hashmap, bucket); free_bucket_content(hashmap, bucket);
} }
/* Free all chained buckets */
bucket = bucket->next; bucket = bucket->next;
while (bucket != NULL) { while (bucket != NULL) {
bucket_s *next = bucket->next; bucket_s *next = bucket->next;
if (bucket->key != NULL || bucket->value != NULL) { free_bucket_content(hashmap, bucket);
if (hashmap->free_key != NULL && bucket->key != NULL) {
hashmap->free_key(bucket->key);
} else if (bucket->key != NULL) {
free(bucket->key);
}
if (hashmap->free_value != NULL && bucket->value != NULL) {
hashmap->free_value(bucket->value);
} else if (bucket->value != NULL) {
free(bucket->value);
}
}
free(bucket); free(bucket);
bucket = next; bucket = next;
} }
} }
/* Free the mutexes */
for (size_t i = 0; i < hashmap->num_locks; ++i) { for (size_t i = 0; i < hashmap->num_locks; ++i) {
mtx_destroy(&(hashmap->locks[i])); mtx_destroy(&(hashmap->locks[i]));
} }
free(hashmap->locks); free(hashmap->locks);
/* Free the hash map structure */
free(hashmap); free(hashmap);
} }
@@ -174,8 +157,10 @@ bool hm_set(hashmap_s *hashmap, void *key, void *value) {
return false; return false;
} }
size_t mutex_id = get_mutex(hashmap, (size_t)hashmap->hash(key)); unsigned int hash = hashmap->hash(key);
size_t mutex_id = get_mutex(hashmap, hash);
mtx_t *mutex = &(hashmap->locks[mutex_id]); mtx_t *mutex = &(hashmap->locks[mutex_id]);
if (mtx_lock(mutex) != thrd_success) { if (mtx_lock(mutex) != thrd_success) {
return false; return false;
} }
@@ -184,30 +169,32 @@ bool hm_set(hashmap_s *hashmap, void *key, void *value) {
bucket_s *existing = find_bucket(hashmap, key, &prev); bucket_s *existing = find_bucket(hashmap, key, &prev);
if (existing != NULL) { if (existing != NULL) {
/* Key exists - update value */
void *new_value = malloc(hashmap->value_size);
if (new_value == NULL) {
mtx_unlock(mutex);
return false;
}
memcpy(new_value, value, hashmap->value_size);
/* Free old value and assign new one */
if (hashmap->free_value != NULL && existing->value != NULL) { if (hashmap->free_value != NULL && existing->value != NULL) {
hashmap->free_value(existing->value); hashmap->free_value(existing->value);
} else if (existing->value != NULL) { } else if (existing->value != NULL) {
free(existing->value); free(existing->value);
} }
existing->value = new_value;
existing->value = malloc(hashmap->value_size);
if (existing->value == NULL) {
mtx_unlock(mutex);
return false;
}
memcpy(existing->value, value, hashmap->value_size);
mtx_unlock(mutex); mtx_unlock(mutex);
return true; return true;
} }
/* Key doesn't exist, need to insert new bucket */ /* Key doesn't exist - insert new bucket */
size_t index = get_bucket_index(hashmap, key); size_t index = get_bucket_index(hashmap, key);
bucket_s *bucket = &hashmap->map[index]; bucket_s *bucket = &hashmap->map[index];
if (bucket->key == NULL) { if (bucket->key == NULL) {
/* First bucket is empty, use it */ /* Primary bucket is empty */
bucket->key = malloc(hashmap->key_size); bucket->key = malloc(hashmap->key_size);
if (bucket->key == NULL) { if (bucket->key == NULL) {
mtx_unlock(mutex); mtx_unlock(mutex);
@@ -226,20 +213,17 @@ bool hm_set(hashmap_s *hashmap, void *key, void *value) {
memcpy(bucket->value, value, hashmap->value_size); memcpy(bucket->value, value, hashmap->value_size);
bucket->next = NULL; bucket->next = NULL;
hashmap->size++; atomic_fetch_add(&hashmap->size, 1);
mtx_unlock(mutex); mtx_unlock(mutex);
return true; return true;
} }
/* Collision: create new bucket and insert at head (after primary) */ /* Collision - create new bucket and chain it */
bucket_s *new_bucket = malloc(sizeof(bucket_s)); bucket_s *new_bucket = malloc(sizeof(bucket_s));
if (new_bucket == NULL) { if (new_bucket == NULL) {
mtx_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
new_bucket->key = NULL;
new_bucket->value = NULL;
new_bucket->next = NULL;
new_bucket->key = malloc(hashmap->key_size); new_bucket->key = malloc(hashmap->key_size);
if (new_bucket->key == NULL) { if (new_bucket->key == NULL) {
@@ -259,15 +243,18 @@ bool hm_set(hashmap_s *hashmap, void *key, void *value) {
memcpy(new_bucket->key, key, hashmap->key_size); memcpy(new_bucket->key, key, hashmap->key_size);
memcpy(new_bucket->value, value, hashmap->value_size); memcpy(new_bucket->value, value, hashmap->value_size);
/* Insert new_bucket at head of chain */ /* Insert at head of chain */
new_bucket->next = bucket->next; new_bucket->next = bucket->next;
bucket->next = new_bucket; bucket->next = new_bucket;
hashmap->size++; atomic_fetch_add(&hashmap->size, 1);
mtx_unlock(mutex); mtx_unlock(mutex);
return true; return true;
} }
/*
* @brief Create a copy of a bucket.
*/
static bucket_s *get_bucket_copy(bucket_s *from, size_t key_size, size_t value_size) { static bucket_s *get_bucket_copy(bucket_s *from, size_t key_size, size_t value_size) {
if (from == NULL) { if (from == NULL) {
return NULL; return NULL;
@@ -300,8 +287,6 @@ static bucket_s *get_bucket_copy(bucket_s *from, size_t key_size, size_t value_s
memcpy(copy->value, from->value, value_size); memcpy(copy->value, from->value, value_size);
} }
copy->next = NULL;
return copy; return copy;
} }
@@ -310,8 +295,10 @@ bucket_s *hm_get(hashmap_s *hashmap, void *key) {
return NULL; return NULL;
} }
size_t mutex_id = get_mutex(hashmap, (size_t)hashmap->hash(key)); unsigned int hash = hashmap->hash(key);
size_t mutex_id = get_mutex(hashmap, hash);
mtx_t *mutex = &(hashmap->locks[mutex_id]); mtx_t *mutex = &(hashmap->locks[mutex_id]);
if (mtx_lock(mutex) != thrd_success) { if (mtx_lock(mutex) != thrd_success) {
return NULL; return NULL;
} }
@@ -319,14 +306,13 @@ bucket_s *hm_get(hashmap_s *hashmap, void *key) {
bucket_s *prev = NULL; bucket_s *prev = NULL;
bucket_s *found = find_bucket(hashmap, key, &prev); bucket_s *found = find_bucket(hashmap, key, &prev);
if (found) { bucket_s *copy = NULL;
bucket_s *copy = get_bucket_copy(found, hashmap->key_size, hashmap->value_size); if (found != NULL) {
mtx_unlock(mutex); copy = get_bucket_copy(found, hashmap->key_size, hashmap->value_size);
return copy;
} }
mtx_unlock(mutex); mtx_unlock(mutex);
return NULL; return copy;
} }
bool hm_remove(hashmap_s *hashmap, void *key) { bool hm_remove(hashmap_s *hashmap, void *key) {
@@ -334,8 +320,10 @@ bool hm_remove(hashmap_s *hashmap, void *key) {
return false; return false;
} }
size_t mutex_id = get_mutex(hashmap, (size_t)hashmap->hash(key)); unsigned int hash = hashmap->hash(key);
size_t mutex_id = get_mutex(hashmap, hash);
mtx_t *mutex = &(hashmap->locks[mutex_id]); mtx_t *mutex = &(hashmap->locks[mutex_id]);
if (mtx_lock(mutex) != thrd_success) { if (mtx_lock(mutex) != thrd_success) {
return false; return false;
} }
@@ -349,7 +337,9 @@ bool hm_remove(hashmap_s *hashmap, void *key) {
} }
if (prev == NULL) { if (prev == NULL) {
/* Removing primary bucket */
if (to_remove->next != NULL) { if (to_remove->next != NULL) {
/* Move next bucket content to primary */
bucket_s *next_bucket = to_remove->next; bucket_s *next_bucket = to_remove->next;
free_bucket_content(hashmap, to_remove); free_bucket_content(hashmap, to_remove);
@@ -360,19 +350,18 @@ bool hm_remove(hashmap_s *hashmap, void *key) {
free(next_bucket); free(next_bucket);
} else { } else {
/* No chain, just clear */
free_bucket_content(hashmap, to_remove); free_bucket_content(hashmap, to_remove);
to_remove->next = NULL; to_remove->next = NULL;
} }
} else { } else {
free_bucket_content(hashmap, to_remove); /* Removing from chain */
prev->next = to_remove->next; prev->next = to_remove->next;
free_bucket_content(hashmap, to_remove);
free(to_remove); free(to_remove);
} }
if (hashmap->size > 0) { atomic_fetch_sub(&hashmap->size, 1);
hashmap->size--;
}
mtx_unlock(mutex); mtx_unlock(mutex);
return true; return true;
@@ -383,17 +372,7 @@ size_t hm_size(hashmap_s *hashmap) {
return 0; return 0;
} }
/* Use the first mutex to protect read of size */ return atomic_load(&hashmap->size);
mtx_t *mutex = &hashmap->locks[0];
if (mtx_lock(mutex) != thrd_success) {
return 0;
}
size_t size = hashmap->size;
mtx_unlock(mutex);
return size;
} }
bool hm_contains(hashmap_s *hashmap, void *key) { bool hm_contains(hashmap_s *hashmap, void *key) {
@@ -401,15 +380,13 @@ bool hm_contains(hashmap_s *hashmap, void *key) {
return false; return false;
} }
bool res = false;
bucket_s *b = hm_get(hashmap, key); bucket_s *b = hm_get(hashmap, key);
if (b != NULL) { if (b != NULL) {
res = true;
hm_free_bucket(b); hm_free_bucket(b);
return true;
} }
return res; return false;
} }
void hm_foreach(hashmap_s *hashmap, void (*callback)(bucket_s *bucket)) { void hm_foreach(hashmap_s *hashmap, void (*callback)(bucket_s *bucket)) {
@@ -417,14 +394,14 @@ void hm_foreach(hashmap_s *hashmap, void (*callback)(bucket_s *bucket)) {
return; return;
} }
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) { /* Lock all mutexes */
size_t mutex_id = get_mutex(hashmap, i); for (size_t i = 0; i < hashmap->num_locks; ++i) {
mtx_t *mutex = &hashmap->locks[mutex_id]; mtx_lock(&hashmap->locks[i]);
if (mtx_lock(mutex) != thrd_success) {
continue;
} }
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) {
bucket_s *bucket = &hashmap->map[i]; bucket_s *bucket = &hashmap->map[i];
if (bucket->key != NULL) { if (bucket->key != NULL) {
bucket_s *copy = get_bucket_copy(bucket, hashmap->key_size, hashmap->value_size); bucket_s *copy = get_bucket_copy(bucket, hashmap->key_size, hashmap->value_size);
if (copy != NULL) { if (copy != NULL) {
@@ -442,8 +419,11 @@ void hm_foreach(hashmap_s *hashmap, void (*callback)(bucket_s *bucket)) {
} }
bucket = bucket->next; bucket = bucket->next;
} }
}
mtx_unlock(mutex); /* Unlock all mutexes */
for (size_t i = 0; i < hashmap->num_locks; ++i) {
mtx_unlock(&hashmap->locks[i]);
} }
} }
@@ -452,13 +432,12 @@ void hm_clear(hashmap_s *hashmap) {
return; return;
} }
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) { /* Lock all mutexes */
size_t mutex_id = get_mutex(hashmap, i); for (size_t i = 0; i < hashmap->num_locks; ++i) {
mtx_t *mutex = &hashmap->locks[mutex_id]; mtx_lock(&hashmap->locks[i]);
if (mtx_lock(mutex) != thrd_success) {
continue;
} }
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) {
bucket_s *bucket = &hashmap->map[i]; bucket_s *bucket = &hashmap->map[i];
if (bucket->key != NULL || bucket->value != NULL) { if (bucket->key != NULL || bucket->value != NULL) {
@@ -468,20 +447,7 @@ void hm_clear(hashmap_s *hashmap) {
bucket = bucket->next; bucket = bucket->next;
while (bucket != NULL) { while (bucket != NULL) {
bucket_s *next = bucket->next; bucket_s *next = bucket->next;
if (bucket->key != NULL) { free_bucket_content(hashmap, bucket);
if (hashmap->free_key != NULL) {
hashmap->free_key(bucket->key);
} else {
free(bucket->key);
}
}
if (bucket->value != NULL) {
if (hashmap->free_value != NULL) {
hashmap->free_value(bucket->value);
} else {
free(bucket->value);
}
}
free(bucket); free(bucket);
bucket = next; bucket = next;
} }
@@ -489,9 +455,12 @@ void hm_clear(hashmap_s *hashmap) {
hashmap->map[i].key = NULL; hashmap->map[i].key = NULL;
hashmap->map[i].value = NULL; hashmap->map[i].value = NULL;
hashmap->map[i].next = NULL; hashmap->map[i].next = NULL;
mtx_unlock(mutex);
} }
hashmap->size = 0; atomic_store(&hashmap->size, 0);
/* Unlock all mutexes */
for (size_t i = 0; i < hashmap->num_locks; ++i) {
mtx_unlock(&hashmap->locks[i]);
}
} }

View File

@@ -1,6 +1,7 @@
#ifndef MYCLIB_HASHMAP_H #ifndef MYCLIB_HASHMAP_H
#define MYCLIB_HASHMAP_H #define MYCLIB_HASHMAP_H
#include <stdatomic.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <threads.h> #include <threads.h>
@@ -50,6 +51,7 @@ typedef void free_value_f(void *value);
/** /**
* @brief Main structure representing the hash map. * @brief Main structure representing the hash map.
* Thread-safe for concurrent operations on different keys.
*/ */
typedef struct hashmap { typedef struct hashmap {
hash_f *hash; /**< Hash function */ hash_f *hash; /**< Hash function */
@@ -59,7 +61,7 @@ typedef struct hashmap {
size_t key_size; /**< Size in bytes of the key */ size_t key_size; /**< Size in bytes of the key */
size_t value_size; /**< Size in bytes of the value */ size_t value_size; /**< Size in bytes of the value */
bucket_s map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */ bucket_s map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */
size_t size; /* Hashmap size (number of keys) */ atomic_size_t size; /**< Hashmap size (number of keys) - atomic */
mtx_t *locks; /**< Mutex array */ mtx_t *locks; /**< Mutex array */
size_t num_locks; /**< Number of mutex */ size_t num_locks; /**< Number of mutex */
} hashmap_s; } hashmap_s;
@@ -113,7 +115,7 @@ bool hm_set(hashmap_s *hashmap, void *key, void *value);
* @param[in] hashmap Pointer to the hash map. * @param[in] hashmap Pointer to the hash map.
* @param[in] key Pointer to the key to search for. * @param[in] key Pointer to the key to search for.
* @return Pointer to the copy of the bucket or NULL on failure. * @return Pointer to the copy of the bucket or NULL on failure.
* @note Free after use. * @note Free after use with hm_free_bucket().
*/ */
bucket_s *hm_get(hashmap_s *hashmap, void *key); bucket_s *hm_get(hashmap_s *hashmap, void *key);
@@ -129,9 +131,36 @@ bucket_s *hm_get(hashmap_s *hashmap, void *key);
*/ */
bool hm_remove(hashmap_s *hashmap, void *key); bool hm_remove(hashmap_s *hashmap, void *key);
/**
* @brief Get the number of entries in the hash map.
*
* @param[in] hashmap Pointer to the hash map.
* @return Number of key-value pairs in the map.
*/
size_t hm_size(hashmap_s *hashmap); size_t hm_size(hashmap_s *hashmap);
/**
* @brief Check if a key exists in the hash map.
*
* @param[in] hashmap Pointer to the hash map.
* @param[in] key Pointer to the key to search for.
* @return true if the key exists, false otherwise.
*/
bool hm_contains(hashmap_s *hashmap, void *key); bool hm_contains(hashmap_s *hashmap, void *key);
/**
* @brief Iterate over all entries in the hash map.
*
* @param[in] hashmap Pointer to the hash map.
* @param[in] callback Function called for each bucket.
*/
void hm_foreach(hashmap_s *hashmap, void (*callback)(bucket_s *bucket)); void hm_foreach(hashmap_s *hashmap, void (*callback)(bucket_s *bucket));
/**
* @brief Remove all entries from the hash map.
*
* @param[in] hashmap Pointer to the hash map.
*/
void hm_clear(hashmap_s *hashmap); void hm_clear(hashmap_s *hashmap);
#endif /* MYCLIB_HASHMAP_H */ #endif /* MYCLIB_HASHMAP_H */