i965/miptree: avoid uninitialized variable warnings
[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 struct hash_table *
114 _mesa_hash_table_create(void *mem_ctx,
115 uint32_t (*key_hash_function)(const void *key),
116 bool (*key_equals_function)(const void *a,
117 const void *b))
118 {
119 struct hash_table *ht;
120
121 ht = ralloc(mem_ctx, struct hash_table);
122 if (ht == NULL)
123 return NULL;
124
125 ht->size_index = 0;
126 ht->size = hash_sizes[ht->size_index].size;
127 ht->rehash = hash_sizes[ht->size_index].rehash;
128 ht->max_entries = hash_sizes[ht->size_index].max_entries;
129 ht->key_hash_function = key_hash_function;
130 ht->key_equals_function = key_equals_function;
131 ht->table = rzalloc_array(ht, struct hash_entry, ht->size);
132 ht->entries = 0;
133 ht->deleted_entries = 0;
134 ht->deleted_key = &deleted_key_value;
135
136 if (ht->table == NULL) {
137 ralloc_free(ht);
138 return NULL;
139 }
140
141 return ht;
142 }
143
144 struct hash_table *
145 _mesa_hash_table_clone(struct hash_table *src, void *dst_mem_ctx)
146 {
147 struct hash_table *ht;
148
149 ht = ralloc(dst_mem_ctx, struct hash_table);
150 if (ht == NULL)
151 return NULL;
152
153 memcpy(ht, src, sizeof(struct hash_table));
154
155 ht->table = ralloc_array(ht, struct hash_entry, ht->size);
156 if (ht->table == NULL) {
157 ralloc_free(ht);
158 return NULL;
159 }
160
161 memcpy(ht->table, src->table, ht->size * sizeof(struct hash_entry));
162
163 return ht;
164 }
165
166 /**
167 * Frees the given hash table.
168 *
169 * If delete_function is passed, it gets called on each entry present before
170 * freeing.
171 */
172 void
173 _mesa_hash_table_destroy(struct hash_table *ht,
174 void (*delete_function)(struct hash_entry *entry))
175 {
176 if (!ht)
177 return;
178
179 if (delete_function) {
180 struct hash_entry *entry;
181
182 hash_table_foreach(ht, entry) {
183 delete_function(entry);
184 }
185 }
186 ralloc_free(ht);
187 }
188
189 /**
190 * Deletes all entries of the given hash table without deleting the table
191 * itself or changing its structure.
192 *
193 * If delete_function is passed, it gets called on each entry present.
194 */
195 void
196 _mesa_hash_table_clear(struct hash_table *ht,
197 void (*delete_function)(struct hash_entry *entry))
198 {
199 struct hash_entry *entry;
200
201 for (entry = ht->table; entry != ht->table + ht->size; entry++) {
202 if (entry->key == NULL)
203 continue;
204
205 if (delete_function != NULL && entry->key != ht->deleted_key)
206 delete_function(entry);
207
208 entry->key = NULL;
209 }
210
211 ht->entries = 0;
212 ht->deleted_entries = 0;
213 }
214
215 /** Sets the value of the key pointer used for deleted entries in the table.
216 *
217 * The assumption is that usually keys are actual pointers, so we use a
218 * default value of a pointer to an arbitrary piece of storage in the library.
219 * But in some cases a consumer wants to store some other sort of value in the
220 * table, like a uint32_t, in which case that pointer may conflict with one of
221 * their valid keys. This lets that user select a safe value.
222 *
223 * This must be called before any keys are actually deleted from the table.
224 */
225 void
226 _mesa_hash_table_set_deleted_key(struct hash_table *ht, const void *deleted_key)
227 {
228 ht->deleted_key = deleted_key;
229 }
230
231 static struct hash_entry *
232 hash_table_search(struct hash_table *ht, uint32_t hash, const void *key)
233 {
234 uint32_t start_hash_address = hash % ht->size;
235 uint32_t hash_address = start_hash_address;
236
237 do {
238 uint32_t double_hash;
239
240 struct hash_entry *entry = ht->table + hash_address;
241
242 if (entry_is_free(entry)) {
243 return NULL;
244 } else if (entry_is_present(ht, entry) && entry->hash == hash) {
245 if (ht->key_equals_function(key, entry->key)) {
246 return entry;
247 }
248 }
249
250 double_hash = 1 + hash % ht->rehash;
251
252 hash_address = (hash_address + double_hash) % ht->size;
253 } while (hash_address != start_hash_address);
254
255 return NULL;
256 }
257
258 /**
259 * Finds a hash table entry with the given key and hash of that key.
260 *
261 * Returns NULL if no entry is found. Note that the data pointer may be
262 * modified by the user.
263 */
264 struct hash_entry *
265 _mesa_hash_table_search(struct hash_table *ht, const void *key)
266 {
267 assert(ht->key_hash_function);
268 return hash_table_search(ht, ht->key_hash_function(key), key);
269 }
270
271 struct hash_entry *
272 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
273 const void *key)
274 {
275 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
276 return hash_table_search(ht, hash, key);
277 }
278
279 static struct hash_entry *
280 hash_table_insert(struct hash_table *ht, uint32_t hash,
281 const void *key, void *data);
282
283 static void
284 _mesa_hash_table_rehash(struct hash_table *ht, unsigned new_size_index)
285 {
286 struct hash_table old_ht;
287 struct hash_entry *table, *entry;
288
289 if (new_size_index >= ARRAY_SIZE(hash_sizes))
290 return;
291
292 table = rzalloc_array(ht, struct hash_entry,
293 hash_sizes[new_size_index].size);
294 if (table == NULL)
295 return;
296
297 old_ht = *ht;
298
299 ht->table = table;
300 ht->size_index = new_size_index;
301 ht->size = hash_sizes[ht->size_index].size;
302 ht->rehash = hash_sizes[ht->size_index].rehash;
303 ht->max_entries = hash_sizes[ht->size_index].max_entries;
304 ht->entries = 0;
305 ht->deleted_entries = 0;
306
307 hash_table_foreach(&old_ht, entry) {
308 hash_table_insert(ht, entry->hash, entry->key, entry->data);
309 }
310
311 ralloc_free(old_ht.table);
312 }
313
314 static struct hash_entry *
315 hash_table_insert(struct hash_table *ht, uint32_t hash,
316 const void *key, void *data)
317 {
318 uint32_t start_hash_address, hash_address;
319 struct hash_entry *available_entry = NULL;
320
321 assert(key != NULL);
322
323 if (ht->entries >= ht->max_entries) {
324 _mesa_hash_table_rehash(ht, ht->size_index + 1);
325 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
326 _mesa_hash_table_rehash(ht, ht->size_index);
327 }
328
329 start_hash_address = hash % ht->size;
330 hash_address = start_hash_address;
331 do {
332 struct hash_entry *entry = ht->table + hash_address;
333 uint32_t double_hash;
334
335 if (!entry_is_present(ht, entry)) {
336 /* Stash the first available entry we find */
337 if (available_entry == NULL)
338 available_entry = entry;
339 if (entry_is_free(entry))
340 break;
341 }
342
343 /* Implement replacement when another insert happens
344 * with a matching key. This is a relatively common
345 * feature of hash tables, with the alternative
346 * generally being "insert the new value as well, and
347 * return it first when the key is searched for".
348 *
349 * Note that the hash table doesn't have a delete
350 * callback. If freeing of old data pointers is
351 * required to avoid memory leaks, perform a search
352 * before inserting.
353 */
354 if (!entry_is_deleted(ht, entry) &&
355 entry->hash == hash &&
356 ht->key_equals_function(key, entry->key)) {
357 entry->key = key;
358 entry->data = data;
359 return entry;
360 }
361
362
363 double_hash = 1 + hash % ht->rehash;
364
365 hash_address = (hash_address + double_hash) % ht->size;
366 } while (hash_address != start_hash_address);
367
368 if (available_entry) {
369 if (entry_is_deleted(ht, available_entry))
370 ht->deleted_entries--;
371 available_entry->hash = hash;
372 available_entry->key = key;
373 available_entry->data = data;
374 ht->entries++;
375 return available_entry;
376 }
377
378 /* We could hit here if a required resize failed. An unchecked-malloc
379 * application could ignore this result.
380 */
381 return NULL;
382 }
383
384 /**
385 * Inserts the key with the given hash into the table.
386 *
387 * Note that insertion may rearrange the table on a resize or rehash,
388 * so previously found hash_entries are no longer valid after this function.
389 */
390 struct hash_entry *
391 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data)
392 {
393 assert(ht->key_hash_function);
394 return hash_table_insert(ht, ht->key_hash_function(key), key, data);
395 }
396
397 struct hash_entry *
398 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
399 const void *key, void *data)
400 {
401 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
402 return hash_table_insert(ht, hash, key, data);
403 }
404
405 /**
406 * This function deletes the given hash table entry.
407 *
408 * Note that deletion doesn't otherwise modify the table, so an iteration over
409 * the table deleting entries is safe.
410 */
411 void
412 _mesa_hash_table_remove(struct hash_table *ht,
413 struct hash_entry *entry)
414 {
415 if (!entry)
416 return;
417
418 entry->key = ht->deleted_key;
419 ht->entries--;
420 ht->deleted_entries++;
421 }
422
423 /**
424 * Removes the entry with the corresponding key, if exists.
425 */
426 void _mesa_hash_table_remove_key(struct hash_table *ht,
427 const void *key)
428 {
429 _mesa_hash_table_remove(ht, _mesa_hash_table_search(ht, key));
430 }
431
432 /**
433 * This function is an iterator over the hash table.
434 *
435 * Pass in NULL for the first entry, as in the start of a for loop. Note that
436 * an iteration over the table is O(table_size) not O(entries).
437 */
438 struct hash_entry *
439 _mesa_hash_table_next_entry(struct hash_table *ht,
440 struct hash_entry *entry)
441 {
442 if (entry == NULL)
443 entry = ht->table;
444 else
445 entry = entry + 1;
446
447 for (; entry != ht->table + ht->size; entry++) {
448 if (entry_is_present(ht, entry)) {
449 return entry;
450 }
451 }
452
453 return NULL;
454 }
455
456 /**
457 * Returns a random entry from the hash table.
458 *
459 * This may be useful in implementing random replacement (as opposed
460 * to just removing everything) in caches based on this hash table
461 * implementation. @predicate may be used to filter entries, or may
462 * be set to NULL for no filtering.
463 */
464 struct hash_entry *
465 _mesa_hash_table_random_entry(struct hash_table *ht,
466 bool (*predicate)(struct hash_entry *entry))
467 {
468 struct hash_entry *entry;
469 uint32_t i = rand() % ht->size;
470
471 if (ht->entries == 0)
472 return NULL;
473
474 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
475 if (entry_is_present(ht, entry) &&
476 (!predicate || predicate(entry))) {
477 return entry;
478 }
479 }
480
481 for (entry = ht->table; entry != ht->table + i; entry++) {
482 if (entry_is_present(ht, entry) &&
483 (!predicate || predicate(entry))) {
484 return entry;
485 }
486 }
487
488 return NULL;
489 }
490
491
492 /**
493 * Quick FNV-1a hash implementation based on:
494 * http://www.isthe.com/chongo/tech/comp/fnv/
495 *
496 * FNV-1a is not be the best hash out there -- Jenkins's lookup3 is supposed
497 * to be quite good, and it probably beats FNV. But FNV has the advantage
498 * that it involves almost no code. For an improvement on both, see Paul
499 * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
500 */
501 uint32_t
502 _mesa_hash_data(const void *data, size_t size)
503 {
504 return _mesa_fnv32_1a_accumulate_block(_mesa_fnv32_1a_offset_bias,
505 data, size);
506 }
507
508 /** FNV-1a string hash implementation */
509 uint32_t
510 _mesa_hash_string(const void *_key)
511 {
512 uint32_t hash = _mesa_fnv32_1a_offset_bias;
513 const char *key = _key;
514
515 while (*key != 0) {
516 hash = _mesa_fnv32_1a_accumulate(hash, *key);
517 key++;
518 }
519
520 return hash;
521 }
522
523 /**
524 * String compare function for use as the comparison callback in
525 * _mesa_hash_table_create().
526 */
527 bool
528 _mesa_key_string_equal(const void *a, const void *b)
529 {
530 return strcmp(a, b) == 0;
531 }
532
533 bool
534 _mesa_key_pointer_equal(const void *a, const void *b)
535 {
536 return a == b;
537 }
538
539 /**
540 * Hash table wrapper which supports 64-bit keys.
541 *
542 * TODO: unify all hash table implementations.
543 */
544
545 struct hash_key_u64 {
546 uint64_t value;
547 };
548
549 static uint32_t
550 key_u64_hash(const void *key)
551 {
552 return _mesa_hash_data(key, sizeof(struct hash_key_u64));
553 }
554
555 static bool
556 key_u64_equals(const void *a, const void *b)
557 {
558 const struct hash_key_u64 *aa = a;
559 const struct hash_key_u64 *bb = b;
560
561 return aa->value == bb->value;
562 }
563
564 struct hash_table_u64 *
565 _mesa_hash_table_u64_create(void *mem_ctx)
566 {
567 struct hash_table_u64 *ht;
568
569 ht = CALLOC_STRUCT(hash_table_u64);
570 if (!ht)
571 return NULL;
572
573 if (sizeof(void *) == 8) {
574 ht->table = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
575 _mesa_key_pointer_equal);
576 } else {
577 ht->table = _mesa_hash_table_create(mem_ctx, key_u64_hash,
578 key_u64_equals);
579 }
580
581 if (ht->table)
582 _mesa_hash_table_set_deleted_key(ht->table, uint_key(DELETED_KEY_VALUE));
583
584 return ht;
585 }
586
587 void
588 _mesa_hash_table_u64_destroy(struct hash_table_u64 *ht,
589 void (*delete_function)(struct hash_entry *entry))
590 {
591 if (!ht)
592 return;
593
594 if (ht->deleted_key_data) {
595 if (delete_function) {
596 struct hash_table *table = ht->table;
597 struct hash_entry deleted_entry;
598
599 /* Create a fake entry for the delete function. */
600 deleted_entry.hash = table->key_hash_function(table->deleted_key);
601 deleted_entry.key = table->deleted_key;
602 deleted_entry.data = ht->deleted_key_data;
603
604 delete_function(&deleted_entry);
605 }
606 ht->deleted_key_data = NULL;
607 }
608
609 _mesa_hash_table_destroy(ht->table, delete_function);
610 free(ht);
611 }
612
613 void
614 _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
615 void *data)
616 {
617 if (key == DELETED_KEY_VALUE) {
618 ht->deleted_key_data = data;
619 return;
620 }
621
622 if (sizeof(void *) == 8) {
623 _mesa_hash_table_insert(ht->table, (void *)(uintptr_t)key, data);
624 } else {
625 struct hash_key_u64 *_key = CALLOC_STRUCT(hash_key_u64);
626
627 if (!_key)
628 return;
629 _key->value = key;
630
631 _mesa_hash_table_insert(ht->table, _key, data);
632 }
633 }
634
635 static struct hash_entry *
636 hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key)
637 {
638 if (sizeof(void *) == 8) {
639 return _mesa_hash_table_search(ht->table, (void *)(uintptr_t)key);
640 } else {
641 struct hash_key_u64 _key = { .value = key };
642 return _mesa_hash_table_search(ht->table, &_key);
643 }
644 }
645
646 void *
647 _mesa_hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key)
648 {
649 struct hash_entry *entry;
650
651 if (key == DELETED_KEY_VALUE)
652 return ht->deleted_key_data;
653
654 entry = hash_table_u64_search(ht, key);
655 if (!entry)
656 return NULL;
657
658 return entry->data;
659 }
660
661 void
662 _mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key)
663 {
664 struct hash_entry *entry;
665
666 if (key == DELETED_KEY_VALUE) {
667 ht->deleted_key_data = NULL;
668 return;
669 }
670
671 entry = hash_table_u64_search(ht, key);
672 if (!entry)
673 return;
674
675 if (sizeof(void *) == 8) {
676 _mesa_hash_table_remove(ht->table, entry);
677 } else {
678 struct hash_key *_key = (struct hash_key *)entry->key;
679
680 _mesa_hash_table_remove(ht->table, entry);
681 free(_key);
682 }
683 }