[svn-commits] rmudgett: branch rmudgett/ao2_red_black r371618 - in /team/rmudgett/ao2_red_b...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Aug 21 20:00:26 CDT 2012


Author: rmudgett
Date: Tue Aug 21 20:00:21 2012
New Revision: 371618

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=371618
Log:
Work so far on rbtree.  Need to add node insertion rebalancing and node deletion.

Modified:
    team/rmudgett/ao2_red_black/include/asterisk/astobj2.h
    team/rmudgett/ao2_red_black/main/astobj2.c
    team/rmudgett/ao2_red_black/tests/test_astobj2.c

Modified: team/rmudgett/ao2_red_black/include/asterisk/astobj2.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/ao2_red_black/include/asterisk/astobj2.h?view=diff&rev=371618&r1=371617&r2=371618
==============================================================================
--- team/rmudgett/ao2_red_black/include/asterisk/astobj2.h (original)
+++ team/rmudgett/ao2_red_black/include/asterisk/astobj2.h Tue Aug 21 20:00:21 2012
@@ -1215,30 +1215,30 @@
 
 #if defined(REF_DEBUG)
 
-#define ao2_t_container_alloc_tree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
-	__ao2_container_alloc_tree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
-#define ao2_container_alloc_tree(ao2_options, container_options, , sort_fn, cmp_fn) \
-	__ao2_container_alloc_tree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
+#define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
+	__ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
+#define ao2_container_alloc_rbtree(ao2_options, container_options, , sort_fn, cmp_fn) \
+	__ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
 
 #elif defined(__AST_DEBUG_MALLOC)
 
-#define ao2_t_container_alloc_tree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
-	__ao2_container_alloc_tree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
-#define ao2_container_alloc_tree(ao2_options, container_options, sort_fn, cmp_fn) \
-	__ao2_container_alloc_tree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
+#define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
+	__ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
+#define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
+	__ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
 
 #else
 
-#define ao2_t_container_alloc_tree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
-	__ao2_container_alloc_tree((ao2_options), (container_options), (sort_fn), (cmp_fn))
-#define ao2_container_alloc_tree(ao2_options, container_options, sort_fn, cmp_fn) \
-	__ao2_container_alloc_tree((ao2_options), (container_options), (sort_fn), (cmp_fn))
+#define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
+	__ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
+#define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
+	__ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
 
 #endif
 
-struct ao2_container *__ao2_container_alloc_tree(unsigned int ao2_options, unsigned int container_options,
+struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
 	ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn);
-struct ao2_container *__ao2_container_alloc_tree_debug(unsigned int ao2_options, unsigned int container_options,
+struct ao2_container *__ao2_container_alloc_rbtree_debug(unsigned int ao2_options, unsigned int container_options,
 	ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
 	const char *tag, const char *file, int line, const char *func, int ref_debug);
 

Modified: team/rmudgett/ao2_red_black/main/astobj2.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/ao2_red_black/main/astobj2.c?view=diff&rev=371618&r1=371617&r2=371618
==============================================================================
--- team/rmudgett/ao2_red_black/main/astobj2.c (original)
+++ team/rmudgett/ao2_red_black/main/astobj2.c Tue Aug 21 20:00:21 2012
@@ -742,6 +742,8 @@
 enum ao2_container_rtti {
 	/*! This is a hash container */
 	AO2_CONTAINER_RTTI_HASH,
+	/*! This is a red-black tree container */
+	AO2_CONTAINER_RTTI_RBTREE,
 };
 
 /*!
@@ -1017,6 +1019,8 @@
 			switch (self->v_table->type) {
 			case AO2_CONTAINER_RTTI_HASH:
 				hash_ao2_link_node_stat(self, node);
+				break;
+			case AO2_CONTAINER_RTTI_RBTREE:
 				break;
 			}
 #endif	/* defined(AST_DEVMODE) */
@@ -1286,6 +1290,8 @@
 				case AO2_CONTAINER_RTTI_HASH:
 					hash_ao2_unlink_node_stat(self, node);
 					break;
+				case AO2_CONTAINER_RTTI_RBTREE:
+					break;
 				}
 #endif	/* defined(AST_DEVMODE) */
 
@@ -1511,6 +1517,8 @@
 			switch (iter->c->v_table->type) {
 			case AO2_CONTAINER_RTTI_HASH:
 				hash_ao2_unlink_node_stat(iter->c, node);
+				break;
+			case AO2_CONTAINER_RTTI_RBTREE:
 				break;
 			}
 #endif	/* defined(AST_DEVMODE) */
@@ -2458,6 +2466,7 @@
 		}
 	}
 
+	/* No more nodes in the container left to traverse. */
 	__ao2_ref(prev, -1);
 	return NULL;
 }
@@ -2719,7 +2728,8 @@
 			++count_node_forward;
 
 			if (bucket != node->my_bucket) {
-				ast_log(LOG_ERROR, "Node not in correct bucket!\n");
+				ast_log(LOG_ERROR, "Node not in correct bucket! B:%d != N:%d (forward)\n",
+					bucket, node->my_bucket);
 				return -1;
 			}
 
