[asterisk-commits] rmudgett: trunk r347110 - in /trunk: include/asterisk/ tests/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Dec 6 13:10:00 CST 2011


Author: rmudgett
Date: Tue Dec  6 13:09:56 2011
New Revision: 347110

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=347110
Log:
Doubly linked lists unit test and update to implementation.

Update the doubly linked list implementation.  Now safe traversing can
insert before and after the current node when traversing in either
direction.

Updated the linked lists unit test test_linkedlist to also test doubly
linked lists.  The old test_dlinkedlist requires a manual check of results
and probably should be removed.

Review: https://reviewboard.asterisk.org/r/1569/

Modified:
    trunk/include/asterisk/dlinkedlists.h
    trunk/tests/test_linkedlists.c

Modified: trunk/include/asterisk/dlinkedlists.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/dlinkedlists.h?view=diff&rev=347110&r1=347109&r2=347110
==============================================================================
--- trunk/include/asterisk/dlinkedlists.h (original)
+++ trunk/include/asterisk/dlinkedlists.h Tue Dec  6 13:09:56 2011
@@ -28,994 +28,1212 @@
 #include "asterisk/lock.h"
 
 /*!
-  \file dlinkedlists.h
-  \brief A set of macros to manage doubly-linked lists.
-*/
-
-/*!
-  \brief Locks a list.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to place an exclusive lock in the
-  list head structure pointed to by head.
-  \retval 0 on success
-  \retval non-zero on failure
-  \since 1.6.1
-*/
+ * \file dlinkedlists.h
+ * \brief A set of macros to manage doubly-linked lists.
+ */
+
+/*!
+ * \brief Locks a list.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to place an exclusive lock in the
+ * list head structure pointed to by head.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ * \since 1.6.1
+ */
 #define AST_DLLIST_LOCK(head)						\
 	ast_mutex_lock(&(head)->lock)
 
 /*!
-  \brief Write locks a list.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to place an exclusive write lock in the
-  list head structure pointed to by head.
-  \retval 0 on success
-  \retval non-zero on failure
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_WRLOCK(head)                                         \
-        ast_rwlock_wrlock(&(head)->lock)
-
-/*!
-  \brief Read locks a list.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to place a read lock in the
-  list head structure pointed to by head.
-  \retval 0 on success
-  \retval non-zero on failure
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_RDLOCK(head)                                         \
-        ast_rwlock_rdlock(&(head)->lock)
-
-/*!
-  \brief Locks a list, without blocking if the list is locked.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to place an exclusive lock in the
-  list head structure pointed to by head.
-  \retval 0 on success
-  \retval non-zero on failure
-  \since 1.6.1
-*/
-#define AST_DLLIST_TRYLOCK(head)						\
+ * \brief Write locks a list.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to place an exclusive write lock in the
+ * list head structure pointed to by head.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_WRLOCK(head)					\
+	ast_rwlock_wrlock(&(head)->lock)
+
+/*!
+ * \brief Read locks a list.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to place a read lock in the
+ * list head structure pointed to by head.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_RDLOCK(head)					\
+	ast_rwlock_rdlock(&(head)->lock)
+
+/*!
+ * \brief Locks a list, without blocking if the list is locked.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to place an exclusive lock in the
+ * list head structure pointed to by head.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ * \since 1.6.1
+ */
+#define AST_DLLIST_TRYLOCK(head)					\
 	ast_mutex_trylock(&(head)->lock)
 
 /*!
-  \brief Write locks a list, without blocking if the list is locked.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to place an exclusive write lock in the
-  list head structure pointed to by head.
-  \retval 0 on success
-  \retval non-zero on failure
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_TRYWRLOCK(head)                                      \
-        ast_rwlock_trywrlock(&(head)->lock)
-
-/*!
-  \brief Read locks a list, without blocking if the list is locked.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to place a read lock in the
-  list head structure pointed to by head.
-  \retval 0 on success
-  \retval non-zero on failure
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_TRYRDLOCK(head)                                      \
-        ast_rwlock_tryrdlock(&(head)->lock)
-
-/*!
-  \brief Attempts to unlock a list.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to remove an exclusive lock from the
-  list head structure pointed to by head. If the list
-  was not locked by this thread, this macro has no effect.
-  \since 1.6.1
-*/
-#define AST_DLLIST_UNLOCK(head) 						\
+ * \brief Write locks a list, without blocking if the list is locked.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to place an exclusive write lock in the
+ * list head structure pointed to by head.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_TRYWRLOCK(head)				\
+	ast_rwlock_trywrlock(&(head)->lock)
+
+/*!
+ * \brief Read locks a list, without blocking if the list is locked.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to place a read lock in the
+ * list head structure pointed to by head.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_TRYRDLOCK(head)				\
+	ast_rwlock_tryrdlock(&(head)->lock)
+
+/*!
+ * \brief Attempts to unlock a list.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to remove an exclusive lock from the
+ * list head structure pointed to by head. If the list
+ * was not locked by this thread, this macro has no effect.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_UNLOCK(head) 					\
 	ast_mutex_unlock(&(head)->lock)
 
 /*!
-  \brief Attempts to unlock a read/write based list.
-  \param head This is a pointer to the list head structure
-
-  This macro attempts to remove a read or write lock from the
-  list head structure pointed to by head. If the list
-  was not locked by this thread, this macro has no effect.
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_UNLOCK(head)                                         \
-        ast_rwlock_unlock(&(head)->lock)
-
-/*!
-  \brief Defines a structure to be used to hold a list of specified type.
-  \param name This will be the name of the defined structure.
-  \param type This is the type of each list entry.
-
-  This macro creates a structure definition that can be used
-  to hold a list of the entries of type \a type. It does not actually
-  declare (allocate) a structure; to do that, either follow this
-  macro with the desired name of the instance you wish to declare,
-  or use the specified \a name to declare instances elsewhere.
-
-  Example usage:
-  \code
-  static AST_DLLIST_HEAD(entry_list, entry) entries;
-  \endcode
-
-  This would define \c struct \c entry_list, and declare an instance of it named
-  \a entries, all intended to hold a list of type \c struct \c entry.
-  \since 1.6.1
-*/
+ * \brief Attempts to unlock a read/write based list.
+ * \param head This is a pointer to the list head structure
+ *
+ * This macro attempts to remove a read or write lock from the
+ * list head structure pointed to by head. If the list
+ * was not locked by this thread, this macro has no effect.
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_UNLOCK(head)					\
+	ast_rwlock_unlock(&(head)->lock)
+
+/*!
+ * \brief Defines a structure to be used to hold a list of specified type.
+ * \param name This will be the name of the defined structure.
+ * \param type This is the type of each list entry.
+ *
+ * This macro creates a structure definition that can be used
+ * to hold a list of the entries of type \a type. It does not actually
+ * declare (allocate) a structure; to do that, either follow this
+ * macro with the desired name of the instance you wish to declare,
+ * or use the specified \a name to declare instances elsewhere.
+ *
+ * Example usage:
+ * \code
+ * static AST_DLLIST_HEAD(entry_list, entry) entries;
+ * \endcode
+ *
+ * This would define \c struct \c entry_list, and declare an instance of it named
+ * \a entries, all intended to hold a list of type \c struct \c entry.
+ * \since 1.6.1
+ */
 #define AST_DLLIST_HEAD(name, type)					\
