[asterisk-commits] seanbright: trunk r177884 - in /trunk: include/asterisk/hashtab.h main/hashtab.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Feb 21 08:16:44 CST 2009


Author: seanbright
Date: Sat Feb 21 08:16:44 2009
New Revision: 177884

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=177884
Log:
Trailing whitespace, minor coding guideline fixes, and start beefing up the
hashtab documentation a bit.

Modified:
    trunk/include/asterisk/hashtab.h
    trunk/main/hashtab.c

Modified: trunk/include/asterisk/hashtab.h
URL: http://svn.digium.com/svn-view/asterisk/trunk/include/asterisk/hashtab.h?view=diff&rev=177884&r1=177883&r2=177884
==============================================================================
--- trunk/include/asterisk/hashtab.h (original)
+++ trunk/include/asterisk/hashtab.h Sat Feb 21 08:16:44 2009
@@ -81,12 +81,12 @@
 {
 	struct ast_hashtab_bucket **array;
 	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 */
 	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 */
-	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.*/
 	int hash_tab_size;                            /*!< the size of the bucket array */
 	int hash_tab_elements;                        /*!< the number of objects currently stored in the table */
@@ -108,47 +108,87 @@
 
 /* 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);
 
-/*! 
- * \brief assumes a and b are char * 
- * \return 0 if they match 
-*/
+/*!
+ * \brief Compares two strings for equality.
+ *
+ * \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);
 
 /*!
- * \brief assumes a & b are strings
- * \return 0 if they match (strcasecmp) 
-*/
+ * \brief Compares two strings for equality, ignoring case.
+ *
+ * \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);
 
 /*!
- * \brief assumes a & b are int *
- * \retval 0 if match
- * \retval 1 a > b
- * \retval -1 a < b
-*/
+ * \brief Compares two integers for equality.
+ *
+ * \param a an integer pointer (int *)
+ * \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);
 
 /*!
- * \brief assumes a & b are short *
- * \retval 0 if match
- * \retval 1 a > b
- * \retval -1 a < b
-*/
+ * \brief Compares two shorts for equality.
+ *
+ * \param a a short pointer (short *)
+ * \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);
 
 /*!
- * \brief determine if resize should occur
- * \returns 1 if the table is 75% full or more
-*/
+ * \brief Determines if a table resize should occur using the Java algorithm
+ *        (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);
 
-/*! \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);
 
-/*! \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);
 
 /*! \brief Create a prime number roughly 2x the current table size */
@@ -160,18 +200,37 @@
 /*! \brief always return current size -- no resizing */
 int ast_hashtab_newsize_none(struct ast_hashtab *tab);
 
-/*! 
+/*!
  * \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);
 
-/*! \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_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; */
@@ -183,7 +242,7 @@
 /*!
  * \brief Create the hashtable list
  * \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 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
@@ -191,23 +250,23 @@
 */
 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
 struct ast_hashtab * _ast_hashtab_create(int initial_buckets,
-					int (*compare)(const void *a, const void *b), 
-					int (*resize)(struct ast_hashtab *),	
+					int (*compare)(const void *a, const void *b),
+					int (*resize)(struct ast_hashtab *),
 					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);
 #define ast_hashtab_create(a,b,c,d,e,f)	_ast_hashtab_create(a,b,c,d,e,f,__FILE__,__LINE__,__PRETTY_FUNCTION__)
 #else
 struct ast_hashtab * ast_hashtab_create(int initial_buckets,
-					int (*compare)(const void *a, const void *b), 
-					int (*resize)(struct ast_hashtab *),	
+					int (*compare)(const void *a, const void *b),
+					int (*resize)(struct ast_hashtab *),
 					int (*newsize)(struct ast_hashtab *tab),
-					unsigned int (*hash)(const void *obj), 
+					unsigned int (*hash)(const void *obj),
 					int do_locking );
 #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
  *       specify a destroy func; it will call that func for each
  *       object in the hashtab, remove all the objects, and then
@@ -220,14 +279,14 @@
 
 
 /*!
- * \brief Insert without checking 
+ * \brief Insert without checking
  * \param tab
  * \param obj
  *
  * 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
  * 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
  * \retval 1 on success
  * \retval 0 if there's a problem
@@ -239,7 +298,7 @@
  * \param tab
  * \param obj
  * \param h hashed index value
- * 
+ *
  * \note Will force a resize if the resize func returns 1
  * \retval 1 on success
  * \retval 0 if there's a problem
@@ -250,14 +309,14 @@
  * \brief Check and insert new object only if it is not there.
  * \note Will force a resize if the resize func returns 1
  * \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);
 
 /*!
- * \brief Lookup this object in the hash table. 
- * \param tab 
- * \param obj 
+ * \brief Lookup this object in the hash table.
+ * \param tab
+ * \param obj
  * \retval a ptr if found
  * \retval NULL if not found
 */
@@ -324,15 +383,15 @@
    following locking routines yourself to lock the table between threads. */
 
 /*! \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. */
-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! */
-void ast_hashtab_rdlock(struct ast_hashtab *tab); 
+void ast_hashtab_rdlock(struct ast_hashtab *tab);
 /*! \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. */
-void ast_hashtab_destroylock(struct ast_hashtab *tab); 
+void ast_hashtab_destroylock(struct ast_hashtab *tab);
 
 
 #endif

Modified: trunk/main/hashtab.c
URL: http://svn.digium.com/svn-view/asterisk/trunk/main/hashtab.c?view=diff&rev=177884&r1=177883&r2=177884
==============================================================================
--- trunk/main/hashtab.c (original)
+++ trunk/main/hashtab.c Sat Feb 21 08:16:44 2009
@@ -38,7 +38,7 @@
 #include "asterisk/linkedlists.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);
 
 /* some standard, default routines for general use */
@@ -60,7 +60,7 @@
 
 	if (ai < bi)
 		return -1;
-	
+
 	return !(ai == bi);
 }
 
