]>
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 { | |
16 | * struct hashmap_entry ent; // must be the first member! | |
17 | * long key; | |
18 | * char value[FLEX_ARRAY]; // be careful with allocating on stack! | |
19 | * }; | |
20 | * | |
21 | * #define COMPARE_VALUE 1 | |
22 | * | |
826c778f JS |
23 | * static int long2string_cmp(const void *hashmap_cmp_fn_data, |
24 | * const struct long2string *e1, | |
1ecbf31d | 25 | * const struct long2string *e2, |
826c778f | 26 | * const void *keydata) |
1ecbf31d | 27 | * { |
826c778f JS |
28 | * const char *string = keydata; |
29 | * unsigned flags = *(unsigned *)hashmap_cmp_fn_data; | |
1ecbf31d SB |
30 | * |
31 | * if (flags & COMPARE_VALUE) | |
826c778f JS |
32 | * return e1->key != e2->key || |
33 | * strcmp(e1->value, string ? string : e2->value); | |
1ecbf31d | 34 | * else |
826c778f | 35 | * return e1->key != e2->key; |
1ecbf31d SB |
36 | * } |
37 | * | |
38 | * int main(int argc, char **argv) | |
39 | * { | |
40 | * long key; | |
826c778f JS |
41 | * char value[255], action[32]; |
42 | * unsigned flags = 0; | |
1ecbf31d SB |
43 | * |
44 | * hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0); | |
45 | * | |
826c778f | 46 | * while (scanf("%s %ld %s", action, &key, value)) { |
1ecbf31d SB |
47 | * |
48 | * if (!strcmp("add", action)) { | |
49 | * struct long2string *e; | |
826c778f | 50 | * FLEX_ALLOC_STR(e, value, value); |
d22245a2 | 51 | * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long))); |
1ecbf31d | 52 | * e->key = key; |
b94e5c1d | 53 | * hashmap_add(&map, &e->ent); |
1ecbf31d SB |
54 | * } |
55 | * | |
56 | * if (!strcmp("print_all_by_key", action)) { | |
826c778f | 57 | * struct long2string k, *e; |
6bcbdfb2 | 58 | * struct hashmap_entry *ent; |
d22245a2 | 59 | * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long))); |
1ecbf31d SB |
60 | * k.key = key; |
61 | * | |
826c778f | 62 | * flags &= ~COMPARE_VALUE; |
6bcbdfb2 EW |
63 | * ent = hashmap_get(&map, &k, NULL); |
64 | * if (ent) { | |
65 | * e = container_of(ent, struct long2string, ent); | |
826c778f | 66 | * printf("first: %ld %s\n", e->key, e->value); |
6bcbdfb2 EW |
67 | * while ((ent = hashmap_get_next(&map, ent))) { |
68 | * e = container_of(ent, struct long2string, ent); | |
826c778f | 69 | * printf("found more: %ld %s\n", e->key, e->value); |
6bcbdfb2 | 70 | * } |
1ecbf31d SB |
71 | * } |
72 | * } | |
73 | * | |
74 | * if (!strcmp("has_exact_match", action)) { | |
1ecbf31d | 75 | * struct long2string *e; |
826c778f | 76 | * FLEX_ALLOC_STR(e, value, value); |
d22245a2 | 77 | * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long))); |
1ecbf31d | 78 | * e->key = key; |
1ecbf31d | 79 | * |
826c778f | 80 | * flags |= COMPARE_VALUE; |
b6c52416 EW |
81 | * printf("%sfound\n", |
82 | * hashmap_get(&map, &e->ent, NULL) ? "" : "not "); | |
826c778f | 83 | * free(e); |
1ecbf31d SB |
84 | * } |
85 | * | |
86 | * if (!strcmp("has_exact_match_no_heap_alloc", action)) { | |
826c778f | 87 | * struct long2string k; |
d22245a2 | 88 | * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long))); |
826c778f | 89 | * k.key = key; |
1ecbf31d | 90 | * |
826c778f | 91 | * flags |= COMPARE_VALUE; |
b6c52416 EW |
92 | * printf("%sfound\n", |
93 | * hashmap_get(&map, &k->ent, value) ? "" : "not "); | |
1ecbf31d SB |
94 | * } |
95 | * | |
96 | * if (!strcmp("end", action)) { | |
97 | * hashmap_free(&map, 1); | |
98 | * break; | |
99 | * } | |
100 | * } | |
826c778f JS |
101 | * |
102 | * return 0; | |
1ecbf31d | 103 | * } |
6a364ced KB |
104 | */ |
105 | ||
1ecbf31d SB |
106 | /* |
107 | * Ready-to-use hash functions for strings, using the FNV-1 algorithm (see | |
108 | * http://www.isthe.com/chongo/tech/comp/fnv). | |
109 | * `strhash` and `strihash` take 0-terminated strings, while `memhash` and | |
110 | * `memihash` operate on arbitrary-length memory. | |
111 | * `strihash` and `memihash` are case insensitive versions. | |
112 | * `memihash_cont` is a variant of `memihash` that allows a computation to be | |
113 | * continued with another chunk of data. | |
114 | */ | |
55454427 DL |
115 | unsigned int strhash(const char *buf); |
116 | unsigned int strihash(const char *buf); | |
117 | unsigned int memhash(const void *buf, size_t len); | |
118 | unsigned int memihash(const void *buf, size_t len); | |
119 | unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len); | |
6a364ced | 120 | |
1ecbf31d SB |
121 | /* |
122 | * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code | |
123 | * for use in hash tables. Cryptographic hashes are supposed to have | |
124 | * uniform distribution, so in contrast to `memhash()`, this just copies | |
125 | * the first `sizeof(int)` bytes without shuffling any bits. Note that | |
126 | * the results will be different on big-endian and little-endian | |
127 | * platforms, so they should not be stored or transferred over the net. | |
128 | */ | |
d40abc8e | 129 | static inline unsigned int oidhash(const struct object_id *oid) |
039dc71a KB |
130 | { |
131 | /* | |
d40abc8e | 132 | * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on |
039dc71a KB |
133 | * platforms that don't support unaligned reads. |
134 | */ | |
135 | unsigned int hash; | |
d40abc8e | 136 | memcpy(&hash, oid->hash, sizeof(hash)); |
039dc71a KB |
137 | return hash; |
138 | } | |
139 | ||
1ecbf31d SB |
140 | /* |
141 | * struct hashmap_entry is an opaque structure representing an entry in the | |
142 | * hash table, which must be used as first member of user data structures. | |
143 | * Ideally it should be followed by an int-sized member to prevent unused | |
144 | * memory on 64-bit systems due to alignment. | |
145 | */ | |
6a364ced | 146 | struct hashmap_entry { |
1ecbf31d SB |
147 | /* |
148 | * next points to the next entry in case of collisions (i.e. if | |
149 | * multiple entries map to the same bucket) | |
150 | */ | |
6a364ced | 151 | struct hashmap_entry *next; |
1ecbf31d SB |
152 | |
153 | /* entry's hash code */ | |
6a364ced KB |
154 | unsigned int hash; |
155 | }; | |
156 | ||
1ecbf31d SB |
157 | /* |
158 | * User-supplied function to test two hashmap entries for equality. Shall | |
159 | * return 0 if the entries are equal. | |
160 | * | |
161 | * This function is always called with non-NULL `entry` and `entry_or_key` | |
162 | * parameters that have the same hash code. | |
163 | * | |
164 | * When looking up an entry, the `key` and `keydata` parameters to hashmap_get | |
165 | * and hashmap_remove are always passed as second `entry_or_key` and third | |
166 | * argument `keydata`, respectively. Otherwise, `keydata` is NULL. | |
167 | * | |
168 | * When it is too expensive to allocate a user entry (either because it is | |
169 | * large or varialbe sized, such that it is not on the stack), then the | |
170 | * relevant data to check for equality should be passed via `keydata`. | |
171 | * In this case `key` can be a stripped down version of the user key data | |
172 | * or even just a hashmap_entry having the correct hash. | |
173 | * | |
174 | * The `hashmap_cmp_fn_data` entry is the pointer given in the init function. | |
175 | */ | |
7663cdc8 SB |
176 | typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data, |
177 | const void *entry, const void *entry_or_key, | |
178 | const void *keydata); | |
6a364ced | 179 | |
1ecbf31d SB |
180 | /* |
181 | * struct hashmap is the hash table structure. Members can be used as follows, | |
182 | * but should not be modified directly. | |
183 | */ | |
6a364ced KB |
184 | struct hashmap { |
185 | struct hashmap_entry **table; | |
1ecbf31d SB |
186 | |
187 | /* Stores the comparison function specified in `hashmap_init()`. */ | |
6a364ced | 188 | hashmap_cmp_fn cmpfn; |
7663cdc8 | 189 | const void *cmpfn_data; |
6a364ced | 190 | |
1ecbf31d | 191 | /* total number of entries (0 means the hashmap is empty) */ |
8b604d19 | 192 | unsigned int private_size; /* use hashmap_get_size() */ |
1ecbf31d SB |
193 | |
194 | /* | |
195 | * tablesize is the allocated size of the hash table. A non-0 value | |
196 | * indicates that the hashmap is initialized. It may also be useful | |
197 | * for statistical purposes (i.e. `size / tablesize` is the current | |
198 | * load factor). | |
199 | */ | |
200 | unsigned int tablesize; | |
201 | ||
202 | unsigned int grow_at; | |
203 | unsigned int shrink_at; | |
204 | ||
8b604d19 | 205 | unsigned int do_count_items : 1; |
6a364ced KB |
206 | }; |
207 | ||
208 | /* hashmap functions */ | |
209 | ||
1ecbf31d SB |
210 | /* |
211 | * Initializes a hashmap structure. | |
212 | * | |
213 | * `map` is the hashmap to initialize. | |
214 | * | |
215 | * The `equals_function` can be specified to compare two entries for equality. | |
216 | * If NULL, entries are considered equal if their hash codes are equal. | |
217 | * | |
218 | * The `equals_function_data` parameter can be used to provide additional data | |
219 | * (a callback cookie) that will be passed to `equals_function` each time it | |
220 | * is called. This allows a single `equals_function` to implement multiple | |
221 | * comparison functions. | |
222 | * | |
223 | * If the total number of entries is known in advance, the `initial_size` | |
224 | * parameter may be used to preallocate a sufficiently large table and thus | |
225 | * prevent expensive resizing. If 0, the table is dynamically resized. | |
226 | */ | |
55454427 | 227 | void hashmap_init(struct hashmap *map, |
7663cdc8 SB |
228 | hashmap_cmp_fn equals_function, |
229 | const void *equals_function_data, | |
230 | size_t initial_size); | |
1ecbf31d SB |
231 | |
232 | /* | |
233 | * Frees a hashmap structure and allocated memory. | |
234 | * | |
235 | * If `free_entries` is true, each hashmap_entry in the map is freed as well | |
236 | * using stdlibs free(). | |
237 | */ | |
55454427 | 238 | void hashmap_free(struct hashmap *map, int free_entries); |
6a364ced KB |
239 | |
240 | /* hashmap_entry functions */ | |
241 | ||
1ecbf31d SB |
242 | /* |
243 | * Initializes a hashmap_entry structure. | |
244 | * | |
245 | * `entry` points to the entry to initialize. | |
246 | * `hash` is the hash code of the entry. | |
247 | * | |
248 | * The hashmap_entry structure does not hold references to external resources, | |
249 | * and it is safe to just discard it once you are done with it (i.e. if | |
250 | * your structure was allocated with xmalloc(), you can just free(3) it, | |
251 | * and if it is on stack, you can just let it go out of scope). | |
252 | */ | |
d22245a2 EW |
253 | static inline void hashmap_entry_init(struct hashmap_entry *e, |
254 | unsigned int hash) | |
6a364ced | 255 | { |
6a364ced KB |
256 | e->hash = hash; |
257 | e->next = NULL; | |
258 | } | |
1ecbf31d | 259 | |
8b604d19 JH |
260 | /* |
261 | * Return the number of items in the map. | |
262 | */ | |
263 | static inline unsigned int hashmap_get_size(struct hashmap *map) | |
264 | { | |
265 | if (map->do_count_items) | |
266 | return map->private_size; | |
267 | ||
268 | BUG("hashmap_get_size: size not set"); | |
269 | return 0; | |
270 | } | |
271 | ||
1ecbf31d SB |
272 | /* |
273 | * Returns the hashmap entry for the specified key, or NULL if not found. | |
274 | * | |
275 | * `map` is the hashmap structure. | |
276 | * | |
277 | * `key` is a user data structure that starts with hashmap_entry that has at | |
278 | * least been initialized with the proper hash code (via `hashmap_entry_init`). | |
279 | * | |
280 | * `keydata` is a data structure that holds just enough information to check | |
281 | * for equality to a given entry. | |
282 | * | |
283 | * If the key data is variable-sized (e.g. a FLEX_ARRAY string) or quite large, | |
284 | * it is undesirable to create a full-fledged entry structure on the heap and | |
285 | * copy all the key data into the structure. | |
286 | * | |
287 | * In this case, the `keydata` parameter can be used to pass | |
288 | * variable-sized key data directly to the comparison function, and the `key` | |
289 | * parameter can be a stripped-down, fixed size entry structure allocated on the | |
290 | * stack. | |
291 | * | |
292 | * If an entry with matching hash code is found, `key` and `keydata` are passed | |
293 | * to `hashmap_cmp_fn` to decide whether the entry matches the key. | |
294 | */ | |
b6c52416 | 295 | void *hashmap_get(const struct hashmap *map, const struct hashmap_entry *key, |
1ecbf31d | 296 | const void *keydata); |
6a364ced | 297 | |
1ecbf31d SB |
298 | /* |
299 | * Returns the hashmap entry for the specified hash code and key data, | |
300 | * or NULL if not found. | |
301 | * | |
302 | * `map` is the hashmap structure. | |
303 | * `hash` is the hash code of the entry to look up. | |
304 | * | |
305 | * If an entry with matching hash code is found, `keydata` is passed to | |
306 | * `hashmap_cmp_fn` to decide whether the entry matches the key. The | |
307 | * `entry_or_key` parameter of `hashmap_cmp_fn` points to a hashmap_entry | |
308 | * structure that should not be used in the comparison. | |
309 | */ | |
ab73a9d1 | 310 | static inline void *hashmap_get_from_hash(const struct hashmap *map, |
1ecbf31d SB |
311 | unsigned int hash, |
312 | const void *keydata) | |
ab73a9d1 KB |
313 | { |
314 | struct hashmap_entry key; | |
315 | hashmap_entry_init(&key, hash); | |
316 | return hashmap_get(map, &key, keydata); | |
317 | } | |
318 | ||
1ecbf31d SB |
319 | /* |
320 | * Returns the next equal hashmap entry, or NULL if not found. This can be | |
321 | * used to iterate over duplicate entries (see `hashmap_add`). | |
322 | * | |
323 | * `map` is the hashmap structure. | |
324 | * `entry` is the hashmap_entry to start the search from, obtained via a previous | |
325 | * call to `hashmap_get` or `hashmap_get_next`. | |
326 | */ | |
6bcbdfb2 | 327 | struct hashmap_entry *hashmap_get_next(const struct hashmap *map, |
f6eb6bdc | 328 | const struct hashmap_entry *entry); |
1ecbf31d SB |
329 | |
330 | /* | |
331 | * Adds a hashmap entry. This allows to add duplicate entries (i.e. | |
332 | * separate values with the same key according to hashmap_cmp_fn). | |
333 | * | |
334 | * `map` is the hashmap structure. | |
335 | * `entry` is the entry to add. | |
336 | */ | |
b94e5c1d | 337 | void hashmap_add(struct hashmap *map, struct hashmap_entry *entry); |
1ecbf31d SB |
338 | |
339 | /* | |
340 | * Adds or replaces a hashmap entry. If the hashmap contains duplicate | |
341 | * entries equal to the specified entry, only one of them will be replaced. | |
342 | * | |
343 | * `map` is the hashmap structure. | |
344 | * `entry` is the entry to add or replace. | |
345 | * Returns the replaced entry, or NULL if not found (i.e. the entry was added). | |
346 | */ | |
26b455f2 | 347 | void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry); |
1ecbf31d SB |
348 | |
349 | /* | |
350 | * Removes a hashmap entry matching the specified key. If the hashmap contains | |
351 | * duplicate entries equal to the specified key, only one of them will be | |
352 | * removed. Returns the removed entry, or NULL if not found. | |
353 | * | |
354 | * Argument explanation is the same as in `hashmap_get`. | |
355 | */ | |
28ee7941 | 356 | void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key, |
1ecbf31d SB |
357 | const void *keydata); |
358 | ||
359 | /* | |
360 | * Returns the `bucket` an entry is stored in. | |
361 | * Useful for multithreaded read access. | |
362 | */ | |
0607e100 JH |
363 | int hashmap_bucket(const struct hashmap *map, unsigned int hash); |
364 | ||
1ecbf31d SB |
365 | /* |
366 | * Used to iterate over all entries of a hashmap. Note that it is | |
367 | * not safe to add or remove entries to the hashmap while | |
368 | * iterating. | |
369 | */ | |
370 | struct hashmap_iter { | |
371 | struct hashmap *map; | |
372 | struct hashmap_entry *next; | |
373 | unsigned int tablepos; | |
374 | }; | |
6a364ced | 375 | |
1ecbf31d | 376 | /* Initializes a `hashmap_iter` structure. */ |
55454427 | 377 | void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter); |
1ecbf31d SB |
378 | |
379 | /* Returns the next hashmap_entry, or NULL if there are no more entries. */ | |
55454427 | 380 | void *hashmap_iter_next(struct hashmap_iter *iter); |
1ecbf31d SB |
381 | |
382 | /* Initializes the iterator and returns the first entry, if any. */ | |
6a364ced KB |
383 | static inline void *hashmap_iter_first(struct hashmap *map, |
384 | struct hashmap_iter *iter) | |
385 | { | |
386 | hashmap_iter_init(map, iter); | |
387 | return hashmap_iter_next(iter); | |
388 | } | |
389 | ||
8b604d19 JH |
390 | /* |
391 | * Disable item counting and automatic rehashing when adding/removing items. | |
392 | * | |
393 | * Normally, the hashmap keeps track of the number of items in the map | |
394 | * and uses it to dynamically resize it. This (both the counting and | |
395 | * the resizing) can cause problems when the map is being used by | |
396 | * threaded callers (because the hashmap code does not know about the | |
397 | * locking strategy used by the threaded callers and therefore, does | |
398 | * not know how to protect the "private_size" counter). | |
399 | */ | |
400 | static inline void hashmap_disable_item_counting(struct hashmap *map) | |
401 | { | |
402 | map->do_count_items = 0; | |
403 | } | |
404 | ||
405 | /* | |
406 | * Re-enable item couting when adding/removing items. | |
407 | * If counting is currently disabled, it will force count them. | |
408 | * It WILL NOT automatically rehash them. | |
409 | */ | |
410 | static inline void hashmap_enable_item_counting(struct hashmap *map) | |
411 | { | |
8b604d19 JH |
412 | unsigned int n = 0; |
413 | struct hashmap_iter iter; | |
414 | ||
415 | if (map->do_count_items) | |
416 | return; | |
417 | ||
418 | hashmap_iter_init(map, &iter); | |
7d68bb07 | 419 | while (hashmap_iter_next(&iter)) |
8b604d19 JH |
420 | n++; |
421 | ||
422 | map->do_count_items = 1; | |
423 | map->private_size = n; | |
424 | } | |
425 | ||
1ecbf31d | 426 | /* String interning */ |
7b64d42d | 427 | |
1ecbf31d SB |
428 | /* |
429 | * Returns the unique, interned version of the specified string or data, | |
430 | * similar to the `String.intern` API in Java and .NET, respectively. | |
431 | * Interned strings remain valid for the entire lifetime of the process. | |
432 | * | |
433 | * Can be used as `[x]strdup()` or `xmemdupz` replacement, except that interned | |
434 | * strings / data must not be modified or freed. | |
435 | * | |
436 | * Interned strings are best used for short strings with high probability of | |
437 | * duplicates. | |
438 | * | |
439 | * Uses a hashmap to store the pool of interned strings. | |
440 | */ | |
55454427 | 441 | const void *memintern(const void *data, size_t len); |
7b64d42d KB |
442 | static inline const char *strintern(const char *string) |
443 | { | |
444 | return memintern(string, strlen(string)); | |
445 | } | |
446 | ||
6a364ced | 447 | #endif |