mesa: Remove the size argument from _mesa_alloc_dispatch_table().
[mesa.git] / src / mesa / main / set.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 #include <stdlib.h>
36
37 #include "set.h"
38 #include "ralloc.h"
39
40 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
41
42 /*
43 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
44 * p and p-2 are both prime. These tables are sized to have an extra 10%
45 * free to avoid exponential performance degradation as the hash table fills
46 */
47
48 uint32_t deleted_key_value;
49 const void *deleted_key = &deleted_key_value;
50
51 static const struct {
52 uint32_t max_entries, size, rehash;
53 } hash_sizes[] = {
54 { 2, 5, 3 },
55 { 4, 7, 5 },
56 { 8, 13, 11 },
57 { 16, 19, 17 },
58 { 32, 43, 41 },
59 { 64, 73, 71 },
60 { 128, 151, 149 },
61 { 256, 283, 281 },
62 { 512, 571, 569 },
63 { 1024, 1153, 1151 },
64 { 2048, 2269, 2267 },
65 { 4096, 4519, 4517 },
66 { 8192, 9013, 9011 },
67 { 16384, 18043, 18041 },
68 { 32768, 36109, 36107 },
69 { 65536, 72091, 72089 },
70 { 131072, 144409, 144407 },
71 { 262144, 288361, 288359 },
72 { 524288, 576883, 576881 },
73 { 1048576, 1153459, 1153457 },
74 { 2097152, 2307163, 2307161 },
75 { 4194304, 4613893, 4613891 },
76 { 8388608, 9227641, 9227639 },
77 { 16777216, 18455029, 18455027 },
78 { 33554432, 36911011, 36911009 },
79 { 67108864, 73819861, 73819859 },
80 { 134217728, 147639589, 147639587 },
81 { 268435456, 295279081, 295279079 },
82 { 536870912, 590559793, 590559791 },
83 { 1073741824, 1181116273, 1181116271 },
84 { 2147483648ul, 2362232233ul, 2362232231ul }
85 };
86
87 static int
88 entry_is_free(struct set_entry *entry)
89 {
90 return entry->key == NULL;
91 }
92
93 static int
94 entry_is_deleted(struct set_entry *entry)
95 {
96 return entry->key == deleted_key;
97 }
98
99 static int
100 entry_is_present(struct set_entry *entry)
101 {
102 return entry->key != NULL && entry->key != deleted_key;
103 }
104
105 struct set *
106 _mesa_set_create(void *mem_ctx,
107 bool key_equals_function(const void *a,
108 const void *b))
109 {
110 struct set *ht;
111
112 ht = ralloc(mem_ctx, struct set);
113 if (ht == NULL)
114 return NULL;
115
116 ht->mem_ctx = mem_ctx;
117 ht->size_index = 0;
118 ht->size = hash_sizes[ht->size_index].size;
119 ht->rehash = hash_sizes[ht->size_index].rehash;
120 ht->max_entries = hash_sizes[ht->size_index].max_entries;
121 ht->key_equals_function = key_equals_function;
122 ht->table = rzalloc_array(ht, struct set_entry, ht->size);
123 ht->entries = 0;
124 ht->deleted_entries = 0;
125
126 if (ht->table == NULL) {
127 ralloc_free(ht);
128 return NULL;
129 }
130
131 return ht;
132 }
133
134 /**
135 * Frees the given set.
136 *
137 * If delete_function is passed, it gets called on each entry present before
138 * freeing.
139 */
140 void
141 _mesa_set_destroy(struct set *ht, void (*delete_function)(struct set_entry *entry))
142 {
143 if (!ht)
144 return;
145
146 if (delete_function) {
147 struct set_entry *entry;
148
149 set_foreach (ht, entry) {
150 delete_function(entry);
151 }
152 }
153 ralloc_free(ht->table);
154 ralloc_free(ht);
155 }
156
157 /**
158 * Finds a set entry with the given key and hash of that key.
159 *
160 * Returns NULL if no entry is found.
161 */
162 struct set_entry *
163 _mesa_set_search(const struct set *ht, uint32_t hash, const void *key)
164 {
165 uint32_t hash_address;
166
167 hash_address = hash % ht->size;
168 do {
169 uint32_t double_hash;
170
171 struct set_entry *entry = ht->table + hash_address;
172
173 if (entry_is_free(entry)) {
174 return NULL;
175 } else if (entry_is_present(entry) && entry->hash == hash) {
176 if (ht->key_equals_function(key, entry->key)) {
177 return entry;
178 }
179 }
180
181 double_hash = 1 + hash % ht->rehash;
182
183 hash_address = (hash_address + double_hash) % ht->size;
184 } while (hash_address != hash % ht->size);
185
186 return NULL;
187 }
188
189 static void
190 set_rehash(struct set *ht, int new_size_index)
191 {
192 struct set old_ht;
193 struct set_entry *table, *entry;
194
195 if (new_size_index >= ARRAY_SIZE(hash_sizes))
196 return;
197
198 table = rzalloc_array(ht, struct set_entry,
199 hash_sizes[new_size_index].size);
200 if (table == NULL)
201 return;
202
203 old_ht = *ht;
204
205 ht->table = table;
206 ht->size_index = new_size_index;
207 ht->size = hash_sizes[ht->size_index].size;
208 ht->rehash = hash_sizes[ht->size_index].rehash;
209 ht->max_entries = hash_sizes[ht->size_index].max_entries;
210 ht->entries = 0;
211 ht->deleted_entries = 0;
212
213 for (entry = old_ht.table;
214 entry != old_ht.table + old_ht.size;
215 entry++) {
216 if (entry_is_present(entry)) {
217 _mesa_set_add(ht, entry->hash, entry->key);
218 }
219 }
220
221 ralloc_free(old_ht.table);
222 }
223
224 /**
225 * Inserts the key with the given hash into the table.
226 *
227 * Note that insertion may rearrange the table on a resize or rehash,
228 * so previously found hash_entries are no longer valid after this function.
229 */
230 struct set_entry *
231 _mesa_set_add(struct set *ht, uint32_t hash, const void *key)
232 {
233 uint32_t hash_address;
234
235 if (ht->entries >= ht->max_entries) {
236 set_rehash(ht, ht->size_index + 1);
237 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
238 set_rehash(ht, ht->size_index);
239 }
240
241 hash_address = hash % ht->size;
242 do {
243 struct set_entry *entry = ht->table + hash_address;
244 uint32_t double_hash;
245
246 if (!entry_is_present(entry)) {
247 if (entry_is_deleted(entry))
248 ht->deleted_entries--;
249 entry->hash = hash;
250 entry->key = key;
251 ht->entries++;
252 return entry;
253 }
254
255 /* Implement replacement when another insert happens
256 * with a matching key. This is a relatively common
257 * feature of hash tables, with the alternative
258 * generally being "insert the new value as well, and
259 * return it first when the key is searched for".
260 *
261 * Note that the hash table doesn't have a delete callback.
262 * If freeing of old keys is required to avoid memory leaks,
263 * perform a search before inserting.
264 */
265 if (entry->hash == hash &&
266 ht->key_equals_function(key, entry->key)) {
267 entry->key = key;
268 return entry;
269 }
270
271 double_hash = 1 + hash % ht->rehash;
272
273 hash_address = (hash_address + double_hash) % ht->size;
274 } while (hash_address != hash % ht->size);
275
276 /* We could hit here if a required resize failed. An unchecked-malloc
277 * application could ignore this result.
278 */
279 return NULL;
280 }
281
282 /**
283 * This function deletes the given hash table entry.
284 *
285 * Note that deletion doesn't otherwise modify the table, so an iteration over
286 * the table deleting entries is safe.
287 */
288 void
289 _mesa_set_remove(struct set *ht, struct set_entry *entry)
290 {
291 if (!entry)
292 return;
293
294 entry->key = deleted_key;
295 ht->entries--;
296 ht->deleted_entries++;
297 }
298
299 /**
300 * This function is an iterator over the hash table.
301 *
302 * Pass in NULL for the first entry, as in the start of a for loop. Note that
303 * an iteration over the table is O(table_size) not O(entries).
304 */
305 struct set_entry *
306 _mesa_set_next_entry(const struct set *ht, struct set_entry *entry)
307 {
308 if (entry == NULL)
309 entry = ht->table;
310 else
311 entry = entry + 1;
312
313 for (; entry != ht->table + ht->size; entry++) {
314 if (entry_is_present(entry)) {
315 return entry;
316 }
317 }
318
319 return NULL;
320 }
321
322 struct set_entry *
323 _mesa_set_random_entry(struct set *ht,
324 int (*predicate)(struct set_entry *entry))
325 {
326 struct set_entry *entry;
327 uint32_t i = rand() % ht->size;
328
329 if (ht->entries == 0)
330 return NULL;
331
332 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
333 if (entry_is_present(entry) &&
334 (!predicate || predicate(entry))) {
335 return entry;
336 }
337 }
338
339 for (entry = ht->table; entry != ht->table + i; entry++) {
340 if (entry_is_present(entry) &&
341 (!predicate || predicate(entry))) {
342 return entry;
343 }
344 }
345
346 return NULL;
347 }
348