blob: f287e61fb2b7a0051abb7b610e296d1cf0182484 [file] [log] [blame]
Daryll Straussb3a57661999-12-05 01:19:48 +00001/* xf86drmHash.c -- Small hash table support for integer -> integer mapping
2 * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
Daryll Straussb3a57661999-12-05 01:19:48 +00003 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
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:
Gareth Hughese2b2bff2001-03-13 00:22:05 +000013 *
Daryll Straussb3a57661999-12-05 01:19:48 +000014 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
Gareth Hughese2b2bff2001-03-13 00:22:05 +000017 *
Daryll Straussb3a57661999-12-05 01:19:48 +000018 * 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.
Gareth Hughese2b2bff2001-03-13 00:22:05 +000025 *
Rik Faith1c8b2b52000-06-13 14:22:03 +000026 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27 *
Daryll Straussb3a57661999-12-05 01:19:48 +000028 * DESCRIPTION
29 *
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:
34 *
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
38 * resolution.
39 *
40 * 2) The hash computation uses a table of random integers [Hanson97,
41 * pp. 39-41].
42 *
43 * FUTURE ENHANCEMENTS
44 *
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
55 * implementation.
56 *
57 * REFERENCES
58 *
59 * [Hanson97] David R. Hanson. C Interfaces and Implementations:
60 * Techniques for Creating Reusable Software. Reading, Massachusetts:
61 * Addison-Wesley, 1997.
62 *
63 * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
64 * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
65 *
66 * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
67 * 1988, pp. 446-457.
68 *
69 */
70
George Sapountzisb69b4262007-04-26 14:15:55 +030071#include <stdio.h>
72#include <stdlib.h>
Adam Jacksonf28dddb2005-11-30 03:51:46 +000073
Emil Velikov79f9cf32015-03-22 19:41:12 +000074#include "xf86drm.h"
75#include "xf86drmHash.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000076
Daryll Straussb3a57661999-12-05 01:19:48 +000077#define HASH_MAGIC 0xdeadbeef
Daryll Straussb3a57661999-12-05 01:19:48 +000078
79static unsigned long HashHash(unsigned long key)
80{
81 unsigned long hash = 0;
82 unsigned long tmp = key;
83 static int init = 0;
84 static unsigned long scatter[256];
85 int i;
86
87 if (!init) {
Emil Velikov79f9cf32015-03-22 19:41:12 +000088 void *state;
89 state = drmRandomCreate(37);
90 for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
91 drmRandomDestroy(state);
Daryll Straussb3a57661999-12-05 01:19:48 +000092 ++init;
93 }
94
95 while (tmp) {
96 hash = (hash << 1) + scatter[tmp & 0xff];
97 tmp >>= 8;
98 }
99
100 hash %= HASH_SIZE;
Emil Velikov03cd9df2015-03-26 21:19:26 +0000101#if DEBUG
Emil Velikov37dc0a12015-03-22 21:52:16 +0000102 printf( "Hash(%lu) = %lu\n", key, hash);
Daryll Straussb3a57661999-12-05 01:19:48 +0000103#endif
104 return hash;
105}
106
Adam Jackson22e41ef2006-02-20 23:09:00 +0000107void *drmHashCreate(void)
Daryll Straussb3a57661999-12-05 01:19:48 +0000108{
109 HashTablePtr table;
110 int i;
111
Emil Velikov79f9cf32015-03-22 19:41:12 +0000112 table = drmMalloc(sizeof(*table));
Daryll Straussb3a57661999-12-05 01:19:48 +0000113 if (!table) return NULL;
114 table->magic = HASH_MAGIC;
115 table->entries = 0;
116 table->hits = 0;
117 table->partials = 0;
118 table->misses = 0;
119
120 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
121 return table;
122}
123
Adam Jackson22e41ef2006-02-20 23:09:00 +0000124int drmHashDestroy(void *t)
Daryll Straussb3a57661999-12-05 01:19:48 +0000125{
126 HashTablePtr table = (HashTablePtr)t;
127 HashBucketPtr bucket;
128 HashBucketPtr next;
129 int i;
130
131 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000132
Daryll Straussb3a57661999-12-05 01:19:48 +0000133 for (i = 0; i < HASH_SIZE; i++) {
134 for (bucket = table->buckets[i]; bucket;) {
135 next = bucket->next;
Emil Velikov79f9cf32015-03-22 19:41:12 +0000136 drmFree(bucket);
Daryll Straussb3a57661999-12-05 01:19:48 +0000137 bucket = next;
138 }
139 }
Emil Velikov79f9cf32015-03-22 19:41:12 +0000140 drmFree(table);
Daryll Straussb3a57661999-12-05 01:19:48 +0000141 return 0;
142}
143
144/* Find the bucket and organize the list so that this bucket is at the
145 top. */
146
147static HashBucketPtr HashFind(HashTablePtr table,
148 unsigned long key, unsigned long *h)
149{
150 unsigned long hash = HashHash(key);
151 HashBucketPtr prev = NULL;
152 HashBucketPtr bucket;
153
154 if (h) *h = hash;
155
156 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
157 if (bucket->key == key) {
158 if (prev) {
159 /* Organize */
160 prev->next = bucket->next;
161 bucket->next = table->buckets[hash];
162 table->buckets[hash] = bucket;
163 ++table->partials;
164 } else {
165 ++table->hits;
166 }
167 return bucket;
168 }
169 prev = bucket;
170 }
171 ++table->misses;
172 return NULL;
173}
174
Adam Jackson22e41ef2006-02-20 23:09:00 +0000175int drmHashLookup(void *t, unsigned long key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000176{
177 HashTablePtr table = (HashTablePtr)t;
178 HashBucketPtr bucket;
179
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000180 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
181
Daryll Straussb3a57661999-12-05 01:19:48 +0000182 bucket = HashFind(table, key, NULL);
183 if (!bucket) return 1; /* Not found */
184 *value = bucket->value;
185 return 0; /* Found */
186}
187
Adam Jackson22e41ef2006-02-20 23:09:00 +0000188int drmHashInsert(void *t, unsigned long key, void *value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000189{
190 HashTablePtr table = (HashTablePtr)t;
191 HashBucketPtr bucket;
192 unsigned long hash;
193
194 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000195
Daryll Straussb3a57661999-12-05 01:19:48 +0000196 if (HashFind(table, key, &hash)) return 1; /* Already in table */
197
Emil Velikov79f9cf32015-03-22 19:41:12 +0000198 bucket = drmMalloc(sizeof(*bucket));
Daryll Straussb3a57661999-12-05 01:19:48 +0000199 if (!bucket) return -1; /* Error */
200 bucket->key = key;
201 bucket->value = value;
202 bucket->next = table->buckets[hash];
203 table->buckets[hash] = bucket;
Emil Velikov03cd9df2015-03-26 21:19:26 +0000204#if DEBUG
Emil Velikov37dc0a12015-03-22 21:52:16 +0000205 printf("Inserted %lu at %lu/%p\n", key, hash, bucket);
Daryll Straussb3a57661999-12-05 01:19:48 +0000206#endif
207 return 0; /* Added to table */
208}
209
Adam Jackson22e41ef2006-02-20 23:09:00 +0000210int drmHashDelete(void *t, unsigned long key)
Daryll Straussb3a57661999-12-05 01:19:48 +0000211{
212 HashTablePtr table = (HashTablePtr)t;
213 unsigned long hash;
214 HashBucketPtr bucket;
215
216 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000217
Daryll Straussb3a57661999-12-05 01:19:48 +0000218 bucket = HashFind(table, key, &hash);
219
220 if (!bucket) return 1; /* Not found */
221
222 table->buckets[hash] = bucket->next;
Emil Velikov79f9cf32015-03-22 19:41:12 +0000223 drmFree(bucket);
Daryll Straussb3a57661999-12-05 01:19:48 +0000224 return 0;
225}
226
Adam Jackson22e41ef2006-02-20 23:09:00 +0000227int drmHashNext(void *t, unsigned long *key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000228{
229 HashTablePtr table = (HashTablePtr)t;
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000230
Adam Jackson14d12192006-03-15 01:02:54 +0000231 while (table->p0 < HASH_SIZE) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000232 if (table->p1) {
233 *key = table->p1->key;
234 *value = table->p1->value;
235 table->p1 = table->p1->next;
236 return 1;
237 }
Adam Jackson14d12192006-03-15 01:02:54 +0000238 table->p1 = table->buckets[table->p0];
239 ++table->p0;
Daryll Straussb3a57661999-12-05 01:19:48 +0000240 }
241 return 0;
242}
243
Adam Jackson22e41ef2006-02-20 23:09:00 +0000244int drmHashFirst(void *t, unsigned long *key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000245{
246 HashTablePtr table = (HashTablePtr)t;
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000247
Daryll Straussb3a57661999-12-05 01:19:48 +0000248 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
249
250 table->p0 = 0;
251 table->p1 = table->buckets[0];
Adam Jackson22e41ef2006-02-20 23:09:00 +0000252 return drmHashNext(table, key, value);
Daryll Straussb3a57661999-12-05 01:19:48 +0000253}