[svn-commits] gtjoseph: trunk r415319 - in /trunk: ./ include/asterisk/ main/ tests/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jun 6 09:13:03 CDT 2014


Author: gtjoseph
Date: Fri Jun  6 09:12:57 2014
New Revision: 415319

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=415319
Log:
Split astobj2.c into more maintainable components.

Split astobj2.c into the following files to improve maintainability.

astobj2.c - object primitives, object primitive misc and initialization code.
astobj2_private.h - internal object declarations needed by the containers.
astobj2_container.c - generic conainer and container misc code.
astobj2_container_hash.c - hash container specific code.
astobj2_container_rbtree.c - rbtree container specific code.
astobj2_container_private.h - generic container definitions and rtti prototypes.

https://reviewboard.asterisk.org/r/3576/
........

Merged revisions 415317 from http://svn.asterisk.org/svn/asterisk/branches/12

Added:
    trunk/main/astobj2_container.c
      - copied unchanged from r415317, branches/12/main/astobj2_container.c
    trunk/main/astobj2_container_private.h
      - copied unchanged from r415317, branches/12/main/astobj2_container_private.h
    trunk/main/astobj2_hash.c
      - copied unchanged from r415317, branches/12/main/astobj2_hash.c
    trunk/main/astobj2_private.h
      - copied unchanged from r415317, branches/12/main/astobj2_private.h
    trunk/main/astobj2_rbtree.c
      - copied unchanged from r415317, branches/12/main/astobj2_rbtree.c
Modified:
    trunk/   (props changed)
    trunk/include/asterisk/astobj2.h
    trunk/main/astobj2.c
    trunk/tests/test_astobj2.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-12-merged' - no diff available.

Modified: trunk/include/asterisk/astobj2.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/astobj2.h?view=diff&rev=415319&r1=415318&r2=415319
==============================================================================
--- trunk/include/asterisk/astobj2.h (original)
+++ trunk/include/asterisk/astobj2.h Fri Jun  6 09:12:57 2014
@@ -520,6 +520,13 @@
 #define ao2_ref(o,delta)       __ao2_ref((o), (delta))
 
 #endif
+
+/*!
+ * \brief Retrieve the ao2 options used to create the object.
+ * \param obj pointer to the (user-defined part) of an object.
+ * \return options from enum ao2_alloc_opts.
+ */
+unsigned int ao2_options_get(void *obj);
 
 /*!
  * \since 12

Modified: trunk/main/astobj2.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/astobj2.c?view=diff&rev=415319&r1=415318&r2=415319
==============================================================================
--- trunk/main/astobj2.c (original)
+++ trunk/main/astobj2.c Fri Jun  6 09:12:57 2014
@@ -31,15 +31,10 @@
 
 #include "asterisk/_private.h"
 #include "asterisk/astobj2.h"
-#include "asterisk/dlinkedlists.h"
-#include "asterisk/utils.h"
+#include "astobj2_private.h"
+#include "astobj2_container_private.h"
 #include "asterisk/cli.h"
 #include "asterisk/paths.h"
-
-#if defined(TEST_FRAMEWORK)
-/* We are building with the test framework enabled so enable AO2 debug tests as well. */
-#define AO2_DEBUG 1
-#endif	/* defined(TEST_FRAMEWORK) */
 
 static FILE *ref_log;
 
@@ -98,22 +93,8 @@
 	void *user_data[0];
 };
 
-#if defined(AST_DEVMODE)
-#define AO2_DEVMODE_STAT(stat)	stat
-#else
-#define AO2_DEVMODE_STAT(stat)
-#endif	/* defined(AST_DEVMODE) */
-
-#ifdef AO2_DEBUG
-struct ao2_stats {
-	volatile int total_objects;
-	volatile int total_mem;
-	volatile int total_containers;
-	volatile int total_refs;
-	volatile int total_locked;
-};
-
-static struct ao2_stats ao2;
+#ifdef AO2_DEBUG
+struct ao2_stats ao2;
 #endif
 
 #ifdef HAVE_BKTR
@@ -150,7 +131,7 @@
  *
  * \return the pointer to the astobj2 structure
  */
