1*4882a593Smuzhiyun /* xf86drmHash.c -- Small hash table support for integer -> integer mapping
2*4882a593Smuzhiyun * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5*4882a593Smuzhiyun * All Rights Reserved.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
8*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
9*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
10*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
12*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
15*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
16*4882a593Smuzhiyun * Software.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21*4882a593Smuzhiyun * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * DESCRIPTION
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * This file contains a straightforward implementation of a fixed-sized
31*4882a593Smuzhiyun * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
32*4882a593Smuzhiyun * collision resolution. There are two potentially interesting things
33*4882a593Smuzhiyun * about this implementation:
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * 1) The table is power-of-two sized. Prime sized tables are more
36*4882a593Smuzhiyun * traditional, but do not have a significant advantage over power-of-two
37*4882a593Smuzhiyun * sized table, especially when double hashing is not used for collision
38*4882a593Smuzhiyun * resolution.
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * 2) The hash computation uses a table of random integers [Hanson97,
41*4882a593Smuzhiyun * pp. 39-41].
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * FUTURE ENHANCEMENTS
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * With a table size of 512, the current implementation is sufficient for a
46*4882a593Smuzhiyun * few hundred keys. Since this is well above the expected size of the
47*4882a593Smuzhiyun * tables for which this implementation was designed, the implementation of
48*4882a593Smuzhiyun * dynamic hash tables was postponed until the need arises. A common (and
49*4882a593Smuzhiyun * naive) approach to dynamic hash table implementation simply creates a
50*4882a593Smuzhiyun * new hash table when necessary, rehashes all the data into the new table,
51*4882a593Smuzhiyun * and destroys the old table. The approach in [Larson88] is superior in
52*4882a593Smuzhiyun * two ways: 1) only a portion of the table is expanded when needed,
53*4882a593Smuzhiyun * distributing the expansion cost over several insertions, and 2) portions
54*4882a593Smuzhiyun * of the table can be locked, enabling a scalable thread-safe
55*4882a593Smuzhiyun * implementation.
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * REFERENCES
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * [Hanson97] David R. Hanson. C Interfaces and Implementations:
60*4882a593Smuzhiyun * Techniques for Creating Reusable Software. Reading, Massachusetts:
61*4882a593Smuzhiyun * Addison-Wesley, 1997.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3:
64*4882a593Smuzhiyun * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April
67*4882a593Smuzhiyun * 1988, pp. 446-457.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #include <stdio.h>
72*4882a593Smuzhiyun #include <stdlib.h>
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #include "libdrm_macros.h"
75*4882a593Smuzhiyun #include "xf86drm.h"
76*4882a593Smuzhiyun #include "xf86drmHash.h"
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #define HASH_MAGIC 0xdeadbeef
79*4882a593Smuzhiyun
HashHash(unsigned long key)80*4882a593Smuzhiyun static unsigned long HashHash(unsigned long key)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun unsigned long hash = 0;
83*4882a593Smuzhiyun unsigned long tmp = key;
84*4882a593Smuzhiyun static int init = 0;
85*4882a593Smuzhiyun static unsigned long scatter[256];
86*4882a593Smuzhiyun int i;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (!init) {
89*4882a593Smuzhiyun void *state;
90*4882a593Smuzhiyun state = drmRandomCreate(37);
91*4882a593Smuzhiyun for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
92*4882a593Smuzhiyun drmRandomDestroy(state);
93*4882a593Smuzhiyun ++init;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun while (tmp) {
97*4882a593Smuzhiyun hash = (hash << 1) + scatter[tmp & 0xff];
98*4882a593Smuzhiyun tmp >>= 8;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun hash %= HASH_SIZE;
102*4882a593Smuzhiyun return hash;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
drmHashCreate(void)105*4882a593Smuzhiyun drm_public void *drmHashCreate(void)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun HashTablePtr table;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun table = drmMalloc(sizeof(*table));
110*4882a593Smuzhiyun if (!table) return NULL;
111*4882a593Smuzhiyun table->magic = HASH_MAGIC;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun return table;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
drmHashDestroy(void * t)116*4882a593Smuzhiyun drm_public int drmHashDestroy(void *t)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun HashTablePtr table = (HashTablePtr)t;
119*4882a593Smuzhiyun HashBucketPtr bucket;
120*4882a593Smuzhiyun HashBucketPtr next;
121*4882a593Smuzhiyun int i;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun for (i = 0; i < HASH_SIZE; i++) {
126*4882a593Smuzhiyun for (bucket = table->buckets[i]; bucket;) {
127*4882a593Smuzhiyun next = bucket->next;
128*4882a593Smuzhiyun drmFree(bucket);
129*4882a593Smuzhiyun bucket = next;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun drmFree(table);
133*4882a593Smuzhiyun return 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* Find the bucket and organize the list so that this bucket is at the
137*4882a593Smuzhiyun top. */
138*4882a593Smuzhiyun
HashFind(HashTablePtr table,unsigned long key,unsigned long * h)139*4882a593Smuzhiyun static HashBucketPtr HashFind(HashTablePtr table,
140*4882a593Smuzhiyun unsigned long key, unsigned long *h)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun unsigned long hash = HashHash(key);
143*4882a593Smuzhiyun HashBucketPtr prev = NULL;
144*4882a593Smuzhiyun HashBucketPtr bucket;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun if (h) *h = hash;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
149*4882a593Smuzhiyun if (bucket->key == key) {
150*4882a593Smuzhiyun if (prev) {
151*4882a593Smuzhiyun /* Organize */
152*4882a593Smuzhiyun prev->next = bucket->next;
153*4882a593Smuzhiyun bucket->next = table->buckets[hash];
154*4882a593Smuzhiyun table->buckets[hash] = bucket;
155*4882a593Smuzhiyun ++table->partials;
156*4882a593Smuzhiyun } else {
157*4882a593Smuzhiyun ++table->hits;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun return bucket;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun prev = bucket;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun ++table->misses;
164*4882a593Smuzhiyun return NULL;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
drmHashLookup(void * t,unsigned long key,void ** value)167*4882a593Smuzhiyun drm_public int drmHashLookup(void *t, unsigned long key, void **value)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun HashTablePtr table = (HashTablePtr)t;
170*4882a593Smuzhiyun HashBucketPtr bucket;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun bucket = HashFind(table, key, NULL);
175*4882a593Smuzhiyun if (!bucket) return 1; /* Not found */
176*4882a593Smuzhiyun *value = bucket->value;
177*4882a593Smuzhiyun return 0; /* Found */
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
drmHashInsert(void * t,unsigned long key,void * value)180*4882a593Smuzhiyun drm_public int drmHashInsert(void *t, unsigned long key, void *value)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun HashTablePtr table = (HashTablePtr)t;
183*4882a593Smuzhiyun HashBucketPtr bucket;
184*4882a593Smuzhiyun unsigned long hash;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun if (HashFind(table, key, &hash)) return 1; /* Already in table */
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun bucket = drmMalloc(sizeof(*bucket));
191*4882a593Smuzhiyun if (!bucket) return -1; /* Error */
192*4882a593Smuzhiyun bucket->key = key;
193*4882a593Smuzhiyun bucket->value = value;
194*4882a593Smuzhiyun bucket->next = table->buckets[hash];
195*4882a593Smuzhiyun table->buckets[hash] = bucket;
196*4882a593Smuzhiyun return 0; /* Added to table */
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
drmHashDelete(void * t,unsigned long key)199*4882a593Smuzhiyun drm_public int drmHashDelete(void *t, unsigned long key)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun HashTablePtr table = (HashTablePtr)t;
202*4882a593Smuzhiyun unsigned long hash;
203*4882a593Smuzhiyun HashBucketPtr bucket;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun bucket = HashFind(table, key, &hash);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (!bucket) return 1; /* Not found */
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun table->buckets[hash] = bucket->next;
212*4882a593Smuzhiyun drmFree(bucket);
213*4882a593Smuzhiyun return 0;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
drmHashNext(void * t,unsigned long * key,void ** value)216*4882a593Smuzhiyun drm_public int drmHashNext(void *t, unsigned long *key, void **value)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun HashTablePtr table = (HashTablePtr)t;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun while (table->p0 < HASH_SIZE) {
221*4882a593Smuzhiyun if (table->p1) {
222*4882a593Smuzhiyun *key = table->p1->key;
223*4882a593Smuzhiyun *value = table->p1->value;
224*4882a593Smuzhiyun table->p1 = table->p1->next;
225*4882a593Smuzhiyun return 1;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun table->p1 = table->buckets[table->p0];
228*4882a593Smuzhiyun ++table->p0;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun return 0;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
drmHashFirst(void * t,unsigned long * key,void ** value)233*4882a593Smuzhiyun drm_public int drmHashFirst(void *t, unsigned long *key, void **value)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun HashTablePtr table = (HashTablePtr)t;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun table->p0 = 0;
240*4882a593Smuzhiyun table->p1 = table->buckets[0];
241*4882a593Smuzhiyun return drmHashNext(table, key, value);
242*4882a593Smuzhiyun }
243