/src/dovecot/src/lib/hash.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | /* @UNSAFE: whole file */ |
4 | | |
5 | | #include "lib.h" |
6 | | #include "hash.h" |
7 | | #include "primes.h" |
8 | | #include "randgen.h" |
9 | | #include "xxh64.h" |
10 | | |
11 | | #include <ctype.h> |
12 | | |
13 | | #define HASH_TABLE_MIN_SIZE 67 |
14 | | |
15 | | #undef hash_table_create |
16 | | #undef hash_table_create_direct |
17 | | #undef hash_table_destroy |
18 | | #undef hash_table_clear |
19 | | #undef hash_table_lookup |
20 | | #undef hash_table_lookup_full |
21 | | #undef hash_table_insert |
22 | | #undef hash_table_update |
23 | | #undef hash_table_try_remove |
24 | | #undef hash_table_count |
25 | | #undef hash_table_iterate_init |
26 | | #undef hash_table_iterate |
27 | | #undef hash_table_freeze |
28 | | #undef hash_table_thaw |
29 | | #undef hash_table_copy |
30 | | |
31 | | struct hash_node { |
32 | | struct hash_node *next; |
33 | | void *key; |
34 | | void *value; |
35 | | }; |
36 | | |
37 | | struct hash_table { |
38 | | pool_t node_pool; |
39 | | |
40 | | int frozen; |
41 | | unsigned int initial_size, nodes_count, removed_count; |
42 | | |
43 | | unsigned int size; |
44 | | struct hash_node *nodes; |
45 | | struct hash_node *free_nodes; |
46 | | |
47 | | hash_callback_t *hash_cb; |
48 | | hash_cmp_callback_t *key_compare_cb; |
49 | | }; |
50 | | |
51 | | struct hash_iterate_context { |
52 | | struct hash_table *table; |
53 | | struct hash_node *next; |
54 | | unsigned int pos; |
55 | | }; |
56 | | |
57 | | enum hash_table_operation{ |
58 | | HASH_TABLE_OP_INSERT, |
59 | | HASH_TABLE_OP_UPDATE, |
60 | | HASH_TABLE_OP_RESIZE |
61 | | }; |
62 | | |
63 | | uint64_t hash_iv; |
64 | | |
65 | | static bool hash_table_resize(struct hash_table *table, bool grow); |
66 | | |
67 | | void hash_table_create(struct hash_table **table_r, pool_t node_pool, |
68 | | unsigned int initial_size, hash_callback_t *hash_cb, |
69 | | hash_cmp_callback_t *key_compare_cb) |
70 | 0 | { |
71 | 0 | struct hash_table *table; |
72 | |
|
73 | 0 | pool_ref(node_pool); |
74 | 0 | table = i_new(struct hash_table, 1); |
75 | 0 | table->node_pool = node_pool; |
76 | 0 | table->initial_size = |
77 | 0 | I_MAX(primes_closest(initial_size), HASH_TABLE_MIN_SIZE); |
78 | |
|
79 | 0 | table->hash_cb = hash_cb; |
80 | 0 | table->key_compare_cb = key_compare_cb; |
81 | |
|
82 | 0 | table->size = table->initial_size; |
83 | 0 | table->nodes = i_new(struct hash_node, table->size); |
84 | 0 | *table_r = table; |
85 | 0 | } |
86 | | |
87 | | static unsigned int direct_hash(const void *p) |
88 | 0 | { |
89 | | /* NOTE: may truncate the value, but that doesn't matter. */ |
90 | 0 | return POINTER_CAST_TO(p, unsigned int); |
91 | 0 | } |
92 | | |
93 | | static int direct_cmp(const void *p1, const void *p2) |
94 | 0 | { |
95 | 0 | return p1 == p2 ? 0 : 1; |
96 | 0 | } |
97 | | |
98 | | void hash_table_create_direct(struct hash_table **table_r, pool_t node_pool, |
99 | | unsigned int initial_size) |
100 | 0 | { |
101 | 0 | hash_table_create(table_r, node_pool, initial_size, |
102 | 0 | direct_hash, direct_cmp); |
103 | 0 | } |
104 | | |
105 | | static void free_node(struct hash_table *table, struct hash_node *node) |
106 | 0 | { |
107 | 0 | if (!table->node_pool->alloconly_pool) |
108 | 0 | p_free(table->node_pool, node); |
109 | 0 | else { |
110 | 0 | node->next = table->free_nodes; |
111 | 0 | table->free_nodes = node; |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | static void destroy_node_list(struct hash_table *table, struct hash_node *node) |
116 | 0 | { |
117 | 0 | struct hash_node *next; |
118 | |
|
119 | 0 | while (node != NULL) { |
120 | 0 | next = node->next; |
121 | 0 | p_free(table->node_pool, node); |
122 | 0 | node = next; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | | static void hash_table_destroy_nodes(struct hash_table *table) |
127 | 0 | { |
128 | 0 | unsigned int i; |
129 | |
|
130 | 0 | for (i = 0; i < table->size; i++) { |
131 | 0 | if (table->nodes[i].next != NULL) |
132 | 0 | destroy_node_list(table, table->nodes[i].next); |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | void hash_table_destroy(struct hash_table **_table) |
137 | 0 | { |
138 | 0 | struct hash_table *table = *_table; |
139 | |
|
140 | 0 | if (table == NULL) |
141 | 0 | return; |
142 | 0 | *_table = NULL; |
143 | |
|
144 | 0 | i_assert(table->frozen == 0); |
145 | | |
146 | 0 | if (!table->node_pool->alloconly_pool) { |
147 | 0 | hash_table_destroy_nodes(table); |
148 | 0 | destroy_node_list(table, table->free_nodes); |
149 | 0 | } |
150 | |
|
151 | 0 | pool_unref(&table->node_pool); |
152 | 0 | i_free(table->nodes); |
153 | 0 | i_free(table); |
154 | 0 | } |
155 | | |
156 | | void hash_table_clear(struct hash_table *table, bool free_nodes) |
157 | 0 | { |
158 | 0 | i_assert(table->frozen == 0); |
159 | | |
160 | 0 | if (!table->node_pool->alloconly_pool) |
161 | 0 | hash_table_destroy_nodes(table); |
162 | |
|
163 | 0 | if (free_nodes) { |
164 | 0 | if (!table->node_pool->alloconly_pool) |
165 | 0 | destroy_node_list(table, table->free_nodes); |
166 | 0 | table->free_nodes = NULL; |
167 | 0 | } |
168 | |
|
169 | 0 | memset(table->nodes, 0, sizeof(struct hash_node) * table->size); |
170 | |
|
171 | 0 | table->nodes_count = 0; |
172 | 0 | table->removed_count = 0; |
173 | 0 | } |
174 | | |
175 | | static struct hash_node * |
176 | | hash_table_lookup_node(const struct hash_table *table, |
177 | | const void *key, unsigned int hash) |
178 | 0 | { |
179 | 0 | struct hash_node *node; |
180 | |
|
181 | 0 | node = &table->nodes[hash % table->size]; |
182 | |
|
183 | 0 | do { |
184 | 0 | if (node->key != NULL) { |
185 | 0 | if (table->key_compare_cb(node->key, key) == 0) |
186 | 0 | return node; |
187 | 0 | } |
188 | 0 | node = node->next; |
189 | 0 | } while (node != NULL); |
190 | | |
191 | 0 | return NULL; |
192 | 0 | } |
193 | | |
194 | | void *hash_table_lookup(const struct hash_table *table, const void *key) |
195 | 0 | { |
196 | 0 | struct hash_node *node; |
197 | |
|
198 | 0 | node = hash_table_lookup_node(table, key, table->hash_cb(key)); |
199 | 0 | return node != NULL ? node->value : NULL; |
200 | 0 | } |
201 | | |
202 | | bool hash_table_lookup_full(const struct hash_table *table, |
203 | | const void *lookup_key, |
204 | | void **orig_key, void **value) |
205 | 0 | { |
206 | 0 | struct hash_node *node; |
207 | |
|
208 | 0 | node = hash_table_lookup_node(table, lookup_key, |
209 | 0 | table->hash_cb(lookup_key)); |
210 | 0 | if (node == NULL) |
211 | 0 | return FALSE; |
212 | | |
213 | 0 | *orig_key = node->key; |
214 | 0 | *value = node->value; |
215 | 0 | return TRUE; |
216 | 0 | } |
217 | | |
218 | | static void |
219 | | hash_table_insert_node(struct hash_table *table, void *key, void *value, |
220 | | enum hash_table_operation opcode) |
221 | 0 | { |
222 | 0 | struct hash_node *node, *prev; |
223 | 0 | unsigned int hash; |
224 | 0 | bool check_existing = TRUE; |
225 | |
|
226 | 0 | i_assert(table->nodes_count < UINT_MAX); |
227 | 0 | i_assert(key != NULL); |
228 | | |
229 | 0 | if(opcode == HASH_TABLE_OP_RESIZE) |
230 | 0 | check_existing = FALSE; |
231 | 0 | hash = table->hash_cb(key); |
232 | |
|
233 | 0 | if (check_existing && table->removed_count > 0) { |
234 | | /* there may be holes, have to check everything */ |
235 | 0 | node = hash_table_lookup_node(table, key, hash); |
236 | 0 | if (node != NULL) { |
237 | 0 | i_assert(opcode == HASH_TABLE_OP_UPDATE); |
238 | 0 | node->value = value; |
239 | 0 | return; |
240 | 0 | } |
241 | | |
242 | 0 | check_existing = FALSE; |
243 | 0 | } |
244 | | |
245 | | /* a) primary node */ |
246 | 0 | node = &table->nodes[hash % table->size]; |
247 | 0 | if (node->key == NULL) { |
248 | 0 | table->nodes_count++; |
249 | |
|
250 | 0 | node->key = key; |
251 | 0 | node->value = value; |
252 | 0 | return; |
253 | 0 | } |
254 | 0 | if (check_existing) { |
255 | 0 | if (table->key_compare_cb(node->key, key) == 0) { |
256 | 0 | i_assert(opcode == HASH_TABLE_OP_UPDATE); |
257 | 0 | node->value = value; |
258 | 0 | return; |
259 | 0 | } |
260 | 0 | } |
261 | | |
262 | | /* b) collisions list */ |
263 | 0 | prev = node; node = node->next; |
264 | 0 | while (node != NULL) { |
265 | 0 | if (node->key == NULL) |
266 | 0 | break; |
267 | | |
268 | 0 | if (check_existing) { |
269 | 0 | if (table->key_compare_cb(node->key, key) == 0) { |
270 | 0 | i_assert(opcode == HASH_TABLE_OP_UPDATE); |
271 | 0 | node->value = value; |
272 | 0 | return; |
273 | 0 | } |
274 | 0 | } |
275 | 0 | prev = node; |
276 | 0 | node = node->next; |
277 | 0 | } |
278 | | |
279 | 0 | if (node == NULL) { |
280 | 0 | if (table->frozen == 0 && hash_table_resize(table, TRUE)) { |
281 | | /* resized table, try again */ |
282 | 0 | hash_table_insert_node(table, key, value, HASH_TABLE_OP_RESIZE); |
283 | 0 | return; |
284 | 0 | } |
285 | | |
286 | 0 | if (table->free_nodes == NULL) |
287 | 0 | node = p_new(table->node_pool, struct hash_node, 1); |
288 | 0 | else { |
289 | 0 | node = table->free_nodes; |
290 | 0 | table->free_nodes = node->next; |
291 | 0 | node->next = NULL; |
292 | 0 | } |
293 | 0 | prev->next = node; |
294 | 0 | } |
295 | | |
296 | 0 | node->key = key; |
297 | 0 | node->value = value; |
298 | |
|
299 | 0 | table->nodes_count++; |
300 | 0 | } |
301 | | |
302 | | void hash_table_insert(struct hash_table *table, void *key, void *value) |
303 | 0 | { |
304 | 0 | hash_table_insert_node(table, key, value, HASH_TABLE_OP_INSERT); |
305 | 0 | } |
306 | | |
307 | | void hash_table_update(struct hash_table *table, void *key, void *value) |
308 | 0 | { |
309 | 0 | hash_table_insert_node(table, key, value, HASH_TABLE_OP_UPDATE); |
310 | 0 | } |
311 | | |
312 | | static void |
313 | | hash_table_compress(struct hash_table *table, struct hash_node *root) |
314 | 0 | { |
315 | 0 | struct hash_node *node, *next; |
316 | |
|
317 | 0 | i_assert(table->frozen == 0); |
318 | | |
319 | | /* remove deleted nodes from the list */ |
320 | 0 | for (node = root; node->next != NULL; ) { |
321 | 0 | next = node->next; |
322 | |
|
323 | 0 | if (next->key == NULL) { |
324 | 0 | node->next = next->next; |
325 | 0 | free_node(table, next); |
326 | 0 | } else { |
327 | 0 | node = next; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | | /* update root */ |
332 | 0 | if (root->key == NULL && root->next != NULL) { |
333 | 0 | next = root->next; |
334 | 0 | *root = *next; |
335 | 0 | free_node(table, next); |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | | static void hash_table_compress_removed(struct hash_table *table) |
340 | 0 | { |
341 | 0 | unsigned int i; |
342 | |
|
343 | 0 | for (i = 0; i < table->size; i++) |
344 | 0 | hash_table_compress(table, &table->nodes[i]); |
345 | |
|
346 | 0 | table->removed_count = 0; |
347 | 0 | } |
348 | | |
349 | | bool hash_table_try_remove(struct hash_table *table, const void *key) |
350 | 0 | { |
351 | 0 | struct hash_node *node; |
352 | 0 | unsigned int hash; |
353 | |
|
354 | 0 | hash = table->hash_cb(key); |
355 | |
|
356 | 0 | node = hash_table_lookup_node(table, key, hash); |
357 | 0 | if (unlikely(node == NULL)) |
358 | 0 | return FALSE; |
359 | | |
360 | 0 | node->key = NULL; |
361 | 0 | table->nodes_count--; |
362 | |
|
363 | 0 | if (table->frozen != 0) |
364 | 0 | table->removed_count++; |
365 | 0 | else if (!hash_table_resize(table, FALSE)) |
366 | 0 | hash_table_compress(table, &table->nodes[hash % table->size]); |
367 | 0 | return TRUE; |
368 | 0 | } |
369 | | |
370 | | unsigned int hash_table_count(const struct hash_table *table) |
371 | 0 | { |
372 | 0 | return table->nodes_count; |
373 | 0 | } |
374 | | |
375 | | struct hash_iterate_context *hash_table_iterate_init(struct hash_table *table) |
376 | 0 | { |
377 | 0 | struct hash_iterate_context *ctx; |
378 | |
|
379 | 0 | hash_table_freeze(table); |
380 | |
|
381 | 0 | ctx = i_new(struct hash_iterate_context, 1); |
382 | 0 | ctx->table = table; |
383 | 0 | ctx->next = &table->nodes[0]; |
384 | 0 | return ctx; |
385 | 0 | } |
386 | | |
387 | | static struct hash_node * |
388 | | hash_table_iterate_next(struct hash_iterate_context *ctx, |
389 | | struct hash_node *node) |
390 | 0 | { |
391 | 0 | do { |
392 | 0 | node = node->next; |
393 | 0 | if (node == NULL) { |
394 | 0 | if (++ctx->pos == ctx->table->size) { |
395 | 0 | ctx->pos--; |
396 | 0 | return NULL; |
397 | 0 | } |
398 | 0 | node = &ctx->table->nodes[ctx->pos]; |
399 | 0 | } |
400 | 0 | } while (node->key == NULL); |
401 | | |
402 | 0 | return node; |
403 | 0 | } |
404 | | |
405 | | bool hash_table_iterate(struct hash_iterate_context *ctx, |
406 | | void **key_r, void **value_r) |
407 | 0 | { |
408 | 0 | struct hash_node *node; |
409 | |
|
410 | 0 | node = ctx->next; |
411 | 0 | if (node != NULL && node->key == NULL) |
412 | 0 | node = hash_table_iterate_next(ctx, node); |
413 | 0 | if (node == NULL) { |
414 | 0 | *key_r = *value_r = NULL; |
415 | 0 | return FALSE; |
416 | 0 | } |
417 | 0 | *key_r = node->key; |
418 | 0 | *value_r = node->value; |
419 | |
|
420 | 0 | ctx->next = hash_table_iterate_next(ctx, node); |
421 | 0 | return TRUE; |
422 | 0 | } |
423 | | |
424 | | void hash_table_iterate_deinit(struct hash_iterate_context **_ctx) |
425 | 0 | { |
426 | 0 | struct hash_iterate_context *ctx = *_ctx; |
427 | |
|
428 | 0 | if (ctx == NULL) |
429 | 0 | return; |
430 | | |
431 | 0 | *_ctx = NULL; |
432 | 0 | hash_table_thaw(ctx->table); |
433 | 0 | i_free(ctx); |
434 | 0 | } |
435 | | |
436 | | void hash_table_freeze(struct hash_table *table) |
437 | 0 | { |
438 | 0 | table->frozen++; |
439 | 0 | } |
440 | | |
441 | | void hash_table_thaw(struct hash_table *table) |
442 | 0 | { |
443 | 0 | i_assert(table->frozen > 0); |
444 | | |
445 | 0 | if (--table->frozen > 0) |
446 | 0 | return; |
447 | | |
448 | 0 | if (table->removed_count > 0) { |
449 | 0 | if (!hash_table_resize(table, FALSE)) |
450 | 0 | hash_table_compress_removed(table); |
451 | 0 | } |
452 | 0 | } |
453 | | |
454 | | static bool hash_table_resize(struct hash_table *table, bool grow) |
455 | 0 | { |
456 | 0 | struct hash_node *old_nodes, *node, *next; |
457 | 0 | unsigned int next_size, old_size, i; |
458 | 0 | float nodes_per_list; |
459 | |
|
460 | 0 | i_assert(table->frozen == 0); |
461 | | |
462 | 0 | nodes_per_list = (float) table->nodes_count / (float) table->size; |
463 | 0 | if (nodes_per_list > 0.3 && nodes_per_list < 2.0) |
464 | 0 | return FALSE; |
465 | | |
466 | 0 | next_size = I_MAX(primes_closest(table->nodes_count+1), |
467 | 0 | table->initial_size); |
468 | 0 | if (next_size == table->size) |
469 | 0 | return FALSE; |
470 | | |
471 | 0 | if (grow && table->size >= next_size) |
472 | 0 | return FALSE; |
473 | | |
474 | | /* recreate primary table */ |
475 | 0 | old_size = table->size; |
476 | 0 | old_nodes = table->nodes; |
477 | |
|
478 | 0 | table->size = I_MAX(next_size, HASH_TABLE_MIN_SIZE); |
479 | 0 | table->nodes = i_new(struct hash_node, table->size); |
480 | |
|
481 | 0 | table->nodes_count = 0; |
482 | 0 | table->removed_count = 0; |
483 | |
|
484 | 0 | table->frozen++; |
485 | | |
486 | | /* move the data */ |
487 | 0 | for (i = 0; i < old_size; i++) { |
488 | 0 | node = &old_nodes[i]; |
489 | 0 | if (node->key != NULL) { |
490 | 0 | hash_table_insert_node(table, node->key, |
491 | 0 | node->value, HASH_TABLE_OP_RESIZE); |
492 | 0 | } |
493 | |
|
494 | 0 | for (node = node->next; node != NULL; node = next) { |
495 | 0 | next = node->next; |
496 | |
|
497 | 0 | if (node->key != NULL) { |
498 | 0 | hash_table_insert_node(table, node->key, |
499 | 0 | node->value, HASH_TABLE_OP_RESIZE); |
500 | 0 | } |
501 | 0 | free_node(table, node); |
502 | 0 | } |
503 | 0 | } |
504 | |
|
505 | 0 | table->frozen--; |
506 | |
|
507 | 0 | i_free(old_nodes); |
508 | 0 | return TRUE; |
509 | 0 | } |
510 | | |
511 | | void hash_table_copy(struct hash_table *dest, struct hash_table *src) |
512 | 0 | { |
513 | 0 | struct hash_iterate_context *iter; |
514 | 0 | void *key, *value; |
515 | |
|
516 | 0 | hash_table_freeze(dest); |
517 | |
|
518 | 0 | iter = hash_table_iterate_init(src); |
519 | 0 | while (hash_table_iterate(iter, &key, &value)) |
520 | 0 | hash_table_insert(dest, key, value); |
521 | 0 | hash_table_iterate_deinit(&iter); |
522 | |
|
523 | 0 | hash_table_thaw(dest); |
524 | 0 | } |
525 | | |
526 | | void hash_init(void) |
527 | 8.61k | { |
528 | 8.61k | random_fill(&hash_iv, sizeof(hash_iv)); |
529 | 8.61k | } |
530 | | |
531 | | unsigned int str_hash(const char *p) |
532 | 0 | { |
533 | 0 | return xxh64_to_32(xxh64_data(p, strlen(p), hash_iv)); |
534 | 0 | } |
535 | | |
536 | | unsigned int str_stable_hash(const char *p) |
537 | 0 | { |
538 | 0 | return xxh64_to_32(xxh64_data(p, strlen(p), 0)); |
539 | 0 | } |
540 | | |
541 | | unsigned int strcase_hash(const char *p) |
542 | 0 | { |
543 | 0 | struct xxh64_context ctx; |
544 | 0 | unsigned char c; |
545 | |
|
546 | 0 | xxh64_init(&ctx, hash_iv); |
547 | 0 | while (*p != '\0') { |
548 | 0 | c = (unsigned char)i_toupper(*p++); |
549 | 0 | xxh64_loop(&ctx, &c, 1); |
550 | 0 | } |
551 | 0 | return xxh64_to_32(xxh64_result(&ctx)); |
552 | 0 | } |
553 | | |
554 | | unsigned int ATTR_NO_SANITIZE_INTEGER |
555 | | mem_hash(const void *p, unsigned int size) |
556 | 0 | { |
557 | 0 | const unsigned char *s = p; |
558 | 0 | unsigned int i, g, h = 0; |
559 | |
|
560 | 0 | for (i = 0; i < size; i++) { |
561 | 0 | h = (h << 4) + *s; |
562 | 0 | if ((g = h & 0xf0000000UL) != 0) { |
563 | 0 | h = h ^ (g >> 24); |
564 | 0 | h = h ^ g; |
565 | 0 | } |
566 | 0 | s++; |
567 | 0 | } |
568 | 0 | return h; |
569 | 0 | } |
570 | | |
571 | | unsigned int ATTR_NO_SANITIZE_INTEGER |
572 | | strfastcase_hash(const char *p) |
573 | 0 | { |
574 | 0 | const unsigned char *s = (const unsigned char *)p; |
575 | 0 | unsigned int g, h = 0; |
576 | |
|
577 | 0 | while (*s != '\0') { |
578 | 0 | h = (h << 4) + ((*s) & ~0x20U); |
579 | 0 | if ((g = h & 0xf0000000UL) != 0) { |
580 | 0 | h = h ^ (g >> 24); |
581 | 0 | h = h ^ g; |
582 | 0 | } |
583 | 0 | s++; |
584 | 0 | } |
585 | |
|
586 | 0 | return h; |
587 | 0 | } |