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