2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Steve Murphy <murf@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief code to implement generic hash tables
22 * \author Steve Murphy <murf@digium.com>
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision")
31 #include "asterisk/lock.h"
32 #include "asterisk/frame.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/cli.h"
35 #include "asterisk/term.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/threadstorage.h"
38 #include "asterisk/linkedlists.h"
39 #include "asterisk/hashtab.h"
41 static void ast_hashtab_resize( struct ast_hashtab *tab);
43 /* some standard, default routines for general use */
45 int ast_hashtab_compare_strings(const void *a, const void *b)
47 return strcmp((char*)a,(char*)b);
50 int ast_hashtab_compare_strings_nocase(const void *a, const void *b)
52 return strcasecmp((const char*)a,(const char*)b);
55 int ast_hashtab_compare_ints(const void *a, const void *b)
57 int ai = *((int *) a);
58 int bi = *((int *) b);
66 int ast_hashtab_compare_shorts(const void *a, const void *b)
68 short as = *((short *) a);
69 short bs = *((short *) b);
77 int ast_hashtab_resize_java(struct ast_hashtab *tab)
79 double loadfactor = (double) tab->hash_tab_elements / (double) tab->hash_tab_size;
81 return (loadfactor > 0.75);
84 int ast_hashtab_resize_tight(struct ast_hashtab *tab)
86 return (tab->hash_tab_elements > tab->hash_tab_size); /* this is quicker than division */
89 int ast_hashtab_resize_none(struct ast_hashtab *tab) /* always return 0 -- no resizing */
94 int ast_is_prime(int num)
98 if (!(num & 0x1)) /* even number -- not prime */
101 /* Loop through ODD numbers starting with 3 */
105 while (tnum < limit) {
109 /* really, we only need to check sqrt(num) numbers */
112 /* we only check odd numbers */
116 /* if we made it through the loop, the number is a prime */
121 int ast_hashtab_newsize_java(struct ast_hashtab *tab)
123 int i = (tab->hash_tab_size << 1); /* multiply by two */
125 while (!ast_is_prime(i))
131 int ast_hashtab_newsize_tight(struct ast_hashtab *tab)
133 int x = (tab->hash_tab_size << 1);
134 int i = (tab->hash_tab_size + x);
136 while (!ast_is_prime(i))
142 int ast_hashtab_newsize_none(struct ast_hashtab *tab) /* always return current size -- no resizing */
144 return tab->hash_tab_size;
147 unsigned int ast_hashtab_hash_string(const void *obj)
149 unsigned char *str = (unsigned char *) obj;
152 for (total = 0; *str; str++)
154 unsigned int tmp = total;
155 total <<= 1; /* multiply by 2 */
156 total += tmp; /* multiply by 3 */
157 total <<= 2; /* multiply by 12 */
158 total += tmp; /* multiply by 13 */
160 total += ((unsigned int)(*str));
165 unsigned int ast_hashtab_hash_string_sax(const void *obj) /* from Josh */
167 unsigned char *str = (unsigned char *) obj;
168 unsigned int total = 0, c = 0;
171 total ^= (total << 5) + (total >> 2) + (total << 10) + c;
176 unsigned int ast_hashtab_hash_string_nocase(const void *obj)
178 unsigned char *str = (unsigned char*)obj;
181 for (total = 0; *str; str++) {
182 unsigned int tmp = total;
183 unsigned int charval = toupper(*str);
185 /* hopefully, the following is faster than multiplication by 7 */
186 /* why do I go to this bother? A good compiler will do this
187 anyway, if I say total *= 13 */
188 /* BTW, tried *= 7, and it doesn't do as well in spreading things around! */
189 total <<= 1; /* multiply by 2 */
190 total += tmp; /* multiply by 3 */
191 total <<= 2; /* multiply by 12 */
192 total += tmp; /* multiply by 13 */
200 unsigned int ast_hashtab_hash_int(const int x)
205 unsigned int ast_hashtab_hash_short(const short x)
207 /* hmmmm.... modulus is best < 65535 !! */
211 struct ast_hashtab *ast_hashtab_create(int initial_buckets,
212 int (*compare)(const void *a, const void *b), /* a func to compare two elements in the hash -- cannot be null */
213 int (*resize)(struct ast_hashtab *), /* a func to decide if the table needs to be resized, a NULL ptr here will cause a default to be used */
214 int (*newsize)(struct ast_hashtab *tab), /* a ptr to func that returns a new size of the array. A NULL will cause a default to be used */
215 unsigned int (*hash)(const void *obj), /* a func to do the hashing */
216 int do_locking ) /* use locks to guarantee safety of iterators/insertion/deletion -- real simpleminded right now */
218 struct ast_hashtab *ht;
220 if (!(ht = ast_calloc(1, sizeof(*ht))))
223 while (!ast_is_prime(initial_buckets)) /* make sure this is prime */
226 if (!(ht->array = ast_calloc(initial_buckets, sizeof(*(ht->array))))) {
231 ht->hash_tab_size = initial_buckets;
232 ht->compare = compare;
234 ht->newsize = newsize;
236 ht->do_locking = do_locking;
239 ast_rwlock_init(&ht->lock);
242 ht->resize = ast_hashtab_resize_java;
245 ht->newsize = ast_hashtab_newsize_java;
250 struct ast_hashtab *ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_func)(const void *obj))
252 struct ast_hashtab *ht;
255 if (!(ht = ast_calloc(1, sizeof(*ht))))
258 if (!(ht->array = ast_calloc(tab->hash_tab_size, sizeof(*(ht->array))))) {
263 ht->hash_tab_size = tab->hash_tab_size;
264 ht->compare = tab->compare;
265 ht->resize = tab->resize;
266 ht->newsize = tab->newsize;
267 ht->hash = tab->hash;
268 ht->do_locking = tab->do_locking;
271 ast_rwlock_init(&ht->lock);
273 /* now, dup the objects in the buckets and get them into the table */
274 /* the fast way is to use the existing array index, and not have to hash
276 for (i = 0; i < ht->hash_tab_size; i++) {
277 struct ast_hashtab_bucket *b = tab->array[i];
279 void *newobj = (*obj_dup_func)(b->object);
281 ast_hashtab_insert_immediate_bucket(ht, newobj, i);
289 static void tlist_del_item(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
291 /* item had better be in the list! or suffer the weirdness that occurs, later! */
292 if (*head == item) { /* first item in the list */
295 item->tnext->tprev = NULL;
297 /* short circuit stuff */
298 item->tprev->tnext = item->tnext;
300 item->tnext->tprev = item->tprev;
304 static void tlist_add_head(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
309 (*head)->tprev = item;
312 /* the list is empty */
319 /* user-controlled hashtab locking. Create a hashtab without locking, then call the
320 following locking routines yourself to lock the table between threads. */
322 void ast_hashtab_wrlock(struct ast_hashtab *tab)
324 ast_rwlock_wrlock(&tab->lock);
327 void ast_hashtab_rdlock(struct ast_hashtab *tab)
329 ast_rwlock_rdlock(&tab->lock);
332 void ast_hashtab_initlock(struct ast_hashtab *tab)
334 ast_rwlock_init(&tab->lock);
337 void ast_hashtab_destroylock(struct ast_hashtab *tab)
339 ast_rwlock_destroy(&tab->lock);
342 void ast_hashtab_unlock(struct ast_hashtab *tab)
344 ast_rwlock_unlock(&tab->lock);
347 void ast_hashtab_destroy( struct ast_hashtab *tab, void (*objdestroyfunc)(void *obj))
349 /* this func will free the hash table and all its memory. It
350 doesn't touch the objects stored in it */
354 ast_rwlock_wrlock(&tab->lock);
357 /* go thru and destroy the buckets */
358 struct ast_hashtab_bucket *t;
363 if (t->object && objdestroyfunc)
364 (*objdestroyfunc)((void *) t->object); /* I cast this because I'm not going to MOD it, I'm going to DESTROY it */
366 tlist_del_item(&(tab->tlist), tab->tlist);
370 for (i = 0; i < tab->hash_tab_size; i++)
371 tab->array[i] = NULL; /* not totally necc., but best to destroy old ptrs */
375 if (tab->do_locking) {
376 ast_rwlock_unlock(&tab->lock);
377 ast_rwlock_destroy(&tab->lock);
383 int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj)
392 ast_rwlock_wrlock(&tab->lock);
394 h = (*tab->hash)(obj) % tab->hash_tab_size;
396 res = ast_hashtab_insert_immediate_bucket(tab,obj,h);
399 ast_rwlock_unlock(&tab->lock);
404 int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h)
407 struct ast_hashtab_bucket *b;
412 for (c = 0, b = tab->array[h]; b; b= b->next)
415 if (c + 1 > tab->largest_bucket_size)
416 tab->largest_bucket_size = c + 1;
418 if (!(b = ast_calloc(1, sizeof(*b))))
422 b->next = tab->array[h];
428 tlist_add_head(&(tab->tlist), b);
429 tab->hash_tab_elements++;
431 if ((*tab->resize)(tab))
432 ast_hashtab_resize(tab);
437 int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj)
439 /* check to see if the element is already there; insert only if
441 /* will force a resize if the resize func returns 1 */
442 /* returns 1 on success, 0 if there's a problem, or it's already there. */
443 unsigned int bucket = 0;
446 ast_rwlock_wrlock(&tab->lock);
448 if (!ast_hashtab_lookup_bucket(tab, obj, &bucket)) {
449 int ret2 = ast_hashtab_insert_immediate_bucket(tab, obj, bucket);
452 ast_rwlock_unlock(&tab->lock);
458 ast_rwlock_unlock(&tab->lock);
463 void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
465 /* lookup this object in the hash table. return a ptr if found, or NULL if not */
468 struct ast_hashtab_bucket *b;
474 ast_rwlock_rdlock(&tab->lock);
476 h = (*tab->hash)(obj) % tab->hash_tab_size;
477 for (b = tab->array[h]; b; b = b->next) {
478 if (!(*tab->compare)(obj,b->object)) {
481 ast_rwlock_unlock(&tab->lock);
482 return (void*) ret; /* I can't touch obj in this func, but the outside world is welcome to */
487 ast_rwlock_unlock(&tab->lock);
492 void *ast_hashtab_lookup_with_hash(struct ast_hashtab *tab, const void *obj, unsigned int hashval)
494 /* lookup this object in the hash table. return a ptr if found, or NULL if not */
497 struct ast_hashtab_bucket *b;
503 ast_rwlock_rdlock(&tab->lock);
505 h = hashval % tab->hash_tab_size;
506 for (b = tab->array[h]; b; b = b->next) {
507 if (!(*tab->compare)(obj,b->object)) {
510 ast_rwlock_unlock(&tab->lock);
511 return (void*) ret; /* I can't touch obj in this func, but the outside world is welcome to */
516 ast_rwlock_unlock(&tab->lock);
521 void * ast_hashtab_lookup_bucket(struct ast_hashtab *tab, const void *obj, unsigned int *bucket)
523 /* lookup this object in the hash table. return a ptr if found, or NULL if not */
525 struct ast_hashtab_bucket *b;
530 h = (*tab->hash)(obj) % tab->hash_tab_size;
531 for (b = tab->array[h]; b; b = b->next) {
532 if (!(*tab->compare)(obj,b->object))
533 return (void*) b->object; /* I can't touch obj in this func, but the outside world is welcome to */
541 void ast_hashtab_get_stats( struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets)
543 /* returns key stats for the table */
545 ast_rwlock_rdlock(&tab->lock);
546 *biggest_bucket_size = tab->largest_bucket_size;
547 *resize_count = tab->resize_count;
548 *num_objects = tab->hash_tab_elements;
549 *num_buckets = tab->hash_tab_size;
551 ast_rwlock_unlock(&tab->lock);
554 /* this function returns the number of elements stored in the hashtab */
555 int ast_hashtab_size( struct ast_hashtab *tab)
557 return tab->hash_tab_elements;
560 /* this function returns the size of the bucket array in the hashtab */
561 int ast_hashtab_capacity( struct ast_hashtab *tab)
563 return tab->hash_tab_size;
568 /* the insert operation calls this, and is wrlock'd when it does. */
569 /* if you want to call it, you should set the wrlock yourself */
572 static void ast_hashtab_resize( struct ast_hashtab *tab)
574 /* this function is called either internally, when the resize func returns 1, or
575 externally by the user to force a resize of the hash table */
576 int newsize = (*tab->newsize)(tab), i, c;
578 struct ast_hashtab_bucket *b,*bn;
580 /* Since we keep a DLL of all the buckets in tlist,
581 all we have to do is free the array, malloc a new one,
582 and then go thru the tlist array and reassign them into
585 for (i = 0; i < tab->hash_tab_size; i++) { /* don't absolutely have to do this, but
586 why leave ptrs laying around */
587 tab->array[i] = 0; /* erase old ptrs */
590 if (!(tab->array = ast_calloc(newsize, sizeof(*(tab->array)))))
593 /* now sort the buckets into their rightful new slots */
595 tab->hash_tab_size = newsize;
596 tab->largest_bucket_size = 0;
598 for (b = tab->tlist; b; b = bn) {
601 h = (*tab->hash)(b->object) % tab->hash_tab_size;
602 b->next = tab->array[h];
607 /* recalc the largest bucket size */
608 for (i = 0; i < tab->hash_tab_size; i++) {
609 for (c = 0, b = tab->array[i]; b; b = b->next)
611 if (c > tab->largest_bucket_size)
612 tab->largest_bucket_size = c;
616 struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab)
618 /* returns an iterator */
619 struct ast_hashtab_iter *it;
621 if (!(it = ast_calloc(1, sizeof(*it))))
624 it->next = tab->tlist;
627 ast_rwlock_rdlock(&tab->lock);
632 /* use this function to get a write lock */
633 struct ast_hashtab_iter *ast_hashtab_start_write_traversal(struct ast_hashtab *tab)
635 /* returns an iterator */
636 struct ast_hashtab_iter *it;
638 if (!(it = ast_calloc(1, sizeof(*it))))
641 it->next = tab->tlist;
644 ast_rwlock_wrlock(&tab->lock);
649 void ast_hashtab_end_traversal(struct ast_hashtab_iter *it)
651 if (it->tab->do_locking)
652 ast_rwlock_unlock(&it->tab->lock);
656 void *ast_hashtab_next(struct ast_hashtab_iter *it)
658 /* returns the next object in the list, advances iter one step */
659 struct ast_hashtab_bucket *retval;
661 if (it && it->next) { /* there's a next in the bucket list */
663 it->next = retval->tnext;
664 return (void *) retval->object;
670 static void *ast_hashtab_remove_object_internal(struct ast_hashtab *tab, struct ast_hashtab_bucket *b, int h)
675 b->prev->next = b->next;
677 tab->array[h] = b->next;
680 b->next->prev = b->prev;
682 tlist_del_item(&(tab->tlist), b);
685 b->object = b->next = (void*)2;
686 free(b); /* free up the hashbucket */
687 tab->hash_tab_elements--;
691 struct ast_hashtab_bucket *b2;
692 /* do a little checking */
693 for (c2 = 0, b2 = tab->tlist; b2; b2 = b2->tnext) {
696 if (c2 != tab->hash_tab_elements) {
697 printf("Hey! we didn't delete right! there are %d elements in the list, and we expected %d\n",
698 c2, tab->hash_tab_elements);
700 for (c2 = 0, b2 = tab->tlist; b2; b2 = b2->tnext) {
701 unsigned int obj3 = (unsigned long) obj2;
702 unsigned int b3 = (unsigned long) b;
703 if (b2->object == obj2)
704 printf("Hey-- you've still got a bucket pointing at ht_element %x\n", obj3);
706 printf("Hey-- you've still got a bucket with next ptr pointing to deleted bucket %x\n", b3);
708 printf("Hey-- you've still got a bucket with prev ptr pointing to deleted bucket %x\n", b3);
710 printf("Hey-- you've still got a bucket with tprev ptr pointing to deleted bucket %x\n", b3);
712 printf("Hey-- you've still got a bucket with tnext ptr pointing to deleted bucket %x\n", b3);
716 return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
719 void *ast_hashtab_remove_object_via_lookup(struct ast_hashtab *tab, void *obj)
721 /* looks up the object; removes the corresponding bucket */
728 ast_rwlock_wrlock(&tab->lock);
730 obj2 = ast_hashtab_remove_object_via_lookup_nolock(tab,obj);
733 ast_rwlock_unlock(&tab->lock);
738 void *ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab *tab, void *obj)
740 /* looks up the object; removes the corresponding bucket */
742 struct ast_hashtab_bucket *b;
747 h = (*tab->hash)(obj) % tab->hash_tab_size;
748 for (b = tab->array[h]; b; b = b->next) {
750 if (!(*tab->compare)(obj, b->object)) {
753 obj2 = ast_hashtab_remove_object_internal(tab, b, h);
755 return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
762 void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj)
764 /* looks up the object by hash and then comparing pts in bucket list instead of
765 calling the compare routine; removes the bucket -- a slightly cheaper operation */
766 /* looks up the object; removes the corresponding bucket */
773 ast_rwlock_wrlock(&tab->lock);
775 obj2 = ast_hashtab_remove_this_object_nolock(tab,obj);
778 ast_rwlock_unlock(&tab->lock);
783 void *ast_hashtab_remove_this_object_nolock(struct ast_hashtab *tab, void *obj)
785 /* looks up the object by hash and then comparing pts in bucket list instead of
786 calling the compare routine; removes the bucket -- a slightly cheaper operation */
787 /* looks up the object; removes the corresponding bucket */
789 struct ast_hashtab_bucket *b;
794 h = (*tab->hash)(obj) % tab->hash_tab_size;
795 for (b = tab->array[h]; b; b = b->next) {
797 if (obj == b->object) {
799 obj2 = ast_hashtab_remove_object_internal(tab, b, h);
801 return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */