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