util/hash_table: Pull out loop-invariant computations
[mesa.git] / src / util / hash_table.c
1 /*
2 * Copyright © 2009,2012 Intel Corporation
3 * Copyright © 1988-2004 Keith Packard and Bart Massey.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * Except as contained in this notice, the names of the authors
25 * or their institutions shall not be used in advertising or
26 * otherwise to promote the sale, use or other dealings in this
27 * Software without prior written authorization from the
28 * authors.
29 *
30 * Authors:
31 * Eric Anholt <eric@anholt.net>
32 * Keith Packard <keithp@keithp.com>
33 */
34
35 /**
36 * Implements an open-addressing, linear-reprobing hash table.
37 *
38 * For more information, see:
39 *
40 * http://cgit.freedesktop.org/~anholt/hash_table/tree/README
41 */
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include <assert.h>
46
47 #include "hash_table.h"
48 #include "ralloc.h"
49 #include "macros.h"
50 #include "main/hash.h"
51
52 static const uint32_t deleted_key_value;
53
54 /**
55 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
56 * p and p-2 are both prime. These tables are sized to have an extra 10%
57 * free to avoid exponential performance degradation as the hash table fills
58 */
59 static const struct {
60 uint32_t max_entries, size, rehash;
61 } hash_sizes[] = {
62 { 2, 5, 3 },
63 { 4, 7, 5 },
64 { 8, 13, 11 },
65 { 16, 19, 17 },
66 { 32, 43, 41 },
67 { 64, 73, 71 },
68 { 128, 151, 149 },
69 { 256, 283, 281 },
70 { 512, 571, 569 },
71 { 1024, 1153, 1151 },
72 { 2048, 2269, 2267 },
73 { 4096, 4519, 4517 },
74 { 8192, 9013, 9011 },
75 { 16384, 18043, 18041 },
76 { 32768, 36109, 36107 },
77 { 65536, 72091, 72089 },
78 { 131072, 144409, 144407 },
79 { 262144, 288361, 288359 },
80 { 524288, 576883, 576881 },
81 { 1048576, 1153459, 1153457 },
82 { 2097152, 2307163, 2307161 },
83 { 4194304, 4613893, 4613891 },
84 { 8388608, 9227641, 9227639 },
85 { 16777216, 18455029, 18455027 },
86 { 33554432, 36911011, 36911009 },
87 { 67108864, 73819861, 73819859 },
88 { 134217728, 147639589, 147639587 },
89 { 268435456, 295279081, 295279079 },
90 { 536870912, 590559793, 590559791 },
91 { 1073741824, 1181116273, 1181116271},
92 { 2147483648ul, 2362232233ul, 2362232231ul}
93 };
94
95 static int
96 entry_is_free(const struct hash_entry *entry)
97 {
98 return entry->key == NULL;
99 }
100
101 static int
102 entry_is_deleted(const struct hash_table *ht, struct hash_entry *entry)
103 {
104 return entry->key == ht->deleted_key;
105 }
106
107 static int
108 entry_is_present(const struct hash_table *ht, struct hash_entry *entry)
109 {
110 return entry->key != NULL && entry->key != ht->deleted_key;
111 }
112
113 bool
114 _mesa_hash_table_init(struct hash_table *ht,
115 void *mem_ctx,
116 uint32_t (*key_hash_function)(const void *key),
117 bool (*key_equals_function)(const void *a,
118 const void *b))
119 {
120 ht->size_index = 0;
121 ht->size = hash_sizes[ht->size_index].size;
122 ht->rehash = hash_sizes[ht->size_index].rehash;
123 ht->max_entries = hash_sizes[ht->size_index].max_entries;
124 ht->key_hash_function = key_hash_function;
125 ht->key_equals_function = key_equals_function;
126 ht->table = rzalloc_array(mem_ctx, struct hash_entry, ht->size);
127 ht->entries = 0;
128 ht->deleted_entries = 0;
129 ht->deleted_key = &deleted_key_value;
130
131 return ht->table != NULL;
132 }
133
134 struct hash_table *
135 _mesa_hash_table_create(void *mem_ctx,
136 uint32_t (*key_hash_function)(const void *key),
137 bool (*key_equals_function)(const void *a,
138 const void *b))
139 {
140 struct hash_table *ht;
141
142 /* mem_ctx is used to allocate the hash table, but the hash table is used
143 * to allocate all of the suballocations.
144 */
145 ht = ralloc(mem_ctx, struct hash_table);
146 if (ht == NULL)
147 return NULL;
148
149 if (!_mesa_hash_table_init(ht, ht, key_hash_function, key_equals_function)) {
150 ralloc_free(ht);
151 return NULL;
152 }
153
154 return ht;
155 }
156
157 struct hash_table *
158 _mesa_hash_table_clone(struct hash_table *src, void *dst_mem_ctx)
159 {
160 struct hash_table *ht;
161
162 ht = ralloc(dst_mem_ctx, struct hash_table);
163 if (ht == NULL)
164 return NULL;
165
166 memcpy(ht, src, sizeof(struct hash_table));
167
168 ht->table = ralloc_array(ht, struct hash_entry, ht->size);
169 if (ht->table == NULL) {
170 ralloc_free(ht);
171 return NULL;
172 }
173
174 memcpy(ht->table, src->table, ht->size * sizeof(struct hash_entry));
175
176 return ht;
177 }
178
179 /**
180 * Frees the given hash table.
181 *
182 * If delete_function is passed, it gets called on each entry present before
183 * freeing.
184 */
185 void
186 _mesa_hash_table_destroy(struct hash_table *ht,
187 void (*delete_function)(struct hash_entry *entry))
188 {
189 if (!ht)
190 return;
191
192 if (delete_function) {
193 hash_table_foreach(ht, entry) {
194 delete_function(entry);
195 }
196 }
197 ralloc_free(ht);
198 }
199
200 /**
201 * Deletes all entries of the given hash table without deleting the table
202 * itself or changing its structure.
203 *
204 * If delete_function is passed, it gets called on each entry present.
205 */
206 void
207 _mesa_hash_table_clear(struct hash_table *ht,
208 void (*delete_function)(struct hash_entry *entry))
209 {
210 struct hash_entry *entry;
211
212 for (entry = ht->table; entry != ht->table + ht->size; entry++) {
213 if (entry->key == NULL)
214 continue;
215
216 if (delete_function != NULL && entry->key != ht->deleted_key)
217 delete_function(entry);
218
219 entry->key = NULL;
220 }
221
222 ht->entries = 0;
223 ht->deleted_entries = 0;
224 }
225
226 /** Sets the value of the key pointer used for deleted entries in the table.
227 *
228 * The assumption is that usually keys are actual pointers, so we use a
229 * default value of a pointer to an arbitrary piece of storage in the library.
230 * But in some cases a consumer wants to store some other sort of value in the
231 * table, like a uint32_t, in which case that pointer may conflict with one of
232 * their valid keys. This lets that user select a safe value.
233 *
234 * This must be called before any keys are actually deleted from the table.
235 */
236 void
237 _mesa_hash_table_set_deleted_key(struct hash_table *ht, const void *deleted_key)
238 {
239 ht->deleted_key = deleted_key;
240 }
241
242 static struct hash_entry *
243 hash_table_search(struct hash_table *ht, uint32_t hash, const void *key)
244 {
245 uint32_t size = ht->size;
246 uint32_t start_hash_address = hash % size;
247 uint32_t hash_address = start_hash_address;
248 uint32_t double_hash = 1 + hash % ht->rehash;
249
250 do {
251 struct hash_entry *entry = ht->table + hash_address;
252
253 if (entry_is_free(entry)) {
254 return NULL;
255 } else if (entry_is_present(ht, entry) && entry->hash == hash) {
256 if (ht->key_equals_function(key, entry->key)) {
257 return entry;
258 }
259 }
260
261 hash_address += double_hash;
262 if (hash_address >= size)
263 hash_address -= size;
264 } while (hash_address != start_hash_address);
265
266 return NULL;
267 }
268
269 /**
270 * Finds a hash table entry with the given key and hash of that key.
271 *
272 * Returns NULL if no entry is found. Note that the data pointer may be
273 * modified by the user.
274 */
275 struct hash_entry *
276 _mesa_hash_table_search(struct hash_table *ht, const void *key)
277 {
278 assert(ht->key_hash_function);
279 return hash_table_search(ht, ht->key_hash_function(key), key);
280 }
281
282 struct hash_entry *
283 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
284 const void *key)
285 {
286 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
287 return hash_table_search(ht, hash, key);
288 }
289
290 static struct hash_entry *
291 hash_table_insert(struct hash_table *ht, uint32_t hash,
292 const void *key, void *data);
293
294 static void
295 _mesa_hash_table_rehash(struct hash_table *ht, unsigned new_size_index)
296 {
297 struct hash_table old_ht;
298 struct hash_entry *table;
299
300 if (new_size_index >= ARRAY_SIZE(hash_sizes))
301 return;
302
303 table = rzalloc_array(ralloc_parent(ht->table), struct hash_entry,
304 hash_sizes[new_size_index].size);
305 if (table == NULL)
306 return;
307
308 old_ht = *ht;
309
310 ht->table = table;
311 ht->size_index = new_size_index;
312 ht->size = hash_sizes[ht->size_index].size;
313 ht->rehash = hash_sizes[ht->size_index].rehash;
314 ht->max_entries = hash_sizes[ht->size_index].max_entries;
315 ht->entries = 0;
316 ht->deleted_entries = 0;
317
318 hash_table_foreach(&old_ht, entry) {
319 hash_table_insert(ht, entry->hash, entry->key, entry->data);
320 }
321
322 ralloc_free(old_ht.table);
323 }
324
325 static struct hash_entry *
326 hash_table_insert(struct hash_table *ht, uint32_t hash,
327 const void *key, void *data)
328 {
329 struct hash_entry *available_entry = NULL;
330
331 assert(key != NULL);
332
333 if (ht->entries >= ht->max_entries) {
334 _mesa_hash_table_rehash(ht, ht->size_index + 1);
335 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
336 _mesa_hash_table_rehash(ht, ht->size_index);
337 }
338
339 uint32_t size = ht->size;
340 uint32_t start_hash_address = hash % size;
341 uint32_t hash_address = start_hash_address;
342 uint32_t double_hash = 1 + hash % ht->rehash;
343 do {
344 struct hash_entry *entry = ht->table + hash_address;
345
346 if (!entry_is_present(ht, entry)) {
347 /* Stash the first available entry we find */
348 if (available_entry == NULL)
349 available_entry = entry;
350 if (entry_is_free(entry))
351 break;
352 }
353
354 /* Implement replacement when another insert happens
355 * with a matching key. This is a relatively common
356 * feature of hash tables, with the alternative
357 * generally being "insert the new value as well, and
358 * return it first when the key is searched for".
359 *
360 * Note that the hash table doesn't have a delete
361 * callback. If freeing of old data pointers is
362 * required to avoid memory leaks, perform a search
363 * before inserting.
364 */
365 if (!entry_is_deleted(ht, entry) &&
366 entry->hash == hash &&
367 ht->key_equals_function(key, entry->key)) {
368 entry->key = key;
369 entry->data = data;
370 return entry;
371 }
372
373 hash_address += double_hash;
374 if (hash_address >= size)
375 hash_address -= size;
376 } while (hash_address != start_hash_address);
377
378 if (available_entry) {
379 if (entry_is_deleted(ht, available_entry))
380 ht->deleted_entries--;
381 available_entry->hash = hash;
382 available_entry->key = key;
383 available_entry->data = data;
384 ht->entries++;
385 return available_entry;
386 }
387
388 /* We could hit here if a required resize failed. An unchecked-malloc
389 * application could ignore this result.
390 */
391 return NULL;
392 }
393
394 /**
395 * Inserts the key with the given hash into the table.
396 *
397 * Note that insertion may rearrange the table on a resize or rehash,
398 * so previously found hash_entries are no longer valid after this function.
399 */
400 struct hash_entry *
401 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data)
402 {
403 assert(ht->key_hash_function);
404 return hash_table_insert(ht, ht->key_hash_function(key), key, data);
405 }
406
407 struct hash_entry *
408 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
409 const void *key, void *data)
410 {
411 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
412 return hash_table_insert(ht, hash, key, data);
413 }
414
415 /**
416 * This function deletes the given hash table entry.
417 *
418 * Note that deletion doesn't otherwise modify the table, so an iteration over
419 * the table deleting entries is safe.
420 */
421 void
422 _mesa_hash_table_remove(struct hash_table *ht,
423 struct hash_entry *entry)
424 {
425 if (!entry)
426 return;
427
428 entry->key = ht->deleted_key;
429 ht->entries--;
430 ht->deleted_entries++;
431 }
432
433 /**
434 * Removes the entry with the corresponding key, if exists.
435 */
436 void _mesa_hash_table_remove_key(struct hash_table *ht,
437 const void *key)
438 {
439 _mesa_hash_table_remove(ht, _mesa_hash_table_search(ht, key));
440 }
441
442 /**
443 * This function is an iterator over the hash table.
444 *
445 * Pass in NULL for the first entry, as in the start of a for loop. Note that
446 * an iteration over the table is O(table_size) not O(entries).
447 */
448 struct hash_entry *
449 _mesa_hash_table_next_entry(struct hash_table *ht,
450 struct hash_entry *entry)
451 {
452 if (entry == NULL)
453 entry = ht->table;
454 else
455 entry = entry + 1;
456
457 for (; entry != ht->table + ht->size; entry++) {
458 if (entry_is_present(ht, entry)) {
459 return entry;
460 }
461 }
462
463 return NULL;
464 }
465
466 /**
467 * Returns a random entry from the hash table.
468 *
469 * This may be useful in implementing random replacement (as opposed
470 * to just removing everything) in caches based on this hash table
471 * implementation. @predicate may be used to filter entries, or may
472 * be set to NULL for no filtering.
473 */
474 struct hash_entry *
475 _mesa_hash_table_random_entry(struct hash_table *ht,
476 bool (*predicate)(struct hash_entry *entry))
477 {
478 struct hash_entry *entry;
479 uint32_t i = rand() % ht->size;
480
481 if (ht->entries == 0)
482 return NULL;
483
484 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
485 if (entry_is_present(ht, entry) &&
486 (!predicate || predicate(entry))) {
487 return entry;
488 }
489 }
490
491 for (entry = ht->table; entry != ht->table + i; entry++) {
492 if (entry_is_present(ht, entry) &&
493 (!predicate || predicate(entry))) {
494 return entry;
495 }
496 }
497
498 return NULL;
499 }
500
501
502 /**
503 * Quick FNV-1a hash implementation based on:
504 * http://www.isthe.com/chongo/tech/comp/fnv/
505 *
506 * FNV-1a is not be the best hash out there -- Jenkins's lookup3 is supposed
507 * to be quite good, and it probably beats FNV. But FNV has the advantage
508 * that it involves almost no code. For an improvement on both, see Paul
509 * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
510 */
511 uint32_t
512 _mesa_hash_data(const void *data, size_t size)
513 {
514 return _mesa_fnv32_1a_accumulate_block(_mesa_fnv32_1a_offset_bias,
515 data, size);
516 }
517
518 /** FNV-1a string hash implementation */
519 uint32_t
520 _mesa_hash_string(const void *_key)
521 {
522 uint32_t hash = _mesa_fnv32_1a_offset_bias;
523 const char *key = _key;
524
525 while (*key != 0) {
526 hash = _mesa_fnv32_1a_accumulate(hash, *key);
527 key++;
528 }
529
530 return hash;
531 }
532
533 /**
534 * String compare function for use as the comparison callback in
535 * _mesa_hash_table_create().
536 */
537 bool
538 _mesa_key_string_equal(const void *a, const void *b)
539 {
540 return strcmp(a, b) == 0;
541 }
542
543 bool
544 _mesa_key_pointer_equal(const void *a, const void *b)
545 {
546 return a == b;
547 }
548
549 /**
550 * Helper to create a hash table with pointer keys.
551 */
552 struct hash_table *
553 _mesa_pointer_hash_table_create(void *mem_ctx)
554 {
555 return _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
556 _mesa_key_pointer_equal);
557 }
558
559 /**
560 * Hash table wrapper which supports 64-bit keys.
561 *
562 * TODO: unify all hash table implementations.
563 */
564
565 struct hash_key_u64 {
566 uint64_t value;
567 };
568
569 static uint32_t
570 key_u64_hash(const void *key)
571 {
572 return _mesa_hash_data(key, sizeof(struct hash_key_u64));
573 }
574
575 static bool
576 key_u64_equals(const void *a, const void *b)
577 {
578 const struct hash_key_u64 *aa = a;
579 const struct hash_key_u64 *bb = b;
580
581 return aa->value == bb->value;
582 }
583
584 struct hash_table_u64 *
585 _mesa_hash_table_u64_create(void *mem_ctx)
586 {
587 struct hash_table_u64 *ht;
588
589 ht = CALLOC_STRUCT(hash_table_u64);
590 if (!ht)
591 return NULL;
592
593 if (sizeof(void *) == 8) {
594 ht->table = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
595 _mesa_key_pointer_equal);
596 } else {
597 ht->table = _mesa_hash_table_create(mem_ctx, key_u64_hash,
598 key_u64_equals);
599 }
600
601 if (ht->table)
602 _mesa_hash_table_set_deleted_key(ht->table, uint_key(DELETED_KEY_VALUE));
603
604 return ht;
605 }
606
607 void
608 _mesa_hash_table_u64_destroy(struct hash_table_u64 *ht,
609 void (*delete_function)(struct hash_entry *entry))
610 {
611 if (!ht)
612 return;
613
614 if (ht->deleted_key_data) {
615 if (delete_function) {
616 struct hash_table *table = ht->table;
617 struct hash_entry deleted_entry;
618
619 /* Create a fake entry for the delete function. */
620 deleted_entry.hash = table->key_hash_function(table->deleted_key);
621 deleted_entry.key = table->deleted_key;
622 deleted_entry.data = ht->deleted_key_data;
623
624 delete_function(&deleted_entry);
625 }
626 ht->deleted_key_data = NULL;
627 }
628
629 _mesa_hash_table_destroy(ht->table, delete_function);
630 free(ht);
631 }
632
633 void
634 _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
635 void *data)
636 {
637 if (key == DELETED_KEY_VALUE) {
638 ht->deleted_key_data = data;
639 return;
640 }
641
642 if (sizeof(void *) == 8) {
643 _mesa_hash_table_insert(ht->table, (void *)(uintptr_t)key, data);
644 } else {
645 struct hash_key_u64 *_key = CALLOC_STRUCT(hash_key_u64);
646
647 if (!_key)
648 return;
649 _key->value = key;
650
651 _mesa_hash_table_insert(ht->table, _key, data);
652 }
653 }
654
655 static struct hash_entry *
656 hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key)
657 {
658 if (sizeof(void *) == 8) {
659 return _mesa_hash_table_search(ht->table, (void *)(uintptr_t)key);
660 } else {
661 struct hash_key_u64 _key = { .value = key };
662 return _mesa_hash_table_search(ht->table, &_key);
663 }
664 }
665
666 void *
667 _mesa_hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key)
668 {
669 struct hash_entry *entry;
670
671 if (key == DELETED_KEY_VALUE)
672 return ht->deleted_key_data;
673
674 entry = hash_table_u64_search(ht, key);
675 if (!entry)
676 return NULL;
677
678 return entry->data;
679 }
680
681 void
682 _mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key)
683 {
684 struct hash_entry *entry;
685
686 if (key == DELETED_KEY_VALUE) {
687 ht->deleted_key_data = NULL;
688 return;
689 }
690
691 entry = hash_table_u64_search(ht, key);
692 if (!entry)
693 return;
694
695 if (sizeof(void *) == 8) {
696 _mesa_hash_table_remove(ht->table, entry);
697 } else {
698 struct hash_key *_key = (struct hash_key *)entry->key;
699
700 _mesa_hash_table_remove(ht->table, entry);
701 free(_key);
702 }
703 }