diff -Nru a/include/asterisk/linkedlists.h b/include/asterisk/linkedlists.h --- a/include/asterisk/linkedlists.h 2005-01-01 09:26:16 -07:00 +++ b/include/asterisk/linkedlists.h 2005-01-01 09:26:16 -07:00 @@ -148,6 +148,51 @@ for((var) = (head)->first; (var); (var) = (var)->field.next) /*! + \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 type This is the type of each list entry. + \param field This is the name of the field (declared using AST_LIST_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_LIST_HEAD(entry_list, list_entry) entries; + ... + struct list_entry { + ... + AST_LIST_ENTRY(list_entry) list; + } + ... + struct list_entry *current; + ... + AST_LIST_TRAVERSE_SAFE_BEGIN(&entries, current, list_entry, list) { + (do something with current here) + } + AST_LIST_TRAVERSE_SAFE_END + \endcode + + It differs from AST_LIST_TRAVERSE in that the code inside the loop can modify + (or even free) the entry pointed to by the \a current pointer without affecting + the loop traversal. +*/ +#define AST_LIST_TRAVERSE_SAFE_BEGIN(head,var,type,field) { \ + struct type *__list_next; \ + for ((var) = (head)->first, __list_next = (var) ? (var)->field.next : NULL; \ + (var); \ + (var) = __list_next, __list_next = (var) ? (var)->field.next : NULL \ + ) + +/*! + \brief Closes a safe loop traversal block. + */ +#define AST_LIST_TRAVERSE_SAFE_END } + +/*! \brief Initializes a list head structure. \param head This is a pointer to the list head structure