-struct name {								\
-	struct type *first;						\
-	struct type *last;						\
-	ast_mutex_t lock;						\
-}
-
-/*!
-  \brief Defines a structure to be used to hold a read/write list of specified type.
-  \param name This will be the name of the defined structure.
-  \param type This is the type of each list entry.
-
-  This macro creates a structure definition that can be used
-  to hold a list of the entries of type \a type. It does not actually
-  declare (allocate) a structure; to do that, either follow this
-  macro with the desired name of the instance you wish to declare,
-  or use the specified \a name to declare instances elsewhere.
-
-  Example usage:
-  \code
-  static AST_RWDLLIST_HEAD(entry_list, entry) entries;
-  \endcode
-
-  This would define \c struct \c entry_list, and declare an instance of it named
-  \a entries, all intended to hold a list of type \c struct \c entry.
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_HEAD(name, type)                                     \
-struct name {                                                           \
-        struct type *first;                                             \
-        struct type *last;                                              \
-        ast_rwlock_t lock;                                              \
-}
-
-/*!
-  \brief Defines a structure to be used to hold a list of specified type (with no lock).
-  \param name This will be the name of the defined structure.
-  \param type This is the type of each list entry.
-
-  This macro creates a structure definition that can be used
-  to hold a list of the entries of type \a type. It does not actually
-  declare (allocate) a structure; to do that, either follow this
-  macro with the desired name of the instance you wish to declare,
-  or use the specified \a name to declare instances elsewhere.
-
-  Example usage:
-  \code
-  static AST_DLLIST_HEAD_NOLOCK(entry_list, entry) entries;
-  \endcode
-
-  This would define \c struct \c entry_list, and declare an instance of it named
-  \a entries, all intended to hold a list of type \c struct \c entry.
-  \since 1.6.1
-*/
-#define AST_DLLIST_HEAD_NOLOCK(name, type)				\
-struct name {								\
-	struct type *first;						\
-	struct type *last;						\
-}
-
-/*!
-  \brief Defines initial values for a declaration of AST_DLLIST_HEAD
-  \since 1.6.1
-*/
-#define AST_DLLIST_HEAD_INIT_VALUE	{		\
-	.first = NULL,					\
-	.last = NULL,					\
-	.lock = AST_MUTEX_INIT_VALUE,			\
+	struct name {									\
+		struct type *first;							\
+		struct type *last;							\
+		ast_mutex_t lock;							\
 	}
 
 /*!
-  \brief Defines initial values for a declaration of AST_RWDLLIST_HEAD
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_HEAD_INIT_VALUE      {               \
-        .first = NULL,                                  \
-        .last = NULL,                                   \
-        .lock = AST_RWLOCK_INIT_VALUE,                  \
-        }
-
-/*!
-  \brief Defines initial values for a declaration of AST_DLLIST_HEAD_NOLOCK
-  \since 1.6.1
-*/
-#define AST_DLLIST_HEAD_NOLOCK_INIT_VALUE	{	\
-	.first = NULL,					\
-	.last = NULL,					\
+ * \brief Defines a structure to be used to hold a read/write list of specified type.
+ * \param name This will be the name of the defined structure.
+ * \param type This is the type of each list entry.
+ *
+ * This macro creates a structure definition that can be used
+ * to hold a list of the entries of type \a type. It does not actually
+ * declare (allocate) a structure; to do that, either follow this
+ * macro with the desired name of the instance you wish to declare,
+ * or use the specified \a name to declare instances elsewhere.
+ *
+ * Example usage:
+ * \code
+ * static AST_RWDLLIST_HEAD(entry_list, entry) entries;
+ * \endcode
+ *
+ * This would define \c struct \c entry_list, and declare an instance of it named
+ * \a entries, all intended to hold a list of type \c struct \c entry.
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_HEAD(name, type)				\
+	struct name {									\
+		struct type *first;							\
+		struct type *last;							\
+		ast_rwlock_t lock;							\
 	}
 
 /*!
-  \brief Defines a structure to be used to hold a list of specified type, statically initialized.
-  \param name This will be the name of the defined structure.
-  \param type This is the type of each list entry.
-
-  This macro creates a structure definition that can be used
-  to hold a list of the entries of type \a type, and allocates an instance
-  of it, initialized to be empty.
-
-  Example usage:
-  \code
-  static AST_DLLIST_HEAD_STATIC(entry_list, entry);
-  \endcode
-
-  This would define \c struct \c entry_list, intended to hold a list of
-  type \c struct \c entry.
-  \since 1.6.1
-*/
+ * \brief Defines a structure to be used to hold a list of specified type (with no lock).
+ * \param name This will be the name of the defined structure.
+ * \param type This is the type of each list entry.
+ *
+ * This macro creates a structure definition that can be used
+ * to hold a list of the entries of type \a type. It does not actually
+ * declare (allocate) a structure; to do that, either follow this
+ * macro with the desired name of the instance you wish to declare,
+ * or use the specified \a name to declare instances elsewhere.
+ *
+ * Example usage:
+ * \code
+ * static AST_DLLIST_HEAD_NOLOCK(entry_list, entry) entries;
+ * \endcode
+ *
+ * This would define \c struct \c entry_list, and declare an instance of it named
+ * \a entries, all intended to hold a list of type \c struct \c entry.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_HEAD_NOLOCK(name, type)			\
+	struct name {									\
+		struct type *first;							\
+		struct type *last;							\
+	}
+
+/*!
+ * \brief Defines initial values for a declaration of AST_DLLIST_HEAD
+ * \since 1.6.1
+ */
+#define AST_DLLIST_HEAD_INIT_VALUE					\
+	{												\
+		.first = NULL,								\
+		.last = NULL,								\
+		.lock = AST_MUTEX_INIT_VALUE,				\
+	}
+
+/*!
+ * \brief Defines initial values for a declaration of AST_RWDLLIST_HEAD
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_HEAD_INIT_VALUE				\
+	{												\
+		.first = NULL,								\
+		.last = NULL,								\
+		.lock = AST_RWLOCK_INIT_VALUE,				\
+	}
+
+/*!
+ * \brief Defines initial values for a declaration of AST_DLLIST_HEAD_NOLOCK
+ * \since 1.6.1
+ */
+#define AST_DLLIST_HEAD_NOLOCK_INIT_VALUE			\
+	{												\
+		.first = NULL,								\
+		.last = NULL,								\
+	}
+
+/*!
+ * \brief Defines a structure to be used to hold a list of specified type, statically initialized.
+ * \param name This will be the name of the defined structure.
+ * \param type This is the type of each list entry.
+ *
+ * This macro creates a structure definition that can be used
+ * to hold a list of the entries of type \a type, and allocates an instance
+ * of it, initialized to be empty.
+ *
+ * Example usage:
+ * \code
+ * static AST_DLLIST_HEAD_STATIC(entry_list, entry);
+ * \endcode
+ *
+ * This would define \c struct \c entry_list, intended to hold a list of
+ * type \c struct \c entry.
+ * \since 1.6.1
+ */
 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
