fix up radeon span functions using latest r200 code from Brian,
[mesa.git] / src / mesa / drivers / dri / dri_client / xf86drmHash.c
1 /* xf86drmHash.c -- Small hash table support for integer -> integer mapping
2 * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27 *
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmHash.c,v 1.4 2001/03/21 18:08:54 dawes Exp $
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 #define HASH_MAIN 0
74
75 #if HASH_MAIN
76 # include <stdio.h>
77 # include <stdlib.h>
78 #else
79 # include "xf86drm.h"
80 # ifdef XFree86LOADER
81 # include "xf86.h"
82 # include "xf86_ansic.h"
83 # else
84 # include <stdio.h>
85 # include <stdlib.h>
86 # endif
87 #endif
88
89 #define HASH_MAGIC 0xdeadbeef
90 #define HASH_DEBUG 0
91 #define HASH_SIZE 512 /* Good for about 100 entries */
92 /* If you change this value, you probably
93 have to change the HashHash hashing
94 function! */
95
96 #if HASH_MAIN
97 #define HASH_ALLOC malloc
98 #define HASH_FREE free
99 #define HASH_RANDOM_DECL
100 #define HASH_RANDOM_INIT(seed) srandom(seed)
101 #define HASH_RANDOM random()
102 #else
103 #define HASH_ALLOC drmMalloc
104 #define HASH_FREE drmFree
105 #define HASH_RANDOM_DECL void *state
106 #define HASH_RANDOM_INIT(seed) state = drmRandomCreate(seed)
107 #define HASH_RANDOM drmRandom(state)
108
109 #endif
110
111 typedef struct HashBucket {
112 unsigned long key;
113 void *value;
114 struct HashBucket *next;
115 } HashBucket, *HashBucketPtr;
116
117 typedef struct HashTable {
118 unsigned long magic;
119 unsigned long entries;
120 unsigned long hits; /* At top of linked list */
121 unsigned long partials; /* Not at top of linked list */
122 unsigned long misses; /* Not in table */
123 HashBucketPtr buckets[HASH_SIZE];
124 int p0;
125 HashBucketPtr p1;
126 } HashTable, *HashTablePtr;
127
128 #if HASH_MAIN
129 extern void *drmHashCreate(void);
130 extern int drmHashDestroy(void *t);
131 extern int drmHashLookup(void *t, unsigned long key, unsigned long *value);
132 extern int drmHashInsert(void *t, unsigned long key, unsigned long value);
133 extern int drmHashDelete(void *t, unsigned long key);
134 #endif
135
136 static unsigned long HashHash(unsigned long key)
137 {
138 unsigned long hash = 0;
139 unsigned long tmp = key;
140 static int init = 0;
141 static unsigned long scatter[256];
142 int i;
143
144 if (!init) {
145 HASH_RANDOM_DECL;
146 HASH_RANDOM_INIT(37);
147 for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
148 ++init;
149 }
150
151 while (tmp) {
152 hash = (hash << 1) + scatter[tmp & 0xff];
153 tmp >>= 8;
154 }
155
156 hash %= HASH_SIZE;
157 #if HASH_DEBUG
158 printf( "Hash(%d) = %d\n", key, hash);
159 #endif
160 return hash;
161 }
162
163 void *drmHashCreate(void)
164 {
165 HashTablePtr table;
166 int i;
167
168 table = HASH_ALLOC(sizeof(*table));
169 if (!table) return NULL;
170 table->magic = HASH_MAGIC;
171 table->entries = 0;
172 table->hits = 0;
173 table->partials = 0;
174 table->misses = 0;
175
176 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
177 return table;
178 }
179
180 int drmHashDestroy(void *t)
181 {
182 HashTablePtr table = (HashTablePtr)t;
183 HashBucketPtr bucket;
184 HashBucketPtr next;
185 int i;
186
187 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
188
189 for (i = 0; i < HASH_SIZE; i++) {
190 for (bucket = table->buckets[i]; bucket;) {
191 next = bucket->next;
192 HASH_FREE(bucket);
193 bucket = next;
194 }
195 }
196 HASH_FREE(table);
197 return 0;
198 }
199
200 /* Find the bucket and organize the list so that this bucket is at the
201 top. */
202
203 static HashBucketPtr HashFind(HashTablePtr table,
204 unsigned long key, unsigned long *h)
205 {
206 unsigned long hash = HashHash(key);
207 HashBucketPtr prev = NULL;
208 HashBucketPtr bucket;
209
210 if (h) *h = hash;
211
212 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
213 if (bucket->key == key) {
214 if (prev) {
215 /* Organize */
216 prev->next = bucket->next;
217 bucket->next = table->buckets[hash];
218 table->buckets[hash] = bucket;
219 ++table->partials;
220 } else {
221 ++table->hits;
222 }
223 return bucket;
224 }
225 prev = bucket;
226 }
227 ++table->misses;
228 return NULL;
229 }
230
231 int drmHashLookup(void *t, unsigned long key, void **value)
232 {
233 HashTablePtr table = (HashTablePtr)t;
234 HashBucketPtr bucket;
235
236 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
237
238 bucket = HashFind(table, key, NULL);
239 if (!bucket) return 1; /* Not found */
240 *value = bucket->value;
241 return 0; /* Found */
242 }
243
244 int drmHashInsert(void *t, unsigned long key, void *value)
245 {
246 HashTablePtr table = (HashTablePtr)t;
247 HashBucketPtr bucket;
248 unsigned long hash;
249
250 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
251
252 if (HashFind(table, key, &hash)) return 1; /* Already in table */
253
254 bucket = HASH_ALLOC(sizeof(*bucket));
255 if (!bucket) return -1; /* Error */
256 bucket->key = key;
257 bucket->value = value;
258 bucket->next = table->buckets[hash];
259 table->buckets[hash] = bucket;
260 #if HASH_DEBUG
261 printf("Inserted %d at %d/%p\n", key, hash, bucket);
262 #endif
263 return 0; /* Added to table */
264 }
265
266 int drmHashDelete(void *t, unsigned long key)
267 {
268 HashTablePtr table = (HashTablePtr)t;
269 unsigned long hash;
270 HashBucketPtr bucket;
271
272 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
273
274 bucket = HashFind(table, key, &hash);
275
276 if (!bucket) return 1; /* Not found */
277
278 table->buckets[hash] = bucket->next;
279 HASH_FREE(bucket);
280 return 0;
281 }
282
283 int drmHashNext(void *t, unsigned long *key, void **value)
284 {
285 HashTablePtr table = (HashTablePtr)t;
286
287 for (; table->p0 < HASH_SIZE;
288 ++table->p0, table->p1 = table->buckets[table->p0]) {
289 if (table->p1) {
290 *key = table->p1->key;
291 *value = table->p1->value;
292 table->p1 = table->p1->next;
293 return 1;
294 }
295 }
296 return 0;
297 }
298
299 int drmHashFirst(void *t, unsigned long *key, void **value)
300 {
301 HashTablePtr table = (HashTablePtr)t;
302
303 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
304
305 table->p0 = 0;
306 table->p1 = table->buckets[0];
307 return drmHashNext(table, key, value);
308 }
309
310 #if HASH_MAIN
311 #define DIST_LIMIT 10
312 static int dist[DIST_LIMIT];
313
314 static void clear_dist(void) {
315 int i;
316
317 for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
318 }
319
320 static int count_entries(HashBucketPtr bucket)
321 {
322 int count = 0;
323
324 for (; bucket; bucket = bucket->next) ++count;
325 return count;
326 }
327
328 static void update_dist(int count)
329 {
330 if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
331 else ++dist[count];
332 }
333
334 static void compute_dist(HashTablePtr table)
335 {
336 int i;
337 HashBucketPtr bucket;
338
339 printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
340 table->entries, table->hits, table->partials, table->misses);
341 clear_dist();
342 for (i = 0; i < HASH_SIZE; i++) {
343 bucket = table->buckets[i];
344 update_dist(count_entries(bucket));
345 }
346 for (i = 0; i < DIST_LIMIT; i++) {
347 if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
348 else printf("other %10d\n", dist[i]);
349 }
350 }
351
352 static void check_table(HashTablePtr table,
353 unsigned long key, unsigned long value)
354 {
355 unsigned long retval = 0;
356 int retcode = drmHashLookup(table, key, &retval);
357
358 switch (retcode) {
359 case -1:
360 printf("Bad magic = 0x%08lx:"
361 " key = %lu, expected = %lu, returned = %lu\n",
362 table->magic, key, value, retval);
363 break;
364 case 1:
365 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
366 key, value, retval);
367 break;
368 case 0:
369 if (value != retval)
370 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
371 key, value, retval);
372 break;
373 default:
374 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
375 retcode, key, value, retval);
376 break;
377 }
378 }
379
380 int main(void)
381 {
382 HashTablePtr table;
383 int i;
384
385 printf("\n***** 256 consecutive integers ****\n");
386 table = drmHashCreate();
387 for (i = 0; i < 256; i++) drmHashInsert(table, i, i);
388 for (i = 0; i < 256; i++) check_table(table, i, i);
389 for (i = 256; i >= 0; i--) check_table(table, i, i);
390 compute_dist(table);
391 drmHashDestroy(table);
392
393 printf("\n***** 1024 consecutive integers ****\n");
394 table = drmHashCreate();
395 for (i = 0; i < 1024; i++) drmHashInsert(table, i, i);
396 for (i = 0; i < 1024; i++) check_table(table, i, i);
397 for (i = 1024; i >= 0; i--) check_table(table, i, i);
398 compute_dist(table);
399 drmHashDestroy(table);
400
401 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
402 table = drmHashCreate();
403 for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, i);
404 for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
405 for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
406 compute_dist(table);
407 drmHashDestroy(table);
408
409 printf("\n***** 1024 random integers ****\n");
410 table = drmHashCreate();
411 srandom(0xbeefbeef);
412 for (i = 0; i < 1024; i++) drmHashInsert(table, random(), i);
413 srandom(0xbeefbeef);
414 for (i = 0; i < 1024; i++) check_table(table, random(), i);
415 srandom(0xbeefbeef);
416 for (i = 0; i < 1024; i++) check_table(table, random(), i);
417 compute_dist(table);
418 drmHashDestroy(table);
419
420 printf("\n***** 5000 random integers ****\n");
421 table = drmHashCreate();
422 srandom(0xbeefbeef);
423 for (i = 0; i < 5000; i++) drmHashInsert(table, random(), i);
424 srandom(0xbeefbeef);
425 for (i = 0; i < 5000; i++) check_table(table, random(), i);
426 srandom(0xbeefbeef);
427 for (i = 0; i < 5000; i++) check_table(table, random(), i);
428 compute_dist(table);
429 drmHashDestroy(table);
430
431 return 0;
432 }
433 #endif