ac/nir: set cache policy when loading/storing buffer images
[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 start_hash_address = hash % ht->size;
246 uint32_t hash_address = start_hash_address;
247
248 do {
249 uint32_t double_hash;
250
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 double_hash = 1 + hash % ht->rehash;
262
263 hash_address = (hash_address + double_hash) % ht->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 uint32_t start_hash_address, hash_address;
330 struct hash_entry *available_entry = NULL;
331
332 assert(key != NULL);
333
334 if (ht->entries >= ht->max_entries) {
335 _mesa_hash_table_rehash(ht, ht->size_index + 1);
336 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
337 _mesa_hash_table_rehash(ht, ht->size_index);
338 }
339
340 start_hash_address = hash % ht->size;
341 hash_address = start_hash_address;
342 do {
343 struct hash_entry *entry = ht->table + hash_address;
344 uint32_t double_hash;
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
374 double_hash = 1 + hash % ht->rehash;
375
376 hash_address = (hash_address + double_hash) % ht->size;
377 } while (hash_address != start_hash_address);
378
379 if (available_entry) {
380 if (entry_is_deleted(ht, available_entry))
381 ht->deleted_entries--;
382 available_entry->hash = hash;
383 available_entry->key = key;
384 available_entry->data = data;
385 ht->entries++;
386 return available_entry;
387 }
388
389 /* We could hit here if a required resize failed. An unchecked-malloc
390 * application could ignore this result.
391 */
392 return NULL;
393 }
394
395 /**
396 * Inserts the key with the given hash into the table.
397 *
398 * Note that insertion may rearrange the table on a resize or rehash,
399 * so previously found hash_entries are no longer valid after this function.
400 */
401 struct hash_entry *
402 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data)
403 {
404 assert(ht->key_hash_function);
405 return hash_table_insert(ht, ht->key_hash_function(key), key, data);
406 }
407
408 struct hash_entry *
409 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
410 const void *key, void *data)
411 {
412 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
413 return hash_table_insert(ht, hash, key, data);
414 }
415
416 /**
417 * This function deletes the given hash table entry.
418 *
419 * Note that deletion doesn't otherwise modify the table, so an iteration over
420 * the table deleting entries is safe.
421 */
422 void
423 _mesa_hash_table_remove(struct hash_table *ht,
424 struct hash_entry *entry)
425 {
426 if (!entry)
427 return;
428
429 entry->key = ht->deleted_key;
430 ht->entries--;
431 ht->deleted_entries++;
432 }
433
434 /**
435 * Removes the entry with the corresponding key, if exists.
436 */
437 void _mesa_hash_table_remove_key(struct hash_table *ht,
438 const void *key)
439 {
440 _mesa_hash_table_remove(ht, _mesa_hash_table_search(ht, key));
441 }
442
443 /**
444 * This function is an iterator over the hash table.
445 *
446 * Pass in NULL for the first entry, as in the start of a for loop. Note that
447 * an iteration over the table is O(table_size) not O(entries).
448 */
449 struct hash_entry *
450 _mesa_hash_table_next_entry(struct hash_table *ht,
451 struct hash_entry *entry)
452 {
453 if (entry == NULL)
454 entry = ht->table;
455 else
456 entry = entry + 1;
457
458 for (; entry != ht->table + ht->size; entry++) {
459 if (entry_is_present(ht, entry)) {
460 return entry;
461 }
462 }
463
464 return NULL;
465 }
466
467 /**
468 * Returns a random entry from the hash table.
469 *
470 * This may be useful in implementing random replacement (as opposed
471 * to just removing everything) in caches based on this hash table
472 * implementation. @predicate may be used to filter entries, or may
473 * be set to NULL for no filtering.
474 */
475 struct hash_entry *
476 _mesa_hash_table_random_entry(struct hash_table *ht,
477 bool (*predicate)(struct hash_entry *entry))
478 {
479 struct hash_entry *entry;
480 uint32_t i = rand() % ht->size;
481
482 if (ht->entries == 0)
483 return NULL;
484
485 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
486 if (entry_is_present(ht, entry) &&
487 (!predicate || predicate(entry))) {
488 return entry;
489 }
490 }
491
492 for (entry = ht->table; entry != ht->table + i; entry++) {
493 if (entry_is_present(ht, entry) &&
494 (!predicate || predicate(entry))) {
495 return entry;
496 }
497 }
498
499 return NULL;
500 }
501
502
503 /**
504 * Quick FNV-1a hash implementation based on:
505 * http://www.isthe.com/chongo/tech/comp/fnv/
506 *
507 * FNV-1a is not be the best hash out there -- Jenkins's lookup3 is supposed
508 * to be quite good, and it probably beats FNV. But FNV has the advantage
509 * that it involves almost no code. For an improvement on both, see Paul
510 * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
511 */
512 uint32_t
513 _mesa_hash_data(const void *data, size_t size)
514 {
515 return _mesa_fnv32_1a_accumulate_block(_mesa_fnv32_1a_offset_bias,
516 data, size);
517 }
518
519 /** FNV-1a string hash implementation */
520 uint32_t
521 _mesa_hash_string(const void *_key)
522 {
523 uint32_t hash = _mesa_fnv32_1a_offset_bias;
524 const char *key = _key;
525
526 while (*key != 0) {
527 hash = _mesa_fnv32_1a_accumulate(hash, *key);
528 key++;
529 }
530
531 return hash;
532 }
533
534 /**
535 * String compare function for use as the comparison callback in
536 * _mesa_hash_table_create().
537 */
538 bool
539 _mesa_key_string_equal(const void *a, const void *b)
540 {
541 return strcmp(a, b) == 0;
542 }
543
544 bool
545 _mesa_key_pointer_equal(const void *a, const void *b)
546 {
547 return a == b;
548 }
549
550 /**
551 * Hash table wrapper which supports 64-bit keys.
552 *
553 * TODO: unify all hash table implementations.
554 */
555
556 struct hash_key_u64 {
557 uint64_t value;
558 };
559
560 static uint32_t
561 key_u64_hash(const void *key)
562 {
563 return _mesa_hash_data(key, sizeof(struct hash_key_u64));
564 }
565
566 static bool
567 key_u64_equals(const void *a, const void *b)
568 {
569 const struct hash_key_u64 *aa = a;
570 const struct hash_key_u64 *bb = b;
571
572 return aa->value == bb->value;
573 }
574
575 struct hash_table_u64 *
576 _mesa_hash_table_u64_create(void *mem_ctx)
577 {
578 struct hash_table_u64 *ht;
579
580 ht = CALLOC_STRUCT(hash_table_u64);
581 if (!ht)
582 return NULL;
583
584 if (sizeof(void *) == 8) {
585 ht->table = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
586 _mesa_key_pointer_equal);
587 } else {
588 ht->table = _mesa_hash_table_create(mem_ctx, key_u64_hash,
589 key_u64_equals);
590 }
591
592 if (ht->table)
593 _mesa_hash_table_set_deleted_key(ht->table, uint_key(DELETED_KEY_VALUE));
594
595 return ht;
596 }
597
598 void
599 _mesa_hash_table_u64_destroy(struct hash_table_u64 *ht,
600 void (*delete_function)(struct hash_entry *entry))
601 {
602 if (!ht)
603 return;
604
605 if (ht->deleted_key_data) {
606 if (delete_function) {
607 struct hash_table *table = ht->table;
608 struct hash_entry deleted_entry;
609
610 /* Create a fake entry for the delete function. */
611 deleted_entry.hash = table->key_hash_function(table->deleted_key);
612 deleted_entry.key = table->deleted_key;
613 deleted_entry.data = ht->deleted_key_data;
614
615 delete_function(&deleted_entry);
616 }
617 ht->deleted_key_data = NULL;
618 }
619
620 _mesa_hash_table_destroy(ht->table, delete_function);
621 free(ht);
622 }
623
624 void
625 _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
626 void *data)
627 {
628 if (key == DELETED_KEY_VALUE) {
629 ht->deleted_key_data = data;
630 return;
631 }
632
633 if (sizeof(void *) == 8) {
634 _mesa_hash_table_insert(ht->table, (void *)(uintptr_t)key, data);
635 } else {
636 struct hash_key_u64 *_key = CALLOC_STRUCT(hash_key_u64);
637
638 if (!_key)
639 return;
640 _key->value = key;
641
642 _mesa_hash_table_insert(ht->table, _key, data);
643 }
644 }
645
646 static struct hash_entry *
647 hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key)
648 {
649 if (sizeof(void *) == 8) {
650 return _mesa_hash_table_search(ht->table, (void *)(uintptr_t)key);
651 } else {
652 struct hash_key_u64 _key = { .value = key };
653 return _mesa_hash_table_search(ht->table, &_key);
654 }
655 }
656
657 void *
658 _mesa_hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key)
659 {
660 struct hash_entry *entry;
661
662 if (key == DELETED_KEY_VALUE)
663 return ht->deleted_key_data;
664
665 entry = hash_table_u64_search(ht, key);
666 if (!entry)
667 return NULL;
668
669 return entry->data;
670 }
671
672 void
673 _mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key)
674 {
675 struct hash_entry *entry;
676
677 if (key == DELETED_KEY_VALUE) {
678 ht->deleted_key_data = NULL;
679 return;
680 }
681
682 entry = hash_table_u64_search(ht, key);
683 if (!entry)
684 return;
685
686 if (sizeof(void *) == 8) {
687 _mesa_hash_table_remove(ht->table, entry);
688 } else {
689 struct hash_key *_key = (struct hash_key *)entry->key;
690
691 _mesa_hash_table_remove(ht->table, entry);
692 free(_key);
693 }
694 }