-#define AST_DLLIST_HEAD_STATIC(name, type)				\
-struct name {								\
-	struct type *first;						\
-	struct type *last;						\
-	ast_mutex_t lock;						\
-} name;									\
-static void  __attribute__((constructor)) __init_##name(void)		\
-{									\
-        AST_DLLIST_HEAD_INIT(&name);					\
-}									\
-static void  __attribute__((destructor)) __fini_##name(void)		\
-{									\
-        AST_DLLIST_HEAD_DESTROY(&name);					\
-}									\
-struct __dummy_##name
+#define AST_DLLIST_HEAD_STATIC(name, type)							\
+	struct name {													\
+		struct type *first;											\
+		struct type *last;											\
+		ast_mutex_t lock;											\
+	} name;															\
+	static void  __attribute__((constructor)) __init_##name(void)	\
+	{																\
+		AST_DLLIST_HEAD_INIT(&name);								\
+	}																\
+	static void  __attribute__((destructor)) __fini_##name(void)	\
+	{																\
+		AST_DLLIST_HEAD_DESTROY(&name);								\
+	}																\
+	struct __dummy_##name
 #else
-#define AST_DLLIST_HEAD_STATIC(name, type)				\
-struct name {								\
-	struct type *first;						\
-	struct type *last;						\
-	ast_mutex_t lock;						\
-} name = AST_DLLIST_HEAD_INIT_VALUE
+#define AST_DLLIST_HEAD_STATIC(name, type)			\
+	struct name {									\
+		struct type *first;							\
+		struct type *last;							\
+		ast_mutex_t lock;							\
+	} name = AST_DLLIST_HEAD_INIT_VALUE
 #endif
 
 /*!
-  \brief Defines a structure to be used to hold a read/write list of specified type, statically initialized.
-  \param name This will be the name of the defined structure.
-  \param type This is the type of each list entry.
-
-  This macro creates a structure definition that can be used
-  to hold a list of the entries of type \a type, and allocates an instance
-  of it, initialized to be empty.
-
-  Example usage:
-  \code
-  static AST_RWDLLIST_HEAD_STATIC(entry_list, entry);
-  \endcode
-
-  This would define \c struct \c entry_list, intended to hold a list of
-  type \c struct \c entry.
-  \since 1.6.1
-*/
-#ifndef AST_RWLOCK_INIT_VALUE
-#define AST_RWDLLIST_HEAD_STATIC(name, type)                              \
-struct name {                                                           \
-        struct type *first;                                             \
-        struct type *last;                                              \
-        ast_rwlock_t lock;                                              \
-} name;                                                                 \
-static void  __attribute__((constructor)) __init_##name(void)          \
-{                                                                       \
-        AST_RWDLLIST_HEAD_INIT(&name);                                    \
-}                                                                       \
-static void  __attribute__((destructor)) __fini_##name(void)           \
-{                                                                       \
-        AST_RWDLLIST_HEAD_DESTROY(&name);                                 \
-}                                                                       \
-struct __dummy_##name
+ * \brief Defines a structure to be used to hold a read/write list of specified type, statically initialized.
+ * \param name This will be the name of the defined structure.
+ * \param type This is the type of each list entry.
+ *
+ * This macro creates a structure definition that can be used
+ * to hold a list of the entries of type \a type, and allocates an instance
+ * of it, initialized to be empty.
+ *
+ * Example usage:
+ * \code
+ * static AST_RWDLLIST_HEAD_STATIC(entry_list, entry);
+ * \endcode
+ *
+ * This would define \c struct \c entry_list, intended to hold a list of
+ * type \c struct \c entry.
+ * \since 1.6.1
+ */
+#ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER
+#define AST_RWDLLIST_HEAD_STATIC(name, type)						\
+	struct name {													\
+		struct type *first;											\
+		struct type *last;											\
+		ast_rwlock_t lock;											\
+	} name;															\
+	static void  __attribute__((constructor)) __init_##name(void)	\
+	{																\
+		AST_RWDLLIST_HEAD_INIT(&name);								\
+	}																\
+	static void  __attribute__((destructor)) __fini_##name(void)	\
+	{																\
+		AST_RWDLLIST_HEAD_DESTROY(&name);							\
+	}																\
+	struct __dummy_##name
 #else
