Fix build with GCC 2.95.
[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 {
97 unsigned long key;
98 void *value;
99 struct __glxHashBucket *next;
100 } __glxHashBucket, *__glxHashBucketPtr;
101
102 typedef struct __glxHashTable *__glxHashTablePtr;
103 struct __glxHashTable
104 {
105 unsigned long magic;
106 unsigned long hits; /* At top of linked list */
107 unsigned long partials; /* Not at top of linked list */
108 unsigned long misses; /* Not in table */
109 __glxHashBucketPtr buckets[HASH_SIZE];
110 int p0;
111 __glxHashBucketPtr p1;
112 };
113
114 static unsigned long
115 HashHash(unsigned long key)
116 {
117 unsigned long hash = 0;
118 unsigned long tmp = key;
119 static int init = 0;
120 static unsigned long scatter[256];
121 int i;
122
123 if (!init) {
124 HASH_RANDOM_DECL;
125 HASH_RANDOM_INIT(37);
126 for (i = 0; i < 256; i++)
127 scatter[i] = HASH_RANDOM;
128 HASH_RANDOM_DESTROY;
129 ++init;
130 }
131
132 while (tmp) {
133 hash = (hash << 1) + scatter[tmp & 0xff];
134 tmp >>= 8;
135 }
136
137 hash %= HASH_SIZE;
138 #if HASH_DEBUG
139 printf("Hash(%d) = %d\n", key, hash);
140 #endif
141 return hash;
142 }
143
144 _X_HIDDEN __glxHashTable *
145 __glxHashCreate(void)
146 {
147 __glxHashTablePtr table;
148 int i;
149
150 table = HASH_ALLOC(sizeof(*table));
151 if (!table)
152 return NULL;
153 table->magic = HASH_MAGIC;
154 table->hits = 0;
155 table->partials = 0;
156 table->misses = 0;
157
158 for (i = 0; i < HASH_SIZE; i++)
159 table->buckets[i] = NULL;
160 return table;
161 }
162
163 _X_HIDDEN int
164 __glxHashDestroy(__glxHashTable * t)
165 {
166 __glxHashTablePtr table = (__glxHashTablePtr) t;
167 __glxHashBucketPtr bucket;
168 __glxHashBucketPtr next;
169 int i;
170
171 if (table->magic != HASH_MAGIC)
172 return -1; /* Bad magic */
173
174 for (i = 0; i < HASH_SIZE; i++) {
175 for (bucket = table->buckets[i]; bucket;) {
176 next = bucket->next;
177 HASH_FREE(bucket);
178 bucket = next;
179 }
180 }
181 HASH_FREE(table);
182 return 0;
183 }
184
185 /* Find the bucket and organize the list so that this bucket is at the
186 top. */
187
188 static __glxHashBucketPtr
189 HashFind(__glxHashTablePtr table, unsigned long key, unsigned long *h)
190 {
191 unsigned long hash = HashHash(key);
192 __glxHashBucketPtr prev = NULL;
193 __glxHashBucketPtr bucket;
194
195 if (h)
196 *h = hash;
197
198 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
199 if (bucket->key == key) {
200 if (prev) {
201 /* Organize */
202 prev->next = bucket->next;
203 bucket->next = table->buckets[hash];
204 table->buckets[hash] = bucket;
205 ++table->partials;
206 }
207 else {
208 ++table->hits;
209 }
210 return bucket;
211 }
212 prev = bucket;
213 }
214 ++table->misses;
215 return NULL;
216 }
217
218 _X_HIDDEN int
219 __glxHashLookup(__glxHashTable * t, unsigned long key, void **value)
220 {
221 __glxHashTablePtr table = (__glxHashTablePtr) t;
222 __glxHashBucketPtr bucket;
223
224 if (!table || table->magic != HASH_MAGIC)
225 return -1; /* Bad magic */
226
227 bucket = HashFind(table, key, NULL);
228 if (!bucket)
229 return 1; /* Not found */
230 *value = bucket->value;
231 return 0; /* Found */
232 }
233
234 _X_HIDDEN int
235 __glxHashInsert(__glxHashTable * t, unsigned long key, void *value)
236 {
237 __glxHashTablePtr table = (__glxHashTablePtr) t;
238 __glxHashBucketPtr bucket;
239 unsigned long hash;
240
241 if (table->magic != HASH_MAGIC)
242 return -1; /* Bad magic */
243
244 if (HashFind(table, key, &hash))
245 return 1; /* Already in table */
246
247 bucket = HASH_ALLOC(sizeof(*bucket));
248 if (!bucket)
249 return -1; /* Error */
250 bucket->key = key;
251 bucket->value = value;
252 bucket->next = table->buckets[hash];
253 table->buckets[hash] = bucket;
254 #if HASH_DEBUG
255 printf("Inserted %d at %d/%p\n", key, hash, bucket);
256 #endif
257 return 0; /* Added to table */
258 }
259
260 _X_HIDDEN int
261 __glxHashDelete(__glxHashTable * t, unsigned long key)
262 {
263 __glxHashTablePtr table = (__glxHashTablePtr) t;
264 unsigned long hash;
265 __glxHashBucketPtr bucket;
266
267 if (table->magic != HASH_MAGIC)
268 return -1; /* Bad magic */
269
270 bucket = HashFind(table, key, &hash);
271
272 if (!bucket)
273 return 1; /* Not found */
274
275 table->buckets[hash] = bucket->next;
276 HASH_FREE(bucket);
277 return 0;
278 }
279
280 _X_HIDDEN int
281 __glxHashNext(__glxHashTable * t, unsigned long *key, void **value)
282 {
283 __glxHashTablePtr table = (__glxHashTablePtr) t;
284
285 while (table->p0 < HASH_SIZE) {
286 if (table->p1) {
287 *key = table->p1->key;
288 *value = table->p1->value;
289 table->p1 = table->p1->next;
290 return 1;
291 }
292 table->p1 = table->buckets[table->p0];
293 ++table->p0;
294 }
295 return 0;
296 }
297
298 _X_HIDDEN int
299 __glxHashFirst(__glxHashTable * t, unsigned long *key, void **value)
300 {
301 __glxHashTablePtr table = (__glxHashTablePtr) t;
302
303 if (table->magic != HASH_MAGIC)
304 return -1; /* Bad magic */
305
306 table->p0 = 0;
307 table->p1 = table->buckets[0];
308 return __glxHashNext(table, key, value);
309 }
310
311 #if HASH_MAIN
312 #define DIST_LIMIT 10
313 static int dist[DIST_LIMIT];
314
315 static void
316 clear_dist(void)
317 {
318 int i;
319
320 for (i = 0; i < DIST_LIMIT; i++)
321 dist[i] = 0;
322 }
323
324 static int
325 count_entries(__glxHashBucketPtr bucket)
326 {
327 int count = 0;
328
329 for (; bucket; bucket = bucket->next)
330 ++count;
331 return count;
332 }
333
334 static void
335 update_dist(int count)
336 {
337 if (count >= DIST_LIMIT)
338 ++dist[DIST_LIMIT - 1];
339 else
340 ++dist[count];
341 }
342
343 static void
344 compute_dist(__glxHashTablePtr table)
345 {
346 int i;
347 __glxHashBucketPtr bucket;
348
349 printf("Hits = %ld, partials = %ld, misses = %ld\n",
350 table->hits, table->partials, table->misses);
351 clear_dist();
352 for (i = 0; i < HASH_SIZE; i++) {
353 bucket = table->buckets[i];
354 update_dist(count_entries(bucket));
355 }
356 for (i = 0; i < DIST_LIMIT; i++) {
357 if (i != DIST_LIMIT - 1)
358 printf("%5d %10d\n", i, dist[i]);
359 else
360 printf("other %10d\n", dist[i]);
361 }
362 }
363
364 static void
365 check_table(__glxHashTablePtr table, unsigned long key, unsigned long value)
366 {
367 unsigned long retval = 0;
368 int retcode = __glxHashLookup(table, key, &retval);
369
370 switch (retcode) {
371 case -1:
372 printf("Bad magic = 0x%08lx:"
373 " key = %lu, expected = %lu, returned = %lu\n",
374 table->magic, key, value, retval);
375 break;
376 case 1:
377 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
378 key, value, retval);
379 break;
380 case 0:
381 if (value != retval)
382 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
383 key, value, retval);
384 break;
385 default:
386 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
387 retcode, key, value, retval);
388 break;
389 }
390 }
391
392 int
393 main(void)
394 {
395 __glxHashTablePtr table;
396 int i;
397
398 printf("\n***** 256 consecutive integers ****\n");
399 table = __glxHashCreate();
400 for (i = 0; i < 256; i++)
401 __glxHashInsert(table, i, i);
402 for (i = 0; i < 256; i++)
403 check_table(table, i, i);
404 for (i = 256; i >= 0; i--)
405 check_table(table, i, i);
406 compute_dist(table);
407 __glxHashDestroy(table);
408
409 printf("\n***** 1024 consecutive integers ****\n");
410 table = __glxHashCreate();
411 for (i = 0; i < 1024; i++)
412 __glxHashInsert(table, i, i);
413 for (i = 0; i < 1024; i++)
414 check_table(table, i, i);
415 for (i = 1024; i >= 0; i--)
416 check_table(table, i, i);
417 compute_dist(table);
418 __glxHashDestroy(table);
419
420 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
421 table = __glxHashCreate();
422 for (i = 0; i < 1024; i++)
423 __glxHashInsert(table, i * 4096, i);
424 for (i = 0; i < 1024; i++)
425 check_table(table, i * 4096, i);
426 for (i = 1024; i >= 0; i--)
427 check_table(table, i * 4096, i);
428 compute_dist(table);
429 __glxHashDestroy(table);
430
431 printf("\n***** 1024 random integers ****\n");
432 table = __glxHashCreate();
433 srandom(0xbeefbeef);
434 for (i = 0; i < 1024; i++)
435 __glxHashInsert(table, random(), i);
436 srandom(0xbeefbeef);
437 for (i = 0; i < 1024; i++)
438 check_table(table, random(), i);
439 srandom(0xbeefbeef);
440 for (i = 0; i < 1024; i++)
441 check_table(table, random(), i);
442 compute_dist(table);
443 __glxHashDestroy(table);
444
445 printf("\n***** 5000 random integers ****\n");
446 table = __glxHashCreate();
447 srandom(0xbeefbeef);
448 for (i = 0; i < 5000; i++)
449 __glxHashInsert(table, random(), i);
450 srandom(0xbeefbeef);
451 for (i = 0; i < 5000; i++)
452 check_table(table, random(), i);
453 srandom(0xbeefbeef);
454 for (i = 0; i < 5000; i++)
455 check_table(table, random(), i);
456 compute_dist(table);
457 __glxHashDestroy(table);
458
459 return 0;
460 }
461 #endif