[svn-commits] murf: branch murf/fast-ast r47255 - /team/murf/fast-ast/main/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Tue Nov 7 06:59:34 MST 2006


Author: murf
Date: Tue Nov  7 07:59:33 2006
New Revision: 47255

URL: http://svn.digium.com/view/asterisk?rev=47255&view=rev
Log:
Some improvements/cleanups

Modified:
    team/murf/fast-ast/main/hashtab.c
    team/murf/fast-ast/main/hashtab.h
    team/murf/fast-ast/main/stringtab.c
    team/murf/fast-ast/main/stringtab.h

Modified: team/murf/fast-ast/main/hashtab.c
URL: http://svn.digium.com/view/asterisk/team/murf/fast-ast/main/hashtab.c?rev=47255&r1=47254&r2=47255&view=diff
==============================================================================
--- team/murf/fast-ast/main/hashtab.c (original)
+++ team/murf/fast-ast/main/hashtab.c Tue Nov  7 07:59:33 2006
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * Copyright (C) 2006, Digium, Inc.
  *
  * Steve Murphy <murf at digium.com>
  *
@@ -70,8 +70,8 @@
 
 int ast_hashtab_resize_tight(struct ast_hashtab *tab)
 {
-	double loadfactor = (double)tab->hash_tab_elements / (double)tab->hash_tab_size;
-	if (loadfactor > 1.0)
+	
+	if (tab->hash_tab_elements > tab->hash_tab_size) /* this is quicker than division */
 		return 1;
 	return 0;
 }
@@ -111,7 +111,8 @@
 
 int ast_hashtab_newsize_tight(struct ast_hashtab *tab)
 {
-	int i = (tab->hash_tab_size<<1); /* multiply by two */
+	int x = (tab->hash_tab_size<<1);
+	int i = (tab->hash_tab_size+x);
 	while (!isPrime(i))
 		i++;
 	return i;
@@ -146,6 +147,9 @@
 		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 
+		   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 */
@@ -174,10 +178,10 @@
 										int (*hash)(const void *obj, int modulus), /* a func to do the hashing */
 										int do_locking ) /* use locks to guarantee safety of iterators/insertion/deletion */
 {
-	struct ast_hashtab *ht = (struct ast_hashtab *)calloc(1,sizeof(struct ast_hashtab));
+	struct ast_hashtab *ht = ast_calloc(1,sizeof(struct ast_hashtab));
 	while (!isPrime(initial_buckets)) /* make sure this is prime */
 		initial_buckets++;
-	ht->array = (struct ast_hashtab_bucket **)calloc(initial_buckets,sizeof(struct ast_hashtab_bucket*));
+	ht->array = ast_calloc(initial_buckets,sizeof(struct ast_hashtab_bucket*));
 	ht->hash_tab_size = initial_buckets;
 	ht->compare = compare;
 	ht->resize = resize;
@@ -218,7 +222,7 @@
 	}
 }
 
-int ast_hashtab_insert_immediate(struct ast_hashtab *tab, void *obj)
+int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *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
@@ -239,7 +243,7 @@
 	}
 	if (c+1 > tab->largest_bucket_size)
 		tab->largest_bucket_size = c+1;
-	b = (struct ast_hashtab_bucket*)malloc(sizeof(struct ast_hashtab_bucket));
+	b = ast_malloc(sizeof(struct ast_hashtab_bucket));
 	b->object = obj;
 	b->next = tab->array[h];
 	tab->array[h] = b;
@@ -249,7 +253,7 @@
 	return 1;
 }
 
-int ast_hashtab_insert_safe(struct ast_hashtab *tab, void *obj)
+int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj)
 {
 	/* check to see if the element is already there; insert only if
 	   it is not there. */
@@ -262,7 +266,7 @@
 	return 0;
 }
 
