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
);
42 static void *ast_hashtab_lookup_internal(struct ast_hashtab
*tab
, const void *obj
, unsigned int h
);
44 /* some standard, default routines for general use */
46 int ast_hashtab_compare_strings(const void *a
, const void *b
)
51 int ast_hashtab_compare_strings_nocase(const void *a
, const void *b
)
53 return strcasecmp(a
, b
);
56 int ast_hashtab_compare_ints(const void *a
, const void *b
)
58 int ai
= *((int *) a
);
59 int bi
= *((int *) b
);
67 int ast_hashtab_compare_shorts(const void *a
, const void *b
)
69 short as
= *((short *) a
);
70 short bs
= *((short *) b
);
78 int ast_hashtab_resize_java(struct ast_hashtab
*tab
)
80 double loadfactor
= (double) tab
->hash_tab_elements
/ (double) tab
->hash_tab_size
;
82 return (loadfactor
> 0.75);
85 int ast_hashtab_resize_tight(struct ast_hashtab
*tab
)
87 return (tab
->hash_tab_elements
> tab
->hash_tab_size
); /* this is quicker than division */
90 int ast_hashtab_resize_none(struct ast_hashtab
*tab
) /* always return 0 -- no resizing */
95 int ast_is_prime(int num
)
99 if (!(num
& 0x1)) /* even number -- not prime */
102 /* Loop through ODD numbers starting with 3 */
106 while (tnum
< limit
) {
110 /* really, we only need to check sqrt(num) numbers */
113 /* we only check odd numbers */
117 /* if we made it through the loop, the number is a prime */
122 int ast_hashtab_newsize_java(struct ast_hashtab
*tab
)
124 int i
= (tab
->hash_tab_size
<< 1); /* multiply by two */
126 while (!ast_is_prime(i
))
132 int ast_hashtab_newsize_tight(struct ast_hashtab
*tab
)
134 int x
= (tab
->hash_tab_size
<< 1);
135 int i
= (tab
->hash_tab_size
+ x
);
137 while (!ast_is_prime(i
))
143 int ast_hashtab_newsize_none(struct ast_hashtab
*tab
) /* always return current size -- no resizing */
145 return tab
->hash_tab_size
;
148 unsigned int ast_hashtab_hash_string(const void *obj
)
150 unsigned char *str
= (unsigned char *) obj
;
153 for (total
= 0; *str
; str
++)
155 unsigned int tmp
= total
;
156 total
<<= 1; /* multiply by 2 */
157 total
+= tmp
; /* multiply by 3 */
158 total
<<= 2; /* multiply by 12 */
159 total
+= tmp
; /* multiply by 13 */
161 total
+= ((unsigned int)(*str
));
166 unsigned int ast_hashtab_hash_string_sax(const void *obj
) /* from Josh */
168 const unsigned char *str
= obj
;
169 unsigned int total
= 0, c
= 0;
172 total
^= (total
<< 5) + (total
>> 2) + (total
<< 10) + c
;
177 unsigned int ast_hashtab_hash_string_nocase(const void *obj
)
179 const unsigned char *str
= obj
;
182 for (total
= 0; *str
; str
++) {
183 unsigned int tmp
= total
;
184 unsigned int charval
= toupper(*str
);
186 /* hopefully, the following is faster than multiplication by 7 */
187 /* why do I go to this bother? A good compiler will do this
188 anyway, if I say total *= 13 */
189 /* BTW, tried *= 7, and it doesn't do as well in spreading things around! */
190 total
<<= 1; /* multiply by 2 */
191 total
+= tmp
; /* multiply by 3 */
192 total
<<= 2; /* multiply by 12 */
193 total
+= tmp
; /* multiply by 13 */
201 unsigned int ast_hashtab_hash_int(const int x
)
206 unsigned int ast_hashtab_hash_short(const short x
)
208 /* hmmmm.... modulus is best < 65535 !! */
212 struct ast_hashtab
*ast_hashtab_create(int initial_buckets
,
213 int (*compare
)(const void *a
, const void *b
),
214 int (*resize
)(struct ast_hashtab
*),
215 int (*newsize
)(struct ast_hashtab
*tab
),
216 unsigned int (*hash
)(const void *obj
),
219 struct ast_hashtab
*ht
;
221 if (!(ht
= ast_calloc(1, sizeof(*ht
))))
224 while (!ast_is_prime(initial_buckets
)) /* make sure this is prime */
227 if (!(ht
->array
= ast_calloc(initial_buckets
, sizeof(*(ht
->array
))))) {
232 ht
->hash_tab_size
= initial_buckets
;
233 ht
->compare
= compare
;
235 ht
->newsize
= newsize
;
237 ht
->do_locking
= do_locking
;
240 ast_rwlock_init(&ht
->lock
);
243 ht
->resize
= ast_hashtab_resize_java
;
246 ht
->newsize
= ast_hashtab_newsize_java
;
251 struct ast_hashtab
*ast_hashtab_dup(struct ast_hashtab
*tab
, void *(*obj_dup_func
)(const void *obj
))
253 struct ast_hashtab
*ht
;
256 if (!(ht
= ast_calloc(1, sizeof(*ht
))))
259 if (!(ht
->array
= ast_calloc(tab
->hash_tab_size
, sizeof(*(ht
->array
))))) {
264 ht
->hash_tab_size
= tab
->hash_tab_size
;
265 ht
->compare
= tab
->compare
;
266 ht
->resize
= tab
->resize
;
267 ht
->newsize
= tab
->newsize
;
268 ht
->hash
= tab
->hash
;
269 ht
->do_locking
= tab
->do_locking
;
272 ast_rwlock_init(&ht
->lock
);
274 /* now, dup the objects in the buckets and get them into the table */
275 /* the fast way is to use the existing array index, and not have to hash
277 for (i
= 0; i
< ht
->hash_tab_size
; i
++) {
278 struct ast_hashtab_bucket
*b
= tab
->array
[i
];
280 void *newobj
= (*obj_dup_func
)(b
->object
);
282 ast_hashtab_insert_immediate_bucket(ht
, newobj
, i
);
290 static void tlist_del_item(struct ast_hashtab_bucket
**head
, struct ast_hashtab_bucket
*item
)
292 /* item had better be in the list! or suffer the weirdness that occurs, later! */
293 if (*head
== item
) { /* first item in the list */
296 item
->tnext
->tprev
= NULL
;
298 /* short circuit stuff */
299 item
->tprev
->tnext
= item
->tnext
;
301 item
->tnext
->tprev
= item
->tprev
;
305 static void tlist_add_head(struct ast_hashtab_bucket
**head
, struct ast_hashtab_bucket
*item
)
310 (*head
)->tprev
= item
;
313 /* the list is empty */
320 /* user-controlled hashtab locking. Create a hashtab without locking, then call the
321 following locking routines yourself to lock the table between threads. */
323 void ast_hashtab_wrlock(struct ast_hashtab
*tab
)
325 ast_rwlock_wrlock(&tab
->lock
);
328 void ast_hashtab_rdlock(struct ast_hashtab
*tab
)
330 ast_rwlock_rdlock(&tab
->lock
);
333 void ast_hashtab_initlock(struct ast_hashtab
*tab
)
335 ast_rwlock_init(&tab
->lock
);
338 void ast_hashtab_destroylock(struct ast_hashtab
*tab
)
340 ast_rwlock_destroy(&tab
->lock
);
343 void ast_hashtab_unlock(struct ast_hashtab
*tab
)
345 ast_rwlock_unlock(&tab
->lock
);
348 void ast_hashtab_destroy( struct ast_hashtab
*tab
, void (*objdestroyfunc
)(void *obj
))
350 /* this func will free the hash table and all its memory. It
351 doesn't touch the objects stored in it */
355 ast_rwlock_wrlock(&tab
->lock
);
358 /* go thru and destroy the buckets */
359 struct ast_hashtab_bucket
*t
;
364 if (t
->object
&& objdestroyfunc
)
365 (*objdestroyfunc
)((void *) t
->object
); /* I cast this because I'm not going to MOD it, I'm going to DESTROY it */
367 tlist_del_item(&(tab
->tlist
), tab
->tlist
);
371 for (i
= 0; i
< tab
->hash_tab_size
; i
++)
372 tab
->array
[i
] = NULL
; /* not totally necc., but best to destroy old ptrs */
376 if (tab
->do_locking
) {
377 ast_rwlock_unlock(&tab
->lock
);
378 ast_rwlock_destroy(&tab
->lock
);
384 int ast_hashtab_insert_immediate(struct ast_hashtab
*tab
, const void *obj
)
393 ast_rwlock_wrlock(&tab
->lock
);
395 h
= (*tab
->hash
)(obj
) % tab
->hash_tab_size
;
397 res
= ast_hashtab_insert_immediate_bucket(tab
,obj
,h
);
400 ast_rwlock_unlock(&tab
->lock
);
405 int ast_hashtab_insert_immediate_bucket(struct ast_hashtab
*tab
, const void *obj
, unsigned int h
)
408 struct ast_hashtab_bucket
*b
;
413 for (c
= 0, b
= tab
->array
[h
]; b
; b
= b
->next
)
416 if (c
+ 1 > tab
->largest_bucket_size
)
417 tab
->largest_bucket_size
= c
+ 1;
419 if (!(b
= ast_calloc(1, sizeof(*b
))))
423 b
->next
= tab
->array
[h
];
429 tlist_add_head(&(tab
->tlist
), b
);
430 tab
->hash_tab_elements
++;
432 if ((*tab
->resize
)(tab
))
433 ast_hashtab_resize(tab
);
438 int ast_hashtab_insert_safe(struct ast_hashtab
*tab
, const void *obj
)
440 /* check to see if the element is already there; insert only if
442 /* will force a resize if the resize func returns 1 */
443 /* returns 1 on success, 0 if there's a problem, or it's already there. */
444 unsigned int bucket
= 0;
447 ast_rwlock_wrlock(&tab
->lock
);
449 if (!ast_hashtab_lookup_bucket(tab
, obj
, &bucket
)) {
450 int ret2
= ast_hashtab_insert_immediate_bucket(tab
, obj
, bucket
);
453 ast_rwlock_unlock(&tab
->lock
);
459 ast_rwlock_unlock(&tab
->lock
);
464 void *ast_hashtab_lookup(struct ast_hashtab
*tab
, const void *obj
)
466 /* lookup this object in the hash table. return a ptr if found, or NULL if not */
474 ast_rwlock_rdlock(&tab
->lock
);
476 h
= (*tab
->hash
)(obj
) % tab
->hash_tab_size
;
478 ret
= ast_hashtab_lookup_internal(tab
,obj
,h
);
481 ast_rwlock_unlock(&tab
->lock
);
487 void *ast_hashtab_lookup_with_hash(struct ast_hashtab
*tab
, const void *obj
, unsigned int hashval
)
489 /* lookup this object in the hash table. return a ptr if found, or NULL if not */
497 ast_rwlock_rdlock(&tab
->lock
);
499 h
= hashval
% tab
->hash_tab_size
;
501 ret
= ast_hashtab_lookup_internal(tab
,obj
,h
);
504 ast_rwlock_unlock(&tab
->lock
);
509 void *ast_hashtab_lookup_bucket(struct ast_hashtab
*tab
, const void *obj
, unsigned int *bucket
)
511 /* lookup this object in the hash table. return a ptr if found, or NULL if not */
518 h
= (*tab
->hash
)(obj
) % tab
->hash_tab_size
;
520 ret
= ast_hashtab_lookup_internal(tab
,obj
,h
);
527 static void *ast_hashtab_lookup_internal(struct ast_hashtab
*tab
, const void *obj
, unsigned int h
)
529 struct ast_hashtab_bucket
*b
;
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 */
540 void ast_hashtab_get_stats( struct ast_hashtab
*tab
, int *biggest_bucket_size
, int *resize_count
, int *num_objects
, int *num_buckets
)
542 /* returns key stats for the table */
544 ast_rwlock_rdlock(&tab
->lock
);
545 *biggest_bucket_size
= tab
->largest_bucket_size
;
546 *resize_count
= tab
->resize_count
;
547 *num_objects
= tab
->hash_tab_elements
;
548 *num_buckets
= tab
->hash_tab_size
;
550 ast_rwlock_unlock(&tab
->lock
);
553 /* this function returns the number of elements stored in the hashtab */
554 int ast_hashtab_size( struct ast_hashtab
*tab
)
556 return tab
->hash_tab_elements
;
559 /* this function returns the size of the bucket array in the hashtab */
560 int ast_hashtab_capacity( struct ast_hashtab
*tab
)
562 return tab
->hash_tab_size
;
567 /* the insert operation calls this, and is wrlock'd when it does. */
568 /* if you want to call it, you should set the wrlock yourself */
571 static void ast_hashtab_resize( struct ast_hashtab
*tab
)
573 /* this function is called either internally, when the resize func returns 1, or
574 externally by the user to force a resize of the hash table */
575 int newsize
= (*tab
->newsize
)(tab
), i
, c
;
577 struct ast_hashtab_bucket
*b
,*bn
;
579 /* Since we keep a DLL of all the buckets in tlist,
580 all we have to do is free the array, malloc a new one,
581 and then go thru the tlist array and reassign them into
584 for (i
= 0; i
< tab
->hash_tab_size
; i
++) { /* don't absolutely have to do this, but
585 why leave ptrs laying around */
586 tab
->array
[i
] = 0; /* erase old ptrs */
589 if (!(tab
->array
= ast_calloc(newsize
, sizeof(*(tab
->array
)))))
592 /* now sort the buckets into their rightful new slots */
594 tab
->hash_tab_size
= newsize
;
595 tab
->largest_bucket_size
= 0;
597 for (b
= tab
->tlist
; b
; b
= bn
) {
600 h
= (*tab
->hash
)(b
->object
) % tab
->hash_tab_size
;
601 b
->next
= tab
->array
[h
];
606 /* recalc the largest bucket size */
607 for (i
= 0; i
< tab
->hash_tab_size
; i
++) {
608 for (c
= 0, b
= tab
->array
[i
]; b
; b
= b
->next
)
610 if (c
> tab
->largest_bucket_size
)
611 tab
->largest_bucket_size
= c
;
615 struct ast_hashtab_iter
*ast_hashtab_start_traversal(struct ast_hashtab
*tab
)
617 /* returns an iterator */
618 struct ast_hashtab_iter
*it
;
620 if (!(it
= ast_calloc(1, sizeof(*it
))))
623 it
->next
= tab
->tlist
;
626 ast_rwlock_rdlock(&tab
->lock
);
631 /* use this function to get a write lock */
632 struct ast_hashtab_iter
*ast_hashtab_start_write_traversal(struct ast_hashtab
*tab
)
634 /* returns an iterator */
635 struct ast_hashtab_iter
*it
;
637 if (!(it
= ast_calloc(1, sizeof(*it
))))
640 it
->next
= tab
->tlist
;
643 ast_rwlock_wrlock(&tab
->lock
);
648 void ast_hashtab_end_traversal(struct ast_hashtab_iter
*it
)
650 if (it
->tab
->do_locking
)
651 ast_rwlock_unlock(&it
->tab
->lock
);
655 void *ast_hashtab_next(struct ast_hashtab_iter
*it
)
657 /* returns the next object in the list, advances iter one step */
658 struct ast_hashtab_bucket
*retval
;
660 if (it
&& it
->next
) { /* there's a next in the bucket list */
662 it
->next
= retval
->tnext
;
663 return (void *) retval
->object
;
669 static void *ast_hashtab_remove_object_internal(struct ast_hashtab
*tab
, struct ast_hashtab_bucket
*b
, int h
)
674 b
->prev
->next
= b
->next
;
676 tab
->array
[h
] = b
->next
;
679 b
->next
->prev
= b
->prev
;
681 tlist_del_item(&(tab
->tlist
), b
);
684 b
->object
= b
->next
= (void*)2;
685 free(b
); /* free up the hashbucket */
686 tab
->hash_tab_elements
--;
690 struct ast_hashtab_bucket
*b2
;
691 /* do a little checking */
692 for (c2
= 0, b2
= tab
->tlist
; b2
; b2
= b2
->tnext
) {
695 if (c2
!= tab
->hash_tab_elements
) {
696 printf("Hey! we didn't delete right! there are %d elements in the list, and we expected %d\n",
697 c2
, tab
->hash_tab_elements
);
699 for (c2
= 0, b2
= tab
->tlist
; b2
; b2
= b2
->tnext
) {
700 unsigned int obj3
= (unsigned long) obj2
;
701 unsigned int b3
= (unsigned long) b
;
702 if (b2
->object
== obj2
)
703 printf("Hey-- you've still got a bucket pointing at ht_element %x\n", obj3
);
705 printf("Hey-- you've still got a bucket with next ptr pointing to deleted bucket %x\n", b3
);
707 printf("Hey-- you've still got a bucket with prev ptr pointing to deleted bucket %x\n", b3
);
709 printf("Hey-- you've still got a bucket with tprev ptr pointing to deleted bucket %x\n", b3
);
711 printf("Hey-- you've still got a bucket with tnext ptr pointing to deleted bucket %x\n", b3
);
715 return (void *) obj2
; /* inside this code, the obj's are untouchable, but outside, they aren't */
718 void *ast_hashtab_remove_object_via_lookup(struct ast_hashtab
*tab
, void *obj
)
720 /* looks up the object; removes the corresponding bucket */
727 ast_rwlock_wrlock(&tab
->lock
);
729 obj2
= ast_hashtab_remove_object_via_lookup_nolock(tab
,obj
);
732 ast_rwlock_unlock(&tab
->lock
);
737 void *ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab
*tab
, void *obj
)
739 /* looks up the object; removes the corresponding bucket */
741 struct ast_hashtab_bucket
*b
;
746 h
= (*tab
->hash
)(obj
) % tab
->hash_tab_size
;
747 for (b
= tab
->array
[h
]; b
; b
= b
->next
) {
749 if (!(*tab
->compare
)(obj
, b
->object
)) {
752 obj2
= ast_hashtab_remove_object_internal(tab
, b
, h
);
754 return (void *) obj2
; /* inside this code, the obj's are untouchable, but outside, they aren't */
761 void *ast_hashtab_remove_this_object(struct ast_hashtab
*tab
, void *obj
)
763 /* looks up the object by hash and then comparing pts in bucket list instead of
764 calling the compare routine; removes the bucket -- a slightly cheaper operation */
765 /* looks up the object; removes the corresponding bucket */
772 ast_rwlock_wrlock(&tab
->lock
);
774 obj2
= ast_hashtab_remove_this_object_nolock(tab
,obj
);
777 ast_rwlock_unlock(&tab
->lock
);
782 void *ast_hashtab_remove_this_object_nolock(struct ast_hashtab
*tab
, void *obj
)
784 /* looks up the object by hash and then comparing pts in bucket list instead of
785 calling the compare routine; removes the bucket -- a slightly cheaper operation */
786 /* looks up the object; removes the corresponding bucket */
788 struct ast_hashtab_bucket
*b
;
793 h
= (*tab
->hash
)(obj
) % tab
->hash_tab_size
;
794 for (b
= tab
->array
[h
]; b
; b
= b
->next
) {
796 if (obj
== b
->object
) {
798 obj2
= ast_hashtab_remove_object_internal(tab
, b
, h
);
800 return (void *) obj2
; /* inside this code, the obj's are untouchable, but outside, they aren't */