Finish off the previous fix for TFP.
[mesa.git] / src / glx / x11 / glxhash.c
1 /* glxhash.c -- Small hash table support for integer -> integer mapping
2 * Taken from libdrm.
3 *
4 * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
5 *
6 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
29 *
30 * DESCRIPTION
31 *
32 * This file contains a straightforward implementation of a fixed-sized
33 * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
34 * collision resolution. There are two potentially interesting things
35 * about this implementation:
36 *
37 * 1) The table is power-of-two sized. Prime sized tables are more
38 * traditional, but do not have a significant advantage over power-of-two
39 * sized table, especially when double hashing is not used for collision
40 * resolution.
41 *
42 * 2) The hash computation uses a table of random integers [Hanson97,
43 * pp. 39-41].
44 *
45 * FUTURE ENHANCEMENTS
46 *
47 * With a table size of 512, the current implementation is sufficient for a
48 * few hundred keys. Since this is well above the expected size of the
49 * tables for which this implementation was designed, the implementation of
50 * dynamic hash tables was postponed until the need arises. A common (and
51 * naive) approach to dynamic hash table implementation simply creates a
52 * new hash table when necessary, rehashes all the data into the new table,
53 * and destroys the old table. The approach in [Larson88] is superior in
54 * two ways: 1) only a portion of the table is expanded when needed,
55 * distributing the expansion cost over several insertions, and 2) portions
56 * of the table can be locked, enabling a scalable thread-safe
57 * implementation.
58 *
59 * REFERENCES
60 *
61 * [Hanson97] David R. Hanson. C Interfaces and Implementations:
62 * Techniques for Creating Reusable Software. Reading, Massachusetts:
63 * Addison-Wesley, 1997.
64 *
65 * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
66 * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
67 *
68 * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
69 * 1988, pp. 446-457.
70 *
71 */
72
73 #include "glxhash.h"
74 #include <X11/Xfuncproto.h>
75
76 #define HASH_MAIN 0
77
78 #include <stdio.h>
79 #include <stdlib.h>
80
81 #define HASH_MAGIC 0xdeadbeef
82 #define HASH_DEBUG 0
83 #define HASH_SIZE 512 /* Good for about 100 entries */
84 /* If you change this value, you probably
85 have to change the HashHash hashing
86 function! */
87
88 #define HASH_ALLOC malloc
89 #define HASH_FREE free
90 #define HASH_RANDOM_DECL
91 #define HASH_RANDOM_INIT(seed) srandom(seed)
92 #define HASH_RANDOM random()
93 #define HASH_RANDOM_DESTROY
94
95 typedef struct __glxHashBucket {
96 unsigned long key;
97 void *value;
98 struct __glxHashBucket *next;
99 } __glxHashBucket, *__glxHashBucketPtr;
100
101 typedef struct __glxHashTable *__glxHashTablePtr;
102 struct __glxHashTable {
103 unsigned long magic;
104 unsigned long hits; /* At top of linked list */
105 unsigned long partials; /* Not at top of linked list */
106 unsigned long misses; /* Not in table */
107 __glxHashBucketPtr buckets[HASH_SIZE];
108 int p0;
109 __glxHashBucketPtr p1;
110 };
111
112 static unsigned long HashHash(unsigned long key)
113 {
114 unsigned long hash = 0;
115 unsigned long tmp = key;
116 static int init = 0;
117 static unsigned long scatter[256];
118 int i;
119
120 if (!init) {
121 HASH_RANDOM_DECL;
122 HASH_RANDOM_INIT(37);
123 for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
124 HASH_RANDOM_DESTROY;
125 ++init;
126 }
127
128 while (tmp) {
129 hash = (hash << 1) + scatter[tmp & 0xff];
130 tmp >>= 8;
131 }
132
133 hash %= HASH_SIZE;
134 #if HASH_DEBUG
135 printf( "Hash(%d) = %d\n", key, hash);
136 #endif
137 return hash;
138 }
139
140 _X_HIDDEN __glxHashTable *__glxHashCreate(void)
141 {
142 __glxHashTablePtr table;
143 int i;
144
145 table = HASH_ALLOC(sizeof(*table));
146 if (!table) return NULL;
147 table->magic = HASH_MAGIC;
148 table->hits = 0;
149 table->partials = 0;
150 table->misses = 0;
151
152 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
153 return table;
154 }
155
156 _X_HIDDEN int __glxHashDestroy(__glxHashTable *t)
157 {
158 __glxHashTablePtr table = (__glxHashTablePtr)t;
159 __glxHashBucketPtr bucket;
160 __glxHashBucketPtr next;
161 int i;
162
163 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
164
165 for (i = 0; i < HASH_SIZE; i++) {
166 for (bucket = table->buckets[i]; bucket;) {
167 next = bucket->next;
168 HASH_FREE(bucket);
169 bucket = next;
170 }
171 }
172 HASH_FREE(table);
173 return 0;
174 }
175
176 /* Find the bucket and organize the list so that this bucket is at the
177 top. */
178
179 static __glxHashBucketPtr HashFind(__glxHashTablePtr table,
180 unsigned long key, unsigned long *h)
181 {
182 unsigned long hash = HashHash(key);
183 __glxHashBucketPtr prev = NULL;
184 __glxHashBucketPtr bucket;
185
186 if (h) *h = hash;
187
188 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
189 if (bucket->key == key) {
190 if (prev) {
191 /* Organize */
192 prev->next = bucket->next;
193 bucket->next = table->buckets[hash];
194 table->buckets[hash] = bucket;
195 ++table->partials;
196 } else {
197 ++table->hits;
198 }
199 return bucket;
200 }
201 prev = bucket;
202 }
203 ++table->misses;
204 return NULL;
205 }
206
207 _X_HIDDEN int __glxHashLookup(__glxHashTable *t,
208 unsigned long key, void **value)
209 {
210 __glxHashTablePtr table = (__glxHashTablePtr)t;
211 __glxHashBucketPtr bucket;
212
213 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
214
215 bucket = HashFind(table, key, NULL);
216 if (!bucket) return 1; /* Not found */
217 *value = bucket->value;
218 return 0; /* Found */
219 }
220
221 _X_HIDDEN int __glxHashInsert(__glxHashTable *t,
222 unsigned long key, void *value)
223 {
224 __glxHashTablePtr table = (__glxHashTablePtr)t;
225 __glxHashBucketPtr bucket;
226 unsigned long hash;
227
228 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
229
230 if (HashFind(table, key, &hash)) return 1; /* Already in table */
231
232 bucket = HASH_ALLOC(sizeof(*bucket));
233 if (!bucket) return -1; /* Error */
234 bucket->key = key;
235 bucket->value = value;
236 bucket->next = table->buckets[hash];
237 table->buckets[hash] = bucket;
238 #if HASH_DEBUG
239 printf("Inserted %d at %d/%p\n", key, hash, bucket);
240 #endif
241 return 0; /* Added to table */
242 }
243
244 _X_HIDDEN int __glxHashDelete(__glxHashTable *t, unsigned long key)
245 {
246 __glxHashTablePtr table = (__glxHashTablePtr)t;
247 unsigned long hash;
248 __glxHashBucketPtr bucket;
249
250 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
251
252 bucket = HashFind(table, key, &hash);
253
254 if (!bucket) return 1; /* Not found */
255
256 table->buckets[hash] = bucket->next;
257 HASH_FREE(bucket);
258 return 0;
259 }
260
261 _X_HIDDEN int __glxHashNext(__glxHashTable *t,
262 unsigned long *key, void **value)
263 {
264 __glxHashTablePtr table = (__glxHashTablePtr)t;
265
266 while (table->p0 < HASH_SIZE) {
267 if (table->p1) {
268 *key = table->p1->key;
269 *value = table->p1->value;
270 table->p1 = table->p1->next;
271 return 1;
272 }
273 table->p1 = table->buckets[table->p0];
274 ++table->p0;
275 }
276 return 0;
277 }
278
279 _X_HIDDEN int __glxHashFirst(__glxHashTable *t,
280 unsigned long *key, void **value)
281 {
282 __glxHashTablePtr table = (__glxHashTablePtr)t;
283
284 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
285
286 table->p0 = 0;
287 table->p1 = table->buckets[0];
288 return __glxHashNext(table, key, value);
289 }
290
291 #if HASH_MAIN
292 #define DIST_LIMIT 10
293 static int dist[DIST_LIMIT];
294
295 static void clear_dist(void) {
296 int i;
297
298 for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
299 }
300
301 static int count_entries(__glxHashBucketPtr bucket)
302 {
303 int count = 0;
304
305 for (; bucket; bucket = bucket->next) ++count;
306 return count;
307 }
308
309 static void update_dist(int count)
310 {
311 if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
312 else ++dist[count];
313 }
314
315 static void compute_dist(__glxHashTablePtr table)
316 {
317 int i;
318 __glxHashBucketPtr bucket;
319
320 printf("Hits = %ld, partials = %ld, misses = %ld\n",
321 table->hits, table->partials, table->misses);
322 clear_dist();
323 for (i = 0; i < HASH_SIZE; i++) {
324 bucket = table->buckets[i];
325 update_dist(count_entries(bucket));
326 }
327 for (i = 0; i < DIST_LIMIT; i++) {
328 if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
329 else printf("other %10d\n", dist[i]);
330 }
331 }
332
333 static void check_table(__glxHashTablePtr table,
334 unsigned long key, unsigned long value)
335 {
336 unsigned long retval = 0;
337 int retcode = __glxHashLookup(table, key, &retval);
338
339 switch (retcode) {
340 case -1:
341 printf("Bad magic = 0x%08lx:"
342 " key = %lu, expected = %lu, returned = %lu\n",
343 table->magic, key, value, retval);
344 break;
345 case 1:
346 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
347 key, value, retval);
348 break;
349 case 0:
350 if (value != retval)
351 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
352 key, value, retval);
353 break;
354 default:
355 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
356 retcode, key, value, retval);
357 break;
358 }
359 }
360
361 int main(void)
362 {
363 __glxHashTablePtr table;
364 int i;
365
366 printf("\n***** 256 consecutive integers ****\n");
367 table = __glxHashCreate();
368 for (i = 0; i < 256; i++) __glxHashInsert(table, i, i);
369 for (i = 0; i < 256; i++) check_table(table, i, i);
370 for (i = 256; i >= 0; i--) check_table(table, i, i);
371 compute_dist(table);
372 __glxHashDestroy(table);
373
374 printf("\n***** 1024 consecutive integers ****\n");
375 table = __glxHashCreate();
376 for (i = 0; i < 1024; i++) __glxHashInsert(table, i, i);
377 for (i = 0; i < 1024; i++) check_table(table, i, i);
378 for (i = 1024; i >= 0; i--) check_table(table, i, i);
379 compute_dist(table);
380 __glxHashDestroy(table);
381
382 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
383 table = __glxHashCreate();
384 for (i = 0; i < 1024; i++) __glxHashInsert(table, i*4096, i);
385 for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
386 for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
387 compute_dist(table);
388 __glxHashDestroy(table);
389
390 printf("\n***** 1024 random integers ****\n");
391 table = __glxHashCreate();
392 srandom(0xbeefbeef);
393 for (i = 0; i < 1024; i++) __glxHashInsert(table, random(), i);
394 srandom(0xbeefbeef);
395 for (i = 0; i < 1024; i++) check_table(table, random(), i);
396 srandom(0xbeefbeef);
397 for (i = 0; i < 1024; i++) check_table(table, random(), i);
398 compute_dist(table);
399 __glxHashDestroy(table);
400
401 printf("\n***** 5000 random integers ****\n");
402 table = __glxHashCreate();
403 srandom(0xbeefbeef);
404 for (i = 0; i < 5000; i++) __glxHashInsert(table, random(), i);
405 srandom(0xbeefbeef);
406 for (i = 0; i < 5000; i++) check_table(table, random(), i);
407 srandom(0xbeefbeef);
408 for (i = 0; i < 5000; i++) check_table(table, random(), i);
409 compute_dist(table);
410 __glxHashDestroy(table);
411
412 return 0;
413 }
414 #endif