From 979de81cc18784dd88516655a20650ce69196096 Mon Sep 17 00:00:00 2001 From: Mosder Date: Sat, 12 Sep 2026 20:06:13 +0200 Subject: fix: fixed stale cap in rehash and added an error when MOS_HM_LOAD_FACTOR would have an incorrect value --- include/moslib/ds/hashmap.h | 3 +++ src/ds/hashmap.c | 2 +- tests/ds/hashmap_tests.c | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/moslib/ds/hashmap.h b/include/moslib/ds/hashmap.h index ee5de6f..d299b58 100644 --- a/include/moslib/ds/hashmap.h +++ b/include/moslib/ds/hashmap.h @@ -129,6 +129,9 @@ #ifndef MOS_HM_LOAD_FACTOR #define MOS_HM_LOAD_FACTOR 75 #endif // MOS_HM_LOAD_FACTOR +#if MOS_HM_LOAD_FACTOR < 1 || MOS_HM_LOAD_FACTOR > 100 +#error MOS_HM_LOAD_FACTOR should be an integer value between 1 and 100 +#endif #ifndef MOS_FORCE_PREFIXES diff --git a/src/ds/hashmap.c b/src/ds/hashmap.c index 9cdc663..c547af4 100644 --- a/src/ds/hashmap.c +++ b/src/ds/hashmap.c @@ -163,7 +163,7 @@ static void rehash(void *p_hm, size_t entry_size, size_t key_size, size_t key_of .filled = mos_safe_calloc((new_cap - 1) / 8 + 1, 1), .dead = mos_safe_calloc((new_cap - 1) / 8 + 1, 1), .size = mos_hm_size(hm), - .cap = 2 * hdr->cap, + .cap = new_cap, .hash = hdr->hash, .eq = hdr->eq, }; diff --git a/tests/ds/hashmap_tests.c b/tests/ds/hashmap_tests.c index 844ffb2..b6983fa 100644 --- a/tests/ds/hashmap_tests.c +++ b/tests/ds/hashmap_tests.c @@ -1,4 +1,5 @@ #define _GNU_SOURCE +#define MOS_HM_LOAD_FACTOR 1 #include "hashmap_tests.h" #include "moslib/ds/hashmap.h" -- cgit v1.2.3