1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Zack Rusin <zackr@vmware.com>
33 #include "util_hash.h"
38 #define MAX(a, b) ((a > b) ? (a) : (b))
40 static const int MinNumBits = 4;
42 static const unsigned char prime_deltas[] = {
43 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 9, 25, 3,
44 1, 21, 3, 21, 7, 15, 9, 5, 3, 29, 15, 0, 0, 0, 0, 0
47 static int primeForNumBits(int numBits)
49 return (1 << numBits) + prime_deltas[numBits];
52 /* Returns the smallest integer n such that
53 primeForNumBits(n) >= hint.
55 static int countBits(int hint)
65 if (numBits >= (int)sizeof(prime_deltas)) {
66 numBits = sizeof(prime_deltas) - 1;
67 } else if (primeForNumBits(numBits) < hint) {
74 struct util_node *next;
79 struct util_hash_data {
80 struct util_node *fakeNext;
81 struct util_node **buckets;
91 struct util_hash_data *d;
96 static void *util_data_allocate_node(struct util_hash_data *hash)
98 return malloc(hash->nodeSize);
101 static void util_free_node(struct util_node *node)
106 static struct util_node *
107 util_hash_create_node(struct util_hash *hash,
108 unsigned akey, void *avalue,
109 struct util_node **anextNode)
111 struct util_node *node = util_data_allocate_node(hash->data.d);
117 node->value = avalue;
119 node->next = (struct util_node*)(*anextNode);
121 ++hash->data.d->size;
125 static void util_data_rehash(struct util_hash_data *hash, int hint)
128 hint = countBits(-hint);
129 if (hint < MinNumBits)
131 hash->userNumBits = (short)hint;
132 while (primeForNumBits(hint) < (hash->size >> 1))
134 } else if (hint < MinNumBits) {
138 if (hash->numBits != hint) {
139 struct util_node *e = (struct util_node *)(hash);
140 struct util_node **oldBuckets = hash->buckets;
141 int oldNumBuckets = hash->numBuckets;
144 hash->numBits = (short)hint;
145 hash->numBuckets = primeForNumBits(hint);
146 hash->buckets = malloc(sizeof(struct util_node*) * hash->numBuckets);
147 for (i = 0; i < hash->numBuckets; ++i)
148 hash->buckets[i] = e;
150 for (i = 0; i < oldNumBuckets; ++i) {
151 struct util_node *firstNode = oldBuckets[i];
152 while (firstNode != e) {
153 unsigned h = firstNode->key;
154 struct util_node *lastNode = firstNode;
155 struct util_node *afterLastNode;
156 struct util_node **beforeFirstNode;
158 while (lastNode->next != e && lastNode->next->key == h)
159 lastNode = lastNode->next;
161 afterLastNode = lastNode->next;
162 beforeFirstNode = &hash->buckets[h % hash->numBuckets];
163 while (*beforeFirstNode != e)
164 beforeFirstNode = &(*beforeFirstNode)->next;
165 lastNode->next = *beforeFirstNode;
166 *beforeFirstNode = firstNode;
167 firstNode = afterLastNode;
174 static void util_data_might_grow(struct util_hash_data *hash)
176 if (hash->size >= hash->numBuckets)
177 util_data_rehash(hash, hash->numBits + 1);
180 static void util_data_has_shrunk(struct util_hash_data *hash)
182 if (hash->size <= (hash->numBuckets >> 3) &&
183 hash->numBits > hash->userNumBits) {
184 int max = MAX(hash->numBits-2, hash->userNumBits);
185 util_data_rehash(hash, max);
189 static struct util_node *util_data_first_node(struct util_hash_data *hash)
191 struct util_node *e = (struct util_node *)(hash);
192 struct util_node **bucket = hash->buckets;
193 int n = hash->numBuckets;
202 static struct util_node **util_hash_find_node(struct util_hash *hash, unsigned akey)
204 struct util_node **node;
206 if (hash->data.d->numBuckets) {
207 node = (struct util_node **)(&hash->data.d->buckets[akey % hash->data.d->numBuckets]);
208 assert(*node == hash->data.e || (*node)->next);
209 while (*node != hash->data.e && (*node)->key != akey)
210 node = &(*node)->next;
212 node = (struct util_node **)((const struct util_node * const *)(&hash->data.e));
217 drm_private struct util_hash_iter
218 util_hash_insert(struct util_hash *hash, unsigned key, void *data)
220 util_data_might_grow(hash->data.d);
223 struct util_node **nextNode = util_hash_find_node(hash, key);
224 struct util_node *node = util_hash_create_node(hash, key, data, nextNode);
226 struct util_hash_iter null_iter = {hash, 0};
231 struct util_hash_iter iter = {hash, node};
237 drm_private struct util_hash *util_hash_create(void)
239 struct util_hash *hash = malloc(sizeof(struct util_hash));
243 hash->data.d = malloc(sizeof(struct util_hash_data));
249 hash->data.d->fakeNext = 0;
250 hash->data.d->buckets = 0;
251 hash->data.d->size = 0;
252 hash->data.d->nodeSize = sizeof(struct util_node);
253 hash->data.d->userNumBits = (short)MinNumBits;
254 hash->data.d->numBits = 0;
255 hash->data.d->numBuckets = 0;
260 drm_private void util_hash_delete(struct util_hash *hash)
262 struct util_node *e_for_x = (struct util_node *)(hash->data.d);
263 struct util_node **bucket = (struct util_node **)(hash->data.d->buckets);
264 int n = hash->data.d->numBuckets;
266 struct util_node *cur = *bucket++;
267 while (cur != e_for_x) {
268 struct util_node *next = cur->next;
273 free(hash->data.d->buckets);
278 drm_private struct util_hash_iter
279 util_hash_find(struct util_hash *hash, unsigned key)
281 struct util_node **nextNode = util_hash_find_node(hash, key);
282 struct util_hash_iter iter = {hash, *nextNode};
286 drm_private unsigned util_hash_iter_key(struct util_hash_iter iter)
288 if (!iter.node || iter.hash->data.e == iter.node)
290 return iter.node->key;
293 drm_private void *util_hash_iter_data(struct util_hash_iter iter)
295 if (!iter.node || iter.hash->data.e == iter.node)
297 return iter.node->value;
300 static struct util_node *util_hash_data_next(struct util_node *node)
303 struct util_node *next;
305 struct util_hash_data *d;
308 struct util_node **bucket;
313 /* iterating beyond the last element */
319 start = (node->key % a.d->numBuckets) + 1;
320 bucket = a.d->buckets + start;
321 n = a.d->numBuckets - start;
330 drm_private struct util_hash_iter
331 util_hash_iter_next(struct util_hash_iter iter)
333 struct util_hash_iter next = {iter.hash, util_hash_data_next(iter.node)};
337 drm_private int util_hash_iter_is_null(struct util_hash_iter iter)
339 if (!iter.node || iter.node == iter.hash->data.e)
344 drm_private void *util_hash_take(struct util_hash *hash, unsigned akey)
346 struct util_node **node = util_hash_find_node(hash, akey);
347 if (*node != hash->data.e) {
348 void *t = (*node)->value;
349 struct util_node *next = (*node)->next;
350 util_free_node(*node);
352 --hash->data.d->size;
353 util_data_has_shrunk(hash->data.d);
359 drm_private struct util_hash_iter util_hash_first_node(struct util_hash *hash)
361 struct util_hash_iter iter = {hash, util_data_first_node(hash->data.d)};
365 drm_private struct util_hash_iter
366 util_hash_erase(struct util_hash *hash, struct util_hash_iter iter)
368 struct util_hash_iter ret = iter;
369 struct util_node *node = iter.node;
370 struct util_node **node_ptr;
372 if (node == hash->data.e)
375 ret = util_hash_iter_next(ret);
376 node_ptr = (struct util_node**)(&hash->data.d->buckets[node->key % hash->data.d->numBuckets]);
377 while (*node_ptr != node)
378 node_ptr = &(*node_ptr)->next;
379 *node_ptr = node->next;
380 util_free_node(node);
381 --hash->data.d->size;