-void * ast_hashtab_lookup(struct ast_hashtab *tab, void *obj)
+void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj)
 {
 	/* lookup this object in the hash table. return a ptr if found, or NULL if not */
 	int h;
@@ -273,7 +277,7 @@
 	for(b=tab->array[h]; b; b=b->next)
 	{
 		if( (*tab->compare)(obj,b->object) == 0 )
-			return b->object;
+			return (void*)b->object; /* I can't touch obj in this func, but the outside world is welcome to */
 	}
 	return 0;
 }
@@ -309,7 +313,7 @@
 		tab->array[i] = 0; /* erase old ptrs */
 	}
 	free(tab->array);
-	tab->array = (struct ast_hashtab_bucket **)calloc(newsize,sizeof(struct ast_hashtab_bucket *));
+	tab->array = ast_calloc(newsize,sizeof(struct ast_hashtab_bucket *));
 	/* now sort the buckets into their rightful new slots */
 	tab->resize_count++;
 	tab->hash_tab_size = newsize;
@@ -337,7 +341,7 @@
 struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab)
 {
 	/* returns an iterator */
-	struct ast_hashtab_iter *it = (struct ast_hashtab_iter *)malloc(sizeof(struct ast_hashtab_iter));
+	struct ast_hashtab_iter *it = ast_malloc(sizeof(struct ast_hashtab_iter));
 	it->bucket_num = 0;
 	
 	it->curr = tab->array[0];
@@ -354,14 +358,14 @@
 	if( it->curr && it->curr->next )  /* there's a next in the bucket list */
 	{
 		it->curr = it->curr->next;
-		return it->curr->object;
+		return (void*)it->curr->object;
 	} else if( !it->curr && it->bucket_num == 0 ){ /* first call to this */
 		for(num=it->bucket_num; num<it->tab->hash_tab_size; num++)
 		{
 			if (it->tab->array[num]) {
 				it->bucket_num = num;
 				it->curr = it->tab->array[num];
-				return it->curr->object;
+				return (void*)it->curr->object; /* inside the hash code, the obj's are untouchable, but outside... */
 			} else {
 				continue; /* try the next bucket */
 			}
@@ -374,7 +378,7 @@
 			if (it->tab->array[num]) {
 				it->bucket_num = num;
 				it->curr = it->tab->array[num];
-				return it->curr->object;
+				return (void*)it->curr->object; /* inside the hash code, the obj's are untouchable, but outside... */
 			} else {
 				continue; /* try the next bucket */
 			}
@@ -400,7 +404,7 @@
 	h = (*tab->hash)(obj, tab->hash_tab_size);
 	for(b=tab->array[h]; b; b=b->next)
 	{
-		void *obj2;
+		const void *obj2;
 		
 		if ((*tab->compare)(obj,b->object) == 0) {
 			if (last)
@@ -410,7 +414,7 @@
 			obj2 = b->object;
 			b->object = b->next = 0;
 			free(b); /* free up the hashbucket */
-			return obj2;
+			return (void*)obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
 		}
 		last = b;
 	}
@@ -432,7 +436,7 @@
 	h = (*tab->hash)(obj, tab->hash_tab_size);
 	for (b=tab->array[h]; b; b=b->next)
 	{
-		void *obj2;
+		const void *obj2;
 		
 		if (obj == b->object) {
 			if (last)
@@ -442,7 +446,7 @@
 			obj2 = b->object;
 			b->object = b->next = 0;
 			free(b); /* free up the hashbucket */
-			return obj2;
+			return (void*)obj2; /* inside this code, the obj's are untouchable, but outside, they aren't */
 		}
 		last = b;
 	}

Modified: team/murf/fast-ast/main/hashtab.h
URL: http://svn.digium.com/view/asterisk/team/murf/fast-ast/main/hashtab.h?rev=47255&r1=47254&r2=47255&view=diff
==============================================================================
--- team/murf/fast-ast/main/hashtab.h (original)
+++ team/murf/fast-ast/main/hashtab.h Tue Nov  7 07:59:33 2006
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * Copyright (C) 2006, Digium, Inc.
  *
  * Steve Murphy <murf at digium.com>
  *
@@ -17,6 +17,7 @@
  */
 #ifndef _ASTERISK_HASHTAB_H_
 #define _ASTERISK_HASHTAB_H_
+#include "asterisk/utils.h"
 
 /* generic (perhaps overly so) hashtable implementation */
 
@@ -60,7 +61,7 @@
 
 struct ast_hashtab_bucket
 {
-	void *object;                    /* whatever it is we are storing in this table */
+	const void *object;                    /* whatever it is we are storing in this table */
 	struct ast_hashtab_bucket *next; /* a simple LL of buckets in hash collision */
 };
 
@@ -94,37 +95,37 @@
 
 int isPrime(int num); /* this one is handy for sizing the hash table, tells if num is prime or not */
 
-int ast_hashtab_compare_strings(const void *a, const void *b);
+int ast_hashtab_compare_strings(const void *a, const void *b); /* assumes a and b are char * and returns 0 if they match */
 
 
-int ast_hashtab_compare_strings_nocase(const void *a, const void *b);
+int ast_hashtab_compare_strings_nocase(const void *a, const void *b); /* assumes a & b are strings, returns 0 if they match (strcasecmp) */
 
 
-int ast_hashtab_compare_ints(const void *a, const void *b);
+int ast_hashtab_compare_ints(const void *a, const void *b);  /* assumes a & b are int *, returns a != b */
 
 
-int ast_hashtab_compare_shorts(const void *a, const void *b);
+int ast_hashtab_compare_shorts(const void *a, const void *b);  /* assumes a & b are short *, returns a != b */
 
 
-int ast_hashtab_resize_java(struct ast_hashtab *tab);
+int ast_hashtab_resize_java(struct ast_hashtab *tab); /* returns 1 if the table is 75% full or more */
 
 
-int ast_hashtab_resize_tight(struct ast_hashtab *tab);
+int ast_hashtab_resize_tight(struct ast_hashtab *tab); /* not yet specified; probably will return 1 if table is 100% full */
 
 
-int ast_hashtab_newsize_java(struct ast_hashtab *tab);
+int ast_hashtab_newsize_java(struct ast_hashtab *tab);  /* returns a prime number roughly 2x the current table size */
 
 
-int ast_hashtab_newsize_tight(struct ast_hashtab *tab);
+int ast_hashtab_newsize_tight(struct ast_hashtab *tab); /* not yet specified, probably will return 1.5x the current table size */
 
 
-int ast_hashtab_hash_string(const void *obj, int modulus);
+int ast_hashtab_hash_string(const void *obj, int modulus); /* hashes a string to a number, mod is applied so it in the range 0 to mod-1 */
 
 
-int ast_hashtab_hash_string_nocase(const void *obj, int modulus);
+int ast_hashtab_hash_string_nocase(const void *obj, int modulus);  /* upcases each char before using them for a hash */
 
 
-int ast_hashtab_hash_int(const int num, int modulus);
+int ast_hashtab_hash_int(const int num, int modulus);  /* right now, both these funcs are just result = num%modulus; */
 
 
 int ast_hashtab_hash_short(const short num, int modulus);
@@ -151,18 +152,18 @@
 	   will be found first. */
 	/* will force a resize if the resize func returns 1 */
 	/* returns 1 on success, 0 if there's a problem */
-int ast_hashtab_insert_immediate(struct ast_hashtab *tab, void *obj);
+int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj);
 
 
 	/* check to see if the element is already there; insert only if
 	   it is not there.*/
 	/* will force a resize if the resize func returns 1 */
 	/* returns 1 on success, 0 if there's a problem, or it's already there. */
-int ast_hashtab_insert_safe(struct ast_hashtab *tab, void *obj);
+int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj);
 
 
 	/* lookup this object in the hash table. return a ptr if found, or NULL if not */
-void * ast_hashtab_lookup(struct ast_hashtab *tab, void *obj);
+void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj);
 
 	/* returns key stats for the table */
 void ast_hashtab_get_stats( struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets);