-#define AST_RWDLLIST_HEAD_STATIC(name, type)                              \
-struct name {                                                           \
-        struct type *first;                                             \
-        struct type *last;                                              \
-        ast_rwlock_t lock;                                              \
-} name = AST_RWDLLIST_HEAD_INIT_VALUE
+#define AST_RWDLLIST_HEAD_STATIC(name, type)		\
+	struct name {									\
+		struct type *first;							\
+		struct type *last;							\
+		ast_rwlock_t lock;							\
+	} name = AST_RWDLLIST_HEAD_INIT_VALUE
 #endif
 
 /*!
-  \brief Defines a structure to be used to hold a list of specified type, statically initialized.
-
-  This is the same as AST_DLLIST_HEAD_STATIC, except without the lock included.
-  \since 1.6.1
-*/
-#define AST_DLLIST_HEAD_NOLOCK_STATIC(name, type)				\
-struct name {								\
-	struct type *first;						\
-	struct type *last;						\
-} name = AST_DLLIST_HEAD_NOLOCK_INIT_VALUE
-
-/*!
-  \brief Initializes a list head structure with a specified first entry.
-  \param head This is a pointer to the list head structure
-  \param entry pointer to the list entry that will become the head of the list
-
-  This macro initializes a list head structure by setting the head
-  entry to the supplied value and recreating the embedded lock.
-  \since 1.6.1
-*/
-#define AST_DLLIST_HEAD_SET(head, entry) do {				\
-	(head)->first = (entry);					\
-	(head)->last = (entry);						\
-	ast_mutex_init(&(head)->lock);					\
-} while (0)
-
-/*!
-  \brief Initializes an rwlist head structure with a specified first entry.
-  \param head This is a pointer to the list head structure
-  \param entry pointer to the list entry that will become the head of the list
-
-  This macro initializes a list head structure by setting the head
-  entry to the supplied value and recreating the embedded lock.
-  \since 1.6.1
-*/
-#define AST_RWDLLIST_HEAD_SET(head, entry) do {                           \
-        (head)->first = (entry);                                        \
-        (head)->last = (entry);                                         \
-        ast_rwlock_init(&(head)->lock);                                 \
-} while (0)
-
-/*!
-  \brief Initializes a list head structure with a specified first entry.
-  \param head This is a pointer to the list head structure
-  \param entry pointer to the list entry that will become the head of the list
-
-  This macro initializes a list head structure by setting the head
-  entry to the supplied value.
-  \since 1.6.1
-*/
-#define AST_DLLIST_HEAD_SET_NOLOCK(head, entry) do {			\
-	(head)->first = (entry);					\
-	(head)->last = (entry);						\
-} while (0)
-
-/*!
-  \brief Declare previous/forward links inside a list entry.
-  \param type This is the type of each list entry.
-
-  This macro declares a structure to be used to doubly link list entries together.
-  It must be used inside the definition of the structure named in
-  \a type, as follows:
-
-  \code
-  struct list_entry {
-  	...
-  	AST_DLLIST_ENTRY(list_entry) list;
-  }
-  \endcode
-
-  The field name \a list here is arbitrary, and can be anything you wish.
-  \since 1.6.1
-*/
-#define AST_DLLIST_ENTRY(type)			\
-struct {								\
-	struct type *prev;						\
-	struct type *next;						\
-}
+ * \brief Defines a structure to be used to hold a list of specified type, statically initialized.
+ *
+ * This is the same as AST_DLLIST_HEAD_STATIC, except without the lock included.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_HEAD_NOLOCK_STATIC(name, type)	\
+	struct name {									\
+		struct type *first;							\
+		struct type *last;							\
+	} name = AST_DLLIST_HEAD_NOLOCK_INIT_VALUE
+
+/*!
+ * \brief Initializes a list head structure with a specified first entry.
+ * \param head This is a pointer to the list head structure
+ * \param entry pointer to the list entry that will become the head of the list
+ *
+ * This macro initializes a list head structure by setting the head
+ * entry to the supplied value and recreating the embedded lock.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_HEAD_SET(head, entry)			\
+	do {											\
+		(head)->first = (entry);					\
+		(head)->last = (entry);						\
+		ast_mutex_init(&(head)->lock);				\
+	} while (0)
+
+/*!
+ * \brief Initializes an rwlist head structure with a specified first entry.
+ * \param head This is a pointer to the list head structure
+ * \param entry pointer to the list entry that will become the head of the list
+ *
+ * This macro initializes a list head structure by setting the head
+ * entry to the supplied value and recreating the embedded lock.
+ * \since 1.6.1
+ */
+#define AST_RWDLLIST_HEAD_SET(head, entry)			\
+	do {											\
+		(head)->first = (entry);					\
+		(head)->last = (entry);						\
+		ast_rwlock_init(&(head)->lock);				\
+	} while (0)
+
+/*!
+ * \brief Initializes a list head structure with a specified first entry.
+ * \param head This is a pointer to the list head structure
+ * \param entry pointer to the list entry that will become the head of the list
+ *
+ * This macro initializes a list head structure by setting the head
+ * entry to the supplied value.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_HEAD_SET_NOLOCK(head, entry)		\
+	do {											\
+		(head)->first = (entry);					\
+		(head)->last = (entry);						\
+	} while (0)
+
+/*!
+ * \brief Declare previous/forward links inside a list entry.
+ * \param type This is the type of each list entry.
+ *
+ * This macro declares a structure to be used to doubly link list entries together.
+ * It must be used inside the definition of the structure named in
+ * \a type, as follows:
+ *
+ * \code
+ * struct list_entry {
+ *     ...
+ *     AST_DLLIST_ENTRY(list_entry) list;
+ * }
+ * \endcode
+ *
+ * The field name \a list here is arbitrary, and can be anything you wish.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_ENTRY(type)			AST_DLLIST_HEAD_NOLOCK(, type)
 
 #define AST_RWDLLIST_ENTRY AST_DLLIST_ENTRY
 
 /*!
-  \brief Returns the first entry contained in a list.
-  \param head This is a pointer to the list head structure
-  \since 1.6.1
+ * \brief Returns the first entry contained in a list.
+ * \param head This is a pointer to the list head structure
+ * \since 1.6.1
  */
 #define	AST_DLLIST_FIRST(head)	((head)->first)
 
 #define AST_RWDLLIST_FIRST AST_DLLIST_FIRST
 
 /*!
-  \brief Returns the last entry contained in a list.
-  \param head This is a pointer to the list head structure
-  \since 1.6.1
+ * \brief Returns the last entry contained in a list.
+ * \param head This is a pointer to the list head structure
+ * \since 1.6.1
  */
 #define	AST_DLLIST_LAST(head)	((head)->last)
 
 #define AST_RWDLLIST_LAST AST_DLLIST_LAST
 