@@ -68,7 +68,7 @@
 {
 	short as = *((short *) a);
 	short bs = *((short *) b);
-	
+
 	if (as < bs)
 		return -1;
 
@@ -95,10 +95,10 @@
 int ast_is_prime(int num)
 {
 	int tnum, limit;
-	    
+
 	if (!(num & 0x1)) /* even number -- not prime */
 		return 0;
-		    
+
 	/* Loop through ODD numbers starting with 3 */
 
 	tnum = 3;
@@ -109,20 +109,19 @@
 
 		/* really, we only need to check sqrt(num) numbers */
 		limit = num / tnum;
-		
+
 		/* we only check odd numbers */
 		tnum = tnum + 2;
 	}
 
 	/* if we made it through the loop, the number is a prime */
-
 	return 1;
 }
 
 int ast_hashtab_newsize_java(struct ast_hashtab *tab)
 {
 	int i = (tab->hash_tab_size << 1); /* multiply by two */
-	
+
 	while (!ast_is_prime(i))
 		i++;
 
@@ -133,7 +132,7 @@
 {
 	int x = (tab->hash_tab_size << 1);
 	int i = (tab->hash_tab_size + x);
-	
+
 	while (!ast_is_prime(i))
 		i++;
 
@@ -150,14 +149,13 @@
 	unsigned char *str = (unsigned char *) obj;
 	unsigned int total;
 
-	for (total = 0; *str; str++)
-	{
+	for (total = 0; *str; str++) {
 		unsigned int tmp = total;
 		total <<= 1; /* multiply by 2 */
 		total += tmp; /* multiply by 3 */
 		total <<= 2; /* multiply by 12 */
 		total += tmp; /* multiply by 13 */
-		
+
 		total += ((unsigned int)(*str));
 	}
 	return total;
@@ -184,14 +182,14 @@
 		unsigned int charval = toupper(*str);
 
 		/* 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 */
 		/* BTW, tried *= 7, and it doesn't do as well in spreading things around! */
 		total <<= 1; /* multiply by 2 */
 		total += tmp; /* multiply by 3 */
 		total <<= 2; /* multiply by 12 */
 		total += tmp; /* multiply by 13 */
-		
+
 		total += (charval);
 	}
 
@@ -215,11 +213,11 @@
 #else
 ast_hashtab_create
 #endif
-(int initial_buckets, 
-	int (*compare)(const void *a, const void *b), 
-	int (*resize)(struct ast_hashtab *), 
+(int initial_buckets,
+	int (*compare)(const void *a, const void *b),
+	int (*resize)(struct ast_hashtab *),
 	int (*newsize)(struct ast_hashtab *tab),
-	unsigned int (*hash)(const void *obj), 
+	unsigned int (*hash)(const void *obj),
 	int do_locking
 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
 	, const char *file, int lineno, const char *function
@@ -363,12 +361,12 @@
 	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
 	   doesn't touch the objects stored in it */
 	if (tab) {
-		
+
 		if (tab->do_locking)
 			ast_rwlock_wrlock(&tab->lock);
 
@@ -376,19 +374,24 @@
 			/* go thru and destroy the buckets */
 			struct ast_hashtab_bucket *t;
 			int i;
-			
+
 			while (tab->tlist) {
 				t = tab->tlist;
-				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 */
-				
+				if (t->object && objdestroyfunc) {
+					/* 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);
 				free(t);
 			}
-			
-			for (i = 0; i < tab->hash_tab_size; i++)
-				tab->array[i] = NULL; /* not totally necc., but best to destroy old ptrs */
-			
+
+			for (i = 0; i < tab->hash_tab_size; i++) {
+				/* Not totally necessary, but best to destroy old pointers */
+				tab->array[i] = NULL;
+			}
 			free(tab->array);
 		}
 		if (tab->do_locking) {
@@ -403,7 +406,7 @@
 {
 	unsigned int h;
 	int res=0;
-	
+
 	if (!tab || !obj)
 		return res;
 
@@ -424,10 +427,10 @@
 {
 	int c;
 	struct ast_hashtab_bucket *b;
-	
+
 	if (!tab || !obj)
 		return 0;
-	
+
 	for (c = 0, b = tab->array[h]; b; b= b->next)
 		c++;
 
@@ -469,13 +472,13 @@
 
 		if (tab->do_locking)
 			ast_rwlock_unlock(&tab->lock);
-		
+
 		return ret2;
 	}
 
 	if (tab->do_locking)
 		ast_rwlock_unlock(&tab->lock);
-		
+
 	return 0;
 }
 
@@ -487,7 +490,7 @@
 
 	if (!tab || !obj)
 		return 0;
-	
+
 	if (tab->do_locking)
 		ast_rwlock_rdlock(&tab->lock);
 
@@ -510,14 +513,14 @@
 
 	if (!tab || !obj)
 		return 0;
-	
+
 	if (tab->do_locking)
 		ast_rwlock_rdlock(&tab->lock);
-		
+
 	h = hashval % tab->hash_tab_size;
 
 	ret = ast_hashtab_lookup_internal(tab,obj,h);
-	
+
 	if (tab->do_locking)
 		ast_rwlock_unlock(&tab->lock);
 
@@ -532,13 +535,13 @@
 
 	if (!tab || !obj)
 		return 0;
-	
+
 	h = (*tab->hash)(obj) % tab->hash_tab_size;
-	
+
 	ret = ast_hashtab_lookup_internal(tab,obj,h);
-	
-	*bucket = h; 
-	
+
+	*bucket = h;
+
 	return ret;
 }
 
@@ -548,14 +551,15 @@
 
 	for (b = tab->array[h]; b; b = b->next) {
 		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;
 }
 
-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 */
 	if (tab->do_locking)
@@ -568,23 +572,20 @@
 		ast_rwlock_unlock(&tab->lock);
 }
 
-	/* this function returns the number of elements stored in the hashtab */
-int  ast_hashtab_size( struct ast_hashtab *tab)
+/* this function returns the number of elements stored in the hashtab */
+int ast_hashtab_size(struct ast_hashtab *tab)
 {
 	return tab->hash_tab_elements;
 }
 
-	/* this function returns the size of the bucket array in the hashtab */
-int  ast_hashtab_capacity( struct ast_hashtab *tab)
+/* this function returns the size of the bucket array in the hashtab */
+int ast_hashtab_capacity( struct ast_hashtab *tab)
 {
 	return tab->hash_tab_size;
 }
-
-
 
 /* the insert operation calls this, and is wrlock'd when it does. */
 /* if you want to call it, you should set the wrlock yourself */
-
 
 static void ast_hashtab_resize( struct ast_hashtab *tab)
 {
@@ -593,10 +594,10 @@
 	int newsize = (*tab->newsize)(tab), i, c;
 	unsigned int h;
 	struct ast_hashtab_bucket *b,*bn;
-	
+
 	/* Since we keep a DLL of all the buckets in tlist,
 	   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.
 	*/
 	for (i = 0; i < tab->hash_tab_size; i++) { /* don't absolutely have to do this, but
@@ -634,7 +635,7 @@
 {
 	/* returns an iterator */
 	struct ast_hashtab_iter *it;
-	
+
 	if (!(it = ast_calloc(1, sizeof(*it))))
 		return NULL;
 
@@ -651,7 +652,7 @@
 {
 	/* returns an iterator */
 	struct ast_hashtab_iter *it;
-	
+
 	if (!(it = ast_calloc(1, sizeof(*it))))
 		return NULL;
 
@@ -674,7 +675,7 @@
 {
 	/* returns the next object in the list, advances iter one step */
 	struct ast_hashtab_bucket *retval;
-	
+
 	if (it && it->next) { /* there's a next in the bucket list */
 		retval = it->next;
 		it->next = retval->tnext;
@@ -687,17 +688,17 @@
 static void *ast_hashtab_remove_object_internal(struct ast_hashtab *tab, struct ast_hashtab_bucket *b, int h)
 {
 	const void *obj2;
-	
+
 	if (b->prev)
 		b->prev->next = b->next;
 	else
 		tab->array[h] = b->next;
-	
+
 	if (b->next)
 		b->next->prev = b->prev;
-	
+
 	tlist_del_item(&(tab->tlist), b);
-	
+
 	obj2 = b->object;
 	b->object = b->next = (void*)2;
 	free(b); /* free up the hashbucket */
@@ -763,12 +764,12 @@
 
 	h = (*tab->hash)(obj) % tab->hash_tab_size;
 	for (b = tab->array[h]; b; b = b->next) {
-		
+
 		if (!(*tab->compare)(obj, b->object)) {
 			const void *obj2;
 
 			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 */
 		}
 	}
@@ -785,12 +786,12 @@
 
 	if (!tab || !obj)
 		return 0;
- 
+
 	if (tab->do_locking)
 		ast_rwlock_wrlock(&tab->lock);
 
 	obj2 = ast_hashtab_remove_this_object_nolock(tab,obj);
-	
+
 	if (tab->do_locking)
 		ast_rwlock_unlock(&tab->lock);
 
@@ -807,14 +808,14 @@
 
 	if (!tab || !obj)
 		return 0;
- 
+
 	h = (*tab->hash)(obj) % tab->hash_tab_size;
 	for (b = tab->array[h]; b; b = b->next) {
-		
+
 		if (obj == b->object) {
 			const void *obj2;
 			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 */
 		}
 	}




More information about the asterisk-commits mailing list