1 /* xf86drmHash.c -- Small hash table support for integer -> integer mapping
2 * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
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:
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
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.
26 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
30 * This file contains a straightforward implementation of a fixed-sized
31 * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
32 * collision resolution. There are two potentially interesting things
33 * about this implementation:
35 * 1) The table is power-of-two sized. Prime sized tables are more
36 * traditional, but do not have a significant advantage over power-of-two
37 * sized table, especially when double hashing is not used for collision
40 * 2) The hash computation uses a table of random integers [Hanson97,
45 * With a table size of 512, the current implementation is sufficient for a
46 * few hundred keys. Since this is well above the expected size of the
47 * tables for which this implementation was designed, the implementation of
48 * dynamic hash tables was postponed until the need arises. A common (and
49 * naive) approach to dynamic hash table implementation simply creates a
50 * new hash table when necessary, rehashes all the data into the new table,
51 * and destroys the old table. The approach in [Larson88] is superior in
52 * two ways: 1) only a portion of the table is expanded when needed,
53 * distributing the expansion cost over several insertions, and 2) portions
54 * of the table can be locked, enabling a scalable thread-safe
59 * [Hanson97] David R. Hanson. C Interfaces and Implementations:
60 * Techniques for Creating Reusable Software. Reading, Massachusetts:
61 * Addison-Wesley, 1997.
63 * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
64 * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
66 * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
81 #define HASH_MAGIC 0xdeadbeef
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
89 #define HASH_ALLOC malloc
90 #define HASH_FREE free
91 #define HASH_RANDOM_DECL
92 #define HASH_RANDOM_INIT(seed) srandom(seed)
93 #define HASH_RANDOM random()
94 #define HASH_RANDOM_DESTROY
96 #define HASH_ALLOC drmMalloc
97 #define HASH_FREE drmFree
98 #define HASH_RANDOM_DECL void *state
99 #define HASH_RANDOM_INIT(seed) state = drmRandomCreate(seed)
100 #define HASH_RANDOM drmRandom(state)
101 #define HASH_RANDOM_DESTROY drmRandomDestroy(state)
105 typedef struct HashBucket {
108 struct HashBucket *next;
109 } HashBucket, *HashBucketPtr;
111 typedef struct HashTable {
113 unsigned long entries;
114 unsigned long hits; /* At top of linked list */
115 unsigned long partials; /* Not at top of linked list */
116 unsigned long misses; /* Not in table */
117 HashBucketPtr buckets[HASH_SIZE];
120 } HashTable, *HashTablePtr;
123 extern void *drmHashCreate(void);
124 extern int drmHashDestroy(void *t);
125 extern int drmHashLookup(void *t, unsigned long key, unsigned long *value);
126 extern int drmHashInsert(void *t, unsigned long key, unsigned long value);
127 extern int drmHashDelete(void *t, unsigned long key);
130 static unsigned long HashHash(unsigned long key)
132 unsigned long hash = 0;
133 unsigned long tmp = key;
135 static unsigned long scatter[256];
140 HASH_RANDOM_INIT(37);
141 for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
147 hash = (hash << 1) + scatter[tmp & 0xff];
153 printf( "Hash(%d) = %d\n", key, hash);
158 void *drmHashCreate(void)
163 table = HASH_ALLOC(sizeof(*table));
164 if (!table) return NULL;
165 table->magic = HASH_MAGIC;
171 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
175 int drmHashDestroy(void *t)
177 HashTablePtr table = (HashTablePtr)t;
178 HashBucketPtr bucket;
182 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
184 for (i = 0; i < HASH_SIZE; i++) {
185 for (bucket = table->buckets[i]; bucket;) {
195 /* Find the bucket and organize the list so that this bucket is at the
198 static HashBucketPtr HashFind(HashTablePtr table,
199 unsigned long key, unsigned long *h)
201 unsigned long hash = HashHash(key);
202 HashBucketPtr prev = NULL;
203 HashBucketPtr bucket;
207 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
208 if (bucket->key == key) {
211 prev->next = bucket->next;
212 bucket->next = table->buckets[hash];
213 table->buckets[hash] = bucket;
226 int drmHashLookup(void *t, unsigned long key, void **value)
228 HashTablePtr table = (HashTablePtr)t;
229 HashBucketPtr bucket;
231 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
233 bucket = HashFind(table, key, NULL);
234 if (!bucket) return 1; /* Not found */
235 *value = bucket->value;
236 return 0; /* Found */
239 int drmHashInsert(void *t, unsigned long key, void *value)
241 HashTablePtr table = (HashTablePtr)t;
242 HashBucketPtr bucket;
245 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
247 if (HashFind(table, key, &hash)) return 1; /* Already in table */
249 bucket = HASH_ALLOC(sizeof(*bucket));
250 if (!bucket) return -1; /* Error */
252 bucket->value = value;
253 bucket->next = table->buckets[hash];
254 table->buckets[hash] = bucket;
256 printf("Inserted %d at %d/%p\n", key, hash, bucket);
258 return 0; /* Added to table */
261 int drmHashDelete(void *t, unsigned long key)
263 HashTablePtr table = (HashTablePtr)t;
265 HashBucketPtr bucket;
267 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
269 bucket = HashFind(table, key, &hash);
271 if (!bucket) return 1; /* Not found */
273 table->buckets[hash] = bucket->next;
278 int drmHashNext(void *t, unsigned long *key, void **value)
280 HashTablePtr table = (HashTablePtr)t;
282 while (table->p0 < HASH_SIZE) {
284 *key = table->p1->key;
285 *value = table->p1->value;
286 table->p1 = table->p1->next;
289 table->p1 = table->buckets[table->p0];
295 int drmHashFirst(void *t, unsigned long *key, void **value)
297 HashTablePtr table = (HashTablePtr)t;
299 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
302 table->p1 = table->buckets[0];
303 return drmHashNext(table, key, value);
307 #define DIST_LIMIT 10
308 static int dist[DIST_LIMIT];
310 static void clear_dist(void) {
313 for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
316 static int count_entries(HashBucketPtr bucket)
320 for (; bucket; bucket = bucket->next) ++count;
324 static void update_dist(int count)
326 if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
330 static void compute_dist(HashTablePtr table)
333 HashBucketPtr bucket;
335 printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
336 table->entries, table->hits, table->partials, table->misses);
338 for (i = 0; i < HASH_SIZE; i++) {
339 bucket = table->buckets[i];
340 update_dist(count_entries(bucket));
342 for (i = 0; i < DIST_LIMIT; i++) {
343 if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
344 else printf("other %10d\n", dist[i]);
348 static void check_table(HashTablePtr table,
349 unsigned long key, unsigned long value)
351 unsigned long retval = 0;
352 int retcode = drmHashLookup(table, key, &retval);
356 printf("Bad magic = 0x%08lx:"
357 " key = %lu, expected = %lu, returned = %lu\n",
358 table->magic, key, value, retval);
361 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
366 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
370 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
371 retcode, key, value, retval);
381 printf("\n***** 256 consecutive integers ****\n");
382 table = drmHashCreate();
383 for (i = 0; i < 256; i++) drmHashInsert(table, i, i);
384 for (i = 0; i < 256; i++) check_table(table, i, i);
385 for (i = 256; i >= 0; i--) check_table(table, i, i);
387 drmHashDestroy(table);
389 printf("\n***** 1024 consecutive integers ****\n");
390 table = drmHashCreate();
391 for (i = 0; i < 1024; i++) drmHashInsert(table, i, i);
392 for (i = 0; i < 1024; i++) check_table(table, i, i);
393 for (i = 1024; i >= 0; i--) check_table(table, i, i);
395 drmHashDestroy(table);
397 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
398 table = drmHashCreate();
399 for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, i);
400 for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
401 for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
403 drmHashDestroy(table);
405 printf("\n***** 1024 random integers ****\n");
406 table = drmHashCreate();
408 for (i = 0; i < 1024; i++) drmHashInsert(table, random(), i);
410 for (i = 0; i < 1024; i++) check_table(table, random(), i);
412 for (i = 0; i < 1024; i++) check_table(table, random(), i);
414 drmHashDestroy(table);
416 printf("\n***** 5000 random integers ****\n");
417 table = drmHashCreate();
419 for (i = 0; i < 5000; i++) drmHashInsert(table, random(), i);
421 for (i = 0; i < 5000; i++) check_table(table, random(), i);
423 for (i = 0; i < 5000; i++) check_table(table, random(), i);
425 drmHashDestroy(table);