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
46 #include "main/hash_table.h"
47 #include "main/macros.h"
50 static const uint32_t deleted_key_value
;
53 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
54 * p and p-2 are both prime. These tables are sized to have an extra 10%
55 * free to avoid exponential performance degradation as the hash table fills
58 uint32_t max_entries
, size
, rehash
;
73 { 16384, 18043, 18041 },
74 { 32768, 36109, 36107 },
75 { 65536, 72091, 72089 },
76 { 131072, 144409, 144407 },
77 { 262144, 288361, 288359 },
78 { 524288, 576883, 576881 },
79 { 1048576, 1153459, 1153457 },
80 { 2097152, 2307163, 2307161 },
81 { 4194304, 4613893, 4613891 },
82 { 8388608, 9227641, 9227639 },
83 { 16777216, 18455029, 18455027 },
84 { 33554432, 36911011, 36911009 },
85 { 67108864, 73819861, 73819859 },
86 { 134217728, 147639589, 147639587 },
87 { 268435456, 295279081, 295279079 },
88 { 536870912, 590559793, 590559791 },
89 { 1073741824, 1181116273, 1181116271},
90 { 2147483648ul, 2362232233ul, 2362232231ul}
94 entry_is_free(const struct hash_entry
*entry
)
96 return entry
->key
== NULL
;
100 entry_is_deleted(const struct hash_table
*ht
, struct hash_entry
*entry
)
102 return entry
->key
== ht
->deleted_key
;
106 entry_is_present(const struct hash_table
*ht
, struct hash_entry
*entry
)
108 return entry
->key
!= NULL
&& entry
->key
!= ht
->deleted_key
;
112 _mesa_hash_table_create(void *mem_ctx
,
113 bool (*key_equals_function
)(const void *a
,
116 struct hash_table
*ht
;
118 ht
= ralloc(mem_ctx
, struct hash_table
);
123 ht
->size
= hash_sizes
[ht
->size_index
].size
;
124 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
125 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
126 ht
->key_equals_function
= key_equals_function
;
127 ht
->table
= rzalloc_array(ht
, struct hash_entry
, ht
->size
);
129 ht
->deleted_entries
= 0;
130 ht
->deleted_key
= &deleted_key_value
;
132 if (ht
->table
== NULL
) {
141 * Frees the given hash table.
143 * If delete_function is passed, it gets called on each entry present before
147 _mesa_hash_table_destroy(struct hash_table
*ht
,
148 void (*delete_function
)(struct hash_entry
*entry
))
153 if (delete_function
) {
154 struct hash_entry
*entry
;
156 hash_table_foreach(ht
, entry
) {
157 delete_function(entry
);
163 /** Sets the value of the key pointer used for deleted entries in the table.
165 * The assumption is that usually keys are actual pointers, so we use a
166 * default value of a pointer to an arbitrary piece of storage in the library.
167 * But in some cases a consumer wants to store some other sort of value in the
168 * table, like a uint32_t, in which case that pointer may conflict with one of
169 * their valid keys. This lets that user select a safe value.
171 * This must be called before any keys are actually deleted from the table.
174 _mesa_hash_table_set_deleted_key(struct hash_table
*ht
, const void *deleted_key
)
176 ht
->deleted_key
= deleted_key
;
180 * Finds a hash table entry with the given key and hash of that key.
182 * Returns NULL if no entry is found. Note that the data pointer may be
183 * modified by the user.
186 _mesa_hash_table_search(struct hash_table
*ht
, uint32_t hash
,
189 uint32_t start_hash_address
= hash
% ht
->size
;
190 uint32_t hash_address
= start_hash_address
;
193 uint32_t double_hash
;
195 struct hash_entry
*entry
= ht
->table
+ hash_address
;
197 if (entry_is_free(entry
)) {
199 } else if (entry_is_present(ht
, entry
) && entry
->hash
== hash
) {
200 if (ht
->key_equals_function(key
, entry
->key
)) {
205 double_hash
= 1 + hash
% ht
->rehash
;
207 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
208 } while (hash_address
!= start_hash_address
);
214 _mesa_hash_table_rehash(struct hash_table
*ht
, int new_size_index
)
216 struct hash_table old_ht
;
217 struct hash_entry
*table
, *entry
;
219 if (new_size_index
>= ARRAY_SIZE(hash_sizes
))
222 table
= rzalloc_array(ht
, struct hash_entry
,
223 hash_sizes
[new_size_index
].size
);
230 ht
->size_index
= new_size_index
;
231 ht
->size
= hash_sizes
[ht
->size_index
].size
;
232 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
233 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
235 ht
->deleted_entries
= 0;
237 hash_table_foreach(&old_ht
, entry
) {
238 _mesa_hash_table_insert(ht
, entry
->hash
,
239 entry
->key
, entry
->data
);
242 ralloc_free(old_ht
.table
);
246 * Inserts the key with the given hash into the table.
248 * Note that insertion may rearrange the table on a resize or rehash,
249 * so previously found hash_entries are no longer valid after this function.
252 _mesa_hash_table_insert(struct hash_table
*ht
, uint32_t hash
,
253 const void *key
, void *data
)
255 uint32_t start_hash_address
, hash_address
;
257 if (ht
->entries
>= ht
->max_entries
) {
258 _mesa_hash_table_rehash(ht
, ht
->size_index
+ 1);
259 } else if (ht
->deleted_entries
+ ht
->entries
>= ht
->max_entries
) {
260 _mesa_hash_table_rehash(ht
, ht
->size_index
);
263 start_hash_address
= hash
% ht
->size
;
264 hash_address
= start_hash_address
;
266 struct hash_entry
*entry
= ht
->table
+ hash_address
;
267 uint32_t double_hash
;
269 if (!entry_is_present(ht
, entry
)) {
270 if (entry_is_deleted(ht
, entry
))
271 ht
->deleted_entries
--;
279 /* Implement replacement when another insert happens
280 * with a matching key. This is a relatively common
281 * feature of hash tables, with the alternative
282 * generally being "insert the new value as well, and
283 * return it first when the key is searched for".
285 * Note that the hash table doesn't have a delete
286 * callback. If freeing of old data pointers is
287 * required to avoid memory leaks, perform a search
290 if (entry
->hash
== hash
&&
291 ht
->key_equals_function(key
, entry
->key
)) {
298 double_hash
= 1 + hash
% ht
->rehash
;
300 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
301 } while (hash_address
!= start_hash_address
);
303 /* We could hit here if a required resize failed. An unchecked-malloc
304 * application could ignore this result.
310 * This function deletes the given hash table entry.
312 * Note that deletion doesn't otherwise modify the table, so an iteration over
313 * the table deleting entries is safe.
316 _mesa_hash_table_remove(struct hash_table
*ht
,
317 struct hash_entry
*entry
)
322 entry
->key
= ht
->deleted_key
;
324 ht
->deleted_entries
++;
328 * This function is an iterator over the hash table.
330 * Pass in NULL for the first entry, as in the start of a for loop. Note that
331 * an iteration over the table is O(table_size) not O(entries).
334 _mesa_hash_table_next_entry(struct hash_table
*ht
,
335 struct hash_entry
*entry
)
342 for (; entry
!= ht
->table
+ ht
->size
; entry
++) {
343 if (entry_is_present(ht
, entry
)) {
352 * Returns a random entry from the hash table.
354 * This may be useful in implementing random replacement (as opposed
355 * to just removing everything) in caches based on this hash table
356 * implementation. @predicate may be used to filter entries, or may
357 * be set to NULL for no filtering.
360 _mesa_hash_table_random_entry(struct hash_table
*ht
,
361 bool (*predicate
)(struct hash_entry
*entry
))
363 struct hash_entry
*entry
;
364 uint32_t i
= rand() % ht
->size
;
366 if (ht
->entries
== 0)
369 for (entry
= ht
->table
+ i
; entry
!= ht
->table
+ ht
->size
; entry
++) {
370 if (entry_is_present(ht
, entry
) &&
371 (!predicate
|| predicate(entry
))) {
376 for (entry
= ht
->table
; entry
!= ht
->table
+ i
; entry
++) {
377 if (entry_is_present(ht
, entry
) &&
378 (!predicate
|| predicate(entry
))) {
388 * Quick FNV-1 hash implementation based on:
389 * http://www.isthe.com/chongo/tech/comp/fnv/
391 * FNV-1 is not be the best hash out there -- Jenkins's lookup3 is supposed to
392 * be quite good, and it probably beats FNV. But FNV has the advantage that
393 * it involves almost no code. For an improvement on both, see Paul
394 * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
397 _mesa_hash_data(const void *data
, size_t size
)
399 uint32_t hash
= 2166136261ul;
400 const uint8_t *bytes
= data
;
402 while (size
-- != 0) {
404 hash
= hash
* 0x01000193;
411 /** FNV-1 string hash implementation */
413 _mesa_hash_string(const char *key
)
415 uint32_t hash
= 2166136261ul;
419 hash
= hash
* 0x01000193;
427 * String compare function for use as the comparison callback in
428 * _mesa_hash_table_create().
431 _mesa_key_string_equal(const void *a
, const void *b
)
433 return strcmp(a
, b
) == 0;
437 _mesa_key_pointer_equal(const void *a
, const void *b
)