[asterisk-commits] russell: branch russell/iax_refcount r80288 - /team/russell/iax_refcount/main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 22 12:06:31 CDT 2007


Author: russell
Date: Wed Aug 22 12:06:31 2007
New Revision: 80288

URL: http://svn.digium.com/view/asterisk?view=rev&rev=80288
Log:
Convert bucket handling to use our linked list macros

Modified:
    team/russell/iax_refcount/main/astobj2.c

Modified: team/russell/iax_refcount/main/astobj2.c
URL: http://svn.digium.com/view/asterisk/team/russell/iax_refcount/main/astobj2.c?view=diff&rev=80288&r1=80287&r2=80288
==============================================================================
--- team/russell/iax_refcount/main/astobj2.c (original)
+++ team/russell/iax_refcount/main/astobj2.c Wed Aug 22 12:06:31 2007
@@ -219,10 +219,7 @@
 static void container_destruct(void *c);
 
 /* each bucket in the container is a tailq. */
-struct bucket {
-	struct bucket_list *head;
-	struct bucket_list *tail;
-};
+AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
 
 /*!
  * A container; stores the hash and callback functions, information on
@@ -280,16 +277,15 @@
 
 	ao2_container *c = ao2_alloc(container_size, container_destruct);
 
-	if (c != NULL) {
-		/* init */
-		c->elements = 0;
-		c->version = 1;	/* 0 is a reserved value here */
-		c->n_buckets = n_buckets;
-		c->hash_fn = hash_fn ? hash_fn : hash_zero;
-		c->cmp_fn = cmp_fn;
-		ast_atomic_fetchadd_int(&ao2.total_containers, 1);
-	}
-	ast_verbose("ao2_container_alloc %d returns %p\n", n_buckets, c);
+	if (!c)
+		return NULL;
+	
+	c->version = 1;	/* 0 is a reserved value here */
+	c->n_buckets = n_buckets;
+	c->hash_fn = hash_fn ? hash_fn : hash_zero;
+	c->cmp_fn = cmp_fn;
+	ast_atomic_fetchadd_int(&ao2.total_containers, 1);
+	
 	return c;
 }
 
@@ -307,7 +303,7 @@
  * XXX this should be private to the container code
  */
 struct bucket_list {
-	struct bucket_list *next;	/* pointer to next bucket_list */
+	AST_LIST_ENTRY(bucket_list) entry;
 	int version;
 	struct astobj2 *astobj;		/* pointer to internal data */
 }; 
@@ -338,14 +334,9 @@
 	ao2_lock(c);
 	i %= c->n_buckets;
 
-	p->next = NULL;
 	p->astobj = obj;
 	p->version = ast_atomic_fetchadd_int(&c->version, 1);
-	if (c->buckets[i].head == NULL)
-		c->buckets[i].head = p;
-	else
-		c->buckets[i].tail->next = p;
-	c->buckets[i].tail = p;
+	AST_LIST_INSERT_HEAD(&c->buckets[i], p, entry);
 	ast_atomic_fetchadd_int(&c->elements, 1);
 	ao2_unlock(c);
 	ast_mark(prof_id, 0);
@@ -437,16 +428,13 @@
 
 	for(; i < last ; i++) {
 		/* scan the list with prev-cur pointers */
-		struct bucket_list *prev = NULL, *cur = c->buckets[i].head;
-
-		while (cur) {
+		struct bucket_list *cur;
+
+		AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
 			int match = cb_fn(EXTERNAL_OBJ(cur->astobj), arg, flags) & (CMP_MATCH | CMP_STOP);
-			// ast_verbose("match on %p returns %d\n", arg, match);
 
 			/* we found the object, performing operations according flags */
 			if (match == 0) {	/* no match, no stop, continue */
-				prev = cur;
-				cur = cur->next;
 				continue;
 			} else if (match == CMP_STOP) {	/* no match but stop, we are done */
 				i = last;
@@ -462,23 +450,13 @@
 			if (flags & OBJ_UNLINK) {	/* must unlink */
 				struct bucket_list *x = cur;
 
-				// ast_verbose("++ unlink u %p at %p\n", EXTERNAL_OBJ(cur->astobj), cur);
 				/* we are going to modify the container, so update version */
 				ast_atomic_fetchadd_int(&c->version, 1);
-				if (prev == NULL)	/* gone from head */
-					c->buckets[i].head  = cur->next;
-				else			/* gone from the middle */
-					prev->next = cur->next;
-				if (c->buckets[i].tail == cur) /* update tail if needed */
-					c->buckets[i].tail = prev;
-				cur = cur->next ;	/* prepare for next cycle */
+				AST_LIST_REMOVE_CURRENT(&c->buckets[i], entry);
 				/* update number of elements and version */
 				ast_atomic_fetchadd_int(&c->elements, -1);
 				ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
 				free(x);	/* free the link record */
-			} else {	/* prepare for next cycle */
-				prev = cur;
-				cur = cur->next;
 			}
 
 			if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
@@ -495,6 +473,7 @@
 #endif
 			}
 		}
+		AST_LIST_TRAVERSE_SAFE_END
 	}
 	ao2_unlock(c);
 	return ret;
@@ -540,8 +519,7 @@
 	 * we have a pointer, try follow it
 	 */
 	if (a->c->version == a->c_version && (p = a->obj) ) {
-		// ast_verbose("optimized lookup for %u\n", a->version);
-		if ( (p = p->next) )
+		if ( (p = AST_LIST_NEXT(p, entry)) )
 			goto found;
 		/* nope, start from the next bucket */
 		a->bucket++;
@@ -559,7 +537,7 @@
 	 */
 	for (; a->bucket < lim; a->bucket++, a->version = 0) {
 		/* scan the current bucket */
-		for (p = a->c->buckets[a->bucket].head; p; p = p->next) {
+		AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
 			if (p->version > a->version)
 				goto found;
 		}




More information about the asterisk-commits mailing list