Trailing whitespace, minor coding guideline fixes, and start beefing up the

hashtab documentation a bit.


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@177884 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.6.2
Sean Bright 17 years ago
parent 7dc56a0c27
commit 935185ce8a

@ -81,12 +81,12 @@ struct ast_hashtab
{ {
struct ast_hashtab_bucket **array; struct ast_hashtab_bucket **array;
struct ast_hashtab_bucket *tlist; /*!< the head of a DLList of all the hashbuckets in the table (for traversal). */ struct ast_hashtab_bucket *tlist; /*!< the head of a DLList of all the hashbuckets in the table (for traversal). */
int (*compare) (const void *a, const void *b); /*!< a ptr to func that returns int, and take two void* ptrs, compares them, int (*compare) (const void *a, const void *b); /*!< a ptr to func that returns int, and take two void* ptrs, compares them,
rets -1 if a < b; rets 0 if a==b; rets 1 if a>b */ rets -1 if a < b; rets 0 if a==b; rets 1 if a>b */
int (*newsize) (struct ast_hashtab *tab); /*!< a ptr to func that returns int, a new size for hash tab, based on curr_size */ int (*newsize) (struct ast_hashtab *tab); /*!< a ptr to func that returns int, a new size for hash tab, based on curr_size */
int (*resize) (struct ast_hashtab *tab); /*!< a function to decide whether this hashtable should be resized now */ int (*resize) (struct ast_hashtab *tab); /*!< a function to decide whether this hashtable should be resized now */
unsigned int (*hash) (const void *obj); /*!< a hash func ptr for this table. Given a raw ptr to an obj, unsigned int (*hash) (const void *obj); /*!< a hash func ptr for this table. Given a raw ptr to an obj,
it calcs a hash.*/ it calcs a hash.*/
int hash_tab_size; /*!< the size of the bucket array */ int hash_tab_size; /*!< the size of the bucket array */
int hash_tab_elements; /*!< the number of objects currently stored in the table */ int hash_tab_elements; /*!< the number of objects currently stored in the table */
@ -108,47 +108,87 @@ struct ast_hashtab_iter
/* some standard, default routines for general use */ /* some standard, default routines for general use */
/*! \brief For sizing the hash table, tells if num is prime or not */ /*!
* \brief Determines if the specified number is prime.
*
* \param num the number to test
* \retval 0 if the number is not prime
* \retval 1 if the number is prime
*/
int ast_is_prime(int num); int ast_is_prime(int num);
/*! /*!
* \brief assumes a and b are char * * \brief Compares two strings for equality.
* \return 0 if they match *
*/ * \param a a character string
* \param b a character string
* \retval 0 if the strings match
* \retval <0 if string a is less than string b
* \retval >0 if string a is greather than string b
*/
int ast_hashtab_compare_strings(const void *a, const void *b); int ast_hashtab_compare_strings(const void *a, const void *b);
/*! /*!
* \brief assumes a & b are strings * \brief Compares two strings for equality, ignoring case.
* \return 0 if they match (strcasecmp) *
*/ * \param a a character string
* \param b a character string
* \retval 0 if the strings match
* \retval <0 if string a is less than string b
* \retval >0 if string a is greather than string b
*/
int ast_hashtab_compare_strings_nocase(const void *a, const void *b); int ast_hashtab_compare_strings_nocase(const void *a, const void *b);
/*! /*!
* \brief assumes a & b are int * * \brief Compares two integers for equality.
* \retval 0 if match *
* \retval 1 a > b * \param a an integer pointer (int *)
* \retval -1 a < b * \param b an integer pointer (int *)
*/ * \retval 0 if the integers pointed to are equal
* \retval 1 if a is greater than b
* \retval -1 if a is less than b
*/
int ast_hashtab_compare_ints(const void *a, const void *b); int ast_hashtab_compare_ints(const void *a, const void *b);
/*! /*!
* \brief assumes a & b are short * * \brief Compares two shorts for equality.
* \retval 0 if match *
* \retval 1 a > b * \param a a short pointer (short *)
* \retval -1 a < b * \param b a short pointer (short *)
*/ * \retval 0 if the shorts pointed to are equal
* \retval 1 if a is greater than b
* \retval -1 if a is less than b
*/
int ast_hashtab_compare_shorts(const void *a, const void *b); int ast_hashtab_compare_shorts(const void *a, const void *b);
/*! /*!
* \brief determine if resize should occur * \brief Determines if a table resize should occur using the Java algorithm
* \returns 1 if the table is 75% full or more * (if the table load factor is 75% or higher).
*/ *
* \param tab the hash table to operate on
* \retval 0 if the table load factor is less than or equal to 75%
* \retval 1 if the table load factor is greater than 75%
*/
int ast_hashtab_resize_java(struct ast_hashtab *tab); int ast_hashtab_resize_java(struct ast_hashtab *tab);
/*! \brief no resizing; always return 0 */ /*! \brief Causes a resize whenever the number of elements stored in the table
* exceeds the number of buckets in the table.
*
* \param tab the hash table to operate on
* \retval 0 if the number of elements in the table is less than or equal to
* the number of buckets
* \retval 1 if the number of elements in the table exceeds the number of
* buckets
*/
int ast_hashtab_resize_tight(struct ast_hashtab *tab); int ast_hashtab_resize_tight(struct ast_hashtab *tab);
/*! \brief no resizing; always return 0 */ /*!
* \brief Effectively disables resizing by always returning 0, regardless of
* of load factor.
*
* \param tab the hash table to operate on
* \return 0 is always returned
*/
int ast_hashtab_resize_none(struct ast_hashtab *tab); int ast_hashtab_resize_none(struct ast_hashtab *tab);
/*! \brief Create a prime number roughly 2x the current table size */ /*! \brief Create a prime number roughly 2x the current table size */
@ -160,18 +200,37 @@ int ast_hashtab_newsize_tight(struct ast_hashtab *tab);
/*! \brief always return current size -- no resizing */ /*! \brief always return current size -- no resizing */
int ast_hashtab_newsize_none(struct ast_hashtab *tab); int ast_hashtab_newsize_none(struct ast_hashtab *tab);
/*! /*!
* \brief Hashes a string to a number * \brief Hashes a string to a number
* \param obj *
* \note A modulus is applied so it in the range 0 to mod-1 * \param obj the string to hash
*/ * \return Integer hash of the specified string
* \sa ast_hashtable_hash_string_nocase
* \sa ast_hashtab_hash_string_sax
* \note A modulus will be applied to the return value of this function
*/
unsigned int ast_hashtab_hash_string(const void *obj); unsigned int ast_hashtab_hash_string(const void *obj);
/*! \brief Upperases each char before using them for a hash */ /*!
* \brief Hashes a string to a number ignoring case
*
* \param obj the string to hash
* \return Integer hash of the specified string
* \sa ast_hashtable_hash_string
* \sa ast_hashtab_hash_string_sax
* \note A modulus will be applied to the return value of this function
*/
unsigned int ast_hashtab_hash_string_nocase(const void *obj); unsigned int ast_hashtab_hash_string_nocase(const void *obj);
/*!
unsigned int ast_hashtab_hash_string_sax(const void *obj); /* from Josh */ * \brief Hashes a string to a number using a modified Shift-And-XOR algorithm
*
* \param obj the string to hash
* \return Integer has of the specified string
* \sa ast_hastable_hash_string
* \sa ast_hastable_hash_string_nocase
*/
unsigned int ast_hashtab_hash_string_sax(const void *obj);
unsigned int ast_hashtab_hash_int(const int num); /* right now, both these funcs are just result = num%modulus; */ unsigned int ast_hashtab_hash_int(const int num); /* right now, both these funcs are just result = num%modulus; */
@ -183,7 +242,7 @@ unsigned int ast_hashtab_hash_short(const short num);
/*! /*!
* \brief Create the hashtable list * \brief Create the hashtable list
* \param initial_buckets starting number of buckets * \param initial_buckets starting number of buckets
* \param compare a func ptr to compare two elements in the hash -- cannot be null * \param compare a func ptr to compare two elements in the hash -- cannot be null
* \param resize a func ptr to decide if the table needs to be resized, a NULL ptr here will cause a default to be used * \param resize a func ptr to decide if the table needs to be resized, a NULL ptr here will cause a default to be used
* \param newsize a func ptr that returns a new size of the array. A NULL will cause a default to be used * \param newsize a func ptr that returns a new size of the array. A NULL will cause a default to be used
* \param hash a func ptr to do the hashing * \param hash a func ptr to do the hashing
@ -191,23 +250,23 @@ unsigned int ast_hashtab_hash_short(const short num);
*/ */
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
struct ast_hashtab * _ast_hashtab_create(int initial_buckets, struct ast_hashtab * _ast_hashtab_create(int initial_buckets,
int (*compare)(const void *a, const void *b), int (*compare)(const void *a, const void *b),
int (*resize)(struct ast_hashtab *), int (*resize)(struct ast_hashtab *),
int (*newsize)(struct ast_hashtab *tab), int (*newsize)(struct ast_hashtab *tab),
unsigned int (*hash)(const void *obj), unsigned int (*hash)(const void *obj),
int do_locking, const char *file, int lineno, const char *function); int do_locking, const char *file, int lineno, const char *function);
#define ast_hashtab_create(a,b,c,d,e,f) _ast_hashtab_create(a,b,c,d,e,f,__FILE__,__LINE__,__PRETTY_FUNCTION__) #define ast_hashtab_create(a,b,c,d,e,f) _ast_hashtab_create(a,b,c,d,e,f,__FILE__,__LINE__,__PRETTY_FUNCTION__)
#else #else
struct ast_hashtab * ast_hashtab_create(int initial_buckets, struct ast_hashtab * ast_hashtab_create(int initial_buckets,
int (*compare)(const void *a, const void *b), int (*compare)(const void *a, const void *b),
int (*resize)(struct ast_hashtab *), int (*resize)(struct ast_hashtab *),
int (*newsize)(struct ast_hashtab *tab), int (*newsize)(struct ast_hashtab *tab),
unsigned int (*hash)(const void *obj), unsigned int (*hash)(const void *obj),
int do_locking ); int do_locking );
#endif #endif
/*! /*!
* \brief This func will free the hash table and all its memory. * \brief This func will free the hash table and all its memory.
* \note It doesn't touch the objects stored in it, unless you * \note It doesn't touch the objects stored in it, unless you
* specify a destroy func; it will call that func for each * specify a destroy func; it will call that func for each
* object in the hashtab, remove all the objects, and then * object in the hashtab, remove all the objects, and then
@ -220,14 +279,14 @@ void ast_hashtab_destroy( struct ast_hashtab *tab, void (*objdestroyfunc)(void *
/*! /*!
* \brief Insert without checking * \brief Insert without checking
* \param tab * \param tab
* \param obj * \param obj
* *
* Normally, you'd insert "safely" by checking to see if the element is * Normally, you'd insert "safely" by checking to see if the element is
* already there; in this case, you must already have checked. If an element * already there; in this case, you must already have checked. If an element
* is already in the hashtable, that matches this one, most likely this one * is already in the hashtable, that matches this one, most likely this one
* will be found first. * will be found first.
* \note will force a resize if the resize func returns 1 * \note will force a resize if the resize func returns 1
* \retval 1 on success * \retval 1 on success
* \retval 0 if there's a problem * \retval 0 if there's a problem
@ -239,7 +298,7 @@ int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj);
* \param tab * \param tab
* \param obj * \param obj
* \param h hashed index value * \param h hashed index value
* *
* \note Will force a resize if the resize func returns 1 * \note Will force a resize if the resize func returns 1
* \retval 1 on success * \retval 1 on success
* \retval 0 if there's a problem * \retval 0 if there's a problem
@ -250,14 +309,14 @@ int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj
* \brief Check and insert new object only if it is not there. * \brief Check and insert new object only if it is not there.
* \note Will force a resize if the resize func returns 1 * \note Will force a resize if the resize func returns 1
* \retval 1 on success * \retval 1 on success
* \retval 0 if there's a problem, or it's already there. * \retval 0 if there's a problem, or it's already there.
*/ */
int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj); int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj);
/*! /*!
* \brief Lookup this object in the hash table. * \brief Lookup this object in the hash table.
* \param tab * \param tab
* \param obj * \param obj
* \retval a ptr if found * \retval a ptr if found
* \retval NULL if not found * \retval NULL if not found
*/ */
@ -324,15 +383,15 @@ void *ast_hashtab_remove_this_object_nolock(struct ast_hashtab *tab, void *obj);
following locking routines yourself to lock the table between threads. */ following locking routines yourself to lock the table between threads. */
/*! \brief Call this after you create the table to init the lock */ /*! \brief Call this after you create the table to init the lock */
void ast_hashtab_initlock(struct ast_hashtab *tab); void ast_hashtab_initlock(struct ast_hashtab *tab);
/*! \brief Request a write-lock on the table. */ /*! \brief Request a write-lock on the table. */
void ast_hashtab_wrlock(struct ast_hashtab *tab); void ast_hashtab_wrlock(struct ast_hashtab *tab);
/*! \brief Request a read-lock on the table -- don't change anything! */ /*! \brief Request a read-lock on the table -- don't change anything! */
void ast_hashtab_rdlock(struct ast_hashtab *tab); void ast_hashtab_rdlock(struct ast_hashtab *tab);
/*! \brief release a read- or write- lock. */ /*! \brief release a read- or write- lock. */
void ast_hashtab_unlock(struct ast_hashtab *tab); void ast_hashtab_unlock(struct ast_hashtab *tab);
/*! \brief Call this before you destroy the table. */ /*! \brief Call this before you destroy the table. */
void ast_hashtab_destroylock(struct ast_hashtab *tab); void ast_hashtab_destroylock(struct ast_hashtab *tab);
#endif #endif

