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
75 #include "xf86drmHash.h"
77 #define HASH_MAGIC 0xdeadbeef
79 static unsigned long HashHash(unsigned long key)
81 unsigned long hash = 0;
82 unsigned long tmp = key;
84 static unsigned long scatter[256];
89 state = drmRandomCreate(37);
90 for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
91 drmRandomDestroy(state);
96 hash = (hash << 1) + scatter[tmp & 0xff];
104 void *drmHashCreate(void)
109 table = drmMalloc(sizeof(*table));
110 if (!table) return NULL;
111 table->magic = HASH_MAGIC;
117 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
121 int drmHashDestroy(void *t)
123 HashTablePtr table = (HashTablePtr)t;
124 HashBucketPtr bucket;
128 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
130 for (i = 0; i < HASH_SIZE; i++) {
131 for (bucket = table->buckets[i]; bucket;) {
141 /* Find the bucket and organize the list so that this bucket is at the
144 static HashBucketPtr HashFind(HashTablePtr table,
145 unsigned long key, unsigned long *h)
147 unsigned long hash = HashHash(key);
148 HashBucketPtr prev = NULL;
149 HashBucketPtr bucket;
153 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
154 if (bucket->key == key) {
157 prev->next = bucket->next;
158 bucket->next = table->buckets[hash];
159 table->buckets[hash] = bucket;
172 int drmHashLookup(void *t, unsigned long key, void **value)
174 HashTablePtr table = (HashTablePtr)t;
175 HashBucketPtr bucket;
177 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
179 bucket = HashFind(table, key, NULL);
180 if (!bucket) return 1; /* Not found */
181 *value = bucket->value;
182 return 0; /* Found */
185 int drmHashInsert(void *t, unsigned long key, void *value)
187 HashTablePtr table = (HashTablePtr)t;
188 HashBucketPtr bucket;
191 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
193 if (HashFind(table, key, &hash)) return 1; /* Already in table */
195 bucket = drmMalloc(sizeof(*bucket));
196 if (!bucket) return -1; /* Error */
198 bucket->value = value;
199 bucket->next = table->buckets[hash];
200 table->buckets[hash] = bucket;
201 return 0; /* Added to table */
204 int drmHashDelete(void *t, unsigned long key)
206 HashTablePtr table = (HashTablePtr)t;
208 HashBucketPtr bucket;
210 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
212 bucket = HashFind(table, key, &hash);
214 if (!bucket) return 1; /* Not found */
216 table->buckets[hash] = bucket->next;
221 int drmHashNext(void *t, unsigned long *key, void **value)
223 HashTablePtr table = (HashTablePtr)t;
225 while (table->p0 < HASH_SIZE) {
227 *key = table->p1->key;
228 *value = table->p1->value;
229 table->p1 = table->p1->next;
232 table->p1 = table->buckets[table->p0];
238 int drmHashFirst(void *t, unsigned long *key, void **value)
240 HashTablePtr table = (HashTablePtr)t;
242 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
245 table->p1 = table->buckets[0];
246 return drmHashNext(table, key, value);