@@ -2733,14 +2743,16 @@
 			bucket_exp = abs(self->hash_fn(node->common.obj, OBJ_POINTER));
 			bucket_exp %= self->n_buckets;
 			if (bucket != bucket_exp) {
-				ast_log(LOG_ERROR, "Hash does not match bucket!\n");
+				ast_log(LOG_ERROR, "Hash does not match bucket! B:%d != H:%d (forward)\n",
+					bucket, bucket_exp);
 				return -1;
 			}
 
 			/* Check sort if configured. */
 			if (last && self->common.sort_fn) {
 				if (self->common.sort_fn(last->common.obj, node->common.obj, OBJ_POINTER) > 0) {
-					ast_log(LOG_ERROR, "Bucket nodes out of order!\n");
+					ast_log(LOG_ERROR, "Bucket nodes out of order in bucket %d! (forward)\n",
+						bucket);
 					return -1;
 				}
 			}
@@ -2755,7 +2767,8 @@
 			++count_node_backward;
 
 			if (bucket != node->my_bucket) {
-				ast_log(LOG_ERROR, "Node not in correct bucket!\n");
+				ast_log(LOG_ERROR, "Node not in correct bucket! B:%d != N:%d (backward)\n",
+					bucket, node->my_bucket);
 				return -1;
 			}
 
@@ -2769,14 +2782,16 @@
 			bucket_exp = abs(self->hash_fn(node->common.obj, OBJ_POINTER));
 			bucket_exp %= self->n_buckets;
 			if (bucket != bucket_exp) {
-				ast_log(LOG_ERROR, "Hash does not match bucket!\n");
+				ast_log(LOG_ERROR, "Hash does not match bucket! B:%d != H:%d (backward)\n",
+					bucket, bucket_exp);
 				return -1;
 			}
 
 			/* Check sort if configured. */
 			if (last && self->common.sort_fn) {
 				if (self->common.sort_fn(node->common.obj, last->common.obj, OBJ_POINTER) > 0) {
-					ast_log(LOG_ERROR, "Bucket nodes out of order!\n");
+					ast_log(LOG_ERROR, "Bucket nodes out of order in bucket %d! (backward)\n",
+						bucket);
 					return -1;
 				}
 			}
@@ -2785,19 +2800,22 @@
 
 		/* Check bucket forward/backward node count. */
 		if (count_node_forward != count_node_backward) {
-			ast_log(LOG_ERROR, "Forward/backward node count does not match!\n");
+			ast_log(LOG_ERROR, "Forward/backward node count does not match! F:%d != B:%d\n",
+				count_node_forward, count_node_backward);
 			return -1;
 		}
 
 		/* Check bucket forward/backward obj count. */
 		if (count_obj_forward != count_obj_backward) {
-			ast_log(LOG_ERROR, "Forward/backward object count does not match!\n");
+			ast_log(LOG_ERROR, "Forward/backward object count does not match! F:%d != B:%d\n",
+				count_obj_forward, count_obj_backward);
 			return -1;
 		}
 
 		/* Check bucket obj count statistic. */
 		if (count_obj_forward != self->buckets[bucket].elements) {
-			ast_log(LOG_ERROR, "Bucket object count stat does not match!\n");
+			ast_log(LOG_ERROR, "Bucket object count stat does not match! C:%d != S:%d\n",
+				count_obj_forward, self->buckets[bucket].elements);
 			return -1;
 		}
 
@@ -2807,9 +2825,13 @@
 
 	/* Check total obj count. */
 	if (count_total_obj != ao2_container_count(&self->common)) {
-		ast_log(LOG_ERROR, "Total object count does not match ao2_container_count()!\n");
+		ast_log(LOG_ERROR,
+			"Total object count does not match ao2_container_count()! T:%d != C:%d\n",
+			count_total_obj, ao2_container_count(&self->common));
 		return -1;
 	}
+
+/* BUGBUG check empty node count. */
 
 	return 0;
 }
@@ -2931,7 +2953,1388 @@
 		sort_fn, cmp_fn, tag, file, line, func, ref_debug);
 }
 
