2 * Copyright © 2009,2012 Intel Corporation
3 * Copyright © 1988-2004 Keith Packard and Bart Massey.
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:
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
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
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
31 * Eric Anholt <eric@anholt.net>
32 * Keith Packard <keithp@keithp.com>
36 * Implements an open-addressing, linear-reprobing hash table.
38 * For more information, see:
40 * http://cgit.freedesktop.org/~anholt/hash_table/tree/README
47 #include "hash_table.h"
51 static const uint32_t deleted_key_value;
54 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
55 * p and p-2 are both prime. These tables are sized to have an extra 10%
56 * free to avoid exponential performance degradation as the hash table fills
59 uint32_t max_entries, size, rehash;
74 { 16384, 18043, 18041 },
75 { 32768, 36109, 36107 },
76 { 65536, 72091, 72089 },
77 { 131072, 144409, 144407 },
78 { 262144, 288361, 288359 },
79 { 524288, 576883, 576881 },
80 { 1048576, 1153459, 1153457 },
81 { 2097152, 2307163, 2307161 },
82 { 4194304, 4613893, 4613891 },
83 { 8388608, 9227641, 9227639 },
84 { 16777216, 18455029, 18455027 },
85 { 33554432, 36911011, 36911009 },
86 { 67108864, 73819861, 73819859 },
87 { 134217728, 147639589, 147639587 },
88 { 268435456, 295279081, 295279079 },
89 { 536870912, 590559793, 590559791 },
90 { 1073741824, 1181116273, 1181116271},
91 { 2147483648ul, 2362232233ul, 2362232231ul}
95 entry_is_free(const struct hash_entry *entry)
97 return entry->key == NULL;
101 entry_is_deleted(const struct hash_table *ht, struct hash_entry *entry)
103 return entry->key == ht->deleted_key;
107 entry_is_present(const struct hash_table *ht, struct hash_entry *entry)
109 return entry->key != NULL && entry->key != ht->deleted_key;
113 _mesa_hash_table_create(void *mem_ctx,
114 uint32_t (*key_hash_function)(const void *key),
115 bool (*key_equals_function)(const void *a,
118 struct hash_table *ht;
120 ht = ralloc(mem_ctx, struct hash_table);
125 ht->size = hash_sizes[ht->size_index].size;
126 ht->rehash = hash_sizes[ht->size_index].rehash;
127 ht->max_entries = hash_sizes[ht->size_index].max_entries;
128 ht->key_hash_function = key_hash_function;
129 ht->key_equals_function = key_equals_function;
130 ht->table = rzalloc_array(ht, struct hash_entry, ht->size);
132 ht->deleted_entries = 0;
133 ht->deleted_key = &deleted_key_value;
135 if (ht->table == NULL) {
144 * Frees the given hash table.
146 * If delete_function is passed, it gets called on each entry present before
150 _mesa_hash_table_destroy(struct hash_table *ht,
151 void (*delete_function)(struct hash_entry *entry))
156 if (delete_function) {
157 struct hash_entry *entry;
159 hash_table_foreach(ht, entry) {
160 delete_function(entry);
167 * Deletes all entries of the given hash table without deleting the table
168 * itself or changing its structure.
170 * If delete_function is passed, it gets called on each entry present.
173 _mesa_hash_table_clear(struct hash_table *ht,
174 void (*delete_function)(struct hash_entry *entry))
176 struct hash_entry *entry;
178 for (entry = ht->table; entry != ht->table + ht->size; entry++) {
179 if (entry->key == NULL)
182 if (delete_function != NULL && entry->key != ht->deleted_key)
183 delete_function(entry);
189 ht->deleted_entries = 0;
192 /** Sets the value of the key pointer used for deleted entries in the table.
194 * The assumption is that usually keys are actual pointers, so we use a
195 * default value of a pointer to an arbitrary piece of storage in the library.
196 * But in some cases a consumer wants to store some other sort of value in the
197 * table, like a uint32_t, in which case that pointer may conflict with one of
198 * their valid keys. This lets that user select a safe value.
200 * This must be called before any keys are actually deleted from the table.
203 _mesa_hash_table_set_deleted_key(struct hash_table *ht, const void *deleted_key)
205 ht->deleted_key = deleted_key;
208 static struct hash_entry *
209 hash_table_search(struct hash_table *ht, uint32_t hash, const void *key)
211 uint32_t start_hash_address = hash % ht->size;
212 uint32_t hash_address = start_hash_address;
215 uint32_t double_hash;
217 struct hash_entry *entry = ht->table + hash_address;
219 if (entry_is_free(entry)) {
221 } else if (entry_is_present(ht, entry) && entry->hash == hash) {
222 if (ht->key_equals_function(key, entry->key)) {
227 double_hash = 1 + hash % ht->rehash;
229 hash_address = (hash_address + double_hash) % ht->size;
230 } while (hash_address != start_hash_address);
236 * Finds a hash table entry with the given key and hash of that key.
238 * Returns NULL if no entry is found. Note that the data pointer may be
239 * modified by the user.
242 _mesa_hash_table_search(struct hash_table *ht, const void *key)
244 assert(ht->key_hash_function);
245 return hash_table_search(ht, ht->key_hash_function(key), key);
249 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
252 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
253 return hash_table_search(ht, hash, key);
256 static struct hash_entry *
257 hash_table_insert(struct hash_table *ht, uint32_t hash,
258 const void *key, void *data);
261 _mesa_hash_table_rehash(struct hash_table *ht, unsigned new_size_index)
263 struct hash_table old_ht;
264 struct hash_entry *table, *entry;
266 if (new_size_index >= ARRAY_SIZE(hash_sizes))
269 table = rzalloc_array(ht, struct hash_entry,
270 hash_sizes[new_size_index].size);
277 ht->size_index = new_size_index;
278 ht->size = hash_sizes[ht->size_index].size;
279 ht->rehash = hash_sizes[ht->size_index].rehash;
280 ht->max_entries = hash_sizes[ht->size_index].max_entries;
282 ht->deleted_entries = 0;
284 hash_table_foreach(&old_ht, entry) {
285 hash_table_insert(ht, entry->hash, entry->key, entry->data);
288 ralloc_free(old_ht.table);
291 static struct hash_entry *
292 hash_table_insert(struct hash_table *ht, uint32_t hash,
293 const void *key, void *data)
295 uint32_t start_hash_address, hash_address;
296 struct hash_entry *available_entry = NULL;
300 if (ht->entries >= ht->max_entries) {
301 _mesa_hash_table_rehash(ht, ht->size_index + 1);
302 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
303 _mesa_hash_table_rehash(ht, ht->size_index);
306 start_hash_address = hash % ht->size;
307 hash_address = start_hash_address;
309 struct hash_entry *entry = ht->table + hash_address;
310 uint32_t double_hash;
312 if (!entry_is_present(ht, entry)) {
313 /* Stash the first available entry we find */
314 if (available_entry == NULL)
315 available_entry = entry;
316 if (entry_is_free(entry))
320 /* Implement replacement when another insert happens
321 * with a matching key. This is a relatively common
322 * feature of hash tables, with the alternative
323 * generally being "insert the new value as well, and
324 * return it first when the key is searched for".
326 * Note that the hash table doesn't have a delete
327 * callback. If freeing of old data pointers is
328 * required to avoid memory leaks, perform a search
331 if (!entry_is_deleted(ht, entry) &&
332 entry->hash == hash &&
333 ht->key_equals_function(key, entry->key)) {
340 double_hash = 1 + hash % ht->rehash;
342 hash_address = (hash_address + double_hash) % ht->size;
343 } while (hash_address != start_hash_address);
345 if (available_entry) {
346 if (entry_is_deleted(ht, available_entry))
347 ht->deleted_entries--;
348 available_entry->hash = hash;
349 available_entry->key = key;
350 available_entry->data = data;
352 return available_entry;
355 /* We could hit here if a required resize failed. An unchecked-malloc
356 * application could ignore this result.
362 * Inserts the key with the given hash into the table.
364 * Note that insertion may rearrange the table on a resize or rehash,
365 * so previously found hash_entries are no longer valid after this function.
368 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data)
370 assert(ht->key_hash_function);
371 return hash_table_insert(ht, ht->key_hash_function(key), key, data);
375 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
376 const void *key, void *data)
378 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
379 return hash_table_insert(ht, hash, key, data);
383 * This function deletes the given hash table entry.
385 * Note that deletion doesn't otherwise modify the table, so an iteration over
386 * the table deleting entries is safe.
389 _mesa_hash_table_remove(struct hash_table *ht,
390 struct hash_entry *entry)
395 entry->key = ht->deleted_key;
397 ht->deleted_entries++;
401 * This function is an iterator over the hash table.
403 * Pass in NULL for the first entry, as in the start of a for loop. Note that
404 * an iteration over the table is O(table_size) not O(entries).
407 _mesa_hash_table_next_entry(struct hash_table *ht,
408 struct hash_entry *entry)
415 for (; entry != ht->table + ht->size; entry++) {
416 if (entry_is_present(ht, entry)) {
425 * Returns a random entry from the hash table.
427 * This may be useful in implementing random replacement (as opposed
428 * to just removing everything) in caches based on this hash table
429 * implementation. @predicate may be used to filter entries, or may
430 * be set to NULL for no filtering.
433 _mesa_hash_table_random_entry(struct hash_table *ht,
434 bool (*predicate)(struct hash_entry *entry))
436 struct hash_entry *entry;
437 uint32_t i = rand() % ht->size;
439 if (ht->entries == 0)
442 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
443 if (entry_is_present(ht, entry) &&
444 (!predicate || predicate(entry))) {
449 for (entry = ht->table; entry != ht->table + i; entry++) {
450 if (entry_is_present(ht, entry) &&
451 (!predicate || predicate(entry))) {
461 * Quick FNV-1a hash implementation based on:
462 * http://www.isthe.com/chongo/tech/comp/fnv/
464 * FNV-1a is not be the best hash out there -- Jenkins's lookup3 is supposed
465 * to be quite good, and it probably beats FNV. But FNV has the advantage
466 * that it involves almost no code. For an improvement on both, see Paul
467 * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
470 _mesa_hash_data(const void *data, size_t size)
472 return _mesa_fnv32_1a_accumulate_block(_mesa_fnv32_1a_offset_bias,
476 /** FNV-1a string hash implementation */
478 _mesa_hash_string(const char *key)
480 uint32_t hash = _mesa_fnv32_1a_offset_bias;
483 hash = _mesa_fnv32_1a_accumulate(hash, *key);
491 * String compare function for use as the comparison callback in
492 * _mesa_hash_table_create().
495 _mesa_key_string_equal(const void *a, const void *b)
497 return strcmp(a, b) == 0;
501 _mesa_key_pointer_equal(const void *a, const void *b)