-/*!
-  \brief Returns the next entry in the list after the given entry.
-  \param elm This is a pointer to the current entry.
-  \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
-  used to link entries of this list together.
-  \since 1.6.1
-*/
-#define AST_DLLIST_NEXT(elm, field)	((elm)->field.next)
+#define AST_DLLIST_NEXT_DIRECTION(elm, field, direction)	((elm)->field.direction)
+
+#define AST_RWDLLIST_NEXT_DIRECTION AST_DLLIST_NEXT_DIRECTION
+
+/*!
+ * \brief Returns the next entry in the list after the given entry.
+ * \param elm This is a pointer to the current entry.
+ * \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
+ * used to link entries of this list together.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_NEXT(elm, field)	AST_DLLIST_NEXT_DIRECTION(elm, field, first)
 
 #define AST_RWDLLIST_NEXT AST_DLLIST_NEXT
 
 /*!
-  \brief Returns the previous entry in the list before the given entry.
-  \param elm This is a pointer to the current entry.
-  \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
-  used to link entries of this list together.
-  \since 1.6.1
-*/
-#define AST_DLLIST_PREV(elm, field)	((elm)->field.prev)
+ * \brief Returns the previous entry in the list before the given entry.
+ * \param elm This is a pointer to the current entry.
+ * \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
+ * used to link entries of this list together.
+ * \since 1.6.1
+ */
+#define AST_DLLIST_PREV(elm, field)	AST_DLLIST_NEXT_DIRECTION(elm, field, last)
 
 #define AST_RWDLLIST_PREV AST_DLLIST_PREV
 
 /*!
-  \brief Checks whether the specified list contains any entries.
-  \param head This is a pointer to the list head structure
-
-  \return non-zero if the list has entries
-  \return zero if not.
-  \since 1.6.1
+ * \brief Checks whether the specified list contains any entries.
+ * \param head This is a pointer to the list head structure
+ *
+ * \return non-zero if the list has entries
+ * \return zero if not.
+ * \since 1.6.1
  */
 #define	AST_DLLIST_EMPTY(head)	(AST_DLLIST_FIRST(head) == NULL)
 
 #define AST_RWDLLIST_EMPTY AST_DLLIST_EMPTY
 
 /*!
-  \brief Loops over (traverses) the entries in a list.
-  \param head This is a pointer to the list head structure
-  \param var This is the name of the variable that will hold a pointer to the
-  current list entry on each iteration. It must be declared before calling
-  this macro.
-  \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
-  used to link entries of this list together.
-
-  This macro is use to loop over (traverse) the entries in a list. It uses a
-  \a for loop, and supplies the enclosed code with a pointer to each list
-  entry as it loops. It is typically used as follows:
-  \code
-  static AST_DLLIST_HEAD(entry_list, list_entry) entries;
-  ...
-  struct list_entry {
-  	...
-  	AST_DLLIST_ENTRY(list_entry) list;
-  }
-  ...
-  struct list_entry *current;
-  ...
-  AST_DLLIST_TRAVERSE(&entries, current, list) {
-     (do something with current here)
-  }
-  \endcode
-  \warning If you modify the forward-link pointer contained in the \a current entry while
-  inside the loop, the behavior will be unpredictable. At a minimum, the following
-  macros will modify the forward-link pointer, and should not be used inside
-  AST_DLLIST_TRAVERSE() against the entry pointed to by the \a current pointer without
-  careful consideration of their consequences:
-  \li AST_DLLIST_NEXT() (when used as an lvalue)
-  \li AST_DLLIST_INSERT_AFTER()
-  \li AST_DLLIST_INSERT_HEAD()
-  \li AST_DLLIST_INSERT_TAIL()
-  \since 1.6.1
-*/
+ * \brief Checks whether the specified list contains the element.
+ * \param head This is a pointer to the list head structure
+ * \param elm This is a pointer to the list element to see if in list.
+ * \param field List node field for the next node information.
+ *
+ * \return elm if the list has elm in it.
+ * \return NULL if not.
+ * \since 11
+ */
+#define AST_DLLIST_IS_MEMBER(head, elm, field)	\
+	({												\
+		typeof((head)->first) __cur;				\
+		typeof((elm)) __elm = (elm);				\
+		if (!__elm) {								\
+			__cur = NULL;							\
+		} else {									\
+			__cur = (head)->first;					\
+			while (__cur && __cur != __elm) {		\
+				__cur = __cur->field.first;			\
+			}										\
+		}											\
+		__cur;										\
+	})
+
+#define AST_RWDLLIST_IS_MEMBER	AST_DLLIST_IS_MEMBER
+
+/*!
+ * \brief Traverse a doublly linked list using the specified direction list.
+ *
+ * \param head List head structure pointer.
+ * \param var This is the name of the variable that will hold a pointer to the
+ * current list node on each iteration. It must be declared before calling
+ * this macro.
+ * \param field List node field for the next node information. (declared using AST_DLLIST_ENTRY())
+ * \param start Specified list node to start traversal: first or last
+ *
+ * This macro is use to loop over (traverse) the nodes in a list. It uses a
+ * \a for loop, and supplies the enclosed code with a pointer to each list
+ * node as it loops. It is typically used as follows:
+ * \code
+ * static AST_DLLIST_HEAD(entry_list, list_entry) entries;
+ * ...
+ * struct list_entry {
+ *     ...
+ *     AST_DLLIST_ENTRY(list_entry) list;
+ * }
+ * ...
+ * struct list_entry *current;
+ * ...
+ * AST_DLLIST_TRAVERSE_DIRECTION(&entries, current, list, first) {
+ *    (do something with current here (travers list in forward direction))
+ * }
+ * ...
+ * AST_DLLIST_TRAVERSE_DIRECTION(&entries, current, list, last) {
+ *    (do something with current here (travers list in reverse direction))
+ * }
+ * \endcode
+ *
+ * \since 11
+ */
+#define AST_DLLIST_TRAVERSE_DIRECTION(head, var, field, start) 				\
+	for ((var) = (head)->start; (var); (var) = AST_DLLIST_NEXT_DIRECTION(var, field, start))
+
+#define AST_RWDLLIST_TRAVERSE_DIRECTION AST_DLLIST_TRAVERSE_DIRECTION
+
+/*!
+ * \brief Loops over (traverses) the entries in a list.
+ * \param head This is a pointer to the list head structure
+ * \param var This is the name of the variable that will hold a pointer to the
+ * current list entry on each iteration. It must be declared before calling
+ * this macro.
+ * \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
+ * used to link entries of this list together.
+ *
+ * This macro is use to loop over (traverse) the entries in a list. It uses a
+ * \a for loop, and supplies the enclosed code with a pointer to each list
+ * entry as it loops. It is typically used as follows:
+ * \code
+ * static AST_DLLIST_HEAD(entry_list, list_entry) entries;
+ * ...
+ * struct list_entry {
+ *     ...
+ *     AST_DLLIST_ENTRY(list_entry) list;
+ * }
+ * ...
+ * struct list_entry *current;
+ * ...
+ * AST_DLLIST_TRAVERSE(&entries, current, list) {
+ *    (do something with current here)
+ * }
+ * \endcode
+ * \warning If you modify the forward-link pointer contained in the \a current entry while
+ * inside the loop, the behavior will be unpredictable. At a minimum, the following
+ * macros will modify the forward-link pointer, and should not be used inside
+ * AST_DLLIST_TRAVERSE() against the entry pointed to by the \a current pointer without
+ * careful consideration of their consequences:
+ * \li AST_DLLIST_NEXT() (when used as an lvalue)
+ * \li AST_DLLIST_INSERT_AFTER()
+ * \li AST_DLLIST_INSERT_HEAD()
+ * \li AST_DLLIST_INSERT_TAIL()
+ * \since 1.6.1
+ */
 #define AST_DLLIST_TRAVERSE(head,var,field) 				\
