OSDN Git Service

autotools: make symbols hidden by default
[android-x86/external-libdrm.git] / xf86drmHash.c
1 /* xf86drmHash.c -- Small hash table support for integer -> integer mapping
2  * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
3  *
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:
13  *
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
16  * Software.
17  *
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.
25  *
26  * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27  *
28  * 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
71 #include <stdio.h>
72 #include <stdlib.h>
73
74 #include "libdrm_macros.h"
75 #include "xf86drm.h"
76 #include "xf86drmHash.h"
77
78 #define HASH_MAGIC 0xdeadbeef
79
80 static 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) {
89         void *state;
90         state = drmRandomCreate(37);
91         for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
92         drmRandomDestroy(state);
93         ++init;
94     }
95
96     while (tmp) {
97         hash = (hash << 1) + scatter[tmp & 0xff];
98         tmp >>= 8;
99     }
100
101     hash %= HASH_SIZE;
102     return hash;
103 }
104
105 drm_public void *drmHashCreate(void)
106 {
107     HashTablePtr table;
108     int          i;
109
110     table           = drmMalloc(sizeof(*table));
111     if (!table) return NULL;
112     table->magic    = HASH_MAGIC;
113     table->entries  = 0;
114     table->hits     = 0;
115     table->partials = 0;
116     table->misses   = 0;
117
118     for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
119     return table;
120 }
121
122 drm_public int drmHashDestroy(void *t)
123 {
124     HashTablePtr  table = (HashTablePtr)t;
125     HashBucketPtr bucket;
126     HashBucketPtr next;
127     int           i;
128
129     if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
130
131     for (i = 0; i < HASH_SIZE; i++) {
132         for (bucket = table->buckets[i]; bucket;) {
133             next = bucket->next;
134             drmFree(bucket);
135             bucket = next;
136         }
137     }
138     drmFree(table);
139     return 0;
140 }
141
142 /* Find the bucket and organize the list so that this bucket is at the
143    top. */
144
145 static HashBucketPtr HashFind(HashTablePtr table,
146                               unsigned long key, unsigned long *h)
147 {
148     unsigned long hash = HashHash(key);
149     HashBucketPtr prev = NULL;
150     HashBucketPtr bucket;
151
152     if (h) *h = hash;
153
154     for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
155         if (bucket->key == key) {
156             if (prev) {
157                                 /* Organize */
158                 prev->next           = bucket->next;
159                 bucket->next         = table->buckets[hash];
160                 table->buckets[hash] = bucket;
161                 ++table->partials;
162             } else {
163                 ++table->hits;
164             }
165             return bucket;
166         }
167         prev = bucket;
168     }
169     ++table->misses;
170     return NULL;
171 }
172
173 drm_public int drmHashLookup(void *t, unsigned long key, void **value)
174 {
175     HashTablePtr  table = (HashTablePtr)t;
176     HashBucketPtr bucket;
177
178     if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
179
180     bucket = HashFind(table, key, NULL);
181     if (!bucket) return 1;      /* Not found */
182     *value = bucket->value;
183     return 0;                   /* Found */
184 }
185
186 drm_public int drmHashInsert(void *t, unsigned long key, void *value)
187 {
188     HashTablePtr  table = (HashTablePtr)t;
189     HashBucketPtr bucket;
190     unsigned long hash;
191
192     if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
193
194     if (HashFind(table, key, &hash)) return 1; /* Already in table */
195
196     bucket               = drmMalloc(sizeof(*bucket));
197     if (!bucket) return -1;     /* Error */
198     bucket->key          = key;
199     bucket->value        = value;
200     bucket->next         = table->buckets[hash];
201     table->buckets[hash] = bucket;
202     return 0;                   /* Added to table */
203 }
204
205 drm_public int drmHashDelete(void *t, unsigned long key)
206 {
207     HashTablePtr  table = (HashTablePtr)t;
208     unsigned long hash;
209     HashBucketPtr bucket;
210
211     if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
212
213     bucket = HashFind(table, key, &hash);
214
215     if (!bucket) return 1;      /* Not found */
216
217     table->buckets[hash] = bucket->next;
218     drmFree(bucket);
219     return 0;
220 }
221
222 drm_public int drmHashNext(void *t, unsigned long *key, void **value)
223 {
224     HashTablePtr  table = (HashTablePtr)t;
225
226     while (table->p0 < HASH_SIZE) {
227         if (table->p1) {
228             *key       = table->p1->key;
229             *value     = table->p1->value;
230             table->p1  = table->p1->next;
231             return 1;
232         }
233         table->p1 = table->buckets[table->p0];
234         ++table->p0;
235     }
236     return 0;
237 }
238
239 drm_public int drmHashFirst(void *t, unsigned long *key, void **value)
240 {
241     HashTablePtr  table = (HashTablePtr)t;
242
243     if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
244
245     table->p0 = 0;
246     table->p1 = table->buckets[0];
247     return drmHashNext(table, key, value);
248 }