2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
29 .\" $FreeBSD: src/share/man/man3/queue.3,v 1.46 2011/01/11 13:33:42 gavin Exp $
39 .Nm SLIST_FOREACH_MUTABLE ,
40 .Nm SLIST_FOREACH_PREVPTR ,
42 .Nm SLIST_HEAD_INITIALIZER ,
44 .Nm SLIST_INSERT_AFTER ,
45 .Nm SLIST_INSERT_HEAD ,
47 .Nm SLIST_REMOVE_AFTER ,
48 .Nm SLIST_REMOVE_HEAD ,
55 .Nm STAILQ_FOREACH_MUTABLE ,
57 .Nm STAILQ_HEAD_INITIALIZER ,
59 .Nm STAILQ_INSERT_AFTER ,
60 .Nm STAILQ_INSERT_HEAD ,
61 .Nm STAILQ_INSERT_TAIL ,
64 .Nm STAILQ_REMOVE_AFTER ,
65 .Nm STAILQ_REMOVE_HEAD ,
71 .Nm LIST_FOREACH_MUTABLE ,
73 .Nm LIST_HEAD_INITIALIZER ,
75 .Nm LIST_INSERT_AFTER ,
76 .Nm LIST_INSERT_BEFORE ,
77 .Nm LIST_INSERT_HEAD ,
85 .Nm TAILQ_FOREACH_MUTABLE ,
86 .Nm TAILQ_FOREACH_REVERSE ,
87 .Nm TAILQ_FOREACH_REVERSE_MUTABLE ,
89 .Nm TAILQ_HEAD_INITIALIZER ,
91 .Nm TAILQ_INSERT_AFTER ,
92 .Nm TAILQ_INSERT_BEFORE ,
93 .Nm TAILQ_INSERT_HEAD ,
94 .Nm TAILQ_INSERT_TAIL ,
99 .Nd implementations of singly-linked lists, singly-linked tail queues,
100 lists and tail queues
104 .Fn SLIST_EMPTY "SLIST_HEAD *head"
105 .Fn SLIST_ENTRY "TYPE"
106 .Fn SLIST_FIRST "SLIST_HEAD *head"
107 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
108 .Fn SLIST_FOREACH_MUTABLE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
109 .Fn SLIST_FOREACH_PREVPTR "TYPE *var" "TYPE *varp" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
110 .Fn SLIST_HEAD "HEADNAME" "TYPE"
111 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
112 .Fn SLIST_INIT "SLIST_HEAD *head"
113 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
114 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
115 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
116 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
117 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
118 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
120 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
121 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
122 .Fn STAILQ_ENTRY "TYPE"
123 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
124 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
125 .Fn STAILQ_FOREACH_MUTABLE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
126 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
127 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
128 .Fn STAILQ_INIT "STAILQ_HEAD *head"
129 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
130 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
131 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
132 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
133 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
134 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
135 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
136 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
138 .Fn LIST_EMPTY "LIST_HEAD *head"
139 .Fn LIST_ENTRY "TYPE"
140 .Fn LIST_FIRST "LIST_HEAD *head"
141 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
142 .Fn LIST_FOREACH_MUTABLE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
143 .Fn LIST_HEAD "HEADNAME" "TYPE"
144 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
145 .Fn LIST_INIT "LIST_HEAD *head"
146 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
147 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
148 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
149 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
150 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
152 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
153 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
154 .Fn TAILQ_ENTRY "TYPE"
155 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
156 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
157 .Fn TAILQ_FOREACH_MUTABLE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
158 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
159 .Fn TAILQ_FOREACH_REVERSE_MUTABLE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
160 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
161 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
162 .Fn TAILQ_INIT "TAILQ_HEAD *head"
163 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
164 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
165 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
166 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
167 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
168 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
169 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
170 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
173 These macros define and operate on four types of data structures:
174 singly-linked lists, singly-linked tail queues, lists, and tail queues.
175 All four structures support the following functionality:
176 .Bl -enum -compact -offset indent
178 Insertion of a new entry at the head of the list.
180 Insertion of a new entry after any element in the list.
182 O(1) removal of an entry from the head of the list.
184 Forward traversal through the list.
187 Singly-linked lists are the simplest of the four data structures
188 and support only the above functionality.
189 Singly-linked lists are ideal for applications with large datasets
190 and few or no removals,
191 or for implementing a LIFO queue.
192 Singly-linked lists add the following functionality:
193 .Bl -enum -compact -offset indent
195 O(n) removal of any entry in the list.
198 Singly-linked tail queues add the following functionality:
199 .Bl -enum -compact -offset indent
201 Entries can be added at the end of a list.
203 O(n) removal of any entry in the list.
205 They may be concatenated.
208 .Bl -enum -compact -offset indent
210 All list insertions must specify the head of the list.
212 Each head entry requires two pointers rather than one.
214 Code size is about 15% greater and operations run about 20% slower
215 than singly-linked lists.
218 Singly-linked tailqs are ideal for applications with large datasets and
220 or for implementing a FIFO queue.
222 All doubly linked types of data structures (lists and tail queues)
224 .Bl -enum -compact -offset indent
226 Insertion of a new entry before any element in the list.
228 O(1) removal of any entry in the list.
231 .Bl -enum -compact -offset indent
233 Each element requires two pointers rather than one.
235 Code size and execution time of operations (except for removal) is about
236 twice that of the singly-linked data-structures.
239 Linked lists are the simplest of the doubly linked data structures and support
240 only the above functionality over singly-linked lists.
242 Tail queues add the following functionality:
243 .Bl -enum -compact -offset indent
245 Entries can be added at the end of a list.
247 They may be traversed backwards, from tail to head.
249 They may be concatenated.
252 .Bl -enum -compact -offset indent
254 All list insertions and removals must specify the head of the list.
256 Each head entry requires two pointers rather than one.
258 Code size is about 15% greater and operations run about 20% slower
259 than singly-linked lists.
262 In the macro definitions,
264 is the name of a user defined structure,
265 that must contain a field of type
275 is the name of a user defined structure that must be declared
282 See the examples below for further explanation of how these
284 .Sh SINGLY-LINKED LISTS
285 A singly-linked list is headed by a structure defined by the
288 This structure contains a single pointer to the first element
290 The elements are singly linked for minimum space and pointer manipulation
291 overhead at the expense of O(n) removal for arbitrary elements.
292 New elements can be added to the list after an existing element or
293 at the head of the list.
296 structure is declared as follows:
297 .Bd -literal -offset indent
298 SLIST_HEAD(HEADNAME, TYPE) head;
303 is the name of the structure to be defined, and
305 is the type of the elements to be linked into the list.
306 A pointer to the head of the list can later be declared as:
307 .Bd -literal -offset indent
308 struct HEADNAME *headp;
315 are user selectable.)
318 .Nm SLIST_HEAD_INITIALIZER
319 evaluates to an initializer for the list
324 evaluates to true if there are no elements in the list.
328 declares a structure that connects the elements in
333 returns the first element in the list or NULL if the list is empty.
337 traverses the list referenced by
339 in the forward direction, assigning each element in
344 .Nm SLIST_FOREACH_MUTABLE
345 traverses the list referenced by
347 in the forward direction, assigning each element in
352 here it is permitted to both remove
354 as well as free it from within the loop safely without interfering with the
358 .Nm SLIST_FOREACH_PREVPTR
361 except that it stores a pointer to the previous element in
363 This provides access to the previous element while traversing the list,
364 as one would have with a doubly-linked list.
368 initializes the list referenced by
372 .Nm SLIST_INSERT_HEAD
373 inserts the new element
375 at the head of the list.
378 .Nm SLIST_INSERT_AFTER
379 inserts the new element
386 returns the next element in the list.
389 .Nm SLIST_REMOVE_AFTER
390 removes the element after
392 from the list. Unlike
394 this macro does not traverse the entire list.
397 .Nm SLIST_REMOVE_HEAD
400 from the head of the list.
401 For optimum efficiency,
402 elements being removed from the head of the list should explicitly use
403 this macro instead of the generic
412 .Sh SINGLY-LINKED LIST EXAMPLE
414 SLIST_HEAD(slisthead, entry) head =
415 SLIST_HEAD_INITIALIZER(head);
416 struct slisthead *headp; /* Singly-linked List head. */
419 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
421 } *n1, *n2, *n3, *np;
423 SLIST_INIT(&head); /* Initialize the list. */
425 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
426 SLIST_INSERT_HEAD(&head, n1, entries);
428 n2 = malloc(sizeof(struct entry)); /* Insert after. */
429 SLIST_INSERT_AFTER(n1, n2, entries);
431 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
434 n3 = SLIST_FIRST(&head);
435 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
437 /* Forward traversal. */
438 SLIST_FOREACH(np, &head, entries)
440 /* Safe forward traversal. */
441 SLIST_FOREACH_MUTABLE(np, &head, entries, np_temp) {
444 SLIST_REMOVE(&head, np, entry, entries);
448 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
449 n1 = SLIST_FIRST(&head);
450 SLIST_REMOVE_HEAD(&head, entries);
454 .Sh SINGLY-LINKED TAIL QUEUES
455 A singly-linked tail queue is headed by a structure defined by the
458 This structure contains a pair of pointers,
459 one to the first element in the tail queue and the other to
460 the last element in the tail queue.
461 The elements are singly linked for minimum space and pointer
462 manipulation overhead at the expense of O(n) removal for arbitrary
464 New elements can be added to the tail queue after an existing element,
465 at the head of the tail queue, or at the end of the tail queue.
468 structure is declared as follows:
469 .Bd -literal -offset indent
470 STAILQ_HEAD(HEADNAME, TYPE) head;
475 is the name of the structure to be defined, and
477 is the type of the elements to be linked into the tail queue.
478 A pointer to the head of the tail queue can later be declared as:
479 .Bd -literal -offset indent
480 struct HEADNAME *headp;
487 are user selectable.)
490 .Nm STAILQ_HEAD_INITIALIZER
491 evaluates to an initializer for the tail queue
496 concatenates the tail queue headed by
498 onto the end of the one headed by
500 removing all entries from the former.
504 evaluates to true if there are no items on the tail queue.
508 declares a structure that connects the elements in
513 returns the first item on the tail queue or NULL if the tail queue
518 traverses the tail queue referenced by
520 in the forward direction, assigning each element
525 .Nm STAILQ_FOREACH_MUTABLE
526 traverses the tail queue referenced by
528 in the forward direction, assigning each element
533 here it is permitted to both remove
535 as well as free it from within the loop safely without interfering with the
540 initializes the tail queue referenced by
544 .Nm STAILQ_INSERT_HEAD
545 inserts the new element
547 at the head of the tail queue.
550 .Nm STAILQ_INSERT_TAIL
551 inserts the new element
553 at the end of the tail queue.
556 .Nm STAILQ_INSERT_AFTER
557 inserts the new element
564 returns the last item on the tail queue.
565 If the tail queue is empty the return value is
570 returns the next item on the tail queue, or NULL this item is the last.
573 .Nm STAILQ_REMOVE_AFTER
574 removes the element after
576 from the tail queue. Unlike
578 this macro does not traverse the entire tail queue.
581 .Nm STAILQ_REMOVE_HEAD
582 removes the element at the head of the tail queue.
583 For optimum efficiency,
584 elements being removed from the head of the tail queue should
585 use this macro explicitly rather than the generic
594 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
596 STAILQ_HEAD(stailhead, entry) head =
597 STAILQ_HEAD_INITIALIZER(head);
598 struct stailhead *headp; /* Singly-linked tail queue head. */
601 STAILQ_ENTRY(entry) entries; /* Tail queue. */
603 } *n1, *n2, *n3, *np;
605 STAILQ_INIT(&head); /* Initialize the queue. */
607 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
608 STAILQ_INSERT_HEAD(&head, n1, entries);
610 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
611 STAILQ_INSERT_TAIL(&head, n1, entries);
613 n2 = malloc(sizeof(struct entry)); /* Insert after. */
614 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
616 STAILQ_REMOVE(&head, n2, entry, entries);
618 /* Deletion from the head. */
619 n3 = STAILQ_FIRST(&head);
620 STAILQ_REMOVE_HEAD(&head, entries);
622 /* Forward traversal. */
623 STAILQ_FOREACH(np, &head, entries)
625 /* Safe forward traversal. */
626 STAILQ_FOREACH_MUTABLE(np, &head, entries, np_temp) {
629 STAILQ_REMOVE(&head, np, entry, entries);
632 /* TailQ Deletion. */
633 while (!STAILQ_EMPTY(&head)) {
634 n1 = STAILQ_FIRST(&head);
635 STAILQ_REMOVE_HEAD(&head, entries);
638 /* Faster TailQ Deletion. */
639 n1 = STAILQ_FIRST(&head);
641 n2 = STAILQ_NEXT(n1, entries);
648 A list is headed by a structure defined by the
651 This structure contains a single pointer to the first element
653 The elements are doubly linked so that an arbitrary element can be
654 removed without traversing the list.
655 New elements can be added to the list after an existing element,
656 before an existing element, or at the head of the list.
659 structure is declared as follows:
660 .Bd -literal -offset indent
661 LIST_HEAD(HEADNAME, TYPE) head;
666 is the name of the structure to be defined, and
668 is the type of the elements to be linked into the list.
669 A pointer to the head of the list can later be declared as:
670 .Bd -literal -offset indent
671 struct HEADNAME *headp;
678 are user selectable.)
681 .Nm LIST_HEAD_INITIALIZER
682 evaluates to an initializer for the list
687 evaluates to true if there are no elements in the list.
691 declares a structure that connects the elements in
696 returns the first element in the list or NULL if the list
701 traverses the list referenced by
703 in the forward direction, assigning each element in turn to
707 .Nm LIST_FOREACH_MUTABLE
708 traverses the list referenced by
710 in the forward direction, assigning each element in turn to
714 here it is permitted to both remove
716 as well as free it from within the loop safely without interfering with the
721 initializes the list referenced by
726 inserts the new element
728 at the head of the list.
731 .Nm LIST_INSERT_AFTER
732 inserts the new element
738 .Nm LIST_INSERT_BEFORE
739 inserts the new element
746 returns the next element in the list, or NULL if this is the last.
755 LIST_HEAD(listhead, entry) head =
756 LIST_HEAD_INITIALIZER(head);
757 struct listhead *headp; /* List head. */
760 LIST_ENTRY(entry) entries; /* List. */
762 } *n1, *n2, *n3, *np, *np_temp;
764 LIST_INIT(&head); /* Initialize the list. */
766 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
767 LIST_INSERT_HEAD(&head, n1, entries);
769 n2 = malloc(sizeof(struct entry)); /* Insert after. */
770 LIST_INSERT_AFTER(n1, n2, entries);
772 n3 = malloc(sizeof(struct entry)); /* Insert before. */
773 LIST_INSERT_BEFORE(n2, n3, entries);
775 LIST_REMOVE(n2, entries); /* Deletion. */
777 /* Forward traversal. */
778 LIST_FOREACH(np, &head, entries)
781 /* Safe forward traversal. */
782 LIST_FOREACH_MUTABLE(np, &head, entries, np_temp) {
785 LIST_REMOVE(np, entries);
789 while (!LIST_EMPTY(&head)) { /* List Deletion. */
790 n1 = LIST_FIRST(&head);
791 LIST_REMOVE(n1, entries);
795 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
797 n2 = LIST_NEXT(n1, entries);
804 A tail queue is headed by a structure defined by the
807 This structure contains a pair of pointers,
808 one to the first element in the tail queue and the other to
809 the last element in the tail queue.
810 The elements are doubly linked so that an arbitrary element can be
811 removed without traversing the tail queue.
812 New elements can be added to the tail queue after an existing element,
813 before an existing element, at the head of the tail queue,
814 or at the end of the tail queue.
817 structure is declared as follows:
818 .Bd -literal -offset indent
819 TAILQ_HEAD(HEADNAME, TYPE) head;
824 is the name of the structure to be defined, and
826 is the type of the elements to be linked into the tail queue.
827 A pointer to the head of the tail queue can later be declared as:
828 .Bd -literal -offset indent
829 struct HEADNAME *headp;
836 are user selectable.)
839 .Nm TAILQ_HEAD_INITIALIZER
840 evaluates to an initializer for the tail queue
845 concatenates the tail queue headed by
847 onto the end of the one headed by
849 removing all entries from the former.
853 evaluates to true if there are no items on the tail queue.
857 declares a structure that connects the elements in
862 returns the first item on the tail queue or NULL if the tail queue
867 traverses the tail queue referenced by
869 in the forward direction, assigning each element in turn to
874 if the loop completes normally, or if there were no elements.
877 .Nm TAILQ_FOREACH_REVERSE
878 traverses the tail queue referenced by
880 in the reverse direction, assigning each element in turn to
884 .Nm TAILQ_FOREACH_MUTABLE
886 .Nm TAILQ_FOREACH_REVERSE_MUTABLE
887 traverse the list referenced by
889 in the forward or reverse direction respectively,
890 assigning each element in turn to
892 However, unlike their unsafe counterparts,
895 .Nm TAILQ_FOREACH_REVERSE
896 permit to both remove
898 as well as free it from within the loop safely without interfering with the
903 initializes the tail queue referenced by
907 .Nm TAILQ_INSERT_HEAD
908 inserts the new element
910 at the head of the tail queue.
913 .Nm TAILQ_INSERT_TAIL
914 inserts the new element
916 at the end of the tail queue.
919 .Nm TAILQ_INSERT_AFTER
920 inserts the new element
926 .Nm TAILQ_INSERT_BEFORE
927 inserts the new element
934 returns the last item on the tail queue.
935 If the tail queue is empty the return value is
940 returns the next item on the tail queue, or NULL if this item is the last.
944 returns the previous item on the tail queue, or NULL if this item
952 .Sh TAIL QUEUE EXAMPLE
954 TAILQ_HEAD(tailhead, entry) head =
955 TAILQ_HEAD_INITIALIZER(head);
956 struct tailhead *headp; /* Tail queue head. */
959 TAILQ_ENTRY(entry) entries; /* Tail queue. */
961 } *n1, *n2, *n3, *np;
963 TAILQ_INIT(&head); /* Initialize the queue. */
965 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
966 TAILQ_INSERT_HEAD(&head, n1, entries);
968 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
969 TAILQ_INSERT_TAIL(&head, n1, entries);
971 n2 = malloc(sizeof(struct entry)); /* Insert after. */
972 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
974 n3 = malloc(sizeof(struct entry)); /* Insert before. */
975 TAILQ_INSERT_BEFORE(n2, n3, entries);
977 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
979 /* Forward traversal. */
980 TAILQ_FOREACH(np, &head, entries)
982 /* Safe forward traversal. */
983 TAILQ_FOREACH_MUTABLE(np, &head, entries, np_temp) {
986 TAILQ_REMOVE(&head, np, entries);
989 /* Reverse traversal. */
990 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
992 /* TailQ Deletion. */
993 while (!TAILQ_EMPTY(&head)) {
994 n1 = TAILQ_FIRST(&head);
995 TAILQ_REMOVE(&head, n1, entries);
998 /* Faster TailQ Deletion. */
999 n1 = TAILQ_FIRST(&head);
1000 while (n1 != NULL) {
1001 n2 = TAILQ_NEXT(n1, entries);
1012 functions first appeared in