-	for((var) = (head)->first; (var); (var) = (var)->field.next)
+	AST_DLLIST_TRAVERSE_DIRECTION(head, var, field, first)
 
 #define AST_RWDLLIST_TRAVERSE AST_DLLIST_TRAVERSE
 
 /*!
-  \brief Loops over (traverses) the entries in a list in reverse order, starting at the end.
-  \param head This is a pointer to the list head structure
-  \param var This is the name of the variable that will hold a pointer to the
-  current list entry on each iteration. It must be declared before calling
-  this macro.
-  \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
-  used to link entries of this list together.
-
-  This macro is use to loop over (traverse) the entries in a list in reverse order. It uses a
-  \a for loop, and supplies the enclosed code with a pointer to each list
-  entry as it loops. It is typically used as follows:
-  \code
-  static AST_DLLIST_HEAD(entry_list, list_entry) entries;
-  ...
-  struct list_entry {
-  	...
-  	AST_DLLIST_ENTRY(list_entry) list;
-  }
-  ...
-  struct list_entry *current;
-  ...
-  AST_DLLIST_TRAVERSE_BACKWARDS(&entries, current, list) {
-     (do something with current here)
-  }
-  \endcode
-  \warning If you modify the forward-link pointer contained in the \a current entry while
-  inside the loop, the behavior will be unpredictable. At a minimum, the following
-  macros will modify the forward-link pointer, and should not be used inside
-  AST_DLLIST_TRAVERSE() against the entry pointed to by the \a current pointer without
-  careful consideration of their consequences:
-  \li AST_DLLIST_PREV() (when used as an lvalue)
-  \li AST_DLLIST_INSERT_BEFORE()
-  \li AST_DLLIST_INSERT_HEAD()
-  \li AST_DLLIST_INSERT_TAIL()
-  \since 1.6.1
-*/
+ * \brief Loops over (traverses) the entries in a list in reverse order, starting at the end.
+ * \param head This is a pointer to the list head structure
+ * \param var This is the name of the variable that will hold a pointer to the
+ * current list entry on each iteration. It must be declared before calling
+ * this macro.
+ * \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
+ * used to link entries of this list together.
+ *
+ * This macro is use to loop over (traverse) the entries in a list in reverse order. It uses a
+ * \a for loop, and supplies the enclosed code with a pointer to each list
+ * entry as it loops. It is typically used as follows:
+ * \code
+ * static AST_DLLIST_HEAD(entry_list, list_entry) entries;
+ * ...
+ * struct list_entry {
+ *     ...
+ *     AST_DLLIST_ENTRY(list_entry) list;
+ * }
+ * ...
+ * struct list_entry *current;
+ * ...
+ * AST_DLLIST_TRAVERSE_BACKWARDS(&entries, current, list) {
+ *    (do something with current here)
+ * }
+ * \endcode
+ * \warning If you modify the forward-link pointer contained in the \a current entry while
+ * inside the loop, the behavior will be unpredictable. At a minimum, the following
+ * macros will modify the forward-link pointer, and should not be used inside
+ * AST_DLLIST_TRAVERSE() against the entry pointed to by the \a current pointer without
+ * careful consideration of their consequences:
+ * \li AST_DLLIST_PREV() (when used as an lvalue)
+ * \li AST_DLLIST_INSERT_BEFORE()
+ * \li AST_DLLIST_INSERT_HEAD()
+ * \li AST_DLLIST_INSERT_TAIL()
+ * \since 1.6.1
+ */
 #define AST_DLLIST_TRAVERSE_BACKWARDS(head,var,field) 				\