@ -38,7 +38,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/linkedlists.h" #include "asterisk/linkedlists.h"
#include "asterisk/hashtab.h" #include "asterisk/hashtab.h"
static void ast_hashtab_resize( struct ast_hashtab *tab); static void ast_hashtab_resize(struct ast_hashtab *tab);
static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *obj, unsigned int h); static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *obj, unsigned int h);
/* some standard, default routines for general use */ /* some standard, default routines for general use */
@ -60,7 +60,7 @@ int ast_hashtab_compare_ints(const void *a, const void *b)
if (ai < bi) if (ai < bi)
return -1; return -1;
return !(ai == bi); return !(ai == bi);
} }
@ -68,7 +68,7 @@ int ast_hashtab_compare_shorts(const void *a, const void *b)
{ {
short as = *((short *) a); short as = *((short *) a);
short bs = *((short *) b); short bs = *((short *) b);
if (as < bs) if (as < bs)
return -1; return -1;
@ -95,10 +95,10 @@ int ast_hashtab_resize_none(struct ast_hashtab *tab) /* always return 0 -- no re
int ast_is_prime(int num) int ast_is_prime(int num)
{ {
int tnum, limit; int tnum, limit;
if (!(num & 0x1)) /* even number -- not prime */ if (!(num & 0x1)) /* even number -- not prime */
return 0; return 0;
/* Loop through ODD numbers starting with 3 */ /* Loop through ODD numbers starting with 3 */
tnum = 3; tnum = 3;
@ -109,20 +109,19 @@ int ast_is_prime(int num)
/* really, we only need to check sqrt(num) numbers */ /* really, we only need to check sqrt(num) numbers */
limit = num / tnum; limit = num / tnum;
/* we only check odd numbers */ /* we only check odd numbers */
tnum = tnum + 2; tnum = tnum + 2;
} }
/* if we made it through the loop, the number is a prime */ /* if we made it through the loop, the number is a prime */
return 1; return 1;
} }
int ast_hashtab_newsize_java(struct ast_hashtab *tab) int ast_hashtab_newsize_java(struct ast_hashtab *tab)
{ {
int i = (tab->hash_tab_size << 1); /* multiply by two */ int i = (tab->hash_tab_size << 1); /* multiply by two */
while (!ast_is_prime(i)) while (!ast_is_prime(i))
i++; i++;
@ -133,7 +132,7 @@ int ast_hashtab_newsize_tight(struct ast_hashtab *tab)
{ {
int x = (tab->hash_tab_size << 1); int x = (tab->hash_tab_size << 1);
int i = (tab->hash_tab_size + x); int i = (tab->hash_tab_size + x);
while (!ast_is_prime(i)) while (!ast_is_prime(i))
i++; i++;
@ -150,14 +149,13 @@ unsigned int ast_hashtab_hash_string(const void *obj)
unsigned char *str = (unsigned char *) obj; unsigned char *str = (unsigned char *) obj;
unsigned int total; unsigned int total;
for (total = 0; *str; str++) for (total = 0; *str; str++) {
{
unsigned int tmp = total; unsigned int tmp = total;
total <<= 1; /* multiply by 2 */ total <<= 1; /* multiply by 2 */
total += tmp; /* multiply by 3 */ total += tmp; /* multiply by 3 */
total <<= 2; /* multiply by 12 */ total <<= 2; /* multiply by 12 */
total += tmp; /* multiply by 13 */ total += tmp; /* multiply by 13 */
total += ((unsigned int)(*str)); total += ((unsigned int)(*str));
} }
return total; return total;
@ -184,14 +182,14 @@ unsigned int ast_hashtab_hash_string_nocase(const void *obj)
unsigned int charval = toupper(*str); unsigned int charval = toupper(*str);
/* hopefully, the following is faster than multiplication by 7 */ /* hopefully, the following is faster than multiplication by 7 */
/* why do I go to this bother? A good compiler will do this /* why do I go to this bother? A good compiler will do this
anyway, if I say total *= 13 */ anyway, if I say total *= 13 */
/* BTW, tried *= 7, and it doesn't do as well in spreading things around! */ /* BTW, tried *= 7, and it doesn't do as well in spreading things around! */
total <<= 1; /* multiply by 2 */ total <<= 1; /* multiply by 2 */
total += tmp; /* multiply by 3 */ total += tmp; /* multiply by 3 */
total <<= 2; /* multiply by 12 */ total <<= 2; /* multiply by 12 */
total += tmp; /* multiply by 13 */ total += tmp; /* multiply by 13 */
total += (charval); total += (charval);
} }
@ -215,11 +213,11 @@ _ast_hashtab_create
#else #else
ast_hashtab_create ast_hashtab_create
#endif #endif
(int initial_buckets, (int initial_buckets,
int (*compare)(const void *a, const void *b), int (*compare)(const void *a, const void *b),
int (*resize)(struct ast_hashtab *), int (*resize)(struct ast_hashtab *),
int (*newsize)(struct ast_hashtab *tab), int (*newsize)(struct ast_hashtab *tab),
unsigned int (*hash)(const void *obj), unsigned int (*hash)(const void *obj),
int do_locking int do_locking
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
, const char *file, int lineno, const char *function , const char *file, int lineno, const char *function
@ -363,12 +361,12 @@ void ast_hashtab_unlock(struct ast_hashtab *tab)
ast_rwlock_unlock(&tab->lock); ast_rwlock_unlock(&tab->lock);
} }
void ast_hashtab_destroy( struct ast_hashtab *tab, void (*objdestroyfunc)(void *obj)) void ast_hashtab_destroy(struct ast_hashtab *tab, void (*objdestroyfunc)(void *obj))
{ {
/* this func will free the hash table and all its memory. It /* this func will free the hash table and all its memory. It
doesn't touch the objects stored in it */ doesn't touch the objects stored in it */
if (tab) { if (tab) {
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock); ast_rwlock_wrlock(&tab->lock);
@ -376,19 +374,24 @@ void ast_hashtab_destroy( struct ast_hashtab *tab, void (*objdestroyfunc)(void *
/* go thru and destroy the buckets */ /* go thru and destroy the buckets */
struct ast_hashtab_bucket *t; struct ast_hashtab_bucket *t;
int i; int i;
while (tab->tlist) { while (tab->tlist) {
t = tab->tlist; t = tab->tlist;
if (t->object && objdestroyfunc) if (t->object && objdestroyfunc) {
(*objdestroyfunc)((void *) t->object); /* I cast this because I'm not going to MOD it, I'm going to DESTROY it */ /* I cast this because I'm not going to MOD it, I'm going to DESTROY
* it.
*/
(*objdestroyfunc)((void *) t->object);
}
tlist_del_item(&(tab->tlist), tab->tlist); tlist_del_item(&(tab->tlist), tab->tlist);
free(t); free(t);
} }
for (i = 0; i < tab->hash_tab_size; i++) for (i = 0; i < tab->hash_tab_size; i++) {
tab->array[i] = NULL; /* not totally necc., but best to destroy old ptrs */ /* Not totally necessary, but best to destroy old pointers */
tab->array[i] = NULL;
}
free(tab->array); free(tab->array);
} }
if (tab->do_locking) { if (tab->do_locking) {
@ -403,7 +406,7 @@ int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj)
{ {
unsigned int h; unsigned int h;
int res=0; int res=0;
if (!tab || !obj) if (!tab || !obj)
return res; return res;
@ -424,10 +427,10 @@ int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj
{ {
int c; int c;
struct ast_hashtab_bucket *b; struct ast_hashtab_bucket *b;
if (!tab || !obj) if (!tab || !obj)
return 0; return 0;
for (c = 0, b = tab->array[h]; b; b= b->next) for (c = 0, b = tab->array[h]; b; b= b->next)
c++; c++;
@ -469,13 +472,13 @@ int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj)
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_unlock(&tab->lock); ast_rwlock_unlock(&tab->lock);
return ret2; return ret2;
} }
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_unlock(&tab->lock); ast_rwlock_unlock(&tab->lock);
return 0; return 0;
} }
@ -487,7 +490,7 @@ void *ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
if (!tab || !obj) if (!tab || !obj)
return 0; return 0;
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_rdlock(&tab->lock); ast_rwlock_rdlock(&tab->lock);
@ -510,14 +513,14 @@ void *ast_hashtab_lookup_with_hash(struct ast_hashtab *tab, const void *obj, uns
if (!tab || !obj) if (!tab || !obj)
return 0; return 0;
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_rdlock(&tab->lock); ast_rwlock_rdlock(&tab->lock);
h = hashval % tab->hash_tab_size; h = hashval % tab->hash_tab_size;
ret = ast_hashtab_lookup_internal(tab,obj,h); ret = ast_hashtab_lookup_internal(tab,obj,h);
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_unlock(&tab->lock); ast_rwlock_unlock(&tab->lock);
@ -532,13 +535,13 @@ void *ast_hashtab_lookup_bucket(struct ast_hashtab *tab, const void *obj, unsign
if (!tab || !obj) if (!tab || !obj)
return 0; return 0;
h = (*tab->hash)(obj) % tab->hash_tab_size; h = (*tab->hash)(obj) % tab->hash_tab_size;
ret = ast_hashtab_lookup_internal(tab,obj,h); ret = ast_hashtab_lookup_internal(tab,obj,h);
*bucket = h; *bucket = h;
return ret; return ret;
} }
@ -548,14 +551,15 @@ static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *ob
for (b = tab->array[h]; b; b = b->next) { for (b = tab->array[h]; b; b = b->next) {
if (!(*tab->compare)(obj,b->object)) { if (!(*tab->compare)(obj,b->object)) {
return (void*) b->object; /* I can't touch obj in this func, but the outside world is welcome to */ /* I can't touch obj in this func, but the outside world is welcome to */
return (void*) b->object;
} }
} }
return NULL; return NULL;
} }
void ast_hashtab_get_stats( struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets) void ast_hashtab_get_stats(struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets)
{ {
/* returns key stats for the table */ /* returns key stats for the table */
if (tab->do_locking) if (tab->do_locking)
@ -568,24 +572,21 @@ void ast_hashtab_get_stats( struct ast_hashtab *tab, int *biggest_bucket_size, i
ast_rwlock_unlock(&tab->lock); ast_rwlock_unlock(&tab->lock);
} }
/* this function returns the number of elements stored in the hashtab */ /* this function returns the number of elements stored in the hashtab */
int ast_hashtab_size( struct ast_hashtab *tab) int ast_hashtab_size(struct ast_hashtab *tab)
{ {
return tab->hash_tab_elements; return tab->hash_tab_elements;
} }
/* this function returns the size of the bucket array in the hashtab */ /* this function returns the size of the bucket array in the hashtab */
int ast_hashtab_capacity( struct ast_hashtab *tab) int ast_hashtab_capacity( struct ast_hashtab *tab)
{ {
return tab->hash_tab_size; return tab->hash_tab_size;
} }
/* the insert operation calls this, and is wrlock'd when it does. */ /* the insert operation calls this, and is wrlock'd when it does. */
/* if you want to call it, you should set the wrlock yourself */ /* if you want to call it, you should set the wrlock yourself */
static void ast_hashtab_resize( struct ast_hashtab *tab) static void ast_hashtab_resize( struct ast_hashtab *tab)
{ {
/* this function is called either internally, when the resize func returns 1, or /* this function is called either internally, when the resize func returns 1, or
@ -593,10 +594,10 @@ static void ast_hashtab_resize( struct ast_hashtab *tab)
int newsize = (*tab->newsize)(tab), i, c; int newsize = (*tab->newsize)(tab), i, c;
unsigned int h; unsigned int h;
struct ast_hashtab_bucket *b,*bn; struct ast_hashtab_bucket *b,*bn;
/* Since we keep a DLL of all the buckets in tlist, /* Since we keep a DLL of all the buckets in tlist,
all we have to do is free the array, malloc a new one, all we have to do is free the array, malloc a new one,
and then go thru the tlist array and reassign them into and then go thru the tlist array and reassign them into
the bucket arrayj. the bucket arrayj.
*/ */
for (i = 0; i < tab->hash_tab_size; i++) { /* don't absolutely have to do this, but for (i = 0; i < tab->hash_tab_size; i++) { /* don't absolutely have to do this, but
@ -634,7 +635,7 @@ struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab)
{ {
/* returns an iterator */ /* returns an iterator */
struct ast_hashtab_iter *it; struct ast_hashtab_iter *it;
if (!(it = ast_calloc(1, sizeof(*it)))) if (!(it = ast_calloc(1, sizeof(*it))))
return NULL; return NULL;
@ -651,7 +652,7 @@ struct ast_hashtab_iter *ast_hashtab_start_write_traversal(struct ast_hashtab *t
{ {
/* returns an iterator */ /* returns an iterator */
struct ast_hashtab_iter *it; struct ast_hashtab_iter *it;
if (!(it = ast_calloc(1, sizeof(*it)))) if (!(it = ast_calloc(1, sizeof(*it))))
return NULL; return NULL;
@ -674,7 +675,7 @@ void *ast_hashtab_next(struct ast_hashtab_iter *it)
{ {
/* returns the next object in the list, advances iter one step */ /* returns the next object in the list, advances iter one step */
struct ast_hashtab_bucket *retval; struct ast_hashtab_bucket *retval;
if (it && it->next) { /* there's a next in the bucket list */ if (it && it->next) { /* there's a next in the bucket list */
retval = it->next; retval = it->next;
it->next = retval->tnext; it->next = retval->tnext;
@ -687,17 +688,17 @@ void *ast_hashtab_next(struct ast_hashtab_iter *it)
static void *ast_hashtab_remove_object_internal(struct ast_hashtab *tab, struct ast_hashtab_bucket *b, int h) static void *ast_hashtab_remove_object_internal(struct ast_hashtab *tab, struct ast_hashtab_bucket *b, int h)
{ {
const void *obj2; const void *obj2;
if (b->prev) if (b->prev)
b->prev->next = b->next; b->prev->next = b->next;
else else
tab->array[h] = b->next; tab->array[h] = b->next;
if (b->next) if (b->next)
b->next->prev = b->prev; b->next->prev = b->prev;
tlist_del_item(&(tab->tlist), b); tlist_del_item(&(tab->tlist), b);
obj2 = b->object; obj2 = b->object;
b->object = b->next = (void*)2; b->object = b->next = (void*)2;
free(b); /* free up the hashbucket */ free(b); /* free up the hashbucket */
@ -763,12 +764,12 @@ void *ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab *tab, void
h = (*tab->hash)(obj) % tab->hash_tab_size; h = (*tab->hash)(obj) % tab->hash_tab_size;
for (b = tab->array[h]; b; b = b->next) { for (b = tab->array[h]; b; b = b->next) {
if (!(*tab->compare)(obj, b->object)) { if (!(*tab->compare)(obj, b->object)) {
const void *obj2; const void *obj2;
obj2 = ast_hashtab_remove_object_internal(tab, b, h); obj2 = ast_hashtab_remove_object_internal(tab, b, h);
return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */ return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
} }
} }
@ -785,12 +786,12 @@ void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj)
if (!tab || !obj) if (!tab || !obj)
return 0; return 0;
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_wrlock(&tab->lock); ast_rwlock_wrlock(&tab->lock);
obj2 = ast_hashtab_remove_this_object_nolock(tab,obj); obj2 = ast_hashtab_remove_this_object_nolock(tab,obj);
if (tab->do_locking) if (tab->do_locking)
ast_rwlock_unlock(&tab->lock); ast_rwlock_unlock(&tab->lock);
@ -807,14 +808,14 @@ void *ast_hashtab_remove_this_object_nolock(struct ast_hashtab *tab, void *obj)
if (!tab || !obj) if (!tab || !obj)
return 0; return 0;
h = (*tab->hash)(obj) % tab->hash_tab_size; h = (*tab->hash)(obj) % tab->hash_tab_size;
for (b = tab->array[h]; b; b = b->next) { for (b = tab->array[h]; b; b = b->next) {
if (obj == b->object) { if (obj == b->object) {
const void *obj2; const void *obj2;
obj2 = ast_hashtab_remove_object_internal(tab, b, h); obj2 = ast_hashtab_remove_object_internal(tab, b, h);
return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */ return (void *) obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
} }
} }

Loading…
Cancel
Save