blob: 2cf2b80ed9e02310f0ff844ca48f304759a52195 [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
Lucas De Marchi26f9ce52018-09-13 15:42:26 -070074#include "libdrm_macros.h"
Emil Velikov79f9cf32015-03-22 19:41:12 +000075#include "xf86drm.h"
76#include "xf86drmHash.h"
Daryll Straussb3a57661999-12-05 01:19:48 +000077
Daryll Straussb3a57661999-12-05 01:19:48 +000078#define HASH_MAGIC 0xdeadbeef
Daryll Straussb3a57661999-12-05 01:19:48 +000079
80static unsigned long HashHash(unsigned long key)
81{
82 unsigned long hash = 0;
83 unsigned long tmp = key;
84 static int init = 0;
85 static unsigned long scatter[256];
86 int i;
87
88 if (!init) {
Emil Velikov79f9cf32015-03-22 19:41:12 +000089 void *state;
90 state = drmRandomCreate(37);
91 for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
92 drmRandomDestroy(state);
Daryll Straussb3a57661999-12-05 01:19:48 +000093 ++init;
94 }
95
96 while (tmp) {
97 hash = (hash << 1) + scatter[tmp & 0xff];
98 tmp >>= 8;
99 }
100
101 hash %= HASH_SIZE;
Daryll Straussb3a57661999-12-05 01:19:48 +0000102 return hash;
103}
104
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700105drm_public void *drmHashCreate(void)
Daryll Straussb3a57661999-12-05 01:19:48 +0000106{
107 HashTablePtr table;
Daryll Straussb3a57661999-12-05 01:19:48 +0000108
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;
Daryll Straussb3a57661999-12-05 01:19:48 +0000112
Daryll Straussb3a57661999-12-05 01:19:48 +0000113 return table;
114}
115
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700116drm_public int drmHashDestroy(void *t)
Daryll Straussb3a57661999-12-05 01:19:48 +0000117{
118 HashTablePtr table = (HashTablePtr)t;
119 HashBucketPtr bucket;
120 HashBucketPtr next;
121 int i;
122
123 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000124
Daryll Straussb3a57661999-12-05 01:19:48 +0000125 for (i = 0; i < HASH_SIZE; i++) {
126 for (bucket = table->buckets[i]; bucket;) {
127 next = bucket->next;
Emil Velikov79f9cf32015-03-22 19:41:12 +0000128 drmFree(bucket);
Daryll Straussb3a57661999-12-05 01:19:48 +0000129 bucket = next;
130 }
131 }
Emil Velikov79f9cf32015-03-22 19:41:12 +0000132 drmFree(table);
Daryll Straussb3a57661999-12-05 01:19:48 +0000133 return 0;
134}
135
136/* Find the bucket and organize the list so that this bucket is at the
137 top. */
138
139static HashBucketPtr HashFind(HashTablePtr table,
140 unsigned long key, unsigned long *h)
141{
142 unsigned long hash = HashHash(key);
143 HashBucketPtr prev = NULL;
144 HashBucketPtr bucket;
145
146 if (h) *h = hash;
147
148 for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
149 if (bucket->key == key) {
150 if (prev) {
151 /* Organize */
152 prev->next = bucket->next;
153 bucket->next = table->buckets[hash];
154 table->buckets[hash] = bucket;
155 ++table->partials;
156 } else {
157 ++table->hits;
158 }
159 return bucket;
160 }
161 prev = bucket;
162 }
163 ++table->misses;
164 return NULL;
165}
166
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700167drm_public int drmHashLookup(void *t, unsigned long key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000168{
169 HashTablePtr table = (HashTablePtr)t;
170 HashBucketPtr bucket;
171
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000172 if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
173
Daryll Straussb3a57661999-12-05 01:19:48 +0000174 bucket = HashFind(table, key, NULL);
175 if (!bucket) return 1; /* Not found */
176 *value = bucket->value;
177 return 0; /* Found */
178}
179
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700180drm_public int drmHashInsert(void *t, unsigned long key, void *value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000181{
182 HashTablePtr table = (HashTablePtr)t;
183 HashBucketPtr bucket;
184 unsigned long hash;
185
186 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000187
Daryll Straussb3a57661999-12-05 01:19:48 +0000188 if (HashFind(table, key, &hash)) return 1; /* Already in table */
189
Emil Velikov79f9cf32015-03-22 19:41:12 +0000190 bucket = drmMalloc(sizeof(*bucket));
Daryll Straussb3a57661999-12-05 01:19:48 +0000191 if (!bucket) return -1; /* Error */
192 bucket->key = key;
193 bucket->value = value;
194 bucket->next = table->buckets[hash];
195 table->buckets[hash] = bucket;
Daryll Straussb3a57661999-12-05 01:19:48 +0000196 return 0; /* Added to table */
197}
198
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700199drm_public int drmHashDelete(void *t, unsigned long key)
Daryll Straussb3a57661999-12-05 01:19:48 +0000200{
201 HashTablePtr table = (HashTablePtr)t;
202 unsigned long hash;
203 HashBucketPtr bucket;
204
205 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000206
Daryll Straussb3a57661999-12-05 01:19:48 +0000207 bucket = HashFind(table, key, &hash);
208
209 if (!bucket) return 1; /* Not found */
210
211 table->buckets[hash] = bucket->next;
Emil Velikov79f9cf32015-03-22 19:41:12 +0000212 drmFree(bucket);
Daryll Straussb3a57661999-12-05 01:19:48 +0000213 return 0;
214}
215
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700216drm_public int drmHashNext(void *t, unsigned long *key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000217{
218 HashTablePtr table = (HashTablePtr)t;
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000219
Adam Jackson14d12192006-03-15 01:02:54 +0000220 while (table->p0 < HASH_SIZE) {
Daryll Straussb3a57661999-12-05 01:19:48 +0000221 if (table->p1) {
222 *key = table->p1->key;
223 *value = table->p1->value;
224 table->p1 = table->p1->next;
225 return 1;
226 }
Adam Jackson14d12192006-03-15 01:02:54 +0000227 table->p1 = table->buckets[table->p0];
228 ++table->p0;
Daryll Straussb3a57661999-12-05 01:19:48 +0000229 }
230 return 0;
231}
232
Lucas De Marchi26f9ce52018-09-13 15:42:26 -0700233drm_public int drmHashFirst(void *t, unsigned long *key, void **value)
Daryll Straussb3a57661999-12-05 01:19:48 +0000234{
235 HashTablePtr table = (HashTablePtr)t;
Gareth Hughese2b2bff2001-03-13 00:22:05 +0000236
Daryll Straussb3a57661999-12-05 01:19:48 +0000237 if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
238
239 table->p0 = 0;
240 table->p1 = table->buckets[0];
Adam Jackson22e41ef2006-02-20 23:09:00 +0000241 return drmHashNext(table, key, value);
Daryll Straussb3a57661999-12-05 01:19:48 +0000242}