]>
Commit | Line | Data |
---|---|---|
6a364ced KB |
1 | #ifndef HASHMAP_H |
2 | #define HASHMAP_H | |
3 | ||
d40abc8e JK |
4 | #include "hash.h" |
5 | ||
6a364ced KB |
6 | /* |
7 | * Generic implementation of hash-based key-value mappings. | |
1ecbf31d SB |
8 | * |
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 { | |
e2b5038d | 16 | * struct hashmap_entry ent; |
1ecbf31d SB |
17 | * long key; |
18 | * char value[FLEX_ARRAY]; // be careful with allocating on stack! | |
19 | * }; | |
20 | * | |
21 | * #define COMPARE_VALUE 1 | |
22 | * | |
826c778f | 23 | * static int long2string_cmp(const void *hashmap_cmp_fn_data, |
939af16e EW |
24 | * const struct hashmap_entry *eptr, |
25 | * const struct hashmap_entry *entry_or_key, | |
826c778f | 26 | * const void *keydata) |
1ecbf31d | 27 | * { |
826c778f JS |
28 | * const char *string = keydata; |
29 | * unsigned flags = *(unsigned *)hashmap_cmp_fn_data; | |
939af16e EW |
30 | * 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); | |
1ecbf31d SB |
34 | * |
35 | * if (flags & COMPARE_VALUE) | |
826c778f JS |
36 | * return e1->key != e2->key || |
37 | * strcmp(e1->value, string ? string : e2->value); | |
1ecbf31d | 38 | * else |
826c778f | 39 | * return e1->key != e2->key; |
1ecbf31d SB |
40 | * } |
41 | * | |
42 | * int main(int argc, char **argv) | |
43 | * { | |
44 | * long key; | |
826c778f JS |
45 | * char value[255], action[32]; |
46 | * unsigned flags = 0; | |
1ecbf31d | 47 | * |
939af16e | 48 | * hashmap_init(&map, long2string_cmp, &flags, 0); |
1ecbf31d | 49 | * |
826c778f | 50 | * while (scanf("%s %ld %s", action, &key, value)) { |
1ecbf31d SB |
51 | * |
52 | * if (!strcmp("add", action)) { | |
53 | * struct long2string *e; | |
826c778f | 54 | * FLEX_ALLOC_STR(e, value, value); |
d22245a2 | 55 | * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long))); |
1ecbf31d | 56 | * e->key = key; |
b94e5c1d | 57 | * hashmap_add(&map, &e->ent); |
1ecbf31d SB |
58 | * } |
59 | * | |
60 | * if (!strcmp("print_all_by_key", action)) { | |
826c778f | 61 | * struct long2string k, *e; |
c92faa4d | 62 | * hashmap_entry_init(&k.ent, memhash(&key, sizeof(long))); |
1ecbf31d SB |
63 | * k.key = key; |
64 | * | |
826c778f | 65 | * flags &= ~COMPARE_VALUE; |
404ab78e | 66 | * e = hashmap_get_entry(&map, &k, ent, NULL); |
f0e63c41 | 67 | * if (e) { |
826c778f | 68 | * printf("first: %ld %s\n", e->key, e->value); |
f0e63c41 EW |
69 | * while ((e = hashmap_get_next_entry(&map, e, |
70 | * struct long2string, ent))) { | |
826c778f | 71 | * printf("found more: %ld %s\n", e->key, e->value); |
6bcbdfb2 | 72 | * } |
1ecbf31d SB |
73 | * } |
74 | * } | |
75 | * | |
76 | * if (!strcmp("has_exact_match", action)) { | |
1ecbf31d | 77 | * struct long2string *e; |
826c778f | 78 | * FLEX_ALLOC_STR(e, value, value); |
d22245a2 | 79 | * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long))); |
1ecbf31d | 80 | * e->key = key; |
1ecbf31d | 81 | * |
826c778f | 82 | * flags |= COMPARE_VALUE; |
b6c52416 EW |
83 | * printf("%sfound\n", |
84 | * hashmap_get(&map, &e->ent, NULL) ? "" : "not "); | |
826c778f | 85 | * free(e); |
1ecbf31d SB |
86 | * } |
87 | * | |
88 | * if (!strcmp("has_exact_match_no_heap_alloc", action)) { | |
826c778f | 89 | * struct long2string k; |
c92faa4d | 90 | * hashmap_entry_init(&k.ent, memhash(&key, sizeof(long))); |
826c778f | 91 | * k.key = key; |
1ecbf31d | 92 | * |
826c778f | 93 | * flags |= COMPARE_VALUE; |
b6c52416 | 94 | * printf("%sfound\n", |
c92faa4d | 95 | * hashmap_get(&map, &k.ent, value) ? "" : "not "); |
1ecbf31d SB |
96 | * } |
97 | * | |
98 | * if (!strcmp("end", action)) { | |
c8e424c9 | 99 | * hashmap_free_entries(&map, struct long2string, ent); |
1ecbf31d SB |
100 | * break; |
101 | * } | |
102 | * } | |
826c778f JS |
103 | * |
104 | * return 0; | |
1ecbf31d | 105 | * } |
6a364ced KB |
106 | */ |
107 | ||
1ecbf31d SB |
108 | /* |
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 | */ | |
55454427 DL |
117 | unsigned int strhash(const char *buf); |
118 | unsigned int strihash(const char *buf); | |
119 | unsigned int memhash(const void *buf, size_t len); | |
120 | unsigned int memihash(const void *buf, size_t len); | |
121 | unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len); | |
6a364ced | 122 | |
1ecbf31d SB |
123 | /* |
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 | */ | |
d40abc8e | 131 | static inline unsigned int oidhash(const struct object_id *oid) |
039dc71a KB |
132 | { |
133 | /* | |
d40abc8e | 134 | * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on |
039dc71a KB |
135 | * platforms that don't support unaligned reads. |
136 | */ | |
137 | unsigned int hash; | |
d40abc8e | 138 | memcpy(&hash, oid->hash, sizeof(hash)); |
039dc71a KB |
139 | return hash; |
140 | } | |
141 | ||
1ecbf31d SB |
142 | /* |
143 | * struct hashmap_entry is an opaque structure representing an entry in the | |
e2b5038d | 144 | * hash table. |
1ecbf31d SB |
145 | * Ideally it should be followed by an int-sized member to prevent unused |
146 | * memory on 64-bit systems due to alignment. | |
147 | */ | |
6a364ced | 148 | struct hashmap_entry { |
1ecbf31d SB |
149 | /* |
150 | * next points to the next entry in case of collisions (i.e. if | |
151 | * multiple entries map to the same bucket) | |
152 | */ | |
6a364ced | 153 | struct hashmap_entry *next; |
1ecbf31d SB |
154 | |
155 | /* entry's hash code */ | |
6a364ced KB |
156 | unsigned int hash; |
157 | }; | |
158 | ||
1ecbf31d SB |
159 | /* |
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 | |
861c4ce1 | 171 | * large or variable sized, such that it is not on the stack), then the |
1ecbf31d SB |
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 | */ | |
7663cdc8 | 178 | typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data, |
939af16e EW |
179 | const struct hashmap_entry *entry, |
180 | const struct hashmap_entry *entry_or_key, | |
7663cdc8 | 181 | const void *keydata); |
6a364ced | 182 | |
1ecbf31d SB |
183 | /* |
184 | * struct hashmap is the hash table structure. Members can be used as follows, | |
185 | * but should not be modified directly. | |
186 | */ | |
6a364ced KB |
187 | struct hashmap { |
188 | struct hashmap_entry **table; | |
1ecbf31d SB |
189 | |
190 | /* Stores the comparison function specified in `hashmap_init()`. */ | |
6a364ced | 191 | hashmap_cmp_fn cmpfn; |
7663cdc8 | 192 | const void *cmpfn_data; |
6a364ced | 193 | |
1ecbf31d | 194 | /* total number of entries (0 means the hashmap is empty) */ |
8b604d19 | 195 | unsigned int private_size; /* use hashmap_get_size() */ |
1ecbf31d SB |
196 | |
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 | ||
8b604d19 | 208 | unsigned int do_count_items : 1; |
6a364ced KB |
209 | }; |
210 | ||
211 | /* hashmap functions */ | |
212 | ||
1ecbf31d SB |
213 | /* |
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 | */ | |
55454427 | 230 | void hashmap_init(struct hashmap *map, |
97a39a4a EN |
231 | hashmap_cmp_fn equals_function, |
232 | const void *equals_function_data, | |
233 | size_t initial_size); | |
1ecbf31d | 234 | |
c8e424c9 EW |
235 | /* internal function for freeing hashmap */ |
236 | void hashmap_free_(struct hashmap *map, ssize_t offset); | |
237 | ||
1ecbf31d | 238 | /* |
6474b869 EN |
239 | * Frees a hashmap structure and allocated memory for the table, but does not |
240 | * free the entries nor anything they point to. | |
241 | * | |
242 | * Usage note: | |
243 | * | |
244 | * Many callers will need to iterate over all entries and free the data each | |
245 | * entry points to; in such a case, they can free the entry itself while at it. | |
246 | * Thus, you might see: | |
247 | * | |
248 | * hashmap_for_each_entry(map, hashmap_iter, e, hashmap_entry_name) { | |
249 | * free(e->somefield); | |
250 | * free(e); | |
251 | * } | |
252 | * hashmap_free(map); | |
253 | * | |
254 | * instead of | |
255 | * | |
256 | * hashmap_for_each_entry(map, hashmap_iter, e, hashmap_entry_name) { | |
257 | * free(e->somefield); | |
258 | * } | |
259 | * hashmap_free_entries(map, struct my_entry_struct, hashmap_entry_name); | |
260 | * | |
261 | * to avoid the implicit extra loop over the entries. However, if there are | |
262 | * no special fields in your entry that need to be freed beyond the entry | |
263 | * itself, it is probably simpler to avoid the explicit loop and just call | |
264 | * hashmap_free_entries(). | |
c8e424c9 EW |
265 | */ |
266 | #define hashmap_free(map) hashmap_free_(map, -1) | |
267 | ||
268 | /* | |
269 | * Frees @map and all entries. @type is the struct type of the entry | |
6474b869 EN |
270 | * where @member is the hashmap_entry struct used to associate with @map. |
271 | * | |
272 | * See usage note above hashmap_free(). | |
1ecbf31d | 273 | */ |
c8e424c9 EW |
274 | #define hashmap_free_entries(map, type, member) \ |
275 | hashmap_free_(map, offsetof(type, member)); | |
6a364ced KB |
276 | |
277 | /* hashmap_entry functions */ | |
278 | ||
1ecbf31d SB |
279 | /* |
280 | * Initializes a hashmap_entry structure. | |
281 | * | |
282 | * `entry` points to the entry to initialize. | |
283 | * `hash` is the hash code of the entry. | |
284 | * | |
285 | * The hashmap_entry structure does not hold references to external resources, | |
286 | * and it is safe to just discard it once you are done with it (i.e. if | |
287 | * your structure was allocated with xmalloc(), you can just free(3) it, | |
288 | * and if it is on stack, you can just let it go out of scope). | |
289 | */ | |
d22245a2 | 290 | static inline void hashmap_entry_init(struct hashmap_entry *e, |
97a39a4a | 291 | unsigned int hash) |
6a364ced | 292 | { |
6a364ced KB |
293 | e->hash = hash; |
294 | e->next = NULL; | |
295 | } | |
1ecbf31d | 296 | |
8b604d19 JH |
297 | /* |
298 | * Return the number of items in the map. | |
299 | */ | |
300 | static inline unsigned int hashmap_get_size(struct hashmap *map) | |
301 | { | |
302 | if (map->do_count_items) | |
303 | return map->private_size; | |
304 | ||
305 | BUG("hashmap_get_size: size not set"); | |
306 | return 0; | |
307 | } | |
308 | ||
1ecbf31d SB |
309 | /* |
310 | * Returns the hashmap entry for the specified key, or NULL if not found. | |
311 | * | |
312 | * `map` is the hashmap structure. | |
313 | * | |
314 | * `key` is a user data structure that starts with hashmap_entry that has at | |
315 | * least been initialized with the proper hash code (via `hashmap_entry_init`). | |
316 | * | |
317 | * `keydata` is a data structure that holds just enough information to check | |
318 | * for equality to a given entry. | |
319 | * | |
320 | * If the key data is variable-sized (e.g. a FLEX_ARRAY string) or quite large, | |
321 | * it is undesirable to create a full-fledged entry structure on the heap and | |
322 | * copy all the key data into the structure. | |
323 | * | |
324 | * In this case, the `keydata` parameter can be used to pass | |
325 | * variable-sized key data directly to the comparison function, and the `key` | |
326 | * parameter can be a stripped-down, fixed size entry structure allocated on the | |
327 | * stack. | |
328 | * | |
329 | * If an entry with matching hash code is found, `key` and `keydata` are passed | |
330 | * to `hashmap_cmp_fn` to decide whether the entry matches the key. | |
331 | */ | |
f23a4651 | 332 | struct hashmap_entry *hashmap_get(const struct hashmap *map, |
97a39a4a EN |
333 | const struct hashmap_entry *key, |
334 | const void *keydata); | |
6a364ced | 335 | |
1ecbf31d SB |
336 | /* |
337 | * Returns the hashmap entry for the specified hash code and key data, | |
338 | * or NULL if not found. | |
339 | * | |
340 | * `map` is the hashmap structure. | |
341 | * `hash` is the hash code of the entry to look up. | |
342 | * | |
343 | * If an entry with matching hash code is found, `keydata` is passed to | |
344 | * `hashmap_cmp_fn` to decide whether the entry matches the key. The | |
345 | * `entry_or_key` parameter of `hashmap_cmp_fn` points to a hashmap_entry | |
346 | * structure that should not be used in the comparison. | |
347 | */ | |
f23a4651 EW |
348 | static inline struct hashmap_entry *hashmap_get_from_hash( |
349 | const struct hashmap *map, | |
350 | unsigned int hash, | |
351 | const void *keydata) | |
ab73a9d1 KB |
352 | { |
353 | struct hashmap_entry key; | |
354 | hashmap_entry_init(&key, hash); | |
355 | return hashmap_get(map, &key, keydata); | |
356 | } | |
357 | ||
1ecbf31d SB |
358 | /* |
359 | * Returns the next equal hashmap entry, or NULL if not found. This can be | |
360 | * used to iterate over duplicate entries (see `hashmap_add`). | |
361 | * | |
362 | * `map` is the hashmap structure. | |
363 | * `entry` is the hashmap_entry to start the search from, obtained via a previous | |
364 | * call to `hashmap_get` or `hashmap_get_next`. | |
365 | */ | |
6bcbdfb2 | 366 | struct hashmap_entry *hashmap_get_next(const struct hashmap *map, |
97a39a4a | 367 | const struct hashmap_entry *entry); |
1ecbf31d SB |
368 | |
369 | /* | |
370 | * Adds a hashmap entry. This allows to add duplicate entries (i.e. | |
371 | * separate values with the same key according to hashmap_cmp_fn). | |
372 | * | |
373 | * `map` is the hashmap structure. | |
374 | * `entry` is the entry to add. | |
375 | */ | |
b94e5c1d | 376 | void hashmap_add(struct hashmap *map, struct hashmap_entry *entry); |
1ecbf31d SB |
377 | |
378 | /* | |
379 | * Adds or replaces a hashmap entry. If the hashmap contains duplicate | |
380 | * entries equal to the specified entry, only one of them will be replaced. | |
381 | * | |
382 | * `map` is the hashmap structure. | |
383 | * `entry` is the entry to add or replace. | |
384 | * Returns the replaced entry, or NULL if not found (i.e. the entry was added). | |
385 | */ | |
8a973d0b | 386 | struct hashmap_entry *hashmap_put(struct hashmap *map, |
97a39a4a | 387 | struct hashmap_entry *entry); |
8a973d0b | 388 | |
404ab78e EW |
389 | /* |
390 | * Adds or replaces a hashmap entry contained within @keyvar, | |
391 | * where @keyvar is a pointer to a struct containing a | |
392 | * "struct hashmap_entry" @member. | |
393 | * | |
394 | * Returns the replaced pointer which is of the same type as @keyvar, | |
395 | * or NULL if not found. | |
396 | */ | |
397 | #define hashmap_put_entry(map, keyvar, member) \ | |
398 | container_of_or_null_offset(hashmap_put(map, &(keyvar)->member), \ | |
399 | OFFSETOF_VAR(keyvar, member)) | |
1ecbf31d SB |
400 | |
401 | /* | |
402 | * Removes a hashmap entry matching the specified key. If the hashmap contains | |
403 | * duplicate entries equal to the specified key, only one of them will be | |
404 | * removed. Returns the removed entry, or NULL if not found. | |
405 | * | |
406 | * Argument explanation is the same as in `hashmap_get`. | |
407 | */ | |
8a973d0b | 408 | struct hashmap_entry *hashmap_remove(struct hashmap *map, |
97a39a4a EN |
409 | const struct hashmap_entry *key, |
410 | const void *keydata); | |
8a973d0b | 411 | |
404ab78e EW |
412 | /* |
413 | * Removes a hashmap entry contained within @keyvar, | |
414 | * where @keyvar is a pointer to a struct containing a | |
415 | * "struct hashmap_entry" @member. | |
416 | * | |
417 | * See `hashmap_get` for an explanation of @keydata | |
418 | * | |
419 | * Returns the replaced pointer which is of the same type as @keyvar, | |
420 | * or NULL if not found. | |
421 | */ | |
422 | #define hashmap_remove_entry(map, keyvar, member, keydata) \ | |
423 | container_of_or_null_offset( \ | |
424 | hashmap_remove(map, &(keyvar)->member, keydata), \ | |
425 | OFFSETOF_VAR(keyvar, member)) | |
1ecbf31d SB |
426 | |
427 | /* | |
428 | * Returns the `bucket` an entry is stored in. | |
429 | * Useful for multithreaded read access. | |
430 | */ | |
0607e100 JH |
431 | int hashmap_bucket(const struct hashmap *map, unsigned int hash); |
432 | ||
1ecbf31d SB |
433 | /* |
434 | * Used to iterate over all entries of a hashmap. Note that it is | |
435 | * not safe to add or remove entries to the hashmap while | |
436 | * iterating. | |
437 | */ | |
438 | struct hashmap_iter { | |
439 | struct hashmap *map; | |
440 | struct hashmap_entry *next; | |
441 | unsigned int tablepos; | |
442 | }; | |
6a364ced | 443 | |
1ecbf31d | 444 | /* Initializes a `hashmap_iter` structure. */ |
55454427 | 445 | void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter); |
1ecbf31d SB |
446 | |
447 | /* Returns the next hashmap_entry, or NULL if there are no more entries. */ | |
87571c3f | 448 | struct hashmap_entry *hashmap_iter_next(struct hashmap_iter *iter); |
1ecbf31d SB |
449 | |
450 | /* Initializes the iterator and returns the first entry, if any. */ | |
87571c3f | 451 | static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map, |
97a39a4a | 452 | struct hashmap_iter *iter) |
6a364ced KB |
453 | { |
454 | hashmap_iter_init(map, iter); | |
455 | return hashmap_iter_next(iter); | |
456 | } | |
457 | ||
23dee69f EW |
458 | /* |
459 | * returns the first entry in @map using @iter, where the entry is of | |
460 | * @type (e.g. "struct foo") and @member is the name of the | |
461 | * "struct hashmap_entry" in @type | |
462 | */ | |
87571c3f EW |
463 | #define hashmap_iter_first_entry(map, iter, type, member) \ |
464 | container_of_or_null(hashmap_iter_first(map, iter), type, member) | |
465 | ||
23dee69f EW |
466 | /* internal macro for hashmap_for_each_entry */ |
467 | #define hashmap_iter_next_entry_offset(iter, offset) \ | |
468 | container_of_or_null_offset(hashmap_iter_next(iter), offset) | |
469 | ||
470 | /* internal macro for hashmap_for_each_entry */ | |
471 | #define hashmap_iter_first_entry_offset(map, iter, offset) \ | |
472 | container_of_or_null_offset(hashmap_iter_first(map, iter), offset) | |
473 | ||
474 | /* | |
475 | * iterate through @map using @iter, @var is a pointer to a type | |
476 | * containing a @member which is a "struct hashmap_entry" | |
477 | */ | |
478 | #define hashmap_for_each_entry(map, iter, var, member) \ | |
0ad621f6 JH |
479 | for (var = NULL, /* for systems without typeof */ \ |
480 | var = hashmap_iter_first_entry_offset(map, iter, \ | |
23dee69f | 481 | OFFSETOF_VAR(var, member)); \ |
87571c3f | 482 | var; \ |
23dee69f EW |
483 | var = hashmap_iter_next_entry_offset(iter, \ |
484 | OFFSETOF_VAR(var, member))) | |
87571c3f | 485 | |
f0e63c41 | 486 | /* |
404ab78e EW |
487 | * returns a pointer of type matching @keyvar, or NULL if nothing found. |
488 | * @keyvar is a pointer to a struct containing a | |
489 | * "struct hashmap_entry" @member. | |
f0e63c41 | 490 | */ |
404ab78e EW |
491 | #define hashmap_get_entry(map, keyvar, member, keydata) \ |
492 | container_of_or_null_offset( \ | |
493 | hashmap_get(map, &(keyvar)->member, keydata), \ | |
494 | OFFSETOF_VAR(keyvar, member)) | |
f0e63c41 EW |
495 | |
496 | #define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \ | |
497 | container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \ | |
498 | type, member) | |
499 | /* | |
23dee69f EW |
500 | * returns the next equal pointer to @var, or NULL if not found. |
501 | * @var is a pointer of any type containing "struct hashmap_entry" | |
502 | * @member is the name of the "struct hashmap_entry" field | |
f0e63c41 | 503 | */ |
23dee69f EW |
504 | #define hashmap_get_next_entry(map, var, member) \ |
505 | container_of_or_null_offset(hashmap_get_next(map, &(var)->member), \ | |
506 | OFFSETOF_VAR(var, member)) | |
f0e63c41 EW |
507 | |
508 | /* | |
509 | * iterate @map starting from @var, where @var is a pointer of @type | |
510 | * and @member is the name of the "struct hashmap_entry" field in @type | |
511 | */ | |
23dee69f | 512 | #define hashmap_for_each_entry_from(map, var, member) \ |
f0e63c41 EW |
513 | for (; \ |
514 | var; \ | |
23dee69f | 515 | var = hashmap_get_next_entry(map, var, member)) |
f0e63c41 | 516 | |
8b604d19 JH |
517 | /* |
518 | * Disable item counting and automatic rehashing when adding/removing items. | |
519 | * | |
520 | * Normally, the hashmap keeps track of the number of items in the map | |
521 | * and uses it to dynamically resize it. This (both the counting and | |
522 | * the resizing) can cause problems when the map is being used by | |
523 | * threaded callers (because the hashmap code does not know about the | |
524 | * locking strategy used by the threaded callers and therefore, does | |
525 | * not know how to protect the "private_size" counter). | |
526 | */ | |
527 | static inline void hashmap_disable_item_counting(struct hashmap *map) | |
528 | { | |
529 | map->do_count_items = 0; | |
530 | } | |
531 | ||
532 | /* | |
15beaaa3 | 533 | * Re-enable item counting when adding/removing items. |
8b604d19 JH |
534 | * If counting is currently disabled, it will force count them. |
535 | * It WILL NOT automatically rehash them. | |
536 | */ | |
537 | static inline void hashmap_enable_item_counting(struct hashmap *map) | |
538 | { | |
8b604d19 JH |
539 | unsigned int n = 0; |
540 | struct hashmap_iter iter; | |
541 | ||
542 | if (map->do_count_items) | |
543 | return; | |
544 | ||
545 | hashmap_iter_init(map, &iter); | |
7d68bb07 | 546 | while (hashmap_iter_next(&iter)) |
8b604d19 JH |
547 | n++; |
548 | ||
549 | map->do_count_items = 1; | |
550 | map->private_size = n; | |
551 | } | |
552 | ||
1ecbf31d | 553 | /* String interning */ |
7b64d42d | 554 | |
1ecbf31d SB |
555 | /* |
556 | * Returns the unique, interned version of the specified string or data, | |
557 | * similar to the `String.intern` API in Java and .NET, respectively. | |
558 | * Interned strings remain valid for the entire lifetime of the process. | |
559 | * | |
560 | * Can be used as `[x]strdup()` or `xmemdupz` replacement, except that interned | |
561 | * strings / data must not be modified or freed. | |
562 | * | |
563 | * Interned strings are best used for short strings with high probability of | |
564 | * duplicates. | |
565 | * | |
566 | * Uses a hashmap to store the pool of interned strings. | |
567 | */ | |
55454427 | 568 | const void *memintern(const void *data, size_t len); |
7b64d42d KB |
569 | static inline const char *strintern(const char *string) |
570 | { | |
571 | return memintern(string, strlen(string)); | |
572 | } | |
573 | ||
6a364ced | 574 | #endif |