• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 
28  /*
29   * Authors:
30   *   Zack Rusin <zackr@vmware.com>
31   */
32 
33 #include "util_hash.h"
34 
35 #include <stdlib.h>
36 #include <assert.h>
37 
38 #define MAX(a, b) ((a > b) ? (a) : (b))
39 
40 static const int MinNumBits = 4;
41 
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
45 };
46 
primeForNumBits(int numBits)47 static int primeForNumBits(int numBits)
48 {
49 	return (1 << numBits) + prime_deltas[numBits];
50 }
51 
52 /* Returns the smallest integer n such that
53    primeForNumBits(n) >= hint.
54 */
countBits(int hint)55 static int countBits(int hint)
56 {
57 	int numBits = 0;
58 	int bits = hint;
59 
60 	while (bits > 1) {
61 		bits >>= 1;
62 		numBits++;
63 	}
64 
65 	if (numBits >= (int)sizeof(prime_deltas)) {
66 		numBits = sizeof(prime_deltas) - 1;
67 	} else if (primeForNumBits(numBits) < hint) {
68 		++numBits;
69 	}
70 	return numBits;
71 }
72 
73 struct util_node {
74    struct util_node *next;
75    unsigned key;
76    void *value;
77 };
78 
79 struct util_hash_data {
80    struct util_node *fakeNext;
81    struct util_node **buckets;
82    int size;
83    int nodeSize;
84    short userNumBits;
85    short numBits;
86    int numBuckets;
87 };
88 
89 struct util_hash {
90    union {
91       struct util_hash_data *d;
92       struct util_node      *e;
93    } data;
94 };
95 
util_data_allocate_node(struct util_hash_data * hash)96 static void *util_data_allocate_node(struct util_hash_data *hash)
97 {
98    return malloc(hash->nodeSize);
99 }
100 
util_free_node(struct util_node * node)101 static void util_free_node(struct util_node *node)
102 {
103    free(node);
104 }
105 
106 static struct util_node *
util_hash_create_node(struct util_hash * hash,unsigned akey,void * avalue,struct util_node ** anextNode)107 util_hash_create_node(struct util_hash *hash,
108                       unsigned akey, void *avalue,
109                       struct util_node **anextNode)
110 {
111    struct util_node *node = util_data_allocate_node(hash->data.d);
112 
113    if (!node)
114       return NULL;
115 
116    node->key = akey;
117    node->value = avalue;
118 
119    node->next = (struct util_node*)(*anextNode);
120    *anextNode = node;
121    ++hash->data.d->size;
122    return node;
123 }
124 
util_data_rehash(struct util_hash_data * hash,int hint)125 static void util_data_rehash(struct util_hash_data *hash, int hint)
126 {
127    if (hint < 0) {
128       hint = countBits(-hint);
129       if (hint < MinNumBits)
130          hint = MinNumBits;
131       hash->userNumBits = (short)hint;
132       while (primeForNumBits(hint) < (hash->size >> 1))
133          ++hint;
134    } else if (hint < MinNumBits) {
135       hint = MinNumBits;
136    }
137 
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;
142       int  i = 0;
143 
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;
149 
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;
157 
158             while (lastNode->next != e && lastNode->next->key == h)
159                lastNode = lastNode->next;
160 
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;
168          }
169       }
170       free(oldBuckets);
171    }
172 }
173 
util_data_might_grow(struct util_hash_data * hash)174 static void util_data_might_grow(struct util_hash_data *hash)
175 {
176    if (hash->size >= hash->numBuckets)
177       util_data_rehash(hash, hash->numBits + 1);
178 }
179 
util_data_has_shrunk(struct util_hash_data * hash)180 static void util_data_has_shrunk(struct util_hash_data *hash)
181 {
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);
186    }
187 }
188 
util_data_first_node(struct util_hash_data * hash)189 static struct util_node *util_data_first_node(struct util_hash_data *hash)
190 {
191    struct util_node *e = (struct util_node *)(hash);
192    struct util_node **bucket = hash->buckets;
193    int n = hash->numBuckets;
194    while (n--) {
195       if (*bucket != e)
196          return *bucket;
197       ++bucket;
198    }
199    return e;
200 }
201 
util_hash_find_node(struct util_hash * hash,unsigned akey)202 static struct util_node **util_hash_find_node(struct util_hash *hash, unsigned akey)
203 {
204    struct util_node **node;
205 
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;
211    } else {
212       node = (struct util_node **)((const struct util_node * const *)(&hash->data.e));
213    }
214    return node;
215 }
216 
217 drm_private struct util_hash_iter
util_hash_insert(struct util_hash * hash,unsigned key,void * data)218 util_hash_insert(struct util_hash *hash, unsigned key, void *data)
219 {
220    util_data_might_grow(hash->data.d);
221 
222    {
223       struct util_node **nextNode = util_hash_find_node(hash, key);
224       struct util_node *node = util_hash_create_node(hash, key, data, nextNode);
225       if (!node) {
226          struct util_hash_iter null_iter = {hash, 0};
227          return null_iter;
228       }
229 
230       {
231          struct util_hash_iter iter = {hash, node};
232          return iter;
233       }
234    }
235 }
236 
util_hash_create(void)237 drm_private struct util_hash *util_hash_create(void)
238 {
239    struct util_hash *hash = malloc(sizeof(struct util_hash));
240    if (!hash)
241       return NULL;
242 
243    hash->data.d = malloc(sizeof(struct util_hash_data));
244    if (!hash->data.d) {
245       free(hash);
246       return NULL;
247    }
248 
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;
256 
257    return hash;
258 }
259 
util_hash_delete(struct util_hash * hash)260 drm_private void util_hash_delete(struct util_hash *hash)
261 {
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;
265    while (n--) {
266       struct util_node *cur = *bucket++;
267       while (cur != e_for_x) {
268          struct util_node *next = cur->next;
269          util_free_node(cur);
270          cur = next;
271       }
272    }
273    free(hash->data.d->buckets);
274    free(hash->data.d);
275    free(hash);
276 }
277 
278 drm_private struct util_hash_iter
util_hash_find(struct util_hash * hash,unsigned key)279 util_hash_find(struct util_hash *hash, unsigned key)
280 {
281    struct util_node **nextNode = util_hash_find_node(hash, key);
282    struct util_hash_iter iter = {hash, *nextNode};
283    return iter;
284 }
285 
util_hash_iter_key(struct util_hash_iter iter)286 drm_private unsigned util_hash_iter_key(struct util_hash_iter iter)
287 {
288    if (!iter.node || iter.hash->data.e == iter.node)
289       return 0;
290    return iter.node->key;
291 }
292 
util_hash_iter_data(struct util_hash_iter iter)293 drm_private void *util_hash_iter_data(struct util_hash_iter iter)
294 {
295    if (!iter.node || iter.hash->data.e == iter.node)
296       return 0;
297    return iter.node->value;
298 }
299 
util_hash_data_next(struct util_node * node)300 static struct util_node *util_hash_data_next(struct util_node *node)
301 {
302    union {
303       struct util_node *next;
304       struct util_node *e;
305       struct util_hash_data *d;
306    } a;
307    int start;
308    struct util_node **bucket;
309    int n;
310 
311    a.next = node->next;
312    if (!a.next) {
313       /* iterating beyond the last element */
314       return 0;
315    }
316    if (a.next->next)
317       return a.next;
318 
319    start = (node->key % a.d->numBuckets) + 1;
320    bucket = a.d->buckets + start;
321    n = a.d->numBuckets - start;
322    while (n--) {
323       if (*bucket != a.e)
324          return *bucket;
325       ++bucket;
326    }
327    return a.e;
328 }
329 
330 drm_private struct util_hash_iter
util_hash_iter_next(struct util_hash_iter iter)331 util_hash_iter_next(struct util_hash_iter iter)
332 {
333    struct util_hash_iter next = {iter.hash, util_hash_data_next(iter.node)};
334    return next;
335 }
336 
util_hash_iter_is_null(struct util_hash_iter iter)337 drm_private int util_hash_iter_is_null(struct util_hash_iter iter)
338 {
339    if (!iter.node || iter.node == iter.hash->data.e)
340       return 1;
341    return 0;
342 }
343 
util_hash_take(struct util_hash * hash,unsigned akey)344 drm_private void *util_hash_take(struct util_hash *hash, unsigned akey)
345 {
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);
351       *node = next;
352       --hash->data.d->size;
353       util_data_has_shrunk(hash->data.d);
354       return t;
355    }
356    return 0;
357 }
358 
util_hash_first_node(struct util_hash * hash)359 drm_private struct util_hash_iter util_hash_first_node(struct util_hash *hash)
360 {
361    struct util_hash_iter iter = {hash, util_data_first_node(hash->data.d)};
362    return iter;
363 }
364 
365 drm_private struct util_hash_iter
util_hash_erase(struct util_hash * hash,struct util_hash_iter iter)366 util_hash_erase(struct util_hash *hash, struct util_hash_iter iter)
367 {
368    struct util_hash_iter ret = iter;
369    struct util_node *node = iter.node;
370    struct util_node **node_ptr;
371 
372    if (node == hash->data.e)
373       return iter;
374 
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;
382    return ret;
383 }
384