diff options
| author | Mosder <git@mosder.eu> | 2026-09-12 20:06:13 +0200 |
|---|---|---|
| committer | Mosder <git@mosder.eu> | 2026-09-12 20:11:03 +0200 |
| commit | 979de81cc18784dd88516655a20650ce69196096 (patch) | |
| tree | 4221237da1efaf2a7901669123ce5baf168ea4da | |
| parent | 5f5d33d07a0cd28f0bf8dd4bd106d355bf705a18 (diff) | |
fix: fixed stale cap in rehash and added an error when MOS_HM_LOAD_FACTOR would have an incorrect value
| -rw-r--r-- | include/moslib/ds/hashmap.h | 3 | ||||
| -rw-r--r-- | src/ds/hashmap.c | 2 | ||||
| -rw-r--r-- | tests/ds/hashmap_tests.c | 1 |
3 files changed, 5 insertions, 1 deletions
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" |
