🌐 AI搜索 & 代理 主页
blob: bd2701549f55650748c7cfd5afd2dad6c2321f54 [file] [log] [blame]
Karsten Blees6a364ce2013-11-14 19:17:541#ifndef HASHMAP_H
2#define HASHMAP_H
3
Jeff Kingd40abc82019-06-20 07:41:494#include "hash.h"
5
Karsten Blees6a364ce2013-11-14 19:17:546/*
7 * Generic implementation of hash-based key-value mappings.
Stefan Beller1ecbf312017-06-30 19:14:078 *
9 * An example that maps long to a string:
10 * For the sake of the example this allows to lookup exact values, too
11 * (i.e. it is operated as a set, the value is part of the key)
12 * -------------------------------------
13 *
14 * struct hashmap map;
15 * struct long2string {
Eric Wonge2b50382019-10-06 23:30:4316 * struct hashmap_entry ent;
Stefan Beller1ecbf312017-06-30 19:14:0717 * long key;
18 * char value[FLEX_ARRAY]; // be careful with allocating on stack!
19 * };
20 *
21 * #define COMPARE_VALUE 1
22 *
Johannes Schindelin826c7782017-11-29 23:51:4123 * static int long2string_cmp(const void *hashmap_cmp_fn_data,
Eric Wong939af162019-10-06 23:30:3724 * const struct hashmap_entry *eptr,
25 * const struct hashmap_entry *entry_or_key,
Johannes Schindelin826c7782017-11-29 23:51:4126 * const void *keydata)
Stefan Beller1ecbf312017-06-30 19:14:0727 * {
Johannes Schindelin826c7782017-11-29 23:51:4128 * const char *string = keydata;
29 * unsigned flags = *(unsigned *)hashmap_cmp_fn_data;
Eric Wong939af162019-10-06 23:30:3730 * const struct long2string *e1, *e2;
31 *
32 * e1 = container_of(eptr, const struct long2string, ent);
33 * e2 = container_of(entry_or_key, const struct long2string, ent);
Stefan Beller1ecbf312017-06-30 19:14:0734 *
35 * if (flags & COMPARE_VALUE)
Johannes Schindelin826c7782017-11-29 23:51:4136 * return e1->key != e2->key ||
37 * strcmp(e1->value, string ? string : e2->value);
Stefan Beller1ecbf312017-06-30 19:14:0738 * else
Johannes Schindelin826c7782017-11-29 23:51:4139 * return e1->key != e2->key;
Stefan Beller1ecbf312017-06-30 19:14:0740 * }
41 *
42 * int main(int argc, char **argv)
43 * {
44 * long key;
Johannes Schindelin826c7782017-11-29 23:51:4145 * char value[255], action[32];
46 * unsigned flags = 0;
Stefan Beller1ecbf312017-06-30 19:14:0747 *
Eric Wong939af162019-10-06 23:30:3748 * hashmap_init(&map, long2string_cmp, &flags, 0);
Stefan Beller1ecbf312017-06-30 19:14:0749 *
Johannes Schindelin826c7782017-11-29 23:51:4150 * while (scanf("%s %ld %s", action, &key, value)) {
Stefan Beller1ecbf312017-06-30 19:14:0751 *
52 * if (!strcmp("add", action)) {
53 * struct long2string *e;
Johannes Schindelin826c7782017-11-29 23:51:4154 * FLEX_ALLOC_STR(e, value, value);
Eric Wongd22245a2019-10-06 23:30:2755 * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long)));
Stefan Beller1ecbf312017-06-30 19:14:0756 * e->key = key;
Eric Wongb94e5c12019-10-06 23:30:2957 * hashmap_add(&map, &e->ent);
Stefan Beller1ecbf312017-06-30 19:14:0758 * }
59 *
60 * if (!strcmp("print_all_by_key", action)) {
Johannes Schindelin826c7782017-11-29 23:51:4161 * struct long2string k, *e;
Eric Wongd22245a2019-10-06 23:30:2762 * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
Stefan Beller1ecbf312017-06-30 19:14:0763 * k.key = key;
64 *
Johannes Schindelin826c7782017-11-29 23:51:4165 * flags &= ~COMPARE_VALUE;
Eric Wong404ab782019-10-06 23:30:4266 * e = hashmap_get_entry(&map, &k, ent, NULL);
Eric Wongf0e63c42019-10-06 23:30:3567 * if (e) {
Johannes Schindelin826c7782017-11-29 23:51:4168 * printf("first: %ld %s\n", e->key, e->value);
Eric Wongf0e63c42019-10-06 23:30:3569 * while ((e = hashmap_get_next_entry(&map, e,
70 * struct long2string, ent))) {
Johannes Schindelin826c7782017-11-29 23:51:4171 * printf("found more: %ld %s\n", e->key, e->value);
Eric Wong6bcbdfb2019-10-06 23:30:3472 * }
Stefan Beller1ecbf312017-06-30 19:14:0773 * }
74 * }
75 *
76 * if (!strcmp("has_exact_match", action)) {
Stefan Beller1ecbf312017-06-30 19:14:0777 * struct long2string *e;
Johannes Schindelin826c7782017-11-29 23:51:4178 * FLEX_ALLOC_STR(e, value, value);
Eric Wongd22245a2019-10-06 23:30:2779 * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long)));
Stefan Beller1ecbf312017-06-30 19:14:0780 * e->key = key;
Stefan Beller1ecbf312017-06-30 19:14:0781 *
Johannes Schindelin826c7782017-11-29 23:51:4182 * flags |= COMPARE_VALUE;
Eric Wongb6c52412019-10-06 23:30:3083 * printf("%sfound\n",
84 * hashmap_get(&map, &e->ent, NULL) ? "" : "not ");
Johannes Schindelin826c7782017-11-29 23:51:4185 * free(e);
Stefan Beller1ecbf312017-06-30 19:14:0786 * }
87 *
88 * if (!strcmp("has_exact_match_no_heap_alloc", action)) {
Johannes Schindelin826c7782017-11-29 23:51:4189 * struct long2string k;
Eric Wongd22245a2019-10-06 23:30:2790 * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
Johannes Schindelin826c7782017-11-29 23:51:4191 * k.key = key;
92 *
Stefan Beller1ecbf312017-06-30 19:14:0793 * flags |= COMPARE_VALUE;
Eric Wongb6c52412019-10-06 23:30:3094 * printf("%sfound\n",
95 * hashmap_get(&map, &k->ent, value) ? "" : "not ");
Stefan Beller1ecbf312017-06-30 19:14:0796 * }
97 *
98 * if (!strcmp("end", action)) {
Eric Wongc8e424c2019-10-06 23:30:4099 * hashmap_free_entries(&map, struct long2string, ent);
Stefan Beller1ecbf312017-06-30 19:14:07100 * break;
101 * }
102 * }
Johannes Schindelin826c7782017-11-29 23:51:41103 *
104 * return 0;
Stefan Beller1ecbf312017-06-30 19:14:07105 * }
Karsten Blees6a364ce2013-11-14 19:17:54106 */
107
Stefan Beller1ecbf312017-06-30 19:14:07108/*
109 * Ready-to-use hash functions for strings, using the FNV-1 algorithm (see
110 * http://www.isthe.com/chongo/tech/comp/fnv).
111 * `strhash` and `strihash` take 0-terminated strings, while `memhash` and
112 * `memihash` operate on arbitrary-length memory.
113 * `strihash` and `memihash` are case insensitive versions.
114 * `memihash_cont` is a variant of `memihash` that allows a computation to be
115 * continued with another chunk of data.
116 */
Denton Liu55454422019-04-29 08:28:14117unsigned int strhash(const char *buf);
118unsigned int strihash(const char *buf);
119unsigned int memhash(const void *buf, size_t len);
120unsigned int memihash(const void *buf, size_t len);
121unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len);
Karsten Blees6a364ce2013-11-14 19:17:54122
Stefan Beller1ecbf312017-06-30 19:14:07123/*
124 * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
125 * for use in hash tables. Cryptographic hashes are supposed to have
126 * uniform distribution, so in contrast to `memhash()`, this just copies
127 * the first `sizeof(int)` bytes without shuffling any bits. Note that
128 * the results will be different on big-endian and little-endian
129 * platforms, so they should not be stored or transferred over the net.
130 */
Jeff Kingd40abc82019-06-20 07:41:49131static inline unsigned int oidhash(const struct object_id *oid)
Karsten Blees039dc712014-07-02 22:20:20132{
133 /*
Jeff Kingd40abc82019-06-20 07:41:49134 * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
Karsten Blees039dc712014-07-02 22:20:20135 * platforms that don't support unaligned reads.
136 */
137 unsigned int hash;
Jeff Kingd40abc82019-06-20 07:41:49138 memcpy(&hash, oid->hash, sizeof(hash));
Karsten Blees039dc712014-07-02 22:20:20139 return hash;
140}
141
Stefan Beller1ecbf312017-06-30 19:14:07142/*
143 * struct hashmap_entry is an opaque structure representing an entry in the
Eric Wonge2b50382019-10-06 23:30:43144 * hash table.
Stefan Beller1ecbf312017-06-30 19:14:07145 * Ideally it should be followed by an int-sized member to prevent unused
146 * memory on 64-bit systems due to alignment.
147 */
Karsten Blees6a364ce2013-11-14 19:17:54148struct hashmap_entry {
Stefan Beller1ecbf312017-06-30 19:14:07149 /*
150 * next points to the next entry in case of collisions (i.e. if
151 * multiple entries map to the same bucket)
152 */
Karsten Blees6a364ce2013-11-14 19:17:54153 struct hashmap_entry *next;
Stefan Beller1ecbf312017-06-30 19:14:07154
155 /* entry's hash code */
Karsten Blees6a364ce2013-11-14 19:17:54156 unsigned int hash;
157};
158
Stefan Beller1ecbf312017-06-30 19:14:07159/*
160 * User-supplied function to test two hashmap entries for equality. Shall
161 * return 0 if the entries are equal.
162 *
163 * This function is always called with non-NULL `entry` and `entry_or_key`
164 * parameters that have the same hash code.
165 *
166 * When looking up an entry, the `key` and `keydata` parameters to hashmap_get
167 * and hashmap_remove are always passed as second `entry_or_key` and third
168 * argument `keydata`, respectively. Otherwise, `keydata` is NULL.
169 *
170 * When it is too expensive to allocate a user entry (either because it is
171 * large or varialbe sized, such that it is not on the stack), then the
172 * relevant data to check for equality should be passed via `keydata`.
173 * In this case `key` can be a stripped down version of the user key data
174 * or even just a hashmap_entry having the correct hash.
175 *
176 * The `hashmap_cmp_fn_data` entry is the pointer given in the init function.
177 */
Stefan Beller7663cdc2017-06-30 19:14:05178typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data,
Eric Wong939af162019-10-06 23:30:37179 const struct hashmap_entry *entry,
180 const struct hashmap_entry *entry_or_key,
Stefan Beller7663cdc2017-06-30 19:14:05181 const void *keydata);
Karsten Blees6a364ce2013-11-14 19:17:54182
Stefan Beller1ecbf312017-06-30 19:14:07183/*
184 * struct hashmap is the hash table structure. Members can be used as follows,
185 * but should not be modified directly.
186 */
Karsten Blees6a364ce2013-11-14 19:17:54187struct hashmap {
188 struct hashmap_entry **table;
Stefan Beller1ecbf312017-06-30 19:14:07189
190 /* Stores the comparison function specified in `hashmap_init()`. */
Karsten Blees6a364ce2013-11-14 19:17:54191 hashmap_cmp_fn cmpfn;
Stefan Beller7663cdc2017-06-30 19:14:05192 const void *cmpfn_data;
Karsten Blees6a364ce2013-11-14 19:17:54193
Stefan Beller1ecbf312017-06-30 19:14:07194 /* total number of entries (0 means the hashmap is empty) */
Jeff Hostetler8b604d12017-09-06 15:43:48195 unsigned int private_size; /* use hashmap_get_size() */
Stefan Beller1ecbf312017-06-30 19:14:07196
197 /*
198 * tablesize is the allocated size of the hash table. A non-0 value
199 * indicates that the hashmap is initialized. It may also be useful
200 * for statistical purposes (i.e. `size / tablesize` is the current
201 * load factor).
202 */
203 unsigned int tablesize;
204
205 unsigned int grow_at;
206 unsigned int shrink_at;
207
Jeff Hostetler8b604d12017-09-06 15:43:48208 unsigned int do_count_items : 1;
Karsten Blees6a364ce2013-11-14 19:17:54209};
210
211/* hashmap functions */
212
Stefan Beller1ecbf312017-06-30 19:14:07213/*
214 * Initializes a hashmap structure.
215 *
216 * `map` is the hashmap to initialize.
217 *
218 * The `equals_function` can be specified to compare two entries for equality.
219 * If NULL, entries are considered equal if their hash codes are equal.
220 *
221 * The `equals_function_data` parameter can be used to provide additional data
222 * (a callback cookie) that will be passed to `equals_function` each time it
223 * is called. This allows a single `equals_function` to implement multiple
224 * comparison functions.
225 *
226 * If the total number of entries is known in advance, the `initial_size`
227 * parameter may be used to preallocate a sufficiently large table and thus
228 * prevent expensive resizing. If 0, the table is dynamically resized.
229 */
Denton Liu55454422019-04-29 08:28:14230void hashmap_init(struct hashmap *map,
Stefan Beller7663cdc2017-06-30 19:14:05231 hashmap_cmp_fn equals_function,
232 const void *equals_function_data,
233 size_t initial_size);
Stefan Beller1ecbf312017-06-30 19:14:07234
Eric Wongc8e424c2019-10-06 23:30:40235/* internal function for freeing hashmap */
236void hashmap_free_(struct hashmap *map, ssize_t offset);
237
Stefan Beller1ecbf312017-06-30 19:14:07238/*
Eric Wongc8e424c2019-10-06 23:30:40239 * Frees a hashmap structure and allocated memory, leaves entries undisturbed
Stefan Beller1ecbf312017-06-30 19:14:07240 */
Eric Wongc8e424c2019-10-06 23:30:40241#define hashmap_free(map) hashmap_free_(map, -1)
242
243/*
244 * Frees @map and all entries. @type is the struct type of the entry
245 * where @member is the hashmap_entry struct used to associate with @map
246 */
247#define hashmap_free_entries(map, type, member) \
248 hashmap_free_(map, offsetof(type, member));
Karsten Blees6a364ce2013-11-14 19:17:54249
250/* hashmap_entry functions */
251
Stefan Beller1ecbf312017-06-30 19:14:07252/*
253 * Initializes a hashmap_entry structure.
254 *
255 * `entry` points to the entry to initialize.
256 * `hash` is the hash code of the entry.
257 *
258 * The hashmap_entry structure does not hold references to external resources,
259 * and it is safe to just discard it once you are done with it (i.e. if
260 * your structure was allocated with xmalloc(), you can just free(3) it,
261 * and if it is on stack, you can just let it go out of scope).
262 */
Eric Wongd22245a2019-10-06 23:30:27263static inline void hashmap_entry_init(struct hashmap_entry *e,
264 unsigned int hash)
Karsten Blees6a364ce2013-11-14 19:17:54265{
Karsten Blees6a364ce2013-11-14 19:17:54266 e->hash = hash;
267 e->next = NULL;
268}
Karsten Blees6a364ce2013-11-14 19:17:54269
Stefan Beller1ecbf312017-06-30 19:14:07270/*
Jeff Hostetler8b604d12017-09-06 15:43:48271 * Return the number of items in the map.
272 */
273static inline unsigned int hashmap_get_size(struct hashmap *map)
274{
275 if (map->do_count_items)
276 return map->private_size;
277
278 BUG("hashmap_get_size: size not set");
279 return 0;
280}
281
282/*
Stefan Beller1ecbf312017-06-30 19:14:07283 * Returns the hashmap entry for the specified key, or NULL if not found.
284 *
285 * `map` is the hashmap structure.
286 *
287 * `key` is a user data structure that starts with hashmap_entry that has at
288 * least been initialized with the proper hash code (via `hashmap_entry_init`).
289 *
290 * `keydata` is a data structure that holds just enough information to check
291 * for equality to a given entry.
292 *
293 * If the key data is variable-sized (e.g. a FLEX_ARRAY string) or quite large,
294 * it is undesirable to create a full-fledged entry structure on the heap and
295 * copy all the key data into the structure.
296 *
297 * In this case, the `keydata` parameter can be used to pass
298 * variable-sized key data directly to the comparison function, and the `key`
299 * parameter can be a stripped-down, fixed size entry structure allocated on the
300 * stack.
301 *
302 * If an entry with matching hash code is found, `key` and `keydata` are passed
303 * to `hashmap_cmp_fn` to decide whether the entry matches the key.
304 */
Eric Wongf23a4652019-10-06 23:30:36305struct hashmap_entry *hashmap_get(const struct hashmap *map,
306 const struct hashmap_entry *key,
307 const void *keydata);
Stefan Beller1ecbf312017-06-30 19:14:07308
309/*
310 * Returns the hashmap entry for the specified hash code and key data,
311 * or NULL if not found.
312 *
313 * `map` is the hashmap structure.
314 * `hash` is the hash code of the entry to look up.
315 *
316 * If an entry with matching hash code is found, `keydata` is passed to
317 * `hashmap_cmp_fn` to decide whether the entry matches the key. The
318 * `entry_or_key` parameter of `hashmap_cmp_fn` points to a hashmap_entry
319 * structure that should not be used in the comparison.
320 */
Eric Wongf23a4652019-10-06 23:30:36321static inline struct hashmap_entry *hashmap_get_from_hash(
322 const struct hashmap *map,
323 unsigned int hash,
324 const void *keydata)
Karsten Bleesab73a9d2014-07-02 22:22:11325{
326 struct hashmap_entry key;
327 hashmap_entry_init(&key, hash);
328 return hashmap_get(map, &key, keydata);
329}
330
Stefan Beller1ecbf312017-06-30 19:14:07331/*
332 * Returns the next equal hashmap entry, or NULL if not found. This can be
333 * used to iterate over duplicate entries (see `hashmap_add`).
334 *
335 * `map` is the hashmap structure.
336 * `entry` is the hashmap_entry to start the search from, obtained via a previous
337 * call to `hashmap_get` or `hashmap_get_next`.
338 */
Eric Wong6bcbdfb2019-10-06 23:30:34339struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
Eric Wongf6eb6bd2019-10-06 23:30:28340 const struct hashmap_entry *entry);
Stefan Beller1ecbf312017-06-30 19:14:07341
342/*
343 * Adds a hashmap entry. This allows to add duplicate entries (i.e.
344 * separate values with the same key according to hashmap_cmp_fn).
345 *
346 * `map` is the hashmap structure.
347 * `entry` is the entry to add.
348 */
Eric Wongb94e5c12019-10-06 23:30:29349void hashmap_add(struct hashmap *map, struct hashmap_entry *entry);
Stefan Beller1ecbf312017-06-30 19:14:07350
351/*
352 * Adds or replaces a hashmap entry. If the hashmap contains duplicate
353 * entries equal to the specified entry, only one of them will be replaced.
354 *
355 * `map` is the hashmap structure.
356 * `entry` is the entry to add or replace.
357 * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
358 */
Eric Wong8a973d02019-10-06 23:30:39359struct hashmap_entry *hashmap_put(struct hashmap *map,
360 struct hashmap_entry *entry);
361
Eric Wong404ab782019-10-06 23:30:42362/*
363 * Adds or replaces a hashmap entry contained within @keyvar,
364 * where @keyvar is a pointer to a struct containing a
365 * "struct hashmap_entry" @member.
366 *
367 * Returns the replaced pointer which is of the same type as @keyvar,
368 * or NULL if not found.
369 */
370#define hashmap_put_entry(map, keyvar, member) \
371 container_of_or_null_offset(hashmap_put(map, &(keyvar)->member), \
372 OFFSETOF_VAR(keyvar, member))
Stefan Beller1ecbf312017-06-30 19:14:07373
374/*
375 * Removes a hashmap entry matching the specified key. If the hashmap contains
376 * duplicate entries equal to the specified key, only one of them will be
377 * removed. Returns the removed entry, or NULL if not found.
378 *
379 * Argument explanation is the same as in `hashmap_get`.
380 */
Eric Wong8a973d02019-10-06 23:30:39381struct hashmap_entry *hashmap_remove(struct hashmap *map,
382 const struct hashmap_entry *key,
383 const void *keydata);
384
Eric Wong404ab782019-10-06 23:30:42385/*
386 * Removes a hashmap entry contained within @keyvar,
387 * where @keyvar is a pointer to a struct containing a
388 * "struct hashmap_entry" @member.
389 *
390 * See `hashmap_get` for an explanation of @keydata
391 *
392 * Returns the replaced pointer which is of the same type as @keyvar,
393 * or NULL if not found.
394 */
395#define hashmap_remove_entry(map, keyvar, member, keydata) \
396 container_of_or_null_offset( \
397 hashmap_remove(map, &(keyvar)->member, keydata), \
398 OFFSETOF_VAR(keyvar, member))
Stefan Beller1ecbf312017-06-30 19:14:07399
400/*
401 * Returns the `bucket` an entry is stored in.
402 * Useful for multithreaded read access.
403 */
Jeff Hostetler0607e102017-03-22 17:14:22404int hashmap_bucket(const struct hashmap *map, unsigned int hash);
405
406/*
Stefan Beller1ecbf312017-06-30 19:14:07407 * Used to iterate over all entries of a hashmap. Note that it is
408 * not safe to add or remove entries to the hashmap while
409 * iterating.
410 */
411struct hashmap_iter {
412 struct hashmap *map;
413 struct hashmap_entry *next;
414 unsigned int tablepos;
415};
Karsten Blees6a364ce2013-11-14 19:17:54416
Stefan Beller1ecbf312017-06-30 19:14:07417/* Initializes a `hashmap_iter` structure. */
Denton Liu55454422019-04-29 08:28:14418void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter);
Stefan Beller1ecbf312017-06-30 19:14:07419
420/* Returns the next hashmap_entry, or NULL if there are no more entries. */
Eric Wong87571c32019-10-06 23:30:38421struct hashmap_entry *hashmap_iter_next(struct hashmap_iter *iter);
Stefan Beller1ecbf312017-06-30 19:14:07422
423/* Initializes the iterator and returns the first entry, if any. */
Eric Wong87571c32019-10-06 23:30:38424static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map,
Karsten Blees6a364ce2013-11-14 19:17:54425 struct hashmap_iter *iter)
426{
427 hashmap_iter_init(map, iter);
428 return hashmap_iter_next(iter);
429}
430
Eric Wong23dee692019-10-06 23:30:41431/*
432 * returns the first entry in @map using @iter, where the entry is of
433 * @type (e.g. "struct foo") and @member is the name of the
434 * "struct hashmap_entry" in @type
435 */
Eric Wong87571c32019-10-06 23:30:38436#define hashmap_iter_first_entry(map, iter, type, member) \
437 container_of_or_null(hashmap_iter_first(map, iter), type, member)
438
Eric Wong23dee692019-10-06 23:30:41439/* internal macro for hashmap_for_each_entry */
440#define hashmap_iter_next_entry_offset(iter, offset) \
441 container_of_or_null_offset(hashmap_iter_next(iter), offset)
442
443/* internal macro for hashmap_for_each_entry */
444#define hashmap_iter_first_entry_offset(map, iter, offset) \
445 container_of_or_null_offset(hashmap_iter_first(map, iter), offset)
446
447/*
448 * iterate through @map using @iter, @var is a pointer to a type
449 * containing a @member which is a "struct hashmap_entry"
450 */
451#define hashmap_for_each_entry(map, iter, var, member) \
452 for (var = hashmap_iter_first_entry_offset(map, iter, \
453 OFFSETOF_VAR(var, member)); \
Eric Wong87571c32019-10-06 23:30:38454 var; \
Eric Wong23dee692019-10-06 23:30:41455 var = hashmap_iter_next_entry_offset(iter, \
456 OFFSETOF_VAR(var, member)))
Eric Wong87571c32019-10-06 23:30:38457
Jeff Hostetler8b604d12017-09-06 15:43:48458/*
Eric Wong404ab782019-10-06 23:30:42459 * returns a pointer of type matching @keyvar, or NULL if nothing found.
460 * @keyvar is a pointer to a struct containing a
461 * "struct hashmap_entry" @member.
Eric Wongf0e63c42019-10-06 23:30:35462 */
Eric Wong404ab782019-10-06 23:30:42463#define hashmap_get_entry(map, keyvar, member, keydata) \
464 container_of_or_null_offset( \
465 hashmap_get(map, &(keyvar)->member, keydata), \
466 OFFSETOF_VAR(keyvar, member))
Eric Wongf0e63c42019-10-06 23:30:35467
468#define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \
469 container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \
470 type, member)
471/*
Eric Wong23dee692019-10-06 23:30:41472 * returns the next equal pointer to @var, or NULL if not found.
473 * @var is a pointer of any type containing "struct hashmap_entry"
474 * @member is the name of the "struct hashmap_entry" field
Eric Wongf0e63c42019-10-06 23:30:35475 */
Eric Wong23dee692019-10-06 23:30:41476#define hashmap_get_next_entry(map, var, member) \
477 container_of_or_null_offset(hashmap_get_next(map, &(var)->member), \
478 OFFSETOF_VAR(var, member))
Eric Wongf0e63c42019-10-06 23:30:35479
480/*
481 * iterate @map starting from @var, where @var is a pointer of @type
482 * and @member is the name of the "struct hashmap_entry" field in @type
483 */
Eric Wong23dee692019-10-06 23:30:41484#define hashmap_for_each_entry_from(map, var, member) \
Eric Wongf0e63c42019-10-06 23:30:35485 for (; \
486 var; \
Eric Wong23dee692019-10-06 23:30:41487 var = hashmap_get_next_entry(map, var, member))
Eric Wongf0e63c42019-10-06 23:30:35488
489/*
Jeff Hostetler8b604d12017-09-06 15:43:48490 * Disable item counting and automatic rehashing when adding/removing items.
491 *
492 * Normally, the hashmap keeps track of the number of items in the map
493 * and uses it to dynamically resize it. This (both the counting and
494 * the resizing) can cause problems when the map is being used by
495 * threaded callers (because the hashmap code does not know about the
496 * locking strategy used by the threaded callers and therefore, does
497 * not know how to protect the "private_size" counter).
498 */
499static inline void hashmap_disable_item_counting(struct hashmap *map)
500{
501 map->do_count_items = 0;
502}
503
504/*
505 * Re-enable item couting when adding/removing items.
506 * If counting is currently disabled, it will force count them.
507 * It WILL NOT automatically rehash them.
508 */
509static inline void hashmap_enable_item_counting(struct hashmap *map)
510{
Jeff Hostetler8b604d12017-09-06 15:43:48511 unsigned int n = 0;
512 struct hashmap_iter iter;
513
514 if (map->do_count_items)
515 return;
516
517 hashmap_iter_init(map, &iter);
Randall S. Becker7d68bb02018-01-14 18:07:48518 while (hashmap_iter_next(&iter))
Jeff Hostetler8b604d12017-09-06 15:43:48519 n++;
520
521 map->do_count_items = 1;
522 map->private_size = n;
523}
524
Stefan Beller1ecbf312017-06-30 19:14:07525/* String interning */
Karsten Blees7b64d422014-07-02 22:22:54526
Stefan Beller1ecbf312017-06-30 19:14:07527/*
528 * Returns the unique, interned version of the specified string or data,
529 * similar to the `String.intern` API in Java and .NET, respectively.
530 * Interned strings remain valid for the entire lifetime of the process.
531 *
532 * Can be used as `[x]strdup()` or `xmemdupz` replacement, except that interned
533 * strings / data must not be modified or freed.
534 *
535 * Interned strings are best used for short strings with high probability of
536 * duplicates.
537 *
538 * Uses a hashmap to store the pool of interned strings.
539 */
Denton Liu55454422019-04-29 08:28:14540const void *memintern(const void *data, size_t len);
Karsten Blees7b64d422014-07-02 22:22:54541static inline const char *strintern(const char *string)
542{
543 return memintern(string, strlen(string));
544}
545
Karsten Blees6a364ce2013-11-14 19:17:54546#endif