diff options
Diffstat (limited to 'lib/hmap.c')
-rw-r--r-- | lib/hmap.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/hmap.c b/lib/hmap.c index 1b4816d9d..6bc5ea74b 100644 --- a/lib/hmap.c +++ b/lib/hmap.c @@ -18,6 +18,7 @@ #include "hmap.h" #include <assert.h> #include <stdint.h> +#include <string.h> #include "coverage.h" #include "random.h" #include "util.h" @@ -42,6 +43,22 @@ hmap_destroy(struct hmap *hmap) } } +/* Removes all node from 'hmap', leaving it ready to accept more nodes. Does + * not free memory allocated for 'hmap'. + * + * This function is appropriate when 'hmap' will soon have about as many + * elements as it before. If 'hmap' will likely have fewer elements than + * before, use hmap_destroy() followed by hmap_clear() to save memory and + * iteration time. */ +void +hmap_clear(struct hmap *hmap) +{ + if (hmap->n > 0) { + hmap->n = 0; + memset(hmap->buckets, 0, (hmap->mask + 1) * sizeof *hmap->buckets); + } +} + /* Exchanges hash maps 'a' and 'b'. */ void hmap_swap(struct hmap *a, struct hmap *b) |