Modified: team/murf/fast-ast/main/stringtab.c
URL: http://svn.digium.com/view/asterisk/team/murf/fast-ast/main/stringtab.c?rev=47255&r1=47254&r2=47255&view=diff
==============================================================================
--- team/murf/fast-ast/main/stringtab.c (original)
+++ team/murf/fast-ast/main/stringtab.c Tue Nov  7 07:59:33 2006
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * Copyright (C) 2006, Digium, Inc.
  *
  * Steve Murphy <murf at digium.com>
  *
@@ -21,14 +21,16 @@
 #include <stddef.h>
 #include "stringtab.h"
 
+static char NULLSTR[1] = "";
+
 struct ast_strtab *st_create(int init_buf_size, 
 							 int buf_grow_size, 
 							 int ht_size, 
 							 int case_sensitive)
 {
-	struct ast_strtab *x = (struct ast_strtab*)calloc(1,sizeof(struct ast_strtab));
-	x->buffer = (struct strtab_buffer *)calloc(1,sizeof(struct strtab_buffer));
-	x->buffer->buffer = (char*)malloc(init_buf_size);
+	struct ast_strtab *x = ast_calloc(1,sizeof(struct ast_strtab));
+	x->buffer = ast_calloc(1,sizeof(struct strtab_buffer));
+	x->buffer->buffer = ast_malloc(init_buf_size);
 	x->buffer->bufsize = init_buf_size;
 	x->ht = ast_hashtab_create(ht_size, /* number of buckets */
 							   case_sensitive ? ast_hashtab_compare_strings : ast_hashtab_compare_strings_nocase,
@@ -62,9 +64,16 @@
 const char *st_insert(struct ast_strtab *tab, const char *str)
 {
 	char *newstr;
-	int len = strlen(str)+1;
-	char *z = (char*)ast_hashtab_lookup(tab->ht, str);
+	int len;
+	char *z;
 
+	if (!str)
+		return 0;
+	if (*str==0)
+		return NULLSTR;
+
+	len = strlen(str)+1;
+	z = (char*)ast_hashtab_lookup(tab->ht, str);
 	tab->lookups++;
 	
 	if (z) {
@@ -79,8 +88,8 @@
 		} else {
 			/* not enough room for it */
 			/* allocate a new buffer */
-			struct strtab_buffer *newbs = (struct strtab_buffer *)calloc(1,sizeof(struct strtab_buffer));
-			newbs->buffer = (char*)malloc(tab->buffer_grow_size);
+			struct strtab_buffer *newbs = ast_calloc(1,sizeof(struct strtab_buffer));
+			newbs->buffer = ast_malloc(tab->buffer_grow_size);
 			newbs->bufsize = tab->buffer_grow_size;
 			newbs->next = tab->buffer;
 			tab->buffer = newbs;

Modified: team/murf/fast-ast/main/stringtab.h
URL: http://svn.digium.com/view/asterisk/team/murf/fast-ast/main/stringtab.h?rev=47255&r1=47254&r2=47255&view=diff
==============================================================================
--- team/murf/fast-ast/main/stringtab.h (original)
+++ team/murf/fast-ast/main/stringtab.h Tue Nov  7 07:59:33 2006
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * Copyright (C) 2006, Digium, Inc.
  *
  * Steve Murphy <murf at digium.com>
  *
@@ -45,12 +45,11 @@
 const char *st_lookup(struct ast_strtab *tab, const char *str);
 
 void st_get_stats(struct ast_strtab *tab, 
-		  int *strings_interned, 
-		  long long *total_lookups, 
-		  int *hashtab_size,
-		  int *total_mem_alloc,
-		  int *total_mem_used
-	);
+				  int *strings_interned, 
+				  long long *total_lookups, 
+				  int *hashtab_size,
+				  int *total_mem_alloc,
+				  int *total_mem_used);
 
 
 #endif



More information about the svn-commits mailing list