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
);
122 ht
->mem_ctx
= mem_ctx
;
124 ht
->size
= hash_sizes
[ht
->size_index
].size
;
125 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
126 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
127 ht
->key_equals_function
= key_equals_function
;
128 ht
->table
= rzalloc_array(ht
, struct hash_entry
, ht
->size
);
130 ht
->deleted_entries
= 0;
131 ht
->deleted_key
= &deleted_key_value
;
133 if (ht
->table
== NULL
) {
142 * Frees the given hash table.
144 * If delete_function is passed, it gets called on each entry present before
148 _mesa_hash_table_destroy(struct hash_table
*ht
,
149 void (*delete_function
)(struct hash_entry
*entry
))
154 if (delete_function
) {
155 struct hash_entry
*entry
;
157 hash_table_foreach(ht
, entry
) {
158 delete_function(entry
);
164 /** Sets the value of the key pointer used for deleted entries in the table.
166 * The assumption is that usually keys are actual pointers, so we use a
167 * default value of a pointer to an arbitrary piece of storage in the library.
168 * But in some cases a consumer wants to store some other sort of value in the
169 * table, like a uint32_t, in which case that pointer may conflict with one of
170 * their valid keys. This lets that user select a safe value.
172 * This must be called before any keys are actually deleted from the table.
175 _mesa_hash_table_set_deleted_key(struct hash_table
*ht
, const void *deleted_key
)
177 ht
->deleted_key
= deleted_key
;
181 * Finds a hash table entry with the given key and hash of that key.
183 * Returns NULL if no entry is found. Note that the data pointer may be
184 * modified by the user.
187 _mesa_hash_table_search(struct hash_table
*ht
, uint32_t hash
,
190 uint32_t start_hash_address
= hash
% ht
->size
;
191 uint32_t hash_address
= start_hash_address
;
194 uint32_t double_hash
;
196 struct hash_entry
*entry
= ht
->table
+ hash_address
;
198 if (entry_is_free(entry
)) {
200 } else if (entry_is_present(ht
, entry
) && entry
->hash
== hash
) {
201 if (ht
->key_equals_function(key
, entry
->key
)) {
206 double_hash
= 1 + hash
% ht
->rehash
;
208 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
209 } while (hash_address
!= start_hash_address
);
215 _mesa_hash_table_rehash(struct hash_table
*ht
, int new_size_index
)
217 struct hash_table old_ht
;
218 struct hash_entry
*table
, *entry
;
220 if (new_size_index
>= ARRAY_SIZE(hash_sizes
))
223 table
= rzalloc_array(ht
, struct hash_entry
,
224 hash_sizes
[new_size_index
].size
);
231 ht
->size_index
= new_size_index
;
232 ht
->size
= hash_sizes
[ht
->size_index
].size
;
233 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
234 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
236 ht
->deleted_entries
= 0;
238 hash_table_foreach(&old_ht
, entry
) {
239 _mesa_hash_table_insert(ht
, entry
->hash
,
240 entry
->key
, entry
->data
);
243 ralloc_free(old_ht
.table
);
247 * Inserts the key with the given hash into the table.
249 * Note that insertion may rearrange the table on a resize or rehash,
250 * so previously found hash_entries are no longer valid after this function.
253 _mesa_hash_table_insert(struct hash_table
*ht
, uint32_t hash
,
254 const void *key
, void *data
)
256 uint32_t start_hash_address
, hash_address
;
258 if (ht
->entries
>= ht
->max_entries
) {
259 _mesa_hash_table_rehash(ht
, ht
->size_index
+ 1);
260 } else if (ht
->deleted_entries
+ ht
->entries
>= ht
->max_entries
) {
261 _mesa_hash_table_rehash(ht
, ht
->size_index
);
264 start_hash_address
= hash
% ht
->size
;
265 hash_address
= start_hash_address
;
267 struct hash_entry
*entry
= ht
->table
+ hash_address
;
268 uint32_t double_hash
;
270 if (!entry_is_present(ht
, entry
)) {
271 if (entry_is_deleted(ht
, entry
))
272 ht
->deleted_entries
--;
280 /* Implement replacement when another insert happens
281 * with a matching key. This is a relatively common
282 * feature of hash tables, with the alternative
283 * generally being "insert the new value as well, and
284 * return it first when the key is searched for".
286 * Note that the hash table doesn't have a delete
287 * callback. If freeing of old data pointers is
288 * required to avoid memory leaks, perform a search
291 if (entry
->hash
== hash
&&
292 ht
->key_equals_function(key
, entry
->key
)) {
299 double_hash
= 1 + hash
% ht
->rehash
;
301 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
302 } while (hash_address
!= start_hash_address
);
304 /* We could hit here if a required resize failed. An unchecked-malloc
305 * application could ignore this result.
311 * This function deletes the given hash table entry.
313 * Note that deletion doesn't otherwise modify the table, so an iteration over
314 * the table deleting entries is safe.
317 _mesa_hash_table_remove(struct hash_table
*ht
,
318 struct hash_entry
*entry
)
323 entry
->key
= ht
->deleted_key
;
325 ht
->deleted_entries
++;
329 * This function is an iterator over the hash table.
331 * Pass in NULL for the first entry, as in the start of a for loop. Note that
332 * an iteration over the table is O(table_size) not O(entries).
335 _mesa_hash_table_next_entry(struct hash_table
*ht
,
336 struct hash_entry
*entry
)
343 for (; entry
!= ht
->table
+ ht
->size
; entry
++) {
344 if (entry_is_present(ht
, entry
)) {
353 * Returns a random entry from the hash table.
355 * This may be useful in implementing random replacement (as opposed
356 * to just removing everything) in caches based on this hash table
357 * implementation. @predicate may be used to filter entries, or may
358 * be set to NULL for no filtering.
361 _mesa_hash_table_random_entry(struct hash_table
*ht
,
362 bool (*predicate
)(struct hash_entry
*entry
))
364 struct hash_entry
*entry
;
365 uint32_t i
= rand() % ht
->size
;
367 if (ht
->entries
== 0)
370 for (entry
= ht
->table
+ i
; entry
!= ht
->table
+ ht
->size
; entry
++) {
371 if (entry_is_present(ht
, entry
) &&
372 (!predicate
|| predicate(entry
))) {
377 for (entry
= ht
->table
; entry
!= ht
->table
+ i
; entry
++) {
378 if (entry_is_present(ht
, entry
) &&
379 (!predicate
|| predicate(entry
))) {
389 * Quick FNV-1 hash implementation based on:
390 * http://www.isthe.com/chongo/tech/comp/fnv/
392 * FNV-1 is not be the best hash out there -- Jenkins's lookup3 is supposed to
393 * be quite good, and it probably beats FNV. But FNV has the advantage that
394 * it involves almost no code. For an improvement on both, see Paul
395 * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
398 _mesa_hash_data(const void *data
, size_t size
)
400 uint32_t hash
= 2166136261ul;
401 const uint8_t *bytes
= data
;
403 while (size
-- != 0) {
405 hash
= hash
* 0x01000193;
412 /** FNV-1 string hash implementation */
414 _mesa_hash_string(const char *key
)
416 uint32_t hash
= 2166136261ul;
420 hash
= hash
* 0x01000193;
428 * String compare function for use as the comparison callback in
429 * _mesa_hash_table_create().
432 _mesa_key_string_equal(const void *a
, const void *b
)
434 return strcmp(a
, b
) == 0;
438 _mesa_key_pointer_equal(const void *a
, const void *b
)