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
82 # include "xf86_ansic.h"
89 #define HASH_MAGIC 0xdeadbeef
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
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()
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)
111 typedef struct HashBucket
{
114 struct HashBucket
*next
;
115 } HashBucket
, *HashBucketPtr
;
117 typedef struct HashTable
{
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
];
126 } HashTable
, *HashTablePtr
;
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
);
136 static unsigned long HashHash(unsigned long key
)
138 unsigned long hash
= 0;
139 unsigned long tmp
= key
;
141 static unsigned long scatter
[256];
146 HASH_RANDOM_INIT(37);
147 for (i
= 0; i
< 256; i
++) scatter
[i
] = HASH_RANDOM
;
152 hash
= (hash
<< 1) + scatter
[tmp
& 0xff];
158 printf( "Hash(%d) = %d\n", key
, hash
);
163 void *drmHashCreate(void)
168 table
= HASH_ALLOC(sizeof(*table
));
169 if (!table
) return NULL
;
170 table
->magic
= HASH_MAGIC
;
176 for (i
= 0; i
< HASH_SIZE
; i
++) table
->buckets
[i
] = NULL
;
180 int drmHashDestroy(void *t
)
182 HashTablePtr table
= (HashTablePtr
)t
;
183 HashBucketPtr bucket
;
187 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
189 for (i
= 0; i
< HASH_SIZE
; i
++) {
190 for (bucket
= table
->buckets
[i
]; bucket
;) {
200 /* Find the bucket and organize the list so that this bucket is at the
203 static HashBucketPtr
HashFind(HashTablePtr table
,
204 unsigned long key
, unsigned long *h
)
206 unsigned long hash
= HashHash(key
);
207 HashBucketPtr prev
= NULL
;
208 HashBucketPtr bucket
;
212 for (bucket
= table
->buckets
[hash
]; bucket
; bucket
= bucket
->next
) {
213 if (bucket
->key
== key
) {
216 prev
->next
= bucket
->next
;
217 bucket
->next
= table
->buckets
[hash
];
218 table
->buckets
[hash
] = bucket
;
231 int drmHashLookup(void *t
, unsigned long key
, void **value
)
233 HashTablePtr table
= (HashTablePtr
)t
;
234 HashBucketPtr bucket
;
236 if (!table
|| table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
238 bucket
= HashFind(table
, key
, NULL
);
239 if (!bucket
) return 1; /* Not found */
240 *value
= bucket
->value
;
241 return 0; /* Found */
244 int drmHashInsert(void *t
, unsigned long key
, void *value
)
246 HashTablePtr table
= (HashTablePtr
)t
;
247 HashBucketPtr bucket
;
250 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
252 if (HashFind(table
, key
, &hash
)) return 1; /* Already in table */
254 bucket
= HASH_ALLOC(sizeof(*bucket
));
255 if (!bucket
) return -1; /* Error */
257 bucket
->value
= value
;
258 bucket
->next
= table
->buckets
[hash
];
259 table
->buckets
[hash
] = bucket
;
261 printf("Inserted %d at %d/%p\n", key
, hash
, bucket
);
263 return 0; /* Added to table */
266 int drmHashDelete(void *t
, unsigned long key
)
268 HashTablePtr table
= (HashTablePtr
)t
;
270 HashBucketPtr bucket
;
272 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
274 bucket
= HashFind(table
, key
, &hash
);
276 if (!bucket
) return 1; /* Not found */
278 table
->buckets
[hash
] = bucket
->next
;
283 int drmHashNext(void *t
, unsigned long *key
, void **value
)
285 HashTablePtr table
= (HashTablePtr
)t
;
287 for (; table
->p0
< HASH_SIZE
;
288 ++table
->p0
, table
->p1
= table
->buckets
[table
->p0
]) {
290 *key
= table
->p1
->key
;
291 *value
= table
->p1
->value
;
292 table
->p1
= table
->p1
->next
;
299 int drmHashFirst(void *t
, unsigned long *key
, void **value
)
301 HashTablePtr table
= (HashTablePtr
)t
;
303 if (table
->magic
!= HASH_MAGIC
) return -1; /* Bad magic */
306 table
->p1
= table
->buckets
[0];
307 return drmHashNext(table
, key
, value
);
311 #define DIST_LIMIT 10
312 static int dist
[DIST_LIMIT
];
314 static void clear_dist(void) {
317 for (i
= 0; i
< DIST_LIMIT
; i
++) dist
[i
] = 0;
320 static int count_entries(HashBucketPtr bucket
)
324 for (; bucket
; bucket
= bucket
->next
) ++count
;
328 static void update_dist(int count
)
330 if (count
>= DIST_LIMIT
) ++dist
[DIST_LIMIT
-1];
334 static void compute_dist(HashTablePtr table
)
337 HashBucketPtr bucket
;
339 printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
340 table
->entries
, table
->hits
, table
->partials
, table
->misses
);
342 for (i
= 0; i
< HASH_SIZE
; i
++) {
343 bucket
= table
->buckets
[i
];
344 update_dist(count_entries(bucket
));
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
]);
352 static void check_table(HashTablePtr table
,
353 unsigned long key
, unsigned long value
)
355 unsigned long retval
= 0;
356 int retcode
= drmHashLookup(table
, key
, &retval
);
360 printf("Bad magic = 0x%08lx:"
361 " key = %lu, expected = %lu, returned = %lu\n",
362 table
->magic
, key
, value
, retval
);
365 printf("Not found: key = %lu, expected = %lu returned = %lu\n",
370 printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
374 printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
375 retcode
, key
, value
, retval
);
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
);
391 drmHashDestroy(table
);
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
);
399 drmHashDestroy(table
);
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
);
407 drmHashDestroy(table
);
409 printf("\n***** 1024 random integers ****\n");
410 table
= drmHashCreate();
412 for (i
= 0; i
< 1024; i
++) drmHashInsert(table
, random(), i
);
414 for (i
= 0; i
< 1024; i
++) check_table(table
, random(), i
);
416 for (i
= 0; i
< 1024; i
++) check_table(table
, random(), i
);
418 drmHashDestroy(table
);
420 printf("\n***** 5000 random integers ****\n");
421 table
= drmHashCreate();
423 for (i
= 0; i
< 5000; i
++) drmHashInsert(table
, random(), i
);
425 for (i
= 0; i
< 5000; i
++) check_table(table
, random(), i
);
427 for (i
= 0; i
< 5000; i
++) check_table(table
, random(), i
);
429 drmHashDestroy(table
);