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
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
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:
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
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.
27 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmHash.c,v 1.2 2000/02/23 04:47:23 martin Exp $
31 * This file contains a straightforward implementation of a fixed-sized
32 * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
33 * collision resolution. There are two potentially interesting things
34 * about this implementation:
36 * 1) The table is power-of-two sized. Prime sized tables are more
37 * traditional, but do not have a significant advantage over power-of-two
38 * sized table, especially when double hashing is not used for collision
41 * 2) The hash computation uses a table of random integers [Hanson97,
46 * With a table size of 512, the current implementation is sufficient for a
47 * few hundred keys. Since this is well above the expected size of the
48 * tables for which this implementation was designed, the implementation of
49 * dynamic hash tables was postponed until the need arises. A common (and
50 * naive) approach to dynamic hash table implementation simply creates a
51 * new hash table when necessary, rehashes all the data into the new table,
52 * and destroys the old table. The approach in [Larson88] is superior in
53 * two ways: 1) only a portion of the table is expanded when needed,
54 * distributing the expansion cost over several insertions, and 2) portions
55 * of the table can be locked, enabling a scalable thread-safe
60 * [Hanson97] David R. Hanson. C Interfaces and Implementations:
61 * Techniques for Creating Reusable Software. Reading, Massachusetts:
62 * Addison-Wesley, 1997.
64 * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
65 * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
67 * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
81 # include "xf86_ansic.h"
90 #define HASH_MAGIC 0xdeadbeef
92 #define HASH_SIZE 512 /* Good for about 100 entries */
93 /* If you change this value, you probably
94 have to change the HashHash hashing
98 #define HASH_ALLOC malloc
99 #define HASH_FREE free
100 #define HASH_RANDOM_DECL
101 #define HASH_RANDOM_INIT(seed) srandom(seed)
102 #define HASH_RANDOM random()
104 #define HASH_ALLOC drmMalloc
105 #define HASH_FREE drmFree
106 #define HASH_RANDOM_DECL void *state
107 #define HASH_RANDOM_INIT(seed) state = drmRandomCreate(seed)
108 #define HASH_RANDOM drmRandom(state)
112 typedef struct HashBucket {
115 struct HashBucket *next;
116 } HashBucket, *HashBucketPtr;
118 typedef struct HashTable {
120 unsigned long entries;
121 unsigned long hits; /* At top of linked list */
122 unsigned long partials; /* Not at top of linked list */
123 unsigned long misses; /* Not in table */
124 HashBucketPtr buckets[HASH_SIZE];
127 } HashTable, *HashTablePtr;
130 extern void *N(HashCreate)(void);
131 extern int N(HashDestroy)(void *t);
132 extern int N(HashLookup)(void *t, unsigned long key, unsigned long *value);
133 extern int N(HashInsert)(void *t, unsigned long key, unsigned long value);
134 extern int N(HashDelete)(void *t, unsigned long key);
137 static unsigned long HashHash(unsigned long key)
139 unsigned long hash = 0;
140 unsigned long tmp = key;
142 static unsigned long scatter[256];
147 HASH_RANDOM_INIT(37);
148 for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
153 hash = (hash << 1) + scatter[tmp & 0xff];
159 printf( "Hash(%d) = %d\n", key, hash);
164 void *N(HashCreate)(void)
169 table = HASH_ALLOC(sizeof(*table));
170 if (!table) return NULL;
171 table->magic = HASH_MAGIC;
177 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
181 int N(HashDestroy)(void *t)
183 HashTablePtr table = (HashTablePtr)t;
184 HashBucketPtr bucket;
188 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
190 for (i = 0; i < HASH_SIZE; i++) {
191 for (bucket = table->buckets[i]; bucket;) {
201 /* Find the bucket and organize the list so that this bucket is at the
204 static HashBucketPtr HashFind(HashTablePtr table,
205 unsigned long key, unsigned long *h)
207 unsigned long hash = HashHash(key);
208 HashBucketPtr prev = NULL;
209 HashBucketPtr bucket;
213 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
214 if (bucket->key == key) {
217 prev->next = bucket->next;
218 bucket->next = table->buckets[hash];
219 table->buckets[hash] = bucket;
232 int N(HashLookup)(void *t, unsigned long key, void **value)
234 HashTablePtr table = (HashTablePtr)t;
235 HashBucketPtr bucket;
237 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
239 bucket = HashFind(table, key, NULL);
240 if (!bucket) return 1; /* Not found */
241 *value = bucket->value;
242 return 0; /* Found */
245 int N(HashInsert)(void *t, unsigned long key, void *value)
247 HashTablePtr table = (HashTablePtr)t;
248 HashBucketPtr bucket;
251 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
253 if (HashFind(table, key, &hash)) return 1; /* Already in table */
255 bucket = HASH_ALLOC(sizeof(*bucket));
256 if (!bucket) return -1; /* Error */
258 bucket->value = value;
259 bucket->next = table->buckets[hash];
260 table->buckets[hash] = bucket;
262 printf("Inserted %d at %d/%p\n", key, hash, bucket);
264 return 0; /* Added to table */
267 int N(HashDelete)(void *t, unsigned long key)
269 HashTablePtr table = (HashTablePtr)t;
271 HashBucketPtr bucket;
273 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
275 bucket = HashFind(table, key, &hash);
277 if (!bucket) return 1; /* Not found */
279 table->buckets[hash] = bucket->next;
284 int N(HashNext)(void *t, unsigned long *key, void **value)
286 HashTablePtr table = (HashTablePtr)t;
288 for (; table->p0 < HASH_SIZE;
289 ++table->p0, table->p1 = table->buckets[table->p0]) {
291 *key = table->p1->key;
292 *value = table->p1->value;
293 table->p1 = table->p1->next;
300 int N(HashFirst)(void *t, unsigned long *key, void **value)
302 HashTablePtr table = (HashTablePtr)t;
304 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
307 table->p1 = table->buckets[0];
308 return N(HashNext)(table, key, value);
312 #define DIST_LIMIT 10
313 static int dist[DIST_LIMIT];
315 static void clear_dist(void) {
318 for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
321 static int count_entries(HashBucketPtr bucket)
325 for (; bucket; bucket = bucket->next) ++count;
329 static void update_dist(int count)
331 if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
335 static void compute_dist(HashTablePtr table)
338 HashBucketPtr bucket;
340 printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
341 table->entries, table->hits, table->partials, table->misses);
343 for (i = 0; i < HASH_SIZE; i++) {
344 bucket = table->buckets[i];
345 update_dist(count_entries(bucket));
347 for (i = 0; i < DIST_LIMIT; i++) {
348 if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
349 else printf("other %10d\n", dist[i]);
353 static void check_table(HashTablePtr table,
354 unsigned long key, unsigned long value)
356 unsigned long retval = 0;
357 int retcode = N(HashLookup)(table, key, &retval);
361 printf("Bad magic = 0x%08lx:"
362 " key = %lu, expected = %lu, returned = %lu\n",
363 table->magic, key, value, retval);
366 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
371 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
375 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
376 retcode, key, value, retval);
386 printf("\n***** 256 consecutive integers ****\n");
387 table = N(HashCreate)();
388 for (i = 0; i < 256; i++) N(HashInsert)(table, i, i);
389 for (i = 0; i < 256; i++) check_table(table, i, i);
390 for (i = 256; i >= 0; i--) check_table(table, i, i);
392 N(HashDestroy)(table);
394 printf("\n***** 1024 consecutive integers ****\n");
395 table = N(HashCreate)();
396 for (i = 0; i < 1024; i++) N(HashInsert)(table, i, i);
397 for (i = 0; i < 1024; i++) check_table(table, i, i);
398 for (i = 1024; i >= 0; i--) check_table(table, i, i);
400 N(HashDestroy)(table);
402 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
403 table = N(HashCreate)();
404 for (i = 0; i < 1024; i++) N(HashInsert)(table, i*4096, i);
405 for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
406 for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
408 N(HashDestroy)(table);
410 printf("\n***** 1024 random integers ****\n");
411 table = N(HashCreate)();
413 for (i = 0; i < 1024; i++) N(HashInsert)(table, random(), i);
415 for (i = 0; i < 1024; i++) check_table(table, random(), i);
417 for (i = 0; i < 1024; i++) check_table(table, random(), i);
419 N(HashDestroy)(table);
421 printf("\n***** 5000 random integers ****\n");
422 table = N(HashCreate)();
424 for (i = 0; i < 5000; i++) N(HashInsert)(table, random(), i);
426 for (i = 0; i < 5000; i++) check_table(table, random(), i);
428 for (i = 0; i < 5000; i++) check_table(table, random(), i);
430 N(HashDestroy)(table);