blob: b2fa414e5305f94cd12d5d284ac41be4d7f3377a [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;
Daryll Straussb3a57661999-12-05 01:19:48 +0000101 return hash;
102}
103
Adam Jackson22e41ef2006-02-20 23:09:00 +0000104void *drmHashCreate(void)
Daryll Straussb3a57661999-12-05 01:19:48 +0000105{
106 HashTablePtr table;
107 int i;
108
Emil Velikov79f9cf32015-03-22 19:41:12 +0000109 table = drmMalloc(sizeof(*table));
Daryll Straussb3a57661999-12-05 01:19:48 +0000110 if (!table) return NULL;
111 table->magic = HASH_MAGIC;
112 table->entries = 0;
113 table->hits = 0;
114 table->partials = 0;
115 table->misses = 0;
116
117 for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
118 return table;
119}
120
Adam Jackson22e41ef2006-02-20 23:09:00 +0000121int drmHashDestroy(void *t)
Daryll Straussb3a57661999-12-05 01:19:48 +0000122{
123 HashTablePtr table = (HashTablePtr)t;
124 HashBucketPtr bucket;
125 HashBucketPtr next;
126 int i;
127
128 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000129
Daryll Straussb3a57661999-12-05 01:19:48 +0000130 for (i = 0; i < HASH_SIZE; i++) {
131 for (bucket = table->buckets[i]; bucket;) {
132 next = bucket->next;
Emil Velikov79f9cf32015-03-22 19:41:12 +0000133 drmFree(bucket);
Daryll Straussb3a57661999-12-05 01:19:48 +0000134 bucket = next;
135 }
136 }
Emil Velikov79f9cf32015-03-22 19:41:12 +0000137 drmFree(table);
Daryll Straussb3a57661999-12-05 01:19:48 +0000138 return 0;
139}
140
141/* Find the bucket and organize the list so that this bucket is at the
142 top. */
143
144static HashBucketPtr HashFind(HashTablePtr table,
145 unsigned long key, unsigned long *h)
146{
147 unsigned long hash = HashHash(key);
148 HashBucketPtr prev = NULL;
149 HashBucketPtr bucket;
150
151 if (h) *h = hash;
152
153 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
154 if (bucket->key == key) {
155 if (prev) {
156 /* Organize */
157 prev->next = bucket->next;
158 bucket->next = table->buckets[hash];
159 table->buckets[hash] = bucket;
160 ++table->partials;
161 } else {
162 ++table->hits;
163 }
164 return bucket;
165 }
166 prev = bucket;
167 }
168 ++table->misses;
169 return NULL;
170}
171
Adam Jackson22e41ef2006-02-20 23:09:00 +0000172int drmHashLookup(void *t, unsigned long key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000173{
174 HashTablePtr table = (HashTablePtr)t;
175 HashBucketPtr bucket;
176
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000177 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
178
Daryll Straussb3a57661999-12-05 01:19:48 +0000179 bucket = HashFind(table, key, NULL);
180 if (!bucket) return 1; /* Not found */
181 *value = bucket->value;
182 return 0; /* Found */
183}
184
Adam Jackson22e41ef2006-02-20 23:09:00 +0000185int drmHashInsert(void *t, unsigned long key, void *value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000186{
187 HashTablePtr table = (HashTablePtr)t;
188 HashBucketPtr bucket;
189 unsigned long hash;
190
191 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000192
Daryll Straussb3a57661999-12-05 01:19:48 +0000193 if (HashFind(table, key, &hash)) return 1; /* Already in table */
194
Emil Velikov79f9cf32015-03-22 19:41:12 +0000195 bucket = drmMalloc(sizeof(*bucket));
Daryll Straussb3a57661999-12-05 01:19:48 +0000196 if (!bucket) return -1; /* Error */
197 bucket->key = key;
198 bucket->value = value;
199 bucket->next = table->buckets[hash];
200 table->buckets[hash] = bucket;
Daryll Straussb3a57661999-12-05 01:19:48 +0000201 return 0; /* Added to table */
202}
203
Adam Jackson22e41ef2006-02-20 23:09:00 +0000204int drmHashDelete(void *t, unsigned long key)
Daryll Straussb3a57661999-12-05 01:19:48 +0000205{
206 HashTablePtr table = (HashTablePtr)t;
207 unsigned long hash;
208 HashBucketPtr bucket;
209
210 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000211
Daryll Straussb3a57661999-12-05 01:19:48 +0000212 bucket = HashFind(table, key, &hash);
213
214 if (!bucket) return 1; /* Not found */
215
216 table->buckets[hash] = bucket->next;
Emil Velikov79f9cf32015-03-22 19:41:12 +0000217 drmFree(bucket);
Daryll Straussb3a57661999-12-05 01:19:48 +0000218 return 0;
219}
220
Adam Jackson22e41ef2006-02-20 23:09:00 +0000221int drmHashNext(void *t, unsigned long *key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000222{
223 HashTablePtr table = (HashTablePtr)t;
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000224
Adam Jackson14d12192006-03-15 01:02:54 +0000225 while (table->p0 < HASH_SIZE) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000226 if (table->p1) {
227 *key = table->p1->key;
228 *value = table->p1->value;
229 table->p1 = table->p1->next;
230 return 1;
231 }
Adam Jackson14d12192006-03-15 01:02:54 +0000232 table->p1 = table->buckets[table->p0];
233 ++table->p0;
Daryll Straussb3a57661999-12-05 01:19:48 +0000234 }
235 return 0;
236}
237
Adam Jackson22e41ef2006-02-20 23:09:00 +0000238int drmHashFirst(void *t, unsigned long *key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000239{
240 HashTablePtr table = (HashTablePtr)t;
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000241
Daryll Straussb3a57661999-12-05 01:19:48 +0000242 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
243
244 table->p0 = 0;
245 table->p1 = table->buckets[0];
Adam Jackson22e41ef2006-02-20 23:09:00 +0000246 return drmHashNext(table, key, value);
Daryll Straussb3a57661999-12-05 01:19:48 +0000247}