0338cb3b4748a0883a2664da0e4f7a6b06e8e936
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_hash.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Zack Rusin <zack@tungstengraphics.com>
31 */
32
33 #include "cso_hash.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
39
40 #define MAX(a, b) ((a > b) ? (a) : (b))
41
42 static const int MinNumBits = 4;
43
44 static const unsigned char prime_deltas[] = {
45 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 9, 25, 3,
46 1, 21, 3, 21, 7, 15, 9, 5, 3, 29, 15, 0, 0, 0, 0, 0
47 };
48
49 static int primeForNumBits(int numBits)
50 {
51 return (1 << numBits) + prime_deltas[numBits];
52 }
53
54 /*
55 Returns the smallest integer n such that
56 primeForNumBits(n) >= hint.
57 */
58 static int countBits(int hint)
59 {
60 int numBits = 0;
61 int bits = hint;
62
63 while (bits > 1) {
64 bits >>= 1;
65 numBits++;
66 }
67
68 if (numBits >= (int)sizeof(prime_deltas)) {
69 numBits = sizeof(prime_deltas) - 1;
70 } else if (primeForNumBits(numBits) < hint) {
71 ++numBits;
72 }
73 return numBits;
74 }
75
76 struct cso_node {
77 struct cso_node *next;
78 unsigned key;
79 void *value;
80 };
81
82 struct cso_hash_data {
83 struct cso_node *fakeNext;
84 struct cso_node **buckets;
85 int size;
86 int nodeSize;
87 short userNumBits;
88 short numBits;
89 int numBuckets;
90 };
91
92 struct cso_hash {
93 union {
94 struct cso_hash_data *d;
95 struct cso_node *e;
96 } data;
97 };
98
99 static void *cso_data_allocate_node(struct cso_hash_data *hash)
100 {
101 return malloc(hash->nodeSize);
102 }
103
104 static void cso_data_free_node(struct cso_node *node)
105 {
106 /* XXX still a leak here.
107 * Need to cast value ptr to original cso type, then free the
108 * driver-specific data hanging off of it. For example:
109 struct cso_sampler *csamp = (struct cso_sampler *) node->value;
110 free(csamp->data);
111 */
112 free(node->value);
113 free(node);
114 }
115
116 static struct cso_node *
117 cso_hash_create_node(struct cso_hash *hash,
118 unsigned akey, void *avalue,
119 struct cso_node **anextNode)
120 {
121 struct cso_node *node = cso_data_allocate_node(hash->data.d);
122 node->key = akey;
123 node->value = avalue;
124
125 node->next = (struct cso_node*)(*anextNode);
126 *anextNode = node;
127 ++hash->data.d->size;
128 return node;
129 }
130
131 static void cso_data_rehash(struct cso_hash_data *hash, int hint)
132 {
133 if (hint < 0) {
134 hint = countBits(-hint);
135 if (hint < MinNumBits)
136 hint = MinNumBits;
137 hash->userNumBits = hint;
138 while (primeForNumBits(hint) < (hash->size >> 1))
139 ++hint;
140 } else if (hint < MinNumBits) {
141 hint = MinNumBits;
142 }
143
144 if (hash->numBits != hint) {
145 struct cso_node *e = (struct cso_node *)(hash);
146 struct cso_node **oldBuckets = hash->buckets;
147 int oldNumBuckets = hash->numBuckets;
148 int i = 0;
149
150 hash->numBits = hint;
151 hash->numBuckets = primeForNumBits(hint);
152 hash->buckets = malloc(sizeof(struct cso_node*) * hash->numBuckets);
153 for (i = 0; i < hash->numBuckets; ++i)
154 hash->buckets[i] = e;
155
156 for (i = 0; i < oldNumBuckets; ++i) {
157 struct cso_node *firstNode = oldBuckets[i];
158 while (firstNode != e) {
159 unsigned h = firstNode->key;
160 struct cso_node *lastNode = firstNode;
161 while (lastNode->next != e && lastNode->next->key == h)
162 lastNode = lastNode->next;
163
164 struct cso_node *afterLastNode = lastNode->next;
165 struct cso_node **beforeFirstNode = &hash->buckets[h % hash->numBuckets];
166 while (*beforeFirstNode != e)
167 beforeFirstNode = &(*beforeFirstNode)->next;
168 lastNode->next = *beforeFirstNode;
169 *beforeFirstNode = firstNode;
170 firstNode = afterLastNode;
171 }
172 }
173 free(oldBuckets);
174 }
175 }
176
177 static void cso_data_might_grow(struct cso_hash_data *hash)
178 {
179 if (hash->size >= hash->numBuckets)
180 cso_data_rehash(hash, hash->numBits + 1);
181 }
182
183 static void cso_data_has_shrunk(struct cso_hash_data *hash)
184 {
185 if (hash->size <= (hash->numBuckets >> 3) &&
186 hash->numBits > hash->userNumBits) {
187 int max = MAX(hash->numBits-2, hash->userNumBits);
188 cso_data_rehash(hash, max);
189 }
190 }
191
192 static struct cso_node *cso_data_first_node(struct cso_hash_data *hash)
193 {
194 struct cso_node *e = (struct cso_node *)(hash);
195 struct cso_node **bucket = hash->buckets;
196 int n = hash->numBuckets;
197 while (n--) {
198 if (*bucket != e)
199 return *bucket;
200 ++bucket;
201 }
202 return e;
203 }
204
205 static struct cso_node **cso_hash_find_node(struct cso_hash *hash, unsigned akey)
206 {
207 struct cso_node **node;
208
209 if (hash->data.d->numBuckets) {
210 node = (struct cso_node **)(&hash->data.d->buckets[akey % hash->data.d->numBuckets]);
211 assert(*node == hash->data.e || (*node)->next);
212 while (*node != hash->data.e && (*node)->key != akey)
213 node = &(*node)->next;
214 } else {
215 node = (struct cso_node **)((const struct cso_node * const *)(&hash->data.e));
216 }
217 return node;
218 }
219
220 struct cso_hash_iter cso_hash_insert(struct cso_hash *hash,
221 unsigned key, void *data)
222 {
223 cso_data_might_grow(hash->data.d);
224
225 struct cso_node **nextNode = cso_hash_find_node(hash, key);
226 struct cso_node *node = cso_hash_create_node(hash, key, data, nextNode);
227 struct cso_hash_iter iter = {hash, node};
228 return iter;
229 }
230
231 struct cso_hash * cso_hash_create(void)
232 {
233 struct cso_hash *hash = malloc(sizeof(struct cso_hash));
234 hash->data.d = malloc(sizeof(struct cso_hash_data));
235 hash->data.d->fakeNext = 0;
236 hash->data.d->buckets = 0;
237 hash->data.d->size = 0;
238 hash->data.d->nodeSize = sizeof(struct cso_node);
239 hash->data.d->userNumBits = MinNumBits;
240 hash->data.d->numBits = 0;
241 hash->data.d->numBuckets = 0;
242
243 return hash;
244 }
245
246 void cso_hash_delete(struct cso_hash *hash)
247 {
248 struct cso_node *e_for_x = (struct cso_node *)(hash->data.d);
249 struct cso_node **bucket = (struct cso_node **)(hash->data.d->buckets);
250 int n = hash->data.d->numBuckets;
251 while (n--) {
252 struct cso_node *cur = *bucket++;
253 while (cur != e_for_x) {
254 struct cso_node *next = cur->next;
255 cso_data_free_node(cur);
256 cur = next;
257 }
258 }
259 free(hash->data.d->buckets);
260 free(hash->data.d);
261 free(hash);
262 }
263
264 struct cso_hash_iter cso_hash_find(struct cso_hash *hash,
265 unsigned key)
266 {
267 struct cso_node **nextNode = cso_hash_find_node(hash, key);
268 struct cso_hash_iter iter = {hash, *nextNode};
269 return iter;
270 }
271
272 unsigned cso_hash_iter_key(struct cso_hash_iter iter)
273 {
274 if (!iter.node || iter.hash->data.e == iter.node)
275 return 0;
276 return iter.node->key;
277 }
278
279 void * cso_hash_iter_data(struct cso_hash_iter iter)
280 {
281 if (!iter.node || iter.hash->data.e == iter.node)
282 return 0;
283 return iter.node->value;
284 }
285
286 static struct cso_node *cso_hash_data_next(struct cso_node *node)
287 {
288 union {
289 struct cso_node *next;
290 struct cso_node *e;
291 struct cso_hash_data *d;
292 } a;
293 a.next = node->next;
294 if (!a.next) {
295 fprintf(stderr, "iterating beyond the last element\n");
296 return 0;
297 }
298 if (a.next->next)
299 return a.next;
300
301 int start = (node->key % a.d->numBuckets) + 1;
302 struct cso_node **bucket = a.d->buckets + start;
303 int n = a.d->numBuckets - start;
304 while (n--) {
305 if (*bucket != a.e)
306 return *bucket;
307 ++bucket;
308 }
309 return a.e;
310 }
311
312
313 static struct cso_node *cso_hash_data_prev(struct cso_node *node)
314 {
315 union {
316 struct cso_node *e;
317 struct cso_hash_data *d;
318 } a;
319
320 a.e = node;
321 while (a.e->next)
322 a.e = a.e->next;
323
324 int start;
325 if (node == a.e)
326 start = a.d->numBuckets - 1;
327 else
328 start = node->key % a.d->numBuckets;
329
330 struct cso_node *sentinel = node;
331 struct cso_node **bucket = a.d->buckets + start;
332 while (start >= 0) {
333 if (*bucket != sentinel) {
334 struct cso_node *prev = *bucket;
335 while (prev->next != sentinel)
336 prev = prev->next;
337 return prev;
338 }
339
340 sentinel = a.e;
341 --bucket;
342 --start;
343 }
344 fprintf(stderr, "iterating backward beyond first element\n");
345 return a.e;
346 }
347
348 struct cso_hash_iter cso_hash_iter_next(struct cso_hash_iter iter)
349 {
350 struct cso_hash_iter next = {iter.hash, cso_hash_data_next(iter.node)};
351 return next;
352 }
353
354 int cso_hash_iter_is_null(struct cso_hash_iter iter)
355 {
356 if (!iter.node || iter.node == iter.hash->data.e)
357 return 1;
358 return 0;
359 }
360
361 void * cso_hash_take(struct cso_hash *hash,
362 unsigned akey)
363 {
364 struct cso_node **node = cso_hash_find_node(hash, akey);
365 if (*node != hash->data.e) {
366 void *t = (*node)->value;
367 struct cso_node *next = (*node)->next;
368 cso_data_free_node(*node);
369 *node = next;
370 --hash->data.d->size;
371 cso_data_has_shrunk(hash->data.d);
372 return t;
373 }
374 return 0;
375 }
376
377 struct cso_hash_iter cso_hash_iter_prev(struct cso_hash_iter iter)
378 {
379 struct cso_hash_iter prev = {iter.hash,
380 cso_hash_data_prev(iter.node)};
381 return prev;
382 }
383
384 struct cso_hash_iter cso_hash_first_node(struct cso_hash *hash)
385 {
386 struct cso_hash_iter iter = {hash, cso_data_first_node(hash->data.d)};
387 return iter;
388 }