From c039c886f9c32c81004996f2dc6227c29f6489b5 Mon Sep 17 00:00:00 2001 From: tidwall Date: Fri, 12 Mar 2021 14:39:43 -0700 Subject: [PATCH] Removed VLA --- hashmap.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hashmap.c b/hashmap.c index a2989e7..b171292 100644 --- a/hashmap.c +++ b/hashmap.c @@ -52,6 +52,7 @@ struct hashmap { size_t shrinkat; void *buckets; void *spare; + void *edata; }; static struct bucket *bucket_at(struct hashmap *map, size_t index) { @@ -102,7 +103,9 @@ struct hashmap *hashmap_new(size_t elsize, size_t cap, while (bucketsz & (sizeof(uintptr_t)-1)) { bucketsz++; } - struct hashmap *map = hmmalloc(sizeof(struct hashmap)+bucketsz); + // hashmap + spare + edata + size_t size = sizeof(struct hashmap)+bucketsz+elsize; + struct hashmap *map = hmmalloc(size); if (!map) { return NULL; } @@ -115,6 +118,7 @@ struct hashmap *hashmap_new(size_t elsize, size_t cap, map->compare = compare; map->udata = udata; map->spare = ((char*)map)+sizeof(struct hashmap); + map->edata = map->spare+bucketsz; map->cap = cap; map->nbuckets = cap; map->mask = map->nbuckets-1; @@ -207,8 +211,8 @@ void *hashmap_set(struct hashmap *map, void *item) { } } - char edata[map->bucketsz]; // VLA - struct bucket *entry = (void*)edata; + + struct bucket *entry = map->edata; entry->hash = get_hash(map, item); entry->dib = 1; memcpy(bucket_item(entry), item, map->elsize);