util/hash_table: don't compare deleted entries
[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
51 static const uint32_t deleted_key_value;
52
53 /**
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
57 */
58 static const struct {
59 uint32_t max_entries, size, rehash;
60 } hash_sizes[] = {
61 { 2, 5, 3 },
62 { 4, 7, 5 },
63 { 8, 13, 11 },
64 { 16, 19, 17 },
65 { 32, 43, 41 },
66 { 64, 73, 71 },
67 { 128, 151, 149 },
68 { 256, 283, 281 },
69 { 512, 571, 569 },
70 { 1024, 1153, 1151 },
71 { 2048, 2269, 2267 },
72 { 4096, 4519, 4517 },
73 { 8192, 9013, 9011 },
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}
92 };
93
94 static int
95 entry_is_free(const struct hash_entry *entry)
96 {
97 return entry->key == NULL;
98 }
99
100 static int
101 entry_is_deleted(const struct hash_table *ht, struct hash_entry *entry)
102 {
103 return entry->key == ht->deleted_key;
104 }
105
106 static int
107 entry_is_present(const struct hash_table *ht, struct hash_entry *entry)
108 {
109 return entry->key != NULL && entry->key != ht->deleted_key;
110 }
111
112 struct hash_table *
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,
116 const void *b))
117 {
118 struct hash_table *ht;
119
120 ht = ralloc(mem_ctx, struct hash_table);
121 if (ht == NULL)
122 return NULL;
123
124 ht->size_index = 0;
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);
131 ht->entries = 0;
132 ht->deleted_entries = 0;
133 ht->deleted_key = &deleted_key_value;
134
135 if (ht->table == NULL) {
136 ralloc_free(ht);
137 return NULL;
138 }
139
140 return ht;
141 }
142
143 /**
144 * Frees the given hash table.
145 *
146 * If delete_function is passed, it gets called on each entry present before
147 * freeing.
148 */
149 void
150 _mesa_hash_table_destroy(struct hash_table *ht,
151 void (*delete_function)(struct hash_entry *entry))
152 {
153 if (!ht)
154 return;
155
156 if (delete_function) {
157 struct hash_entry *entry;
158
159 hash_table_foreach(ht, entry) {
160 delete_function(entry);
161 }
162 }
163 ralloc_free(ht);
164 }
165
166 /** Sets the value of the key pointer used for deleted entries in the table.
167 *
168 * The assumption is that usually keys are actual pointers, so we use a
169 * default value of a pointer to an arbitrary piece of storage in the library.
170 * But in some cases a consumer wants to store some other sort of value in the
171 * table, like a uint32_t, in which case that pointer may conflict with one of
172 * their valid keys. This lets that user select a safe value.
173 *
174 * This must be called before any keys are actually deleted from the table.
175 */
176 void
177 _mesa_hash_table_set_deleted_key(struct hash_table *ht, const void *deleted_key)
178 {
179 ht->deleted_key = deleted_key;
180 }
181
182 static struct hash_entry *
183 hash_table_search(struct hash_table *ht, uint32_t hash, const void *key)
184 {
185 uint32_t start_hash_address = hash % ht->size;
186 uint32_t hash_address = start_hash_address;
187
188 do {
189 uint32_t double_hash;
190
191 struct hash_entry *entry = ht->table + hash_address;
192
193 if (entry_is_free(entry)) {
194 return NULL;
195 } else if (entry_is_present(ht, entry) && entry->hash == hash) {
196 if (ht->key_equals_function(key, entry->key)) {
197 return entry;
198 }
199 }
200
201 double_hash = 1 + hash % ht->rehash;
202
203 hash_address = (hash_address + double_hash) % ht->size;
204 } while (hash_address != start_hash_address);
205
206 return NULL;
207 }
208
209 /**
210 * Finds a hash table entry with the given key and hash of that key.
211 *
212 * Returns NULL if no entry is found. Note that the data pointer may be
213 * modified by the user.
214 */
215 struct hash_entry *
216 _mesa_hash_table_search(struct hash_table *ht, const void *key)
217 {
218 assert(ht->key_hash_function);
219 return hash_table_search(ht, ht->key_hash_function(key), key);
220 }
221
222 struct hash_entry *
223 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
224 const void *key)
225 {
226 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
227 return hash_table_search(ht, hash, key);
228 }
229
230 static struct hash_entry *
231 hash_table_insert(struct hash_table *ht, uint32_t hash,
232 const void *key, void *data);
233
234 static void
235 _mesa_hash_table_rehash(struct hash_table *ht, unsigned new_size_index)
236 {
237 struct hash_table old_ht;
238 struct hash_entry *table, *entry;
239
240 if (new_size_index >= ARRAY_SIZE(hash_sizes))
241 return;
242
243 table = rzalloc_array(ht, struct hash_entry,
244 hash_sizes[new_size_index].size);
245 if (table == NULL)
246 return;
247
248 old_ht = *ht;
249
250 ht->table = table;
251 ht->size_index = new_size_index;
252 ht->size = hash_sizes[ht->size_index].size;
253 ht->rehash = hash_sizes[ht->size_index].rehash;
254 ht->max_entries = hash_sizes[ht->size_index].max_entries;
255 ht->entries = 0;
256 ht->deleted_entries = 0;
257
258 hash_table_foreach(&old_ht, entry) {
259 hash_table_insert(ht, entry->hash, entry->key, entry->data);
260 }
261
262 ralloc_free(old_ht.table);
263 }
264
265 static struct hash_entry *
266 hash_table_insert(struct hash_table *ht, uint32_t hash,
267 const void *key, void *data)
268 {
269 uint32_t start_hash_address, hash_address;
270 struct hash_entry *available_entry = NULL;
271
272 if (ht->entries >= ht->max_entries) {
273 _mesa_hash_table_rehash(ht, ht->size_index + 1);
274 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
275 _mesa_hash_table_rehash(ht, ht->size_index);
276 }
277
278 start_hash_address = hash % ht->size;
279 hash_address = start_hash_address;
280 do {
281 struct hash_entry *entry = ht->table + hash_address;
282 uint32_t double_hash;
283
284 if (!entry_is_present(ht, entry)) {
285 /* Stash the first available entry we find */
286 if (available_entry == NULL)
287 available_entry = entry;
288 if (entry_is_free(entry))
289 break;
290 }
291
292 /* Implement replacement when another insert happens
293 * with a matching key. This is a relatively common
294 * feature of hash tables, with the alternative
295 * generally being "insert the new value as well, and
296 * return it first when the key is searched for".
297 *
298 * Note that the hash table doesn't have a delete
299 * callback. If freeing of old data pointers is
300 * required to avoid memory leaks, perform a search
301 * before inserting.
302 */
303 if (!entry_is_deleted(ht, entry) &&
304 entry->hash == hash &&
305 ht->key_equals_function(key, entry->key)) {
306 entry->key = key;
307 entry->data = data;
308 return entry;
309 }
310
311
312 double_hash = 1 + hash % ht->rehash;
313
314 hash_address = (hash_address + double_hash) % ht->size;
315 } while (hash_address != start_hash_address);
316
317 if (available_entry) {
318 if (entry_is_deleted(ht, available_entry))
319 ht->deleted_entries--;
320 available_entry->hash = hash;
321 available_entry->key = key;
322 available_entry->data = data;
323 ht->entries++;
324 return available_entry;
325 }
326
327 /* We could hit here if a required resize failed. An unchecked-malloc
328 * application could ignore this result.
329 */
330 return NULL;
331 }
332
333 /**
334 * Inserts the key with the given hash into the table.
335 *
336 * Note that insertion may rearrange the table on a resize or rehash,
337 * so previously found hash_entries are no longer valid after this function.
338 */
339 struct hash_entry *
340 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data)
341 {
342 assert(ht->key_hash_function);
343 return hash_table_insert(ht, ht->key_hash_function(key), key, data);
344 }
345
346 struct hash_entry *
347 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
348 const void *key, void *data)
349 {
350 assert(ht->key_hash_function == NULL || hash == ht->key_hash_function(key));
351 return hash_table_insert(ht, hash, key, data);
352 }
353
354 /**
355 * This function deletes the given hash table entry.
356 *
357 * Note that deletion doesn't otherwise modify the table, so an iteration over
358 * the table deleting entries is safe.
359 */
360 void
361 _mesa_hash_table_remove(struct hash_table *ht,
362 struct hash_entry *entry)
363 {
364 if (!entry)
365 return;
366
367 entry->key = ht->deleted_key;
368 ht->entries--;
369 ht->deleted_entries++;
370 }
371
372 /**
373 * This function is an iterator over the hash table.
374 *
375 * Pass in NULL for the first entry, as in the start of a for loop. Note that
376 * an iteration over the table is O(table_size) not O(entries).
377 */
378 struct hash_entry *
379 _mesa_hash_table_next_entry(struct hash_table *ht,
380 struct hash_entry *entry)
381 {
382 if (entry == NULL)
383 entry = ht->table;
384 else
385 entry = entry + 1;
386
387 for (; entry != ht->table + ht->size; entry++) {
388 if (entry_is_present(ht, entry)) {
389 return entry;
390 }
391 }
392
393 return NULL;
394 }
395
396 /**
397 * Returns a random entry from the hash table.
398 *
399 * This may be useful in implementing random replacement (as opposed
400 * to just removing everything) in caches based on this hash table
401 * implementation. @predicate may be used to filter entries, or may
402 * be set to NULL for no filtering.
403 */
404 struct hash_entry *
405 _mesa_hash_table_random_entry(struct hash_table *ht,
406 bool (*predicate)(struct hash_entry *entry))
407 {
408 struct hash_entry *entry;
409 uint32_t i = rand() % ht->size;
410
411 if (ht->entries == 0)
412 return NULL;
413
414 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
415 if (entry_is_present(ht, entry) &&
416 (!predicate || predicate(entry))) {
417 return entry;
418 }
419 }
420
421 for (entry = ht->table; entry != ht->table + i; entry++) {
422 if (entry_is_present(ht, entry) &&
423 (!predicate || predicate(entry))) {
424 return entry;
425 }
426 }
427
428 return NULL;
429 }
430
431
432 /**
433 * Quick FNV-1a hash implementation based on:
434 * http://www.isthe.com/chongo/tech/comp/fnv/
435 *
436 * FNV-1a is not be the best hash out there -- Jenkins's lookup3 is supposed
437 * to be quite good, and it probably beats FNV. But FNV has the advantage
438 * that it involves almost no code. For an improvement on both, see Paul
439 * Hsieh's http://www.azillionmonkeys.com/qed/hash.html
440 */
441 uint32_t
442 _mesa_hash_data(const void *data, size_t size)
443 {
444 return _mesa_fnv32_1a_accumulate_block(_mesa_fnv32_1a_offset_bias,
445 data, size);
446 }
447
448 /** FNV-1a string hash implementation */
449 uint32_t
450 _mesa_hash_string(const char *key)
451 {
452 uint32_t hash = _mesa_fnv32_1a_offset_bias;
453
454 while (*key != 0) {
455 hash = _mesa_fnv32_1a_accumulate(hash, *key);
456 key++;
457 }
458
459 return hash;
460 }
461
462 /**
463 * String compare function for use as the comparison callback in
464 * _mesa_hash_table_create().
465 */
466 bool
467 _mesa_key_string_equal(const void *a, const void *b)
468 {
469 return strcmp(a, b) == 0;
470 }
471
472 bool
473 _mesa_key_pointer_equal(const void *a, const void *b)
474 {
475 return a == b;
476 }