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>
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmHash.c,v 1.4 2001/03/21 18:08:54 dawes Exp $
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:
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
42 * 2) The hash computation uses a table of random integers [Hanson97,
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
61 * [Hanson97] David R. Hanson. C Interfaces and Implementations:
62 * Techniques for Creating Reusable Software. Reading, Massachusetts:
63 * Addison-Wesley, 1997.
65 * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
66 * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
68 * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
73 #ifdef HAVE_XORG_CONFIG_H
74 #include <xorg-config.h>
87 # include "xf86_ansic.h"
96 #define HASH_MAGIC 0xdeadbeef
98 #define HASH_SIZE 512 /* Good for about 100 entries */
99 /* If you change this value, you probably
100 have to change the HashHash hashing
104 #define HASH_ALLOC malloc
105 #define HASH_FREE free
106 #define HASH_RANDOM_DECL
107 #define HASH_RANDOM_INIT(seed) srandom(seed)
108 #define HASH_RANDOM random()
109 #define HASH_RANDOM_DESTROY
111 #define HASH_ALLOC drmMalloc
112 #define HASH_FREE drmFree
113 #define HASH_RANDOM_DECL void *state
114 #define HASH_RANDOM_INIT(seed) state = drmRandomCreate(seed)
115 #define HASH_RANDOM drmRandom(state)
116 #define HASH_RANDOM_DESTROY drmRandomDestroy(state)
120 typedef struct HashBucket {
123 struct HashBucket *next;
124 } HashBucket, *HashBucketPtr;
126 typedef struct HashTable {
128 unsigned long entries;
129 unsigned long hits; /* At top of linked list */
130 unsigned long partials; /* Not at top of linked list */
131 unsigned long misses; /* Not in table */
132 HashBucketPtr buckets[HASH_SIZE];
135 } HashTable, *HashTablePtr;
138 extern void *N(HashCreate)(void);
139 extern int N(HashDestroy)(void *t);
140 extern int N(HashLookup)(void *t, unsigned long key, unsigned long *value);
141 extern int N(HashInsert)(void *t, unsigned long key, unsigned long value);
142 extern int N(HashDelete)(void *t, unsigned long key);
145 static unsigned long HashHash(unsigned long key)
147 unsigned long hash = 0;
148 unsigned long tmp = key;
150 static unsigned long scatter[256];
155 HASH_RANDOM_INIT(37);
156 for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
162 hash = (hash << 1) + scatter[tmp & 0xff];
168 printf( "Hash(%d) = %d\n", key, hash);
173 void *N(HashCreate)(void)
178 table = HASH_ALLOC(sizeof(*table));
179 if (!table) return NULL;
180 table->magic = HASH_MAGIC;
186 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
190 int N(HashDestroy)(void *t)
192 HashTablePtr table = (HashTablePtr)t;
193 HashBucketPtr bucket;
197 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
199 for (i = 0; i < HASH_SIZE; i++) {
200 for (bucket = table->buckets[i]; bucket;) {
210 /* Find the bucket and organize the list so that this bucket is at the
213 static HashBucketPtr HashFind(HashTablePtr table,
214 unsigned long key, unsigned long *h)
216 unsigned long hash = HashHash(key);
217 HashBucketPtr prev = NULL;
218 HashBucketPtr bucket;
222 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
223 if (bucket->key == key) {
226 prev->next = bucket->next;
227 bucket->next = table->buckets[hash];
228 table->buckets[hash] = bucket;
241 int N(HashLookup)(void *t, unsigned long key, void **value)
243 HashTablePtr table = (HashTablePtr)t;
244 HashBucketPtr bucket;
246 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
248 bucket = HashFind(table, key, NULL);
249 if (!bucket) return 1; /* Not found */
250 *value = bucket->value;
251 return 0; /* Found */
254 int N(HashInsert)(void *t, unsigned long key, void *value)
256 HashTablePtr table = (HashTablePtr)t;
257 HashBucketPtr bucket;
260 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
262 if (HashFind(table, key, &hash)) return 1; /* Already in table */
264 bucket = HASH_ALLOC(sizeof(*bucket));
265 if (!bucket) return -1; /* Error */
267 bucket->value = value;
268 bucket->next = table->buckets[hash];
269 table->buckets[hash] = bucket;
271 printf("Inserted %d at %d/%p\n", key, hash, bucket);
273 return 0; /* Added to table */
276 int N(HashDelete)(void *t, unsigned long key)
278 HashTablePtr table = (HashTablePtr)t;
280 HashBucketPtr bucket;
282 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
284 bucket = HashFind(table, key, &hash);
286 if (!bucket) return 1; /* Not found */
288 table->buckets[hash] = bucket->next;
293 int N(HashNext)(void *t, unsigned long *key, void **value)
295 HashTablePtr table = (HashTablePtr)t;
297 for (; table->p0 < HASH_SIZE;
298 ++table->p0, table->p1 = table->buckets[table->p0]) {
300 *key = table->p1->key;
301 *value = table->p1->value;
302 table->p1 = table->p1->next;
309 int N(HashFirst)(void *t, unsigned long *key, void **value)
311 HashTablePtr table = (HashTablePtr)t;
313 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
316 table->p1 = table->buckets[0];
317 return N(HashNext)(table, key, value);
321 #define DIST_LIMIT 10
322 static int dist[DIST_LIMIT];
324 static void clear_dist(void) {
327 for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
330 static int count_entries(HashBucketPtr bucket)
334 for (; bucket; bucket = bucket->next) ++count;
338 static void update_dist(int count)
340 if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
344 static void compute_dist(HashTablePtr table)
347 HashBucketPtr bucket;
349 printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
350 table->entries, table->hits, table->partials, table->misses);
352 for (i = 0; i < HASH_SIZE; i++) {
353 bucket = table->buckets[i];
354 update_dist(count_entries(bucket));
356 for (i = 0; i < DIST_LIMIT; i++) {
357 if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
358 else printf("other %10d\n", dist[i]);
362 static void check_table(HashTablePtr table,
363 unsigned long key, unsigned long value)
365 unsigned long retval = 0;
366 int retcode = N(HashLookup)(table, key, &retval);
370 printf("Bad magic = 0x%08lx:"
371 " key = %lu, expected = %lu, returned = %lu\n",
372 table->magic, key, value, retval);
375 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
380 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
384 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
385 retcode, key, value, retval);
395 printf("\n***** 256 consecutive integers ****\n");
396 table = N(HashCreate)();
397 for (i = 0; i < 256; i++) N(HashInsert)(table, i, i);
398 for (i = 0; i < 256; i++) check_table(table, i, i);
399 for (i = 256; i >= 0; i--) check_table(table, i, i);
401 N(HashDestroy)(table);
403 printf("\n***** 1024 consecutive integers ****\n");
404 table = N(HashCreate)();
405 for (i = 0; i < 1024; i++) N(HashInsert)(table, i, i);
406 for (i = 0; i < 1024; i++) check_table(table, i, i);
407 for (i = 1024; i >= 0; i--) check_table(table, i, i);
409 N(HashDestroy)(table);
411 printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
412 table = N(HashCreate)();
413 for (i = 0; i < 1024; i++) N(HashInsert)(table, i*4096, i);
414 for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
415 for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
417 N(HashDestroy)(table);
419 printf("\n***** 1024 random integers ****\n");
420 table = N(HashCreate)();
422 for (i = 0; i < 1024; i++) N(HashInsert)(table, random(), i);
424 for (i = 0; i < 1024; i++) check_table(table, random(), i);
426 for (i = 0; i < 1024; i++) check_table(table, random(), i);
428 N(HashDestroy)(table);
430 printf("\n***** 5000 random integers ****\n");
431 table = N(HashCreate)();
433 for (i = 0; i < 5000; i++) N(HashInsert)(table, random(), i);
435 for (i = 0; i < 5000; i++) check_table(table, random(), i);
437 for (i = 0; i < 5000; i++) check_table(table, random(), i);
439 N(HashDestroy)(table);