-/*! BUGBUG need to add red-black tree container support */
+/*!
+ * A structure to hold the object held by the container and
+ * where it is located in it.
+ *
+ * A red-black tree has the following properties:
+ *
+ * 1) Every node is either black or red.
+ *
+ * 2) If a node has a NULL child, that "child" is considered
+ * black.
+ *
+ * 3) If a node is red, then both of its children are black.
+ *
+ * 4) Every simple path from a node to a descendant NULL child
+ * has the same number of black nodes.  (Including the black
+ * NULL child.)
+ *
+ * 5) The root is black.
+ */
+struct rbtree_node {
+	/*!
+	 * \brief Items common to all container nodes.
+	 * \note Must be first in the specific node struct.
+	 */
+	struct ao2_container_node common;
+	/*! Parent node of this node. NULL if this is the root node. */
+	struct rbtree_node *parent;
+	/*! Left child node of this node.  NULL if does not have this child. */
+	struct rbtree_node *left;
+	/*! Right child node of this node.  NULL if does not have this child. */
+	struct rbtree_node *right;
+	/*! TRUE if the node is red. */
+	unsigned int is_red:1;
+};
+
+/*!
+ * A rbtree container in addition to values common to all
+ * container types, stores the pointer to the root node of the
+ * tree.
+ */
+struct ao2_container_rbtree {
+	/*!
+	 * \brief Items common to all containers.
+	 * \note Must be first in the specific container struct.
+	 */
+	struct ao2_container common;
+	/*! Root node of the tree.  NULL if the tree is empty. */
+	struct rbtree_node *root;
+};
+
+/*!
+ * \internal
+ * \brief Get the most left node in the tree.
+ * \since 12.0
+ *
+ * \param node Starting node to find the most left node.
+ *
+ * \return Left most node.  Never NULL.
+ */
+static struct rbtree_node *rb_node_most_left(struct rbtree_node *node)
+{
+	while (node->left) {
+		node = node->left;
+	}
+
+	return node;
+}
+
+/*!
+ * \internal
+ * \brief Get the most right node in the tree.
+ * \since 12.0
+ *
+ * \param node Starting node to find the most right node.
+ *
+ * \return Right most node.  Never NULL.
+ */
+static struct rbtree_node *rb_node_most_right(struct rbtree_node *node)
+{
+	while (node->right) {
+		node = node->right;
+	}
+
+	return node;
+}
+
+/*!
+ * \internal
+ * \brief Get the next node in ascending sequence.
+ * \since 12.0
+ *
+ * \param node Starting node to find the next node.
+ *
+ * \retval node on success.
+ * \retval NULL if no node.
+ */
+static struct rbtree_node *rb_node_next(struct rbtree_node *node)
+{
+	if (node->right) {
+		return rb_node_most_left(node->right);
+	}
+
+	/* Find the parent that the node is a left child of. */
+	while (node->parent) {
+		if (node->parent->left == node) {
+			/* We are the left child.  The parent is the next node. */
+			return node->parent;
+		}
+		node = node->parent;
+	}
+	return NULL;
+}
+
+/*!
+ * \internal
+ * \brief Get the next node in descending sequence.
+ * \since 12.0
+ *
+ * \param node Starting node to find the previous node.
+ *
+ * \retval node on success.
+ * \retval NULL if no node.
+ */
+static struct rbtree_node *rb_node_prev(struct rbtree_node *node)
+{
+	if (node->left) {
+		return rb_node_most_right(node->left);
+	}
+
+	/* Find the parent that the node is a right child of. */
+	while (node->parent) {
+		if (node->parent->right == node) {
+			/* We are the right child.  The parent is the previous node. */
+			return node->parent;
+		}
+		node = node->parent;
+	}
+	return NULL;
+}
+
+/*!
+ * \internal
+ * \brief Get the next node in pre-order sequence.
+ * \since 12.0
+ *
+ * \param node Starting node to find the next node.
+ *
+ * \retval node on success.
+ * \retval NULL if no node.
+ */
+static struct rbtree_node *rb_node_pre(struct rbtree_node *node)
+{
+	/* Visit the children if the node has any. */
+	if (node->left) {
+		return node->left;
+	}
+	if (node->right) {
+		return node->right;
+	}
+
+	/* Time to go back up. */
+	for (;;) {
+		if (!node->parent) {
+			return NULL;
+		}
+		if (node->parent->left == node && node->parent->right) {
+			/*
+			 * We came up the left child and there's a right child.  Visit
+			 * it.
+			 */
+			return node->parent->right;
+		}
+		node = node->parent;
+	}
+}
+
+/*!
+ * \internal
+ * \brief Get the next node in post-order sequence.
+ * \since 12.0
+ *
+ * \param node Starting node to find the next node.
+ *
+ * \retval node on success.
+ * \retval NULL if no node.
+ */
+static struct rbtree_node *rb_node_post(struct rbtree_node *node)
+{
+	/* This node's children have already been visited. */
+	for (;;) {
+		if (!node->parent) {
+			return NULL;
+		}
+		if (node->parent->left == node) {
+			/* We came up the left child. */
+			node = node->parent;
+
+			/*
+			 * Find the right child's left most childless node.
+			 */
+			while (node->right) {
+				node = rb_node_most_left(node->right);
+			}
+
+			/*
+			 * This node's left child has already been visited or it doesn't
+			 * have any children.
+			 */
+			return node;
+		}
+
+		/*
+		 * We came up the right child.
+		 *
+		 * This node's children have already been visited.  Time to
+		 * visit the parent.
+		 */
+		return node->parent;
+	}
+}
+
+/*!
+ * \internal
+ * \brief Get the next non-empty node in ascending sequence.
+ * \since 12.0
+ *
+ * \param node Starting node to find the next node.
+ *
+ * \retval node on success.
+ * \retval NULL if no node.
+ */
+static struct rbtree_node *rb_node_next_full(struct rbtree_node *node)
+{
+	for (;;) {
+		node = rb_node_next(node);
+		if (!node || node->common.obj) {
+			return node;
+		}
+	}
+}
+
+/*!
+ * \internal
+ * \brief Get the next non-empty node in descending sequence.
+ * \since 12.0
+ *
+ * \param node Starting node to find the previous node.
+ *
+ * \retval node on success.
+ * \retval NULL if no node.
+ */
+static struct rbtree_node *rb_node_prev_full(struct rbtree_node *node)
+{
+	for (;;) {
+		node = rb_node_prev(node);
+		if (!node || node->common.obj) {
+			return node;
+		}
+	}
+}
+
+/*!
+ * \internal
+ * \brief Determine which way to go from an empty node.
+ * \since 12.0
+ *
+ * \param empty Empty node to determine which side obj_right goes on.
+ * \param sort_fn Sort comparison function for non-empty nodes.
+ * \param obj_right pointer to the (user-defined part) of an object.
+ * \param flags flags from ao2_callback()
+ *   OBJ_POINTER - if set, 'obj_right', is an object.
+ *   OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
+ *   OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
+ *
+ * \retval TRUE if go left.
+ * \retval FALSE if go right.
+ */
+static int rb_find_empty_direction(struct rbtree_node *empty, ao2_sort_fn *sort_fn, void *obj_right, enum search_flags flags)
+{
+	int cmp;
+	struct rbtree_node *cur;
+	struct rbtree_node *right_most;
+
+	/* Try for a quick definite go left. */
+	if (!empty->left) {
+		/* The empty node has no left child.  Go right. */
+		return 0;
+	}
+	right_most = rb_node_most_right(empty->left);
+	if (right_most->common.obj) {
+		cmp = sort_fn(right_most->common.obj, obj_right, flags);
+		if (cmp < 0) {
+			/* Go right. */
+			return 0;
+		}
+		/* Go left. */
+		return 1;
+	}
+
+	/* Try for a quick definite go right. */
+	if (!empty->right) {
+		/* The empty node has no right child.  Go left. */
+		return 1;
+	}
+	cur = rb_node_most_left(empty->right);
+	if (cur->common.obj) {
+		cmp = sort_fn(cur->common.obj, obj_right, flags);
+		if (cmp <= 0) {
+			/* Go right. */
+			return 0;
+		}
+		/* Go left. */
+		return 1;
+	}
+
+	/*
+	 * Have to scan the previous nodes from the right_most node of
+	 * the left subtree for the first non-empty node to determine
+	 * direction.
+	 */
+	cur = right_most;
+	for (;;) {
+		/* Find previous node. */
+		if (cur->left) {
+			cur = rb_node_most_right(cur->left);
+		} else {
+			/* Find the parent that the node is a right child of. */
+			for (;;) {
+				if (cur->parent == empty) {
+					/* Go right.  The left side of the empty node is all empty nodes. */
+					return 0;
+				}
+				if (cur->parent->right == cur) {
+					/* We are the right child.  The parent is the previous node. */
+					cur = cur->parent;
+					break;
+				}
+				cur = cur->parent;
+			}
+		}
+
+		if (cur->common.obj) {
+			cmp = sort_fn(cur->common.obj, obj_right, flags);
+			if (cmp < 0) {
+				/* Go right. */
+				return 0;
+			}
+			/* Go left. */
+			return 1;
+		}
+	}
+}
+
+/*!
+ * \internal
+ * \brief Create an empty copy of this container.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ *
+ * \retval empty-clone-container on success.
+ * \retval NULL on error.
+ */
+static struct ao2_container *rb_ao2_alloc_empty_clone(struct ao2_container_rbtree *self)
+{
+	struct astobj2 *orig_obj;
+	unsigned int ao2_options;
+
+	/* Get container ao2 options. */
+	orig_obj = INTERNAL_OBJ(self);
+	if (!orig_obj) {
+		return NULL;
+	}
+	ao2_options = orig_obj->priv_data.options;
+
+	return __ao2_container_alloc_rbtree(ao2_options, self->common.options,
+		self->common.sort_fn, self->common.cmp_fn);
+}
+
+/*!
+ * \internal
+ * \brief Create an empty copy of this container. (Debug version)
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param tag used for debugging.
+ * \param file Debug file name invoked from
+ * \param line Debug line invoked from
+ * \param func Debug function name invoked from
+ * \param ref_debug TRUE if to output a debug reference message.
+ *
+ * \retval empty-clone-container on success.
+ * \retval NULL on error.
+ */
+static struct ao2_container *rb_ao2_alloc_empty_clone_debug(struct ao2_container_rbtree *self, const char *tag, const char *file, int line, const char *func, int ref_debug)
+{
+	struct astobj2 *orig_obj;
+	unsigned int ao2_options;
+
+	/* Get container ao2 options. */
+	orig_obj = INTERNAL_OBJ(self);
+	if (!orig_obj) {
+		return NULL;
+	}
+	ao2_options = orig_obj->priv_data.options;
+
+	return __ao2_container_alloc_rbtree_debug(ao2_options, self->common.options,
+		self->common.sort_fn, self->common.cmp_fn, tag, file, line, func, ref_debug);
+}
+
+/*!
+ * \internal
+ * \brief Destroy a rbtree container node.
+ * \since 12.0
+ *
+ * \param v_doomed Container node to destroy.
+ *
+ * \details
+ * The container node unlinks itself from the container as part
+ * of its destruction.  The node must be destroyed while the
+ * container is already locked.
+ *
+ * \return Nothing
+ */
+static void rb_ao2_node_destructor(void *v_doomed)
+{
+	struct rbtree_node *doomed = v_doomed;
+	struct ao2_container_rbtree *my_container;
+
+/* BUGBUG rb_ao2_node_destructor not written */
+
+	my_container = (struct ao2_container_rbtree *) doomed->common.my_container;
+	if (my_container) {
+		/*
+		 * Promote to write lock if not already there.  Since
+		 * adjust_lock() can potentially release and block waiting for a
+		 * write lock, care must be taken to ensure that node references
+		 * are released before releasing the container references.
+		 *
+		 * Node references held by an iterator can only be held while
+		 * the iterator also holds a reference to the container.  These
+		 * node references must be unreferenced before the container can
+		 * be unreferenced to ensure that the node will not get a
+		 * negative reference and the destructor called twice for the
+		 * same node.
+		 */
+		adjust_lock(my_container, AO2_LOCK_REQ_WRLOCK, 1);
+
+/* BUGBUG remove the node from the tree and rebalance if the container is not being destroyed. */
+/* BUGBUG rb_delete_fixup not written */
+	}
+
+	/*
+	 * We could have an object in the node if the container is being
+	 * destroyed or the node had not been linked in yet.
+	 */
+	if (doomed->common.obj) {
+		ao2_ref(doomed->common.obj, -1);
+		doomed->common.obj = NULL;
+	}
+}
+
+/*!
+ * \internal
+ * \brief Create a new container node.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param obj_new Object to put into the node.
+ * \param tag used for debugging.
+ * \param file Debug file name invoked from
+ * \param line Debug line invoked from
+ * \param func Debug function name invoked from
+ *
+ * \retval initialized-node on success.
+ * \retval NULL on error.
+ */
+static struct rbtree_node *rb_ao2_new_node(struct ao2_container_rbtree *self, void *obj_new, const char *tag, const char *file, int line, const char *func)
+{
+	struct rbtree_node *node;
+
+	node = __ao2_alloc(sizeof(*node), rb_ao2_node_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
+	if (!node) {
+		return NULL;
+	}
+
+	if (tag) {
+		__ao2_ref_debug(obj_new, +1, tag, file, line, func);
+	} else {
+		__ao2_ref(obj_new, +1);
+	}
+	node->common.obj = obj_new;
+	node->common.my_container = (struct ao2_container *) self;
+
+	return node;
+}
+
+/*!
+ * \internal
+ * \brief Fixup the rbtree after inserting a non-root node.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param node Container node just inserted into the container.
+ *
+ * \return Nothing
+ */
+static void rb_insert_fixup(struct ao2_container_rbtree *self, struct rbtree_node *node)
+{
+	/*! \todo BUGBUG rb_insert_fixup() not written */
+}
+
+/*!
+ * \internal
+ * \brief Insert a node into this container.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param node Container node to insert into the container.
+ *
+ * \return enum ao2_container_insert value.
+ */
+static enum ao2_container_insert rb_ao2_insert_node(struct ao2_container_rbtree *self, struct rbtree_node *node)
+{
+	int cmp;
+	struct rbtree_node *cur;
+	struct rbtree_node *next;
+	ao2_sort_fn *sort_fn;
+	uint32_t options;
+
+	if (!self->root) {
+		/* The tree is empty. */
+		self->root = node;
+		return AO2_CONTAINER_INSERT_NODE_INSERTED;
+	}
+
+	sort_fn = self->common.sort_fn;
+	options = self->common.options;
+
+	/*
+	 * New nodes are always colored red when initially inserted into
+	 * the tree.  (Except for the root which is always black.)
+	 */
+	node->is_red = 1;
+
+	/* Find node where normal insert would put a new node. */
+	cur = self->root;
+	for (;;) {
+		if (!cur->common.obj) {
+			/* Which direction do we go to insert this node? */
+			if (rb_find_empty_direction(cur, sort_fn, node->common.obj, OBJ_POINTER)) {
+				if (cur->left) {
+					cur = cur->left;
+					continue;
+				}
+
+				/* Node becomes a left child */
+				cur->left = node;
+				node->parent = cur;
+				rb_insert_fixup(self, node);
+				return AO2_CONTAINER_INSERT_NODE_INSERTED;
+			}
+			if (cur->right) {
+				cur = cur->right;
+				continue;
+			}
+
+			/* Node becomes a right child */
+			cur->right = node;
+			node->parent = cur;
+			rb_insert_fixup(self, node);
+			return AO2_CONTAINER_INSERT_NODE_INSERTED;
+		}
+		cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_POINTER);
+		if (cmp > 0) {
+			if (cur->left) {
+				cur = cur->left;
+				continue;
+			}
+
+			/* Node becomes a left child */
+			cur->left = node;
+			node->parent = cur;
+			rb_insert_fixup(self, node);
+			return AO2_CONTAINER_INSERT_NODE_INSERTED;
+		} else if (cmp < 0) {
+			if (cur->right) {
+				cur = cur->right;
+				continue;
+			}
+
+			/* Node becomes a right child */
+			cur->right = node;
+			node->parent = cur;
+			rb_insert_fixup(self, node);
+			return AO2_CONTAINER_INSERT_NODE_INSERTED;
+		}
+
+		break;
+	}
+
+	/* Node is a dupliate */
+	switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
+	default:
+	case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
+		if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
+			/* Find first duplicate node. */
+			for (;;) {
+				next = rb_node_prev_full(cur);
+				if (!next) {
+					break;
+				}
+				cmp = sort_fn(next->common.obj, node->common.obj, OBJ_POINTER);
+				if (cmp) {
+					break;
+				}
+				cur = next;
+			}
+			if (!cur->left) {
+				/* Node becomes a left child */
+				cur->left = node;
+			} else {
+				/* Node becomes a right child */
+				cur = rb_node_most_right(cur->left);
+				cur->right = node;
+			}
+		} else {
+			/* Find last duplicate node. */
+			for (;;) {
+				next = rb_node_next_full(cur);
+				if (!next) {
+					break;
+				}
+				cmp = sort_fn(next->common.obj, node->common.obj, OBJ_POINTER);
+				if (cmp) {
+					break;
+				}
+				cur = next;
+			}
+			if (!cur->right) {
+				/* Node becomes a right child */
+				cur->right = node;
+			} else {
+				/* Node becomes a left child */
+				cur = rb_node_most_left(cur->right);
+				cur->left = node;
+			}
+		}
+		break;
+	case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
+		/* Reject all objects with the same key. */
+		return AO2_CONTAINER_INSERT_NODE_REJECTED;
+	case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
+		if (cur->common.obj == node->common.obj) {
+			/* Reject inserting the same object */
+			return AO2_CONTAINER_INSERT_NODE_REJECTED;
+		}
+		next = cur;
+		if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) {
+			/* Search to end of duplicates for the same object. */
+			for (;;) {
+				next = rb_node_next_full(next);
+				if (!next) {
+					break;
+				}
+				if (next->common.obj == node->common.obj) {
+					/* Reject inserting the same object */
+					return AO2_CONTAINER_INSERT_NODE_REJECTED;
+				}
+				cmp = sort_fn(next->common.obj, node->common.obj, OBJ_POINTER);
+				if (cmp) {
+					break;
+				}
+			}
+
+			/* Find first duplicate node. */
+			for (;;) {
+				next = rb_node_prev_full(cur);
+				if (!next) {
+					break;
+				}
+				if (next->common.obj == node->common.obj) {
+					/* Reject inserting the same object */
+					return AO2_CONTAINER_INSERT_NODE_REJECTED;
+				}
+				cmp = sort_fn(next->common.obj, node->common.obj, OBJ_POINTER);
+				if (cmp) {
+					break;
+				}
+				cur = next;
+			}
+			if (!cur->left) {
+				/* Node becomes a left child */
+				cur->left = node;
+			} else {
+				/* Node becomes a right child */
+				cur = rb_node_most_right(cur->left);
+				cur->right = node;
+			}
+		} else {
+			/* Search to beginning of duplicates for the same object. */
+			for (;;) {
+				next = rb_node_prev_full(next);
+				if (!next) {
+					break;
+				}
+				if (next->common.obj == node->common.obj) {
+					/* Reject inserting the same object */
+					return AO2_CONTAINER_INSERT_NODE_REJECTED;
+				}
+				cmp = sort_fn(next->common.obj, node->common.obj, OBJ_POINTER);
+				if (cmp) {
+					break;
+				}
+			}
+
+			/* Find last duplicate node. */
+			for (;;) {
+				next = rb_node_next_full(cur);
+				if (!next) {
+					break;
+				}
+				if (next->common.obj == node->common.obj) {
+					/* Reject inserting the same object */
+					return AO2_CONTAINER_INSERT_NODE_REJECTED;
+				}
+				cmp = sort_fn(next->common.obj, node->common.obj, OBJ_POINTER);
+				if (cmp) {
+					break;
+				}
+				cur = next;
+			}
+			if (!cur->right) {
+				/* Node becomes a right child */
+				cur->right = node;
+			} else {
+				/* Node becomes a left child */
+				cur = rb_node_most_left(cur->right);
+				cur->left = node;
+			}
+		}
+		break;
+	case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
+		SWAP(cur->common.obj, node->common.obj);
+		return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED;
+	}
+
+	/* Complete inserting duplicate node. */
+	node->parent = cur;
+	rb_insert_fixup(self, node);
+	return AO2_CONTAINER_INSERT_NODE_INSERTED;
+}
+
+/*! Traversal state to restart a rbtree container traversal. */
+struct rbtree_traversal_state {
+	/*! Active sort function in the traversal if not NULL. */
+	ao2_sort_fn *sort_fn;
+	/*! First node returned if OBJ_CONTINUE flag set. (Reffed) */
+	struct rbtree_node *first_node;
+	/*! Saved comparison callback arg pointer. */
+	void *arg;
+	/*! Saved search flags to control traversing the container. */
+	enum search_flags flags;
+};
+
+struct rbtree_traversal_state_check {
+	/*
+	 * If we have a division by zero compile error here then there
+	 * is not enough room for the state.  Increase AO2_TRAVERSAL_STATE_SIZE.
+	 */
+	char check[1 / (AO2_TRAVERSAL_STATE_SIZE / sizeof(struct rbtree_traversal_state))];
+};
+
+/*!
+ * \internal
+ * \brief Find the next rbtree container node in a traversal.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param state Traversal state to restart rbtree container traversal.
+ * \param prev Previous node returned by the traversal search functions.
+ *    The ref ownership is passed back to this function.
+ *
+ * \retval node-ptr of found node (Reffed).
+ * \retval NULL when no node found.
+ */
+static struct rbtree_node *rb_ao2_find_next(struct ao2_container_rbtree *self, struct rbtree_traversal_state *state, struct rbtree_node *prev)
+{
+	struct rbtree_node *node;
+	void *arg;
+	enum search_flags flags;
+	int cmp;
+
+	arg = state->arg;
+	flags = state->flags;
+
+	node = prev;
+	for (;;) {
+		/* Find next node in traversal order. */
+		switch (flags & OBJ_ORDER_MASK) {
+		default:
+		case OBJ_ORDER_ASCENDING:
+			node = rb_node_next(node);
+			if (!node) {
+				if (!state->first_node) {
+					break;
+				}
+				/* Wrap around to the beginning. */
+				node = rb_node_most_left(self->root);
+			}
+			if (node == state->first_node) {
+				/* We have wrapped back to the starting point. */
+				node = NULL;
+			}
+			break;
+		case OBJ_ORDER_DESCENDING:
+			node = rb_node_prev(node);
+			if (!node) {
+				if (!state->first_node) {
+					break;
+				}
+				/* Wrap around to the end. */
+				node = rb_node_most_right(self->root);
+			}
+			if (node == state->first_node) {
+				/* We have wrapped back to the starting point. */
+				node = NULL;
+			}
+			break;
+		case OBJ_ORDER_PRE:
+			node = rb_node_pre(node);
+			break;
+		case OBJ_ORDER_POST:
+			node = rb_node_post(node);
+			break;
+		}
+		if (!node) {
+			/* No more nodes left to traverse. */
+			break;
+		}
+		if (!node->common.obj) {
+			/* Node is empty */
+			continue;
+		}
+
+		if (state->sort_fn) {
+			/* Filter node through the sort_fn */
+			cmp = state->sort_fn(node->common.obj, arg,
+				flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY));
+			if (cmp) {
+				/* No more nodes in this container are possible to match. */
+				break;
+			}
+		}
+
+		/* We have the next traversal node */
+		__ao2_ref(node, +1);
+
+		/*
+		 * Dereferencing the prev node may result in our next node
+		 * object being removed by another thread.  This could happen if
+		 * the container uses RW locks and the container was read
+		 * locked.
+		 */
+		__ao2_ref(prev, -1);
+		if (node->common.obj) {
+			return node;
+		}
+		prev = node;
+	}
+
+	/* No more nodes in the container left to traverse. */
+	__ao2_ref(prev, -1);
+	return NULL;
+}
+
+/*!
+ * \internal
+ * \brief Find an initial matching node.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param obj_right pointer to the (user-defined part) of an object.
+ * \param flags flags from ao2_callback()
+ *   OBJ_POINTER - if set, 'obj_right', is an object.
+ *   OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
+ *   OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
+ *
+ * \retval node on success.
+ * \retval NULL if not found.
+ */
+static struct rbtree_node *rb_find_initial(struct ao2_container_rbtree *self, void *obj_right, enum search_flags flags)
+{
+	int cmp;
+	struct rbtree_node *node;
+	ao2_sort_fn *sort_fn;
+
+	sort_fn = self->common.sort_fn;
+
+	/* Find node where normal search would find it. */
+	node = self->root;
+	while (node) {
+		if (!node->common.obj) {
+			/* Which direction do we go to find the node? */
+			if (rb_find_empty_direction(node, sort_fn, obj_right, flags)) {
+				node = node->left;
+			} else {
+				node = node->right;
+			}
+		} else {
+			cmp = sort_fn(node->common.obj, obj_right, flags);
+			if (cmp > 0) {
+				node = node->left;
+			} else if (cmp < 0) {
+				node = node->right;
+			} else {
+				break;
+			}
+		}
+	}
+	return node;
+}
+
+/*!
+ * \internal
+ * \brief Find the first rbtree container node in a traversal.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param flags search_flags to control traversing the container
+ * \param arg Comparison callback arg parameter.
+ * \param state Traversal state to restart rbtree container traversal.
+ *
+ * \retval node-ptr of found node (Reffed).
+ * \retval NULL when no node found.
+ */
+static struct rbtree_node *rb_ao2_find_first(struct ao2_container_rbtree *self, enum search_flags flags, void *arg, struct rbtree_traversal_state *state)
+{
+	struct rbtree_node *next;
+	struct rbtree_node *node;
+	int cmp;
+
+	if (self->common.destroying) {
+/* BUGBUG need to check what happens when there are empty nodes during destruction. */
+		/* Force traversal to be post order for tree destruction. */
+		flags = OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_ORDER_POST;
+	}
+
+	memset(state, 0, sizeof(*state));
+	state->arg = arg;
+	state->flags = flags;
+
+	if ((flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY))) {
+		/* We are asked to do a directed search. */
+		state->sort_fn = self->common.sort_fn;
+	} else {
+		/* Don't know, let's visit all nodes */
+		state->sort_fn = NULL;
+	}
+
+	if (!self->root) {
+		/* Tree is empty. */
+		return NULL;
+	}
+
+	/* Find first traversal node. */
+	switch (flags & OBJ_ORDER_MASK) {
+	default:
+	case OBJ_ORDER_ASCENDING:
+		if (!state->sort_fn) {
+			/* Find left most child. */
+			node = rb_node_most_left(self->root);
+			if (!node->common.obj) {
+				node = rb_node_next_full(node);
+				if (!node) {
+					return NULL;
+				}
+			}
+			break;
+		}
+
+		/* Search for initial node. */
+		node = rb_find_initial(self, arg,
+			flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY));
+		if (!node) {
+			return NULL;
+		}
+		switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
+		default:
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
+			/* Find first duplicate node. */
+			for (;;) {
+				next = rb_node_prev_full(node);
+				if (!next) {
+					break;
+				}
+				cmp = state->sort_fn(next->common.obj, arg,
+					flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY));
+				if (cmp) {
+					break;
+				}
+				node = next;
+			}
+			break;
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
+			/* There are no duplicates allowed. */
+			break;
+		}
+		break;
+	case OBJ_ORDER_DESCENDING:
+		if (!state->sort_fn) {
+			/* Find right most child. */
+			node = rb_node_most_right(self->root);
+			if (!node->common.obj) {
+				node = rb_node_prev_full(node);
+				if (!node) {
+					return NULL;
+				}
+			}
+			break;
+		}
+
+		/* Search for initial node. */
+		node = rb_find_initial(self, arg,
+			flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY));
+		if (!node) {
+			return NULL;
+		}
+		switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) {
+		default:
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW:
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT:
+			/* Find last duplicate node. */
+			for (;;) {
+				next = rb_node_next_full(node);
+				if (!next) {
+					break;
+				}
+				cmp = state->sort_fn(next->common.obj, arg,
+					flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY));
+				if (cmp) {
+					break;
+				}
+				node = next;
+			}
+			break;
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT:
+		case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE:
+			/* There are no duplicates allowed. */
+			break;
+		}
+		break;
+	case OBJ_ORDER_PRE:
+		/* This is a tree structure traversal so we must visit all nodes. */
+		state->sort_fn = NULL;
+
+		node = self->root;
+
+		/* Find a non-empty node. */
+		while (!node->common.obj) {
+			node = rb_node_pre(node);
+			if (!node) {
+				return NULL;
+			}
+		}
+		break;
+	case OBJ_ORDER_POST:
+		/* This is a tree structure traversal so we must visit all nodes. */
+		state->sort_fn = NULL;
+
+		/* Find the left most childless node. */
+		node = self->root;
+		for (;;) {
+			node = rb_node_most_left(node);
+			if (!node->right) {
+				/* This node has no children. */
+				break;
+			}
+			node = node->right;
+		}
+
+		/* Find a non-empty node. */
+		while (!node->common.obj) {
+			node = rb_node_post(node);
+			if (!node) {
+				return NULL;
+			}
+		}
+		break;
+	}
+
+	if (state->sort_fn && (flags & OBJ_CONTINUE)) {
+		/* Remember first node when we wrap around. */
+		__ao2_ref(node, +1);
+		state->first_node = node;
+
+		/* From now on all nodes are matching */
+		state->sort_fn = NULL;
+	}
+
+	/* We have the first traversal node */
+	__ao2_ref(node, +1);
+	return node;
+}
+
+/*!
+ * \internal
+ * \brief Cleanup the rbtree container traversal state.
+ * \since 12.0
+ *
+ * \param state Traversal state to cleanup.
+ *
+ * \return Nothing
+ */
+static void rb_ao2_find_cleanup(struct rbtree_traversal_state *state)
+{
+	if (state->first_node) {
+		__ao2_ref(state->first_node, -1);
+	}
+}
+
+/*!
+ * \internal
+ * \brief Find the next non-empty iteration node in the container.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ * \param node Previous node returned by the iterator.
+ * \param flags search_flags to control iterating the container.
+ *   Only AO2_ITERATOR_DESCENDING is useful by the method.
+ *
+ * \note The container is already locked.
+ *
+ * \retval node on success.
+ * \retval NULL on error or no more nodes in the container.
+ */
+static struct rbtree_node *rb_ao2_iterator_next(struct ao2_container_rbtree *self, struct rbtree_node *node, enum ao2_iterator_flags flags)
+{
+	if (flags & AO2_ITERATOR_DESCENDING) {
+		if (!node) {
+			/* Find right most node. */
+			if (!self->root) {
+				return NULL;
+			}
+			node = rb_node_most_right(self->root);
+			if (node->common.obj) {
+				/* Found a non-empty node. */
+				return node;
+			}
+		}
+		/* Find next non-empty node. */
+		node = rb_node_prev_full(node);
+	} else {
+		if (!node) {
+			/* Find left most node. */
+			if (!self->root) {
+				return NULL;
+			}
+			node = rb_node_most_left(self->root);
+			if (node->common.obj) {
+				/* Found a non-empty node. */
+				return node;
+			}
+		}
+		/* Find next non-empty node. */
+		node = rb_node_next_full(node);
+	}
+
+	return node;
+}
+
+#if defined(AST_DEVMODE)
+/*!
+ * \internal
+ *
+ * \brief Destroy this container.
+ * \since 12.0
+ *
+ * \param self Container to operate upon.
+ *
+ * \return Nothing
+ */
+static void rb_ao2_destroy(struct ao2_container_rbtree *self)
+{
+	/* Check that the container no longer has any nodes */
+	if (self->root) {
+		ast_log(LOG_ERROR,
+			"Ref leak destroying container.  Container still has nodes!\n");
+		ast_assert(0);
+	}
+}
+#endif	/* defined(AST_DEVMODE) */
+
+#if defined(AST_DEVMODE)
+/*!
+ * \internal
+ * \brief Perform an integrity check on the specified container.
+ * \since 12.0
+ *
+ * \param self Container to check integrity.
+ *
+ * \note The container is already locked for reading.
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+static int rb_ao2_integrity(struct ao2_container_rbtree *self)
+{
+/* BUGBUG rb_ao2_integrity not written */
+/* BUGBUG Check red/black node flags. */
+
+	int count_node_forward;

[... 186 lines stripped ...]



More information about the svn-commits mailing list