1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * Generic non-thread safe hash map implementation.
6 * Copyright (c) 2019 Facebook
12 #include <linux/err.h>
15 /* start with 4 buckets */
16 #define HASHMAP_MIN_CAP_BITS 2
18 static void hashmap_add_entry(struct hashmap_entry **pprev,
19 struct hashmap_entry *entry)
25 static void hashmap_del_entry(struct hashmap_entry **pprev,
26 struct hashmap_entry *entry)
32 void hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
33 hashmap_equal_fn equal_fn, void *ctx)
35 map->hash_fn = hash_fn;
36 map->equal_fn = equal_fn;
45 struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
46 hashmap_equal_fn equal_fn,
49 struct hashmap *map = malloc(sizeof(struct hashmap));
52 return ERR_PTR(-ENOMEM);
53 hashmap__init(map, hash_fn, equal_fn, ctx);
57 void hashmap__clear(struct hashmap *map)
60 map->cap = map->cap_bits = map->sz = 0;
63 void hashmap__free(struct hashmap *map)
72 size_t hashmap__size(const struct hashmap *map)
77 size_t hashmap__capacity(const struct hashmap *map)
82 static bool hashmap_needs_to_grow(struct hashmap *map)
84 /* grow if empty or more than 75% filled */
85 return (map->cap == 0) || ((map->sz + 1) * 4 / 3 > map->cap);
88 static int hashmap_grow(struct hashmap *map)
90 struct hashmap_entry **new_buckets;
91 struct hashmap_entry *cur, *tmp;
92 size_t new_cap_bits, new_cap;
96 new_cap_bits = map->cap_bits + 1;
97 if (new_cap_bits < HASHMAP_MIN_CAP_BITS)
98 new_cap_bits = HASHMAP_MIN_CAP_BITS;
100 new_cap = 1UL << new_cap_bits;
101 new_buckets = calloc(new_cap, sizeof(new_buckets[0]));
105 hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
106 h = hash_bits(map->hash_fn(cur->key, map->ctx), new_cap_bits);
107 hashmap_add_entry(&new_buckets[h], cur);
111 map->cap_bits = new_cap_bits;
113 map->buckets = new_buckets;
118 static bool hashmap_find_entry(const struct hashmap *map,
119 const void *key, size_t hash,
120 struct hashmap_entry ***pprev,
121 struct hashmap_entry **entry)
123 struct hashmap_entry *cur, **prev_ptr;
128 for (prev_ptr = &map->buckets[hash], cur = *prev_ptr;
130 prev_ptr = &cur->next, cur = cur->next) {
131 if (map->equal_fn(cur->key, key, map->ctx)) {
142 int hashmap__insert(struct hashmap *map, const void *key, void *value,
143 enum hashmap_insert_strategy strategy,
144 const void **old_key, void **old_value)
146 struct hashmap_entry *entry;
155 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
156 if (strategy != HASHMAP_APPEND &&
157 hashmap_find_entry(map, key, h, NULL, &entry)) {
159 *old_key = entry->key;
161 *old_value = entry->value;
163 if (strategy == HASHMAP_SET || strategy == HASHMAP_UPDATE) {
165 entry->value = value;
167 } else if (strategy == HASHMAP_ADD) {
172 if (strategy == HASHMAP_UPDATE)
175 if (hashmap_needs_to_grow(map)) {
176 err = hashmap_grow(map);
179 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
182 entry = malloc(sizeof(struct hashmap_entry));
187 entry->value = value;
188 hashmap_add_entry(&map->buckets[h], entry);
194 bool hashmap__find(const struct hashmap *map, const void *key, void **value)
196 struct hashmap_entry *entry;
199 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
200 if (!hashmap_find_entry(map, key, h, NULL, &entry))
204 *value = entry->value;
208 bool hashmap__delete(struct hashmap *map, const void *key,
209 const void **old_key, void **old_value)
211 struct hashmap_entry **pprev, *entry;
214 h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
215 if (!hashmap_find_entry(map, key, h, &pprev, &entry))
219 *old_key = entry->key;
221 *old_value = entry->value;
223 hashmap_del_entry(pprev, entry);