-static inline struct astobj2 *INTERNAL_OBJ(void *user_data)
+static struct astobj2 *INTERNAL_OBJ(void *user_data)
 {
 	struct astobj2 *p;
 
@@ -183,6 +164,11 @@
  */
 #define EXTERNAL_OBJ(_p)	((_p) == NULL ? NULL : (_p)->user_data)
 
+int is_ao2_object(void *user_data)
+{
+	return (INTERNAL_OBJ(user_data) != NULL);
+}
+
 int __ao2_lock(void *user_data, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var)
 {
 	struct astobj2 *obj = INTERNAL_OBJ(user_data);
@@ -367,7 +353,7 @@
  *
  * \return Original lock level.
  */
-static enum ao2_lock_req adjust_lock(void *user_data, enum ao2_lock_req lock_how, int keep_stronger)
+enum ao2_lock_req __adjust_lock(void *user_data, enum ao2_lock_req lock_how, int keep_stronger)
 {
 	struct astobj2 *obj = INTERNAL_OBJ(user_data);
 	struct astobj2_rwlock *obj_rwlock;
@@ -618,6 +604,15 @@
 	return EXTERNAL_OBJ(obj);
 }
 
+unsigned int ao2_options_get(void *obj)
+{
+	struct astobj2 *orig_obj = INTERNAL_OBJ(obj);
+	if (!orig_obj) {
+		return 0;
+	}
+	return orig_obj->priv_data.options;
+}
+
 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag,
 	const char *file, int line, const char *func, int ref_debug)
 {
@@ -746,4391 +741,6 @@
 	__ast_rwlock_unlock(file, line, func, &holder->lock, name);
 
 	return obj;
-}
-
-enum ao2_callback_type {
-	AO2_CALLBACK_DEFAULT,
-	AO2_CALLBACK_WITH_DATA,
-};
-
-enum ao2_container_insert {
-	/*! The node was inserted into the container. */
-	AO2_CONTAINER_INSERT_NODE_INSERTED,
-	/*! The node object replaced an existing node object. */
-	AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED,
-	/*! The node was rejected (duplicate). */
-	AO2_CONTAINER_INSERT_NODE_REJECTED,
-};
-
-enum ao2_container_rtti {
-	/*! This is a hash container */
-	AO2_CONTAINER_RTTI_HASH,
-	/*! This is a red-black tree container */
-	AO2_CONTAINER_RTTI_RBTREE,
-};
-
-/*!
- * \brief Generic container node.
- *
- * \details This is the base container node type that contains
- * values common to all container nodes.
- */
-struct ao2_container_node {
-	/*! Stored object in node. */
-	void *obj;
-	/*! Container holding the node.  (Does not hold a reference.) */
-	struct ao2_container *my_container;
-	/*! TRUE if the node is linked into the container. */
-	unsigned int is_linked:1;
-};
-
-/*!
- * \brief Destroy this container.
- *
- * \param self Container to operate upon.
- *
- * \return Nothing
- */
-typedef void (*ao2_container_destroy_fn)(struct ao2_container *self);
-
-/*!
- * \brief Create an empty copy of this container.
- *
- * \param self Container to operate upon.
- *
- * \retval empty-container on success.
- * \retval NULL on error.
- */
-typedef struct ao2_container *(*ao2_container_alloc_empty_clone_fn)(struct ao2_container *self);
-
-/*!
- * \brief Create an empty copy of this container. (Debug version)
- *
- * \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-container on success.
- * \retval NULL on error.
- */
-typedef struct ao2_container *(*ao2_container_alloc_empty_clone_debug_fn)(struct ao2_container *self, const char *tag, const char *file, int line, const char *func, int ref_debug);
-
-/*!
- * \brief Create a new container node.
- *
- * \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.
- */
-typedef struct ao2_container_node *(*ao2_container_new_node_fn)(struct ao2_container *self, void *obj_new, const char *tag, const char *file, int line, const char *func);
-
-/*!
- * \brief Insert a node into this container.
- *
- * \param self Container to operate upon.
- * \param node Container node to insert into the container.
- *
- * \return enum ao2_container_insert value.
- */
-typedef enum ao2_container_insert (*ao2_container_insert_fn)(struct ao2_container *self, struct ao2_container_node *node);
-
-/*!
- * \brief Find the first container node in a traversal.
- *
- * \param self Container to operate upon.
- * \param flags search_flags to control traversing the container
- * \param arg Comparison callback arg parameter.
- * \param v_state Traversal state to restart container traversal.
- *
- * \retval node-ptr of found node (Reffed).
- * \retval NULL when no node found.
- */
-typedef struct ao2_container_node *(*ao2_container_find_first_fn)(struct ao2_container *self, enum search_flags flags, void *arg, void *v_state);
-
-/*!
- * \brief Find the next container node in a traversal.
- *
- * \param self Container to operate upon.
- * \param v_state Traversal state to restart 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.
- */
-typedef struct ao2_container_node *(*ao2_container_find_next_fn)(struct ao2_container *self, void *v_state, struct ao2_container_node *prev);
-
-/*!
- * \brief Cleanup the container traversal state.
- *
- * \param v_state Traversal state to cleanup.
- *
- * \return Nothing
- */
-typedef void (*ao2_container_find_cleanup_fn)(void *v_state);
-
-/*!
- * \brief Find the next non-empty iteration node in the container.
- *
- * \param self Container to operate upon.
- * \param prev 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.
- */
-typedef struct ao2_container_node *(*ao2_iterator_next_fn)(struct ao2_container *self, struct ao2_container_node *prev, enum ao2_iterator_flags flags);
-
-/*!
- * \brief Display contents of the specified container.
- *
- * \param self Container to dump.
- * \param where User data needed by prnt to determine where to put output.
- * \param prnt Print output callback function to use.
- * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
- *
- * \return Nothing
- */
-typedef void (*ao2_container_display)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj);
-
-/*!
- * \brief Display statistics of the specified container.
- *
- * \param self Container to display statistics.
- * \param where User data needed by prnt to determine where to put output.
- * \param prnt Print output callback function to use.
- *
- * \note The container is already locked for reading.
- *
- * \return Nothing
- */
-typedef void (*ao2_container_statistics)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt);
-
-/*!
- * \brief Perform an integrity check on the specified container.
- *
- * \param self Container to check integrity.
- *
- * \note The container is already locked for reading.
- *
- * \retval 0 on success.
- * \retval -1 on error.
- */
-typedef int (*ao2_container_integrity)(struct ao2_container *self);
-
-/*! Container virtual methods template. */
-struct ao2_container_methods {
-	/*! Run Time Type Identification */
-	enum ao2_container_rtti type;
-	/*! Destroy this container. */
-	ao2_container_destroy_fn destroy;
-	/*! \brief Create an empty copy of this container. */
-	ao2_container_alloc_empty_clone_fn alloc_empty_clone;
-	/*! \brief Create an empty copy of this container. (Debug version) */
-	ao2_container_alloc_empty_clone_debug_fn alloc_empty_clone_debug;
-	/*! Create a new container node. */
-	ao2_container_new_node_fn new_node;
-	/*! Insert a node into this container. */
-	ao2_container_insert_fn insert;
-	/*! Traverse the container, find the first node. */
-	ao2_container_find_first_fn traverse_first;
-	/*! Traverse the container, find the next node. */
-	ao2_container_find_next_fn traverse_next;
-	/*! Traverse the container, cleanup state. */
-	ao2_container_find_cleanup_fn traverse_cleanup;
-	/*! Find the next iteration element in the container. */
-	ao2_iterator_next_fn iterator_next;
-#if defined(AST_DEVMODE)
-	/*! Display container contents. (Method for debug purposes) */
-	ao2_container_display dump;
-	/*! Display container debug statistics. (Method for debug purposes) */
-	ao2_container_statistics stats;
-	/*! Perform an integrity check on the container. (Method for debug purposes) */
-	ao2_container_integrity integrity;
-#endif	/* defined(AST_DEVMODE) */
-};
-
-/*!
- * \brief Generic container type.
- *
- * \details This is the base container type that contains values
- * common to all container types.
- *
- * \todo Linking and unlinking container objects is typically
- * expensive, as it involves a malloc()/free() of a small object
- * which is very inefficient.  To optimize this, we can allocate
- * larger arrays of container nodes when we run out of them, and
- * then manage our own freelist.  This will be more efficient as
- * we can do the freelist management while we hold the lock
- * (that we need anyway).
- */
-struct ao2_container {
-	/*! Container virtual method table. */
-	const struct ao2_container_methods *v_table;
-	/*! Container sort function if the container is sorted. */
-	ao2_sort_fn *sort_fn;
-	/*! Container traversal matching function for ao2_find. */
-	ao2_callback_fn *cmp_fn;
-	/*! The container option flags */
-	uint32_t options;
-	/*! Number of elements in the container. */
-	int elements;
-#if defined(AST_DEVMODE)
-	/*! Number of nodes in the container. */
-	int nodes;
-	/*! Maximum number of empty nodes in the container. (nodes - elements) */
-	int max_empty_nodes;
-#endif	/* defined(AST_DEVMODE) */
-	/*!
-	 * \brief TRUE if the container is being destroyed.
-	 *
-	 * \note The destruction traversal should override any requested
-	 * search order to do the most efficient order for destruction.
-	 *
-	 * \note There should not be any empty nodes in the container
-	 * during destruction.  If there are then an error needs to be
-	 * issued about container node reference leaks.
-	 */
-	unsigned int destroying:1;
-};
-
-/*!
- * return the number of elements in the container
- */
-int ao2_container_count(struct ao2_container *c)
-{
-	return ast_atomic_fetchadd_int(&c->elements, 0);
-}
-
-#if defined(AST_DEVMODE)
-static void hash_ao2_link_node_stat(struct ao2_container *hash, struct ao2_container_node *hash_node);
-static void hash_ao2_unlink_node_stat(struct ao2_container *hash, struct ao2_container_node *hash_node);
-#endif	/* defined(AST_DEVMODE) */
-
-/*!
- * \internal
- * \brief Link an object into this container.  (internal)
- *
- * \param self Container to operate upon.
- * \param obj_new Object to insert into the container.
- * \param flags search_flags to control linking the object.  (OBJ_NOLOCK)
- * \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 0 on errors.
- * \retval 1 on success.
- */
-static int internal_ao2_link(struct ao2_container *self, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func)
-{
-	int res;
-	enum ao2_lock_req orig_lock;
-	struct ao2_container_node *node;
-
-	if (!INTERNAL_OBJ(obj_new) || !INTERNAL_OBJ(self)
-		|| !self->v_table || !self->v_table->new_node || !self->v_table->insert) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return 0;
-	}
-
-	if (flags & OBJ_NOLOCK) {
-		orig_lock = adjust_lock(self, AO2_LOCK_REQ_WRLOCK, 1);
-	} else {
-		ao2_wrlock(self);
-		orig_lock = AO2_LOCK_REQ_MUTEX;
-	}
-
-	res = 0;
-	node = self->v_table->new_node(self, obj_new, tag, file, line, func);
-	if (node) {
-#if defined(AO2_DEBUG) && defined(AST_DEVMODE)
-		switch (self->v_table->type) {
-		case AO2_CONTAINER_RTTI_HASH:
-			if (!self->sort_fn) {
-				/*
-				 * XXX chan_iax2 plays games with the hash function so we cannot
-				 * routinely do an integrity check on this type of container.
-				 * chan_iax2 should be changed to not abuse the hash function.
-				 */
-				break;
-			}
-			/* Fall through. */
-		case AO2_CONTAINER_RTTI_RBTREE:
-			if (ao2_container_check(self, OBJ_NOLOCK)) {
-				ast_log(LOG_ERROR, "Container integrity failed before insert.\n");
-			}
-			break;
-		}
-#endif	/* defined(AO2_DEBUG) && defined(AST_DEVMODE) */
-		/* Insert the new node. */
-		switch (self->v_table->insert(self, node)) {
-		case AO2_CONTAINER_INSERT_NODE_INSERTED:
-			node->is_linked = 1;
-			ast_atomic_fetchadd_int(&self->elements, 1);
-#if defined(AST_DEVMODE)
-			AO2_DEVMODE_STAT(++self->nodes);
-			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) */
-
-			res = 1;
-			break;
-		case AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED:
-			res = 1;
-			/* Fall through */
-		case AO2_CONTAINER_INSERT_NODE_REJECTED:
-			__ao2_ref(node, -1);
-			break;
-		}
-#if defined(AO2_DEBUG) && defined(AST_DEVMODE)
-		if (res) {
-			switch (self->v_table->type) {
-			case AO2_CONTAINER_RTTI_HASH:
-				if (!self->sort_fn) {
-					/*
-					 * XXX chan_iax2 plays games with the hash function so we cannot
-					 * routinely do an integrity check on this type of container.
-					 * chan_iax2 should be changed to not abuse the hash function.
-					 */
-					break;
-				}
-				/* Fall through. */
-			case AO2_CONTAINER_RTTI_RBTREE:
-				if (ao2_container_check(self, OBJ_NOLOCK)) {
-					ast_log(LOG_ERROR, "Container integrity failed after insert.\n");
-				}
-				break;
-			}
-		}
-#endif	/* defined(AO2_DEBUG) && defined(AST_DEVMODE) */
-	}
-
-	if (flags & OBJ_NOLOCK) {
-		adjust_lock(self, orig_lock, 0);
-	} else {
-		ao2_unlock(self);
-	}
-
-	return res;
-}
-
-int __ao2_link_debug(struct ao2_container *c, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func)
-{
-	return internal_ao2_link(c, obj_new, flags, tag, file, line, func);
-}
-
-int __ao2_link(struct ao2_container *c, void *obj_new, int flags)
-{
-	return internal_ao2_link(c, obj_new, flags, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__);
-}
-
-/*!
- * \brief another convenience function is a callback that matches on address
- */
-int ao2_match_by_addr(void *user_data, void *arg, int flags)
-{
-	return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
-}
-
-/*
- * Unlink an object from the container
- * and destroy the associated * bucket_entry structure.
- */
-void *__ao2_unlink_debug(struct ao2_container *c, void *user_data, int flags,
-	const char *tag, const char *file, int line, const char *func)
-{
-	if (!INTERNAL_OBJ(user_data)) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-
-	flags &= ~OBJ_SEARCH_MASK;
-	flags |= (OBJ_UNLINK | OBJ_SEARCH_OBJECT | OBJ_NODATA);
-	__ao2_callback_debug(c, flags, ao2_match_by_addr, user_data, tag, file, line, func);
-
-	return NULL;
-}
-
-void *__ao2_unlink(struct ao2_container *c, void *user_data, int flags)
-{
-	if (!INTERNAL_OBJ(user_data)) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-
-	flags &= ~OBJ_SEARCH_MASK;
-	flags |= (OBJ_UNLINK | OBJ_SEARCH_OBJECT | OBJ_NODATA);
-	__ao2_callback(c, flags, ao2_match_by_addr, user_data);
-
-	return NULL;
-}
-
-/*!
- * \brief special callback that matches all
- */
-static int cb_true(void *user_data, void *arg, int flags)
-{
-	return CMP_MATCH;
-}
-
-/*!
- * \brief similar to cb_true, but is an ao2_callback_data_fn instead
- */
-static int cb_true_data(void *user_data, void *arg, void *data, int flags)
-{
-	return CMP_MATCH;
-}
-
-/*! Allow enough room for container specific traversal state structs */
-#define AO2_TRAVERSAL_STATE_SIZE	100
-
-/*!
- * \internal
- * \brief Traverse the container.  (internal)
- *
- * \param self Container to operate upon.
- * \param flags search_flags to control traversing the container
- * \param cb_fn Comparison callback function.
- * \param arg Comparison callback arg parameter.
- * \param data Data comparison callback data parameter.
- * \param type Type of comparison callback cb_fn.
- * \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 NULL on failure or no matching object found.
- *
- * \retval object found if OBJ_MULTIPLE is not set in the flags
- * parameter.
- *
- * \retval ao2_iterator pointer if OBJ_MULTIPLE is set in the
- * flags parameter.  The iterator must be destroyed with
- * ao2_iterator_destroy() when the caller no longer needs it.
- */
-static void *internal_ao2_traverse(struct ao2_container *self, enum search_flags flags,
-	void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
-	const char *tag, const char *file, int line, const char *func)
-{
-	void *ret;
-	ao2_callback_fn *cb_default = NULL;
-	ao2_callback_data_fn *cb_withdata = NULL;
-	struct ao2_container_node *node;
-	void *traversal_state;
-
-	enum ao2_lock_req orig_lock;
-	struct ao2_container *multi_container = NULL;
-	struct ao2_iterator *multi_iterator = NULL;
-
-	if (!INTERNAL_OBJ(self) || !self->v_table || !self->v_table->traverse_first
-		|| !self->v_table->traverse_next) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-
-	/*
-	 * This logic is used so we can support OBJ_MULTIPLE with OBJ_NODATA
-	 * turned off.  This if statement checks for the special condition
-	 * where multiple items may need to be returned.
-	 */
-	if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
-		/* we need to return an ao2_iterator with the results,
-		 * as there could be more than one. the iterator will
-		 * hold the only reference to a container that has all the
-		 * matching objects linked into it, so when the iterator
-		 * is destroyed, the container will be automatically
-		 * destroyed as well.
-		 */
-		multi_container = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL,
-			NULL, "OBJ_MULTIPLE return container creation");
-		if (!multi_container) {
-			return NULL;
-		}
-		if (!(multi_iterator = ast_calloc(1, sizeof(*multi_iterator)))) {
-			ao2_t_ref(multi_container, -1, "OBJ_MULTIPLE interator creation failed.");
-			return NULL;
-		}
-	}
-
-	if (!cb_fn) {
-		/* Match everything if no callback match function provided. */
-		if (type == AO2_CALLBACK_WITH_DATA) {
-			cb_withdata = cb_true_data;
-		} else {
-			cb_default = cb_true;
-		}
-	} else {
-		/*
-		 * We do this here to avoid the per object casting penalty (even
-		 * though that is probably optimized away anyway).
-		 */
-		if (type == AO2_CALLBACK_WITH_DATA) {
-			cb_withdata = cb_fn;
-		} else {
-			cb_default = cb_fn;
-		}
-	}
-
-	/* avoid modifications to the content */
-	if (flags & OBJ_NOLOCK) {
-		if (flags & OBJ_UNLINK) {
-			orig_lock = adjust_lock(self, AO2_LOCK_REQ_WRLOCK, 1);
-		} else {
-			orig_lock = adjust_lock(self, AO2_LOCK_REQ_RDLOCK, 1);
-		}
-	} else {
-		orig_lock = AO2_LOCK_REQ_MUTEX;
-		if (flags & OBJ_UNLINK) {
-			ao2_wrlock(self);
-		} else {
-			ao2_rdlock(self);
-		}
-	}
-
-	/* Create a buffer for the traversal state. */
-	traversal_state = alloca(AO2_TRAVERSAL_STATE_SIZE);
-
-	ret = NULL;
-	for (node = self->v_table->traverse_first(self, flags, arg, traversal_state);
-		node;
-		node = self->v_table->traverse_next(self, traversal_state, node)) {
-		int match;
-
-		/* Visit the current node. */
-		match = (CMP_MATCH | CMP_STOP);
-		if (type == AO2_CALLBACK_WITH_DATA) {
-			match &= cb_withdata(node->obj, arg, data, flags);
-		} else {
-			match &= cb_default(node->obj, arg, flags);
-		}
-		if (match == 0) {
-			/* no match, no stop, continue */
-			continue;
-		}
-		if (match == CMP_STOP) {
-			/* no match but stop, we are done */
-			break;
-		}
-
-		/*
-		 * CMP_MATCH is set here
-		 *
-		 * we found the object, performing operations according to flags
-		 */
-		if (node->obj) {
-			/* The object is still in the container. */
-			if (!(flags & OBJ_NODATA)) {
-				/*
-				 * We are returning the object, record the value.  It is
-				 * important to handle this case before the unlink.
-				 */
-				if (multi_container) {
-					/*
-					 * Link the object into the container that will hold the
-					 * results.
-					 */
-					if (tag) {
-						__ao2_link_debug(multi_container, node->obj, flags,
-							tag, file, line, func);
-					} else {
-						__ao2_link(multi_container, node->obj, flags);
-					}
-				} else {
-					ret = node->obj;
-					/* Returning a single object. */
-					if (!(flags & OBJ_UNLINK)) {
-						/*
-						 * Bump the ref count since we are not going to unlink and
-						 * transfer the container's object ref to the returned object.
-						 */
-						if (tag) {
-							__ao2_ref_debug(ret, 1, tag, file, line, func);
-						} else {
-							ao2_t_ref(ret, 1, "Traversal found object");
-						}
-					}
-				}
-			}
-
-			if (flags & OBJ_UNLINK) {
-				/* update number of elements */
-				ast_atomic_fetchadd_int(&self->elements, -1);
-#if defined(AST_DEVMODE)
-				{
-					int empty = self->nodes - self->elements;
-
-					if (self->max_empty_nodes < empty) {
-						self->max_empty_nodes = empty;
-					}
-				}
-				switch (self->v_table->type) {
-				case AO2_CONTAINER_RTTI_HASH:
-					hash_ao2_unlink_node_stat(self, node);
-					break;
-				case AO2_CONTAINER_RTTI_RBTREE:
-					break;
-				}
-#endif	/* defined(AST_DEVMODE) */
-
-				/*
-				 * - When unlinking and not returning the result, OBJ_NODATA is
-				 * set, the ref from the container must be decremented.
-				 *
-				 * - When unlinking with a multi_container the ref from the
-				 * original container must be decremented.  This is because the
-				 * result is returned in a new container that already holds its
-				 * own ref for the object.
-				 *
-				 * If the ref from the original container is not accounted for
-				 * here a memory leak occurs.
-				 */
-				if (multi_container || (flags & OBJ_NODATA)) {
-					if (tag) {
-						__ao2_ref_debug(node->obj, -1, tag, file, line, func);
-					} else {
-						ao2_t_ref(node->obj, -1, "Unlink container obj reference.");
-					}
-				}
-				node->obj = NULL;
-
-				/* Unref the node from the container. */
-				__ao2_ref(node, -1);
-			}
-		}
-
-		if ((match & CMP_STOP) || !(flags & OBJ_MULTIPLE)) {
-			/* We found our only (or last) match, so we are done */
-			break;
-		}
-	}
-	if (self->v_table->traverse_cleanup) {
-		self->v_table->traverse_cleanup(traversal_state);
-	}
-	if (node) {
-		/* Unref the node from self->v_table->traverse_first/traverse_next() */
-		__ao2_ref(node, -1);
-	}
-
-	if (flags & OBJ_NOLOCK) {
-		adjust_lock(self, orig_lock, 0);
-	} else {
-		ao2_unlock(self);
-	}
-
-	/* if multi_container was created, we are returning multiple objects */
-	if (multi_container) {
-		*multi_iterator = ao2_iterator_init(multi_container,
-			AO2_ITERATOR_UNLINK | AO2_ITERATOR_MALLOCD);
-		ao2_t_ref(multi_container, -1,
-			"OBJ_MULTIPLE for multiple objects traversal complete.");
-		return multi_iterator;
-	} else {
-		return ret;
-	}
-}
-
-void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags,
-	ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line,
-	const char *func)
-{
-	return internal_ao2_traverse(c, flags, cb_fn, arg, NULL, AO2_CALLBACK_DEFAULT, tag, file, line, func);
-}
-
-void *__ao2_callback(struct ao2_container *c, enum search_flags flags,
-	ao2_callback_fn *cb_fn, void *arg)
-{
-	return internal_ao2_traverse(c, flags, cb_fn, arg, NULL, AO2_CALLBACK_DEFAULT, NULL, NULL, 0, NULL);
-}
-
-void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags,
-	ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file,
-	int line, const char *func)
-{
-	return internal_ao2_traverse(c, flags, cb_fn, arg, data, AO2_CALLBACK_WITH_DATA, tag, file, line, func);
-}
-
-void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags,
-	ao2_callback_data_fn *cb_fn, void *arg, void *data)
-{
-	return internal_ao2_traverse(c, flags, cb_fn, arg, data, AO2_CALLBACK_WITH_DATA, NULL, NULL, 0, NULL);
-}
-
-/*!
- * the find function just invokes the default callback with some reasonable flags.
- */
-void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags,
-	const char *tag, const char *file, int line, const char *func)
-{
-	void *arged = (void *) arg;/* Done to avoid compiler const warning */
-
-	if (!c) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-	return __ao2_callback_debug(c, flags, c->cmp_fn, arged, tag, file, line, func);
-}
-
-void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags)
-{
-	void *arged = (void *) arg;/* Done to avoid compiler const warning */
-
-	if (!c) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-	return __ao2_callback(c, flags, c->cmp_fn, arged);
-}
-
-/*!
- * initialize an iterator so we start from the first object
- */
-struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
-{
-	struct ao2_iterator a = {
-		.c = c,
-		.flags = flags
-	};
-
-	ao2_t_ref(c, +1, "Init iterator with container.");
-
-	return a;
-}
-
-void ao2_iterator_restart(struct ao2_iterator *iter)
-{
-	/* Release the last container node reference if we have one. */
-	if (iter->last_node) {
-		enum ao2_lock_req orig_lock;
-
-		/*
-		 * Do a read lock in case the container node unref does not
-		 * destroy the node.  If the container node is destroyed then
-		 * the lock will be upgraded to a write lock.
-		 */
-		if (iter->flags & AO2_ITERATOR_DONTLOCK) {
-			orig_lock = adjust_lock(iter->c, AO2_LOCK_REQ_RDLOCK, 1);
-		} else {
-			orig_lock = AO2_LOCK_REQ_MUTEX;
-			ao2_rdlock(iter->c);
-		}
-
-		__ao2_ref(iter->last_node, -1);
-		iter->last_node = NULL;
-
-		if (iter->flags & AO2_ITERATOR_DONTLOCK) {
-			adjust_lock(iter->c, orig_lock, 0);
-		} else {
-			ao2_unlock(iter->c);
-		}
-	}
-
-	/* The iteration is no longer complete. */
-	iter->complete = 0;
-}
-
-void ao2_iterator_destroy(struct ao2_iterator *iter)
-{
-	/* Release any last container node reference. */
-	ao2_iterator_restart(iter);
-
-	/* Release the iterated container reference. */
-	ao2_t_ref(iter->c, -1, "Unref iterator in ao2_iterator_destroy");
-	iter->c = NULL;
-
-	/* Free the malloced iterator. */
-	if (iter->flags & AO2_ITERATOR_MALLOCD) {
-		ast_free(iter);
-	}
-}
-
-void ao2_iterator_cleanup(struct ao2_iterator *iter)
-{
-	if (iter) {
-		ao2_iterator_destroy(iter);
-	}
-}
-
-/*
- * move to the next element in the container.
- */
-static void *internal_ao2_iterator_next(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func)
-{
-	enum ao2_lock_req orig_lock;
-	struct ao2_container_node *node;
-	void *ret;
-
-	if (!INTERNAL_OBJ(iter->c) || !iter->c->v_table || !iter->c->v_table->iterator_next) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-
-	if (iter->complete) {
-		/* Don't return any more objects. */
-		return NULL;
-	}
-
-	if (iter->flags & AO2_ITERATOR_DONTLOCK) {
-		if (iter->flags & AO2_ITERATOR_UNLINK) {
-			orig_lock = adjust_lock(iter->c, AO2_LOCK_REQ_WRLOCK, 1);
-		} else {
-			orig_lock = adjust_lock(iter->c, AO2_LOCK_REQ_RDLOCK, 1);
-		}
-	} else {
-		orig_lock = AO2_LOCK_REQ_MUTEX;
-		if (iter->flags & AO2_ITERATOR_UNLINK) {
-			ao2_wrlock(iter->c);
-		} else {
-			ao2_rdlock(iter->c);
-		}
-	}
-
-	node = iter->c->v_table->iterator_next(iter->c, iter->last_node, iter->flags);
-	if (node) {
-		ret = node->obj;
-
-		if (iter->flags & AO2_ITERATOR_UNLINK) {
-			/* update number of elements */
-			ast_atomic_fetchadd_int(&iter->c->elements, -1);
-#if defined(AST_DEVMODE)
-			{
-				int empty = iter->c->nodes - iter->c->elements;
-
-				if (iter->c->max_empty_nodes < empty) {
-					iter->c->max_empty_nodes = empty;
-				}
-			}
-			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) */
-
-			/* Transfer the object ref from the container to the returned object. */
-			node->obj = NULL;
-
-			/* Transfer the container's node ref to the iterator. */
-		} else {
-			/* Bump ref of returned object */
-			if (tag) {
-				__ao2_ref_debug(ret, +1, tag, file, line, func);
-			} else {
-				ao2_t_ref(ret, +1, "Next iterator object.");
-			}
-
-			/* Bump the container's node ref for the iterator. */
-			__ao2_ref(node, +1);
-		}
-	} else {
-		/* The iteration has completed. */
-		iter->complete = 1;
-		ret = NULL;
-	}
-
-	/* Replace the iterator's node */
-	if (iter->last_node) {
-		__ao2_ref(iter->last_node, -1);
-	}
-	iter->last_node = node;
-
-	if (iter->flags & AO2_ITERATOR_DONTLOCK) {
-		adjust_lock(iter->c, orig_lock, 0);
-	} else {
-		ao2_unlock(iter->c);
-	}
-
-	return ret;
-}
-
-void *__ao2_iterator_next_debug(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func)
-{
-	return internal_ao2_iterator_next(iter, tag, file, line, func);
-}
-
-void *__ao2_iterator_next(struct ao2_iterator *iter)
-{
-	return internal_ao2_iterator_next(iter, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__);
-}
-
-int ao2_iterator_count(struct ao2_iterator *iter)
-{
-	return ao2_container_count(iter->c);
-}
-
-static void container_destruct(void *_c)
-{
-	struct ao2_container *c = _c;
-
-	/* Unlink any stored objects in the container. */
-	c->destroying = 1;
-	__ao2_callback(c, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
-
-	/* Perform any extra container cleanup. */
-	if (c->v_table && c->v_table->destroy) {
-		c->v_table->destroy(c);
-	}
-
-#ifdef AO2_DEBUG
-	ast_atomic_fetchadd_int(&ao2.total_containers, -1);
-#endif
-}
-
-static void container_destruct_debug(void *_c)
-{
-	struct ao2_container *c = _c;
-
-	/* Unlink any stored objects in the container. */
-	c->destroying = 1;
-	__ao2_callback_debug(c, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL,
-		"container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
-
-	/* Perform any extra container cleanup. */
-	if (c->v_table && c->v_table->destroy) {
-		c->v_table->destroy(c);
-	}
-
-#ifdef AO2_DEBUG
-	ast_atomic_fetchadd_int(&ao2.total_containers, -1);
-#endif
-}
-
-/*!
- * \internal
- * \brief Put obj into the arg container.
- * \since 11.0
- *
- * \param obj  pointer to the (user-defined part) of an object.
- * \param arg callback argument from ao2_callback()
- * \param flags flags from ao2_callback()
- *
- * \retval 0 on success.
- * \retval CMP_STOP|CMP_MATCH on error.
- */
-static int dup_obj_cb(void *obj, void *arg, int flags)
-{
-	struct ao2_container *dest = arg;
-
-	return __ao2_link(dest, obj, OBJ_NOLOCK) ? 0 : (CMP_MATCH | CMP_STOP);
-}
-
-int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags)
-{
-	void *obj;
-	int res = 0;
-
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_rdlock(src);
-		ao2_wrlock(dest);
-	}
-	obj = __ao2_callback(src, OBJ_NOLOCK, dup_obj_cb, dest);
-	if (obj) {
-		/* Failed to put this obj into the dest container. */
-		ao2_t_ref(obj, -1, "Failed to put this object into the dest container.");
-
-		/* Remove all items from the dest container. */
-		__ao2_callback(dest, OBJ_NOLOCK | OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL,
-			NULL);
-		res = -1;
-	}
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_unlock(dest);
-		ao2_unlock(src);
-	}
-
-	return res;
-}
-
-struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags)
-{
-	struct ao2_container *clone;
-	int failed;
-
-	/* Create the clone container with the same properties as the original. */
-	if (!INTERNAL_OBJ(orig) || !orig->v_table || !orig->v_table->alloc_empty_clone) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-	clone = orig->v_table->alloc_empty_clone(orig);
-	if (!clone) {
-		return NULL;
-	}
-
-	if (flags & OBJ_NOLOCK) {
-		ao2_wrlock(clone);
-	}
-	failed = ao2_container_dup(clone, orig, flags);
-	if (flags & OBJ_NOLOCK) {
-		ao2_unlock(clone);
-	}
-	if (failed) {
-		/* Object copy into the clone container failed. */
-		ao2_t_ref(clone, -1, "Clone creation failed.");
-		clone = NULL;
-	}
-	return clone;
-}
-
-struct ao2_container *__ao2_container_clone_debug(struct ao2_container *orig, enum search_flags flags, const char *tag, const char *file, int line, const char *func, int ref_debug)
-{
-	struct ao2_container *clone;
-	int failed;
-
-	/* Create the clone container with the same properties as the original. */
-	if (!INTERNAL_OBJ(orig) || !orig->v_table || !orig->v_table->alloc_empty_clone_debug) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return NULL;
-	}
-	clone = orig->v_table->alloc_empty_clone_debug(orig, tag, file, line, func, ref_debug);
-	if (!clone) {
-		return NULL;
-	}
-
-	if (flags & OBJ_NOLOCK) {
-		ao2_wrlock(clone);
-	}
-	failed = ao2_container_dup(clone, orig, flags);
-	if (flags & OBJ_NOLOCK) {
-		ao2_unlock(clone);
-	}
-	if (failed) {
-		/* Object copy into the clone container failed. */
-		if (ref_debug) {
-			__ao2_ref_debug(clone, -1, tag, file, line, func);
-		} else {
-			ao2_t_ref(clone, -1, "Clone creation failed.");
-		}
-		clone = NULL;
-	}
-	return clone;
-}
-
-void ao2_container_dump(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj)
-{
-	if (!INTERNAL_OBJ(self) || !self->v_table) {
-		prnt(where, "Invalid container\n");
-		ast_assert(0);
-		return;
-	}
-
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_rdlock(self);
-	}
-	if (name) {
-		prnt(where, "Container name: %s\n", name);
-	}
-#if defined(AST_DEVMODE)
-	if (self->v_table->dump) {
-		self->v_table->dump(self, where, prnt, prnt_obj);
-	} else
-#endif	/* defined(AST_DEVMODE) */
-	{
-		prnt(where, "Container dump not available.\n");
-	}
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_unlock(self);
-	}
-}
-
-void ao2_container_stats(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt)
-{
-	if (!INTERNAL_OBJ(self) || !self->v_table) {
-		prnt(where, "Invalid container\n");
-		ast_assert(0);
-		return;
-	}
-
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_rdlock(self);
-	}
-	if (name) {
-		prnt(where, "Container name: %s\n", name);
-	}
-	prnt(where, "Number of objects: %d\n", self->elements);
-#if defined(AST_DEVMODE)
-	prnt(where, "Number of nodes: %d\n", self->nodes);
-	prnt(where, "Number of empty nodes: %d\n", self->nodes - self->elements);
-	/*
-	 * XXX
-	 * If the max_empty_nodes count gets out of single digits you
-	 * likely have a code path where ao2_iterator_destroy() is not
-	 * called.
-	 *
-	 * Empty nodes do not harm the container but they do make
-	 * container operations less efficient.
-	 */
-	prnt(where, "Maximum empty nodes: %d\n", self->max_empty_nodes);
-	if (self->v_table->stats) {
-		self->v_table->stats(self, where, prnt);
-	}
-#endif	/* defined(AST_DEVMODE) */
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_unlock(self);
-	}
-}
-
-int ao2_container_check(struct ao2_container *self, enum search_flags flags)
-{
-	int res = 0;
-
-	if (!INTERNAL_OBJ(self) || !self->v_table) {
-		/* Sanity checks. */
-		ast_assert(0);
-		return -1;
-	}
-#if defined(AST_DEVMODE)
-	if (!self->v_table->integrity) {
-		/* No ingetrigy check available.  Assume container is ok. */
-		return 0;
-	}
-
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_rdlock(self);
-	}
-	res = self->v_table->integrity(self);
-	if (!(flags & OBJ_NOLOCK)) {
-		ao2_unlock(self);
-	}
-#endif	/* defined(AST_DEVMODE) */
-	return res;
-}
-
-/*!
- * A structure to create a linked list of entries,
- * used within a bucket.
- */
-struct hash_bucket_node {
-	/*!
-	 * \brief Items common to all container nodes.
-	 * \note Must be first in the specific node struct.
-	 */
-	struct ao2_container_node common;
-	/*! Next node links in the list. */
-	AST_DLLIST_ENTRY(hash_bucket_node) links;

[... 3693 lines stripped ...]



More information about the svn-commits mailing list