libhmap-c

C hashmap library
git clone https://git.sinitax.com/sinitax/libhmap-c
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit c98cbe1264f52a83d960cd8bf23cf0b4d5e43e25
parent c547226bb32624565150e711fab331be13ca8b1e
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri, 17 Feb 2023 20:27:29 +0100

Propogate error and prevent excessive asserting

Diffstat:
Minclude/hashmap.h | 2+-
Msrc/hashmap.c | 20+++++++-------------
2 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/include/hashmap.h b/include/hashmap.h @@ -80,7 +80,7 @@ struct hashmap_link *hashmap_pop(struct hashmap *map, const void *key, size_t si void hashmap_link_set(struct hashmap_link *link, void *key, size_t key_size, void *value, size_t value_size); -void hashmap_set(struct hashmap *map, void *key, size_t key_size, +bool hashmap_set(struct hashmap *map, void *key, size_t key_size, void *value, size_t value_size); void hashmap_iter_init(struct hashmap_iter *iter); diff --git a/src/hashmap.c b/src/hashmap.c @@ -4,29 +4,20 @@ #include <stdio.h> #include <string.h> -static size_t hashmap_key_bucket(struct hashmap *map, - const void *key, size_t size); -static bool hashmap_key_cmp(const void *a, size_t asize, - const void *b, size_t bsize); - static struct hashmap_link **hashmap_get_linkp(struct hashmap *map, const void *key, size_t size); static struct hashmap_link *hashmap_link_alloc(void *key, size_t key_size, void *value, size_t value_size); -size_t +static inline size_t hashmap_key_bucket(struct hashmap *map, const void *key, size_t size) { - LIBHASHMAP_ASSERT(map != NULL && key != NULL); - return map->hash(key, size) % map->size; } -bool +static inline bool hashmap_key_cmp(const void *a, size_t asize, const void *b, size_t bsize) { - LIBHASHMAP_ASSERT(a != NULL && asize != 0 && b != NULL && bsize != 0); - return asize == bsize && !memcmp(a, b, asize); } @@ -35,7 +26,7 @@ hashmap_get_linkp(struct hashmap *map, const void *key, size_t size) { struct hashmap_link **iter, *link; - LIBHASHMAP_ASSERT(map != NULL); + LIBHASHMAP_ASSERT(map != NULL && key != NULL && size != 0); iter = &map->buckets[hashmap_key_bucket(map, key, size)]; while (*iter != NULL) { @@ -185,7 +176,7 @@ hashmap_link_set(struct hashmap_link *link, void *key, size_t key_size, link->value_size = value_size; } -void +bool hashmap_set(struct hashmap *map, void *key, size_t key_size, void *value, size_t value_size) { @@ -203,7 +194,10 @@ hashmap_set(struct hashmap *map, void *key, size_t key_size, hashmap_link_set(*iter, key, key_size, value, value_size); } else { *iter = hashmap_link_alloc(key, key_size, value, value_size); + if (!*iter) return false; } + + return true; } void