-	for((var) = (head)->last; (var); (var) = (var)->field.prev)
+	AST_DLLIST_TRAVERSE_DIRECTION(head, var, field, last)
 
 #define AST_RWDLLIST_TRAVERSE_BACKWARDS AST_DLLIST_TRAVERSE_BACKWARDS
 
 /*!
-  \brief Loops safely over (traverses) the entries in a list.
-  \param head This is a pointer to the list head structure
-  \param var This is the name of the variable that will hold a pointer to the
-  current list entry on each iteration. It must be declared before calling
-  this macro.
-  \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
-  used to link entries of this list together.
-
-  This macro is used to safely loop over (traverse) the entries in a list. It
-  uses a \a for loop, and supplies the enclosed code with a pointer to each list
-  entry as it loops. It is typically used as follows:
-
-  \code
-  static AST_DLLIST_HEAD(entry_list, list_entry) entries;
-  ...
-  struct list_entry {
-  	...
-  	AST_DLLIST_ENTRY(list_entry) list;
-  }
-  ...
-  struct list_entry *current;
-  ...
-  AST_DLLIST_TRAVERSE_SAFE_BEGIN(&entries, current, list) {
-     (do something with current here)
-  }
-  AST_DLLIST_TRAVERSE_SAFE_END;
-  \endcode
-
-  It differs from AST_DLLIST_TRAVERSE() in that the code inside the loop can modify
-  (or even free, after calling AST_DLLIST_REMOVE_CURRENT()) the entry pointed to by
-  the \a current pointer without affecting the loop traversal.
-  \since 1.6.1
-*/
-#define AST_DLLIST_TRAVERSE_SAFE_BEGIN(head, var, field) {				\
-	typeof((head)) __list_head = head;						\
-	typeof(__list_head->first) __list_next;						\
-	typeof(__list_head->first) __list_prev = NULL;					\
-	typeof(__list_head->first) __new_prev = NULL;					\
-	for ((var) = __list_head->first, __new_prev = (var),				\

[... 1493 lines stripped ...]



More information about the asterisk-commits mailing list