2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Steve Murphy <murf@digium.com>
8 * Doubly-Linked List Macros--
9 * Based on linkedlists.h (to the point of plagiarism!), which is by:
11 * Mark Spencer <markster@digium.com>
12 * Kevin P. Fleming <kpfleming@digium.com>
14 * See http://www.asterisk.org for more information about
15 * the Asterisk project. Please do not directly contact
16 * any of the maintainers of this project for assistance;
17 * the project provides a web site, mailing lists and IRC
18 * channels for your use.
20 * This program is free software, distributed under the terms of
21 * the GNU General Public License Version 2. See the LICENSE file
22 * at the top of the source tree.
25 #ifndef ASTERISK_DLINKEDLISTS_H
26 #define ASTERISK_DLINKEDLISTS_H
28 #include "asterisk/lock.h"
32 \brief A set of macros to manage doubly-linked lists.
37 \param head This is a pointer to the list head structure
39 This macro attempts to place an exclusive lock in the
40 list head structure pointed to by head.
42 \retval non-zero on failure
44 #define AST_DLLIST_LOCK(head) \
45 ast_mutex_lock(&(head)->lock)
48 \brief Write locks a list.
49 \param head This is a pointer to the list head structure
51 This macro attempts to place an exclusive write lock in the
52 list head structure pointed to by head.
54 \retval non-zero on failure
56 #define AST_RWDLLIST_WRLOCK(head) \
57 ast_rwlock_wrlock(&(head)->lock)
60 \brief Read locks a list.
61 \param head This is a pointer to the list head structure
63 This macro attempts to place a read lock in the
64 list head structure pointed to by head.
66 \retval non-zero on failure
68 #define AST_RWDLLIST_RDLOCK(head) \
69 ast_rwlock_rdlock(&(head)->lock)
72 \brief Locks a list, without blocking if the list is locked.
73 \param head This is a pointer to the list head structure
75 This macro attempts to place an exclusive lock in the
76 list head structure pointed to by head.
78 \retval non-zero on failure
80 #define AST_DLLIST_TRYLOCK(head) \
81 ast_mutex_trylock(&(head)->lock)
84 \brief Write locks a list, without blocking if the list is locked.
85 \param head This is a pointer to the list head structure
87 This macro attempts to place an exclusive write lock in the
88 list head structure pointed to by head.
90 \retval non-zero on failure
92 #define AST_RWDLLIST_TRYWRLOCK(head) \
93 ast_rwlock_trywrlock(&(head)->lock)
96 \brief Read locks a list, without blocking if the list is locked.
97 \param head This is a pointer to the list head structure
99 This macro attempts to place a read lock in the
100 list head structure pointed to by head.
102 \retval non-zero on failure
104 #define AST_RWDLLIST_TRYRDLOCK(head) \
105 ast_rwlock_tryrdlock(&(head)->lock)
108 \brief Attempts to unlock a list.
109 \param head This is a pointer to the list head structure
111 This macro attempts to remove an exclusive lock from the
112 list head structure pointed to by head. If the list
113 was not locked by this thread, this macro has no effect.
115 #define AST_DLLIST_UNLOCK(head) \
116 ast_mutex_unlock(&(head)->lock)
119 \brief Attempts to unlock a read/write based list.
120 \param head This is a pointer to the list head structure
122 This macro attempts to remove a read or write lock from the
123 list head structure pointed to by head. If the list
124 was not locked by this thread, this macro has no effect.
126 #define AST_RWDLLIST_UNLOCK(head) \
127 ast_rwlock_unlock(&(head)->lock)
130 \brief Defines a structure to be used to hold a list of specified type.
131 \param name This will be the name of the defined structure.
132 \param type This is the type of each list entry.
134 This macro creates a structure definition that can be used
135 to hold a list of the entries of type \a type. It does not actually
136 declare (allocate) a structure; to do that, either follow this
137 macro with the desired name of the instance you wish to declare,
138 or use the specified \a name to declare instances elsewhere.
142 static AST_DLLIST_HEAD(entry_list, entry) entries;
145 This would define \c struct \c entry_list, and declare an instance of it named
146 \a entries, all intended to hold a list of type \c struct \c entry.
148 #define AST_DLLIST_HEAD(name, type) \
150 struct type *first; \
156 \brief Defines a structure to be used to hold a read/write list of specified type.
157 \param name This will be the name of the defined structure.
158 \param type This is the type of each list entry.
160 This macro creates a structure definition that can be used
161 to hold a list of the entries of type \a type. It does not actually
162 declare (allocate) a structure; to do that, either follow this
163 macro with the desired name of the instance you wish to declare,
164 or use the specified \a name to declare instances elsewhere.
168 static AST_RWDLLIST_HEAD(entry_list, entry) entries;
171 This would define \c struct \c entry_list, and declare an instance of it named
172 \a entries, all intended to hold a list of type \c struct \c entry.
174 #define AST_RWDLLIST_HEAD(name, type) \
176 struct type *first; \
182 \brief Defines a structure to be used to hold a list of specified type (with no lock).
183 \param name This will be the name of the defined structure.
184 \param type This is the type of each list entry.
186 This macro creates a structure definition that can be used
187 to hold a list of the entries of type \a type. It does not actually
188 declare (allocate) a structure; to do that, either follow this
189 macro with the desired name of the instance you wish to declare,
190 or use the specified \a name to declare instances elsewhere.
194 static AST_DLLIST_HEAD_NOLOCK(entry_list, entry) entries;
197 This would define \c struct \c entry_list, and declare an instance of it named
198 \a entries, all intended to hold a list of type \c struct \c entry.
200 #define AST_DLLIST_HEAD_NOLOCK(name, type) \
202 struct type *first; \
207 \brief Defines initial values for a declaration of AST_DLLIST_HEAD
209 #define AST_DLLIST_HEAD_INIT_VALUE { \
212 .lock = AST_MUTEX_INIT_VALUE, \
216 \brief Defines initial values for a declaration of AST_RWDLLIST_HEAD
218 #define AST_RWDLLIST_HEAD_INIT_VALUE { \
221 .lock = AST_RWLOCK_INIT_VALUE, \
225 \brief Defines initial values for a declaration of AST_DLLIST_HEAD_NOLOCK
227 #define AST_DLLIST_HEAD_NOLOCK_INIT_VALUE { \
233 \brief Defines a structure to be used to hold a list of specified type, statically initialized.
234 \param name This will be the name of the defined structure.
235 \param type This is the type of each list entry.
237 This macro creates a structure definition that can be used
238 to hold a list of the entries of type \a type, and allocates an instance
239 of it, initialized to be empty.
243 static AST_DLLIST_HEAD_STATIC(entry_list, entry);
246 This would define \c struct \c entry_list, intended to hold a list of
247 type \c struct \c entry.
249 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
250 #define AST_DLLIST_HEAD_STATIC(name, type) \
252 struct type *first; \
256 static void __attribute__ ((constructor)) __init_##name(void) \
258 AST_DLLIST_HEAD_INIT(&name); \
260 static void __attribute__ ((destructor)) __fini_##name(void) \
262 AST_DLLIST_HEAD_DESTROY(&name); \
264 struct __dummy_##name
266 #define AST_DLLIST_HEAD_STATIC(name, type) \
268 struct type *first; \
271 } name = AST_DLLIST_HEAD_INIT_VALUE
275 \brief Defines a structure to be used to hold a read/write list of specified type, statically initialized.
276 \param name This will be the name of the defined structure.
277 \param type This is the type of each list entry.
279 This macro creates a structure definition that can be used
280 to hold a list of the entries of type \a type, and allocates an instance
281 of it, initialized to be empty.
285 static AST_RWDLLIST_HEAD_STATIC(entry_list, entry);
288 This would define \c struct \c entry_list, intended to hold a list of
289 type \c struct \c entry.
291 #ifndef AST_RWLOCK_INIT_VALUE
292 #define AST_RWDLLIST_HEAD_STATIC(name, type) \
294 struct type *first; \
298 static void __attribute__ ((constructor)) __init_##name(void) \
300 AST_RWDLLIST_HEAD_INIT(&name); \
302 static void __attribute__ ((destructor)) __fini_##name(void) \
304 AST_RWDLLIST_HEAD_DESTROY(&name); \
306 struct __dummy_##name
308 #define AST_RWDLLIST_HEAD_STATIC(name, type) \
310 struct type *first; \
313 } name = AST_RWDLLIST_HEAD_INIT_VALUE
317 \brief Defines a structure to be used to hold a list of specified type, statically initialized.
319 This is the same as AST_DLLIST_HEAD_STATIC, except without the lock included.
321 #define AST_DLLIST_HEAD_NOLOCK_STATIC(name, type) \
323 struct type *first; \
325 } name = AST_DLLIST_HEAD_NOLOCK_INIT_VALUE
328 \brief Initializes a list head structure with a specified first entry.
329 \param head This is a pointer to the list head structure
330 \param entry pointer to the list entry that will become the head of the list
332 This macro initializes a list head structure by setting the head
333 entry to the supplied value and recreating the embedded lock.
335 #define AST_DLLIST_HEAD_SET(head, entry) do { \
336 (head)->first = (entry); \
337 (head)->last = (entry); \
338 ast_mutex_init(&(head)->lock); \
342 \brief Initializes an rwlist head structure with a specified first entry.
343 \param head This is a pointer to the list head structure
344 \param entry pointer to the list entry that will become the head of the list
346 This macro initializes a list head structure by setting the head
347 entry to the supplied value and recreating the embedded lock.
349 #define AST_RWDLLIST_HEAD_SET(head, entry) do { \
350 (head)->first = (entry); \
351 (head)->last = (entry); \
352 ast_rwlock_init(&(head)->lock); \
356 \brief Initializes a list head structure with a specified first entry.
357 \param head This is a pointer to the list head structure
358 \param entry pointer to the list entry that will become the head of the list
360 This macro initializes a list head structure by setting the head
361 entry to the supplied value.
363 #define AST_DLLIST_HEAD_SET_NOLOCK(head, entry) do { \
364 (head)->first = (entry); \
365 (head)->last = (entry); \
369 \brief Declare previous/forward links inside a list entry.
370 \param type This is the type of each list entry.
372 This macro declares a structure to be used to doubly link list entries together.
373 It must be used inside the definition of the structure named in
379 AST_DLLIST_ENTRY(list_entry) list;
383 The field name \a list here is arbitrary, and can be anything you wish.
385 #define AST_DLLIST_ENTRY(type) \
391 #define AST_RWDLLIST_ENTRY AST_DLLIST_ENTRY
394 \brief Returns the first entry contained in a list.
395 \param head This is a pointer to the list head structure
397 #define AST_DLLIST_FIRST(head) ((head)->first)
399 #define AST_RWDLLIST_FIRST AST_DLLIST_FIRST
402 \brief Returns the last entry contained in a list.
403 \param head This is a pointer to the list head structure
405 #define AST_DLLIST_LAST(head) ((head)->last)
407 #define AST_RWDLLIST_LAST AST_DLLIST_LAST
410 \brief Returns the next entry in the list after the given entry.
411 \param elm This is a pointer to the current entry.
412 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
413 used to link entries of this list together.
415 #define AST_DLLIST_NEXT(elm, field) ((elm)->field.next)
417 #define AST_RWDLLIST_NEXT AST_DLLIST_NEXT
420 \brief Returns the previous entry in the list before the given entry.
421 \param elm This is a pointer to the current entry.
422 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
423 used to link entries of this list together.
425 #define AST_DLLIST_PREV(elm, field) ((elm)->field.prev)
427 #define AST_RWDLLIST_PREV AST_DLLIST_PREV
430 \brief Checks whether the specified list contains any entries.
431 \param head This is a pointer to the list head structure
433 \return non-zero if the list has entries
436 #define AST_DLLIST_EMPTY(head) (AST_DLLIST_FIRST(head) == NULL)
438 #define AST_RWDLLIST_EMPTY AST_DLLIST_EMPTY
441 \brief Loops over (traverses) the entries in a list.
442 \param head This is a pointer to the list head structure
443 \param var This is the name of the variable that will hold a pointer to the
444 current list entry on each iteration. It must be declared before calling
446 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
447 used to link entries of this list together.
449 This macro is use to loop over (traverse) the entries in a list. It uses a
450 \a for loop, and supplies the enclosed code with a pointer to each list
451 entry as it loops. It is typically used as follows:
453 static AST_DLLIST_HEAD(entry_list, list_entry) entries;
457 AST_DLLIST_ENTRY(list_entry) list;
460 struct list_entry *current;
462 AST_DLLIST_TRAVERSE(&entries, current, list) {
463 (do something with current here)
466 \warning If you modify the forward-link pointer contained in the \a current entry while
467 inside the loop, the behavior will be unpredictable. At a minimum, the following
468 macros will modify the forward-link pointer, and should not be used inside
469 AST_DLLIST_TRAVERSE() against the entry pointed to by the \a current pointer without
470 careful consideration of their consequences:
471 \li AST_DLLIST_NEXT() (when used as an lvalue)
472 \li AST_DLLIST_INSERT_AFTER()
473 \li AST_DLLIST_INSERT_HEAD()
474 \li AST_DLLIST_INSERT_TAIL()
476 #define AST_DLLIST_TRAVERSE(head,var,field) \
477 for((var) = (head)->first; (var); (var) = (var)->field.next)
479 #define AST_RWDLLIST_TRAVERSE AST_DLLIST_TRAVERSE
482 \brief Loops over (traverses) the entries in a list in reverse order, starting at the end.
483 \param head This is a pointer to the list head structure
484 \param var This is the name of the variable that will hold a pointer to the
485 current list entry on each iteration. It must be declared before calling
487 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
488 used to link entries of this list together.
490 This macro is use to loop over (traverse) the entries in a list in reverse order. It uses a
491 \a for loop, and supplies the enclosed code with a pointer to each list
492 entry as it loops. It is typically used as follows:
494 static AST_DLLIST_HEAD(entry_list, list_entry) entries;
498 AST_DLLIST_ENTRY(list_entry) list;
501 struct list_entry *current;
503 AST_DLLIST_TRAVERSE_BACKWARDS(&entries, current, list) {
504 (do something with current here)
507 \warning If you modify the forward-link pointer contained in the \a current entry while
508 inside the loop, the behavior will be unpredictable. At a minimum, the following
509 macros will modify the forward-link pointer, and should not be used inside
510 AST_DLLIST_TRAVERSE() against the entry pointed to by the \a current pointer without
511 careful consideration of their consequences:
512 \li AST_DLLIST_PREV() (when used as an lvalue)
513 \li AST_DLLIST_INSERT_BEFORE()
514 \li AST_DLLIST_INSERT_HEAD()
515 \li AST_DLLIST_INSERT_TAIL()
517 #define AST_DLLIST_TRAVERSE_BACKWARDS(head,var,field) \
518 for((var) = (head)->last; (var); (var) = (var)->field.prev)
520 #define AST_RWDLLIST_TRAVERSE_BACKWARDS AST_DLLIST_TRAVERSE_BACKWARDS
523 \brief Loops safely over (traverses) the entries in a list.
524 \param head This is a pointer to the list head structure
525 \param var This is the name of the variable that will hold a pointer to the
526 current list entry on each iteration. It must be declared before calling
528 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
529 used to link entries of this list together.
531 This macro is used to safely loop over (traverse) the entries in a list. It
532 uses a \a for loop, and supplies the enclosed code with a pointer to each list
533 entry as it loops. It is typically used as follows:
536 static AST_DLLIST_HEAD(entry_list, list_entry) entries;
540 AST_DLLIST_ENTRY(list_entry) list;
543 struct list_entry *current;
545 AST_DLLIST_TRAVERSE_SAFE_BEGIN(&entries, current, list) {
546 (do something with current here)
548 AST_DLLIST_TRAVERSE_SAFE_END;
551 It differs from AST_DLLIST_TRAVERSE() in that the code inside the loop can modify
552 (or even free, after calling AST_DLLIST_REMOVE_CURRENT()) the entry pointed to by
553 the \a current pointer without affecting the loop traversal.
555 #define AST_DLLIST_TRAVERSE_SAFE_BEGIN(head, var, field) { \
556 typeof((head)) __list_head = head; \
557 typeof(__list_head->first) __list_next; \
558 typeof(__list_head->first) __list_prev = NULL; \
559 typeof(__list_head->first) __new_prev = NULL; \
560 for ((var) = __list_head->first, __new_prev = (var), \
561 __list_next = (var) ? (var)->field.next : NULL; \
563 __list_prev = __new_prev, (var) = __list_next, \
564 __new_prev = (var), \
565 __list_next = (var) ? (var)->field.next : NULL \
568 #define AST_RWDLLIST_TRAVERSE_SAFE_BEGIN AST_DLLIST_TRAVERSE_SAFE_BEGIN
571 \brief Loops safely over (traverses) the entries in a list.
572 \param head This is a pointer to the list head structure
573 \param var This is the name of the variable that will hold a pointer to the
574 current list entry on each iteration. It must be declared before calling
576 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
577 used to link entries of this list together.
579 This macro is used to safely loop over (traverse) the entries in a list. It
580 uses a \a for loop, and supplies the enclosed code with a pointer to each list
581 entry as it loops. It is typically used as follows:
584 static AST_DLLIST_HEAD(entry_list, list_entry) entries;
588 AST_DLLIST_ENTRY(list_entry) list;
591 struct list_entry *current;
593 AST_DLLIST_TRAVERSE_SAFE_BEGIN(&entries, current, list) {
594 (do something with current here)
596 AST_DLLIST_TRAVERSE_SAFE_END;
599 It differs from AST_DLLIST_TRAVERSE() in that the code inside the loop can modify
600 (or even free, after calling AST_DLLIST_REMOVE_CURRENT()) the entry pointed to by
601 the \a current pointer without affecting the loop traversal.
603 #define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(head, var, field) { \
604 typeof((head)) __list_head = head; \
605 typeof(__list_head->first) __list_next; \
606 typeof(__list_head->first) __list_prev = NULL; \
607 typeof(__list_head->first) __new_prev = NULL; \
608 for ((var) = __list_head->last, __new_prev = (var), \
609 __list_next = NULL, __list_prev = (var) ? (var)->field.prev : NULL; \
611 __list_next = __new_prev, (var) = __list_prev, \
612 __new_prev = (var), \
613 __list_prev = (var) ? (var)->field.prev : NULL \
616 #define AST_RWDLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN
619 \brief Removes the \a current entry from a list during a traversal.
620 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
621 used to link entries of this list together.
623 \note This macro can \b only be used inside an AST_DLLIST_TRAVERSE_SAFE_BEGIN()
624 block; it is used to unlink the current entry from the list without affecting
625 the list traversal (and without having to re-traverse the list to modify the
626 previous entry, if any).
628 #define AST_DLLIST_REMOVE_CURRENT(field) do { \
629 __new_prev->field.next = NULL; \
630 __new_prev->field.prev = NULL; \
632 __new_prev = __list_prev; \
637 __list_next->field.prev = __list_prev; \
638 __list_prev->field.next = __list_next; \
640 __list_head->first = __list_next; \
642 __list_next->field.prev = NULL; \
645 __list_head->last = __list_prev; \
648 #define AST_RWDLLIST_REMOVE_CURRENT AST_DLLIST_REMOVE_CURRENT
650 #define AST_DLLIST_MOVE_CURRENT(newhead, field) do { \
651 typeof ((newhead)->first) __list_cur = __new_prev; \
652 AST_DLLIST_REMOVE_CURRENT(field); \
653 AST_DLLIST_INSERT_TAIL((newhead), __list_cur, field); \
656 #define AST_RWDLLIST_MOVE_CURRENT AST_DLLIST_MOVE_CURRENT
658 #define AST_DLLIST_MOVE_CURRENT_BACKWARDS(newhead, field) do { \
659 typeof ((newhead)->first) __list_cur = __new_prev; \
660 if (!__list_next) { \
661 AST_DLLIST_REMOVE_CURRENT(field); \
663 AST_DLLIST_INSERT_HEAD((newhead), __list_cur, field); \
665 AST_DLLIST_REMOVE_CURRENT(field); \
666 AST_DLLIST_INSERT_HEAD((newhead), __list_cur, field); \
669 #define AST_RWDLLIST_MOVE_CURRENT_BACKWARDS AST_DLLIST_MOVE_CURRENT
672 \brief Inserts a list entry before the current entry during a traversal.
673 \param elm This is a pointer to the entry to be inserted.
674 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
675 used to link entries of this list together.
677 \note This macro can \b only be used inside an AST_DLLIST_TRAVERSE_SAFE_BEGIN()
680 #define AST_DLLIST_INSERT_BEFORE_CURRENT(elm, field) do { \
682 (elm)->field.next = __list_prev->field.next; \
683 (elm)->field.prev = __list_prev; \
684 if (__list_prev->field.next) \
685 __list_prev->field.next->field.prev = (elm); \
686 __list_prev->field.next = (elm); \
688 (elm)->field.next = __list_head->first; \
689 __list_head->first->field.prev = (elm); \
690 (elm)->field.prev = NULL; \
691 __list_head->first = (elm); \
695 #define AST_RWDLLIST_INSERT_BEFORE_CURRENT AST_DLLIST_INSERT_BEFORE_CURRENT
698 \brief Inserts a list entry after the current entry during a backwards traversal. Since
699 this is a backwards traversal, this will insert the entry AFTER the current
700 element. Since this is a backwards traveral, though, this would be BEFORE
701 the current entry in traversal order. Confusing?
702 \param elm This is a pointer to the entry to be inserted.
703 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
704 used to link entries of this list together.
706 \note This macro can \b only be used inside an AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN()
707 block. If you use this with the AST_DLLIST_TRAVERSE_SAFE_BEGIN(), be prepared for
710 #define AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS(elm, field) do { \
712 (elm)->field.next = __list_next; \
713 (elm)->field.prev = __new_prev; \
714 __new_prev->field.next = (elm); \
715 __list_next->field.prev = (elm); \
717 (elm)->field.prev = __list_head->last; \
718 (elm)->field.next = NULL; \
719 __list_head->last->field.next = (elm); \
720 __list_head->last = (elm); \
724 #define AST_RWDLLIST_INSERT_BEFORE_CURRENT_BACKWARDS AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS
727 \brief Closes a safe loop traversal block.
729 #define AST_DLLIST_TRAVERSE_SAFE_END }
731 #define AST_RWDLLIST_TRAVERSE_SAFE_END AST_DLLIST_TRAVERSE_SAFE_END
734 \brief Closes a safe loop traversal block.
736 #define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END }
738 #define AST_RWDLLIST_TRAVERSE_BACKWARDS_SAFE_END AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END
741 \brief Initializes a list head structure.
742 \param head This is a pointer to the list head structure
744 This macro initializes a list head structure by setting the head
745 entry to \a NULL (empty list) and recreating the embedded lock.
747 #define AST_DLLIST_HEAD_INIT(head) { \
748 (head)->first = NULL; \
749 (head)->last = NULL; \
750 ast_mutex_init(&(head)->lock); \
754 \brief Initializes an rwlist head structure.
755 \param head This is a pointer to the list head structure
757 This macro initializes a list head structure by setting the head
758 entry to \a NULL (empty list) and recreating the embedded lock.
760 #define AST_RWDLLIST_HEAD_INIT(head) { \
761 (head)->first = NULL; \
762 (head)->last = NULL; \
763 ast_rwlock_init(&(head)->lock); \
767 \brief Destroys a list head structure.
768 \param head This is a pointer to the list head structure
770 This macro destroys a list head structure by setting the head
771 entry to \a NULL (empty list) and destroying the embedded lock.
772 It does not free the structure from memory.
774 #define AST_DLLIST_HEAD_DESTROY(head) { \
775 (head)->first = NULL; \
776 (head)->last = NULL; \
777 ast_mutex_destroy(&(head)->lock); \
781 \brief Destroys an rwlist head structure.
782 \param head This is a pointer to the list head structure
784 This macro destroys a list head structure by setting the head
785 entry to \a NULL (empty list) and destroying the embedded lock.
786 It does not free the structure from memory.
788 #define AST_RWDLLIST_HEAD_DESTROY(head) { \
789 (head)->first = NULL; \
790 (head)->last = NULL; \
791 ast_rwlock_destroy(&(head)->lock); \
795 \brief Initializes a list head structure.
796 \param head This is a pointer to the list head structure
798 This macro initializes a list head structure by setting the head
799 entry to \a NULL (empty list). There is no embedded lock handling
802 #define AST_DLLIST_HEAD_INIT_NOLOCK(head) { \
803 (head)->first = NULL; \
804 (head)->last = NULL; \
808 \brief Inserts a list entry after a given entry.
809 \param head This is a pointer to the list head structure
810 \param listelm This is a pointer to the entry after which the new entry should
812 \param elm This is a pointer to the entry to be inserted.
813 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
814 used to link entries of this list together.
816 #define AST_DLLIST_INSERT_AFTER(head, listelm, elm, field) do { \
817 (elm)->field.next = (listelm)->field.next; \
818 (elm)->field.prev = (listelm); \
819 if ((listelm)->field.next) \
820 (listelm)->field.next->field.prev = (elm); \
821 (listelm)->field.next = (elm); \
822 if ((head)->last == (listelm)) \
823 (head)->last = (elm); \
826 #define AST_RWDLLIST_INSERT_AFTER AST_DLLIST_INSERT_AFTER
829 \brief Inserts a list entry before a given entry.
830 \param head This is a pointer to the list head structure
831 \param listelm This is a pointer to the entry before which the new entry should
833 \param elm This is a pointer to the entry to be inserted.
834 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
835 used to link entries of this list together.
837 #define AST_DLLIST_INSERT_BEFORE(head, listelm, elm, field) do { \
838 (elm)->field.next = (listelm); \
839 (elm)->field.prev = (listelm)->field.prev; \
840 if ((listelm)->field.prev) \
841 (listelm)->field.prev->field.next = (elm); \
842 (listelm)->field.prev = (elm); \
843 if ((head)->first == (listelm)) \
844 (head)->first = (elm); \
847 #define AST_RWDLLIST_INSERT_BEFORE AST_DLLIST_INSERT_BEFORE
850 \brief Inserts a list entry at the head of a list.
851 \param head This is a pointer to the list head structure
852 \param elm This is a pointer to the entry to be inserted.
853 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
854 used to link entries of this list together.
856 #define AST_DLLIST_INSERT_HEAD(head, elm, field) do { \
857 (elm)->field.next = (head)->first; \
859 (head)->first->field.prev = (elm); \
860 (elm)->field.prev = NULL; \
861 (head)->first = (elm); \
863 (head)->last = (elm); \
866 #define AST_RWDLLIST_INSERT_HEAD AST_DLLIST_INSERT_HEAD
869 \brief Appends a list entry to the tail of a list.
870 \param head This is a pointer to the list head structure
871 \param elm This is a pointer to the entry to be appended.
872 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
873 used to link entries of this list together.
875 Note: The link field in the appended entry is \b not modified, so if it is
876 actually the head of a list itself, the entire list will be appended
877 temporarily (until the next AST_DLLIST_INSERT_TAIL is performed).
879 #define AST_DLLIST_INSERT_TAIL(head, elm, field) do { \
880 if (!(head)->first) { \
881 (head)->first = (elm); \
882 (head)->last = (elm); \
883 (elm)->field.next = NULL; \
884 (elm)->field.prev = NULL; \
886 (head)->last->field.next = (elm); \
887 (elm)->field.prev = (head)->last; \
888 (elm)->field.next = NULL; \
889 (head)->last = (elm); \
893 #define AST_RWDLLIST_INSERT_TAIL AST_DLLIST_INSERT_TAIL
896 \brief Appends a whole list to the tail of a list.
897 \param head This is a pointer to the list head structure
898 \param list This is a pointer to the list to be appended.
899 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
900 used to link entries of this list together.
902 Note: The source list (the \a list parameter) will be empty after
903 calling this macro (the list entries are \b moved to the target list).
905 #define AST_DLLIST_APPEND_DLLIST(head, list, field) do { \
906 if (!(head)->first) { \
907 (head)->first = (list)->first; \
908 (head)->last = (list)->last; \
910 (head)->last->field.next = (list)->first; \
911 (list)->first->field.prev = (head)->last; \
912 (head)->last = (list)->last; \
914 (list)->first = NULL; \
915 (list)->last = NULL; \
918 #define AST_RWDLLIST_APPEND_DLLIST AST_DLLIST_APPEND_DLLIST
921 \brief Removes and returns the head entry from a list.
922 \param head This is a pointer to the list head structure
923 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
924 used to link entries of this list together.
926 Removes the head entry from the list, and returns a pointer to it.
927 This macro is safe to call on an empty list.
929 #define AST_DLLIST_REMOVE_HEAD(head, field) ({ \
930 typeof((head)->first) cur = (head)->first; \
932 (head)->first = cur->field.next; \
933 if (cur->field.next) \
934 cur->field.next->field.prev = NULL; \
935 cur->field.next = NULL; \
936 if ((head)->last == cur) \
937 (head)->last = NULL; \
942 #define AST_RWDLLIST_REMOVE_HEAD AST_DLLIST_REMOVE_HEAD
945 \brief Removes a specific entry from a list.
946 \param head This is a pointer to the list head structure
947 \param elm This is a pointer to the entry to be removed.
948 \param field This is the name of the field (declared using AST_DLLIST_ENTRY())
949 used to link entries of this list together.
950 \warning The removed entry is \b not freed nor modified in any way.
952 #define AST_DLLIST_REMOVE(head, elm, field) ({ \
953 __typeof(elm) __res = (elm); \
954 if ((head)->first == (elm)) { \
955 (head)->first = (elm)->field.next; \
956 if ((elm)->field.next) \
957 (elm)->field.next->field.prev = NULL; \
958 if ((head)->last == (elm)) \
959 (head)->last = NULL; \
961 (elm)->field.prev->field.next = (elm)->field.next; \
962 if ((elm)->field.next) \
963 (elm)->field.next->field.prev = (elm)->field.prev; \
964 if ((head)->last == (elm)) \
965 (head)->last = (elm)->field.prev; \
967 (elm)->field.next = NULL; \
968 (elm)->field.prev = NULL; \
972 #define AST_RWDLLIST_REMOVE AST_DLLIST_REMOVE
974 #endif /* _ASTERISK_DLINKEDLISTS_H */