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. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
43 .Nm SLIST_FOREACH_SAFE ,
45 .Nm SLIST_HEAD_INITIALIZER ,
47 .Nm SLIST_INSERT_AFTER ,
48 .Nm SLIST_INSERT_HEAD ,
50 .Nm SLIST_REMOVE_HEAD ,
51 .Nm SLIST_REMOVE_NEXT ,
58 .Nm STAILQ_FOREACH_SAFE ,
60 .Nm STAILQ_HEAD_INITIALIZER ,
62 .Nm STAILQ_INSERT_AFTER ,
63 .Nm STAILQ_INSERT_HEAD ,
64 .Nm STAILQ_INSERT_TAIL ,
67 .Nm STAILQ_REMOVE_HEAD ,
68 .Nm STAILQ_REMOVE_NEXT ,
74 .Nm LIST_FOREACH_SAFE ,
76 .Nm LIST_HEAD_INITIALIZER ,
78 .Nm LIST_INSERT_AFTER ,
79 .Nm LIST_INSERT_BEFORE ,
80 .Nm LIST_INSERT_HEAD ,
88 .Nm TAILQ_FOREACH_SAFE ,
89 .Nm TAILQ_FOREACH_REVERSE ,
90 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
92 .Nm TAILQ_HEAD_INITIALIZER ,
94 .Nm TAILQ_INSERT_AFTER ,
95 .Nm TAILQ_INSERT_BEFORE ,
96 .Nm TAILQ_INSERT_HEAD ,
97 .Nm TAILQ_INSERT_TAIL ,
102 .Nd implementations of singly-linked lists, singly-linked tail queues,
103 lists and tail queues
107 .Fn SLIST_EMPTY "SLIST_HEAD *head"
108 .Fn SLIST_ENTRY "TYPE"
109 .Fn SLIST_FIRST "SLIST_HEAD *head"
110 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
111 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
112 .Fn SLIST_HEAD "HEADNAME" "TYPE"
113 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
114 .Fn SLIST_INIT "SLIST_HEAD *head"
115 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
116 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
117 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
118 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
119 .Fn SLIST_REMOVE_NEXT "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
120 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
122 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
123 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
124 .Fn STAILQ_ENTRY "TYPE"
125 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
126 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
127 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
128 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
129 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
130 .Fn STAILQ_INIT "STAILQ_HEAD *head"
131 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
132 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
133 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
134 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
135 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
136 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
137 .Fn STAILQ_REMOVE_NEXT "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
138 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
140 .Fn LIST_EMPTY "LIST_HEAD *head"
141 .Fn LIST_ENTRY "TYPE"
142 .Fn LIST_FIRST "LIST_HEAD *head"
143 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
144 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
145 .Fn LIST_HEAD "HEADNAME" "TYPE"
146 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
147 .Fn LIST_INIT "LIST_HEAD *head"
148 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
149 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
150 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
151 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
152 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
154 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
155 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
156 .Fn TAILQ_ENTRY "TYPE"
157 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
158 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
159 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
160 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
161 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
162 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
163 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
164 .Fn TAILQ_INIT "TAILQ_HEAD *head"
165 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
166 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
167 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
168 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
169 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
170 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
171 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
172 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
175 These macros define and operate on four types of data structures:
176 singly-linked lists, singly-linked tail queues, lists, and tail queues.
177 All four structures support the following functionality:
178 .Bl -enum -compact -offset indent
180 Insertion of a new entry at the head of the list.
182 Insertion of a new entry after any element in the list.
184 O(1) removal of an entry from the head of the list.
186 Forward traversal through the list.
189 O(n) removal of any entry in the list.
190 Singly-linked lists are the simplest of the four data structures
191 and support only the above functionality.
192 Singly-linked lists are ideal for applications with large datasets
193 and few or no removals,
194 or for implementing a LIFO queue.
195 Singly-linked lists add the following functionality:
196 .Bl -enum -compact -offset indent
198 O(n) removal of any entry in the list.
201 Singly-linked tail queues add the following functionality:
202 .Bl -enum -compact -offset indent
204 Entries can be added at the end of a list.
206 O(n) removal of any entry in the list.
208 They may be concatenated.
211 .Bl -enum -compact -offset indent
213 All list insertions must specify the head of the list.
215 Each head entry requires two pointers rather than one.
217 Code size is about 15% greater and operations run about 20% slower
218 than singly-linked lists.
221 Singly-linked tailqs are ideal for applications with large datasets and
223 or for implementing a FIFO queue.
225 All doubly linked types of data structures (lists and tail queues)
227 .Bl -enum -compact -offset indent
229 Insertion of a new entry before any element in the list.
231 O(1) removal of any entry in the list.
234 .Bl -enum -compact -offset indent
236 Each elements requires two pointers rather than one.
238 Code size and execution time of operations (except for removal) is about
239 twice that of the singly-linked data-structures.
242 Linked lists are the simplest of the doubly linked data structures and support
243 only the above functionality over singly-linked lists.
245 Tail queues add the following functionality:
246 .Bl -enum -compact -offset indent
248 Entries can be added at the end of a list.
250 They may be traversed backwards, from tail to head.
252 They may be concatenated.
255 .Bl -enum -compact -offset indent
257 All list insertions and removals must specify the head of the list.
259 Each head entry requires two pointers rather than one.
261 Code size is about 15% greater and operations run about 20% slower
262 than singly-linked lists.
265 In the macro definitions,
267 is the name of a user defined structure,
268 that must contain a field of type
278 is the name of a user defined structure that must be declared
285 See the examples below for further explanation of how these
287 .Sh SINGLY-LINKED LISTS
288 A singly-linked list is headed by a structure defined by the
291 This structure contains a single pointer to the first element
293 The elements are singly linked for minimum space and pointer manipulation
294 overhead at the expense of O(n) removal for arbitrary elements.
295 New elements can be added to the list after an existing element or
296 at the head of the list.
299 structure is declared as follows:
300 .Bd -literal -offset indent
301 SLIST_HEAD(HEADNAME, TYPE) head;
306 is the name of the structure to be defined, and
308 is the type of the elements to be linked into the list.
309 A pointer to the head of the list can later be declared as:
310 .Bd -literal -offset indent
311 struct HEADNAME *headp;
318 are user selectable.)
321 .Nm SLIST_HEAD_INITIALIZER
322 evaluates to an initializer for the list
327 evaluates to true if there are no elements in the list.
331 declares a structure that connects the elements in
336 returns the first element in the list or NULL if the list is empty.
340 traverses the list referenced by
342 in the forward direction, assigning each element in
347 .Nm SLIST_FOREACH_SAFE
348 traverses the list referenced by
350 in the forward direction, assigning each element in
355 here it is permitted to both remove
357 as well as free it from within the loop safely without interfering with the
362 initializes the list referenced by
366 .Nm SLIST_INSERT_HEAD
367 inserts the new element
369 at the head of the list.
372 .Nm SLIST_INSERT_AFTER
373 inserts the new element
380 returns the next element in the list.
383 .Nm SLIST_REMOVE_HEAD
386 from the head of the list.
387 For optimum efficiency,
388 elements being removed from the head of the list should explicitly use
389 this macro instead of the generic
394 .Nm SLIST_REMOVE_NEXT
395 removes the element after
397 from the list. Unlike
399 this macro does not traverse the entire list.
406 .Sh SINGLY-LINKED LIST EXAMPLE
408 SLIST_HEAD(slisthead, entry) head =
409 SLIST_HEAD_INITIALIZER(head);
410 struct slisthead *headp; /* Singly-linked List head. */
413 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
415 } *n1, *n2, *n3, *np;
417 SLIST_INIT(&head); /* Initialize the list. */
419 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
420 SLIST_INSERT_HEAD(&head, n1, entries);
422 n2 = malloc(sizeof(struct entry)); /* Insert after. */
423 SLIST_INSERT_AFTER(n1, n2, entries);
425 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
428 n3 = SLIST_FIRST(&head);
429 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
431 /* Forward traversal. */
432 SLIST_FOREACH(np, &head, entries)
434 /* Safe forward traversal. */
435 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
438 SLIST_REMOVE(&head, np, entry, entries);
442 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
443 n1 = SLIST_FIRST(&head);
444 SLIST_REMOVE_HEAD(&head, entries);
448 .Sh SINGLY-LINKED TAIL QUEUES
449 A singly-linked tail queue is headed by a structure defined by the
452 This structure contains a pair of pointers,
453 one to the first element in the tail queue and the other to
454 the last element in the tail queue.
455 The elements are singly linked for minimum space and pointer
456 manipulation overhead at the expense of O(n) removal for arbitrary
458 New elements can be added to the tail queue after an existing element,
459 at the head of the tail queue, or at the end of the tail queue.
462 structure is declared as follows:
463 .Bd -literal -offset indent
464 STAILQ_HEAD(HEADNAME, TYPE) head;
469 is the name of the structure to be defined, and
471 is the type of the elements to be linked into the tail queue.
472 A pointer to the head of the tail queue can later be declared as:
473 .Bd -literal -offset indent
474 struct HEADNAME *headp;
481 are user selectable.)
484 .Nm STAILQ_HEAD_INITIALIZER
485 evaluates to an initializer for the tail queue
490 concatenates the tail queue headed by
492 onto the end of the one headed by
494 removing all entries from the former.
498 evaluates to true if there are no items on the tail queue.
502 declares a structure that connects the elements in
507 returns the first item on the tail queue or NULL if the tail queue
512 traverses the tail queue referenced by
514 in the forward direction, assigning each element
519 .Nm STAILQ_FOREACH_SAFE
520 traverses the tail queue referenced by
522 in the forward direction, assigning each element
527 here it is permitted to both remove
529 as well as free it from within the loop safely without interfering with the
534 initializes the tail queue referenced by
538 .Nm STAILQ_INSERT_HEAD
539 inserts the new element
541 at the head of the tail queue.
544 .Nm STAILQ_INSERT_TAIL
545 inserts the new element
547 at the end of the tail queue.
550 .Nm STAILQ_INSERT_AFTER
551 inserts the new element
558 returns the last item on the tail queue.
559 If the tail queue is empty the return value is
564 returns the next item on the tail queue, or NULL this item is the last.
567 .Nm STAILQ_REMOVE_HEAD
568 removes the element at the head of the tail queue.
569 For optimum efficiency,
570 elements being removed from the head of the tail queue should
571 use this macro explicitly rather than the generic
576 .Nm STAILQ_REMOVE_NEXT
577 removes the element after
579 from the tail queue. Unlike
581 this macro does not traverse the entire tail queue.
588 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
590 STAILQ_HEAD(stailhead, entry) head =
591 STAILQ_HEAD_INITIALIZER(head);
592 struct stailhead *headp; /* Singly-linked tail queue head. */
595 STAILQ_ENTRY(entry) entries; /* Tail queue. */
597 } *n1, *n2, *n3, *np;
599 STAILQ_INIT(&head); /* Initialize the queue. */
601 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
602 STAILQ_INSERT_HEAD(&head, n1, entries);
604 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
605 STAILQ_INSERT_TAIL(&head, n1, entries);
607 n2 = malloc(sizeof(struct entry)); /* Insert after. */
608 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
610 STAILQ_REMOVE(&head, n2, entry, entries);
612 /* Deletion from the head. */
613 n3 = STAILQ_FIRST(&head);
614 STAILQ_REMOVE_HEAD(&head, entries);
616 /* Forward traversal. */
617 STAILQ_FOREACH(np, &head, entries)
619 /* Safe forward traversal. */
620 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
623 STAILQ_REMOVE(&head, np, entry, entries);
626 /* TailQ Deletion. */
627 while (!STAILQ_EMPTY(&head)) {
628 n1 = STAILQ_FIRST(&head);
629 STAILQ_REMOVE_HEAD(&head, entries);
632 /* Faster TailQ Deletion. */
633 n1 = STAILQ_FIRST(&head);
635 n2 = STAILQ_NEXT(n1, entries);
642 A list is headed by a structure defined by the
645 This structure contains a single pointer to the first element
647 The elements are doubly linked so that an arbitrary element can be
648 removed without traversing the list.
649 New elements can be added to the list after an existing element,
650 before an existing element, or at the head of the list.
653 structure is declared as follows:
654 .Bd -literal -offset indent
655 LIST_HEAD(HEADNAME, TYPE) head;
660 is the name of the structure to be defined, and
662 is the type of the elements to be linked into the list.
663 A pointer to the head of the list can later be declared as:
664 .Bd -literal -offset indent
665 struct HEADNAME *headp;
672 are user selectable.)
675 .Nm LIST_HEAD_INITIALIZER
676 evaluates to an initializer for the list
681 evaluates to true if there are no elements in the list.
685 declares a structure that connects the elements in
690 returns the first element in the list or NULL if the list
695 traverses the list referenced by
697 in the forward direction, assigning each element in turn to
701 .Nm LIST_FOREACH_SAFE
702 traverses the list referenced by
704 in the forward direction, assigning each element in turn to
708 here it is permitted to both remove
710 as well as free it from within the loop safely without interfering with the
715 initializes the list referenced by
720 inserts the new element
722 at the head of the list.
725 .Nm LIST_INSERT_AFTER
726 inserts the new element
732 .Nm LIST_INSERT_BEFORE
733 inserts the new element
740 returns the next element in the list, or NULL if this is the last.
749 LIST_HEAD(listhead, entry) head =
750 LIST_HEAD_INITIALIZER(head);
751 struct listhead *headp; /* List head. */
754 LIST_ENTRY(entry) entries; /* List. */
756 } *n1, *n2, *n3, *np, *np_temp;
758 LIST_INIT(&head); /* Initialize the list. */
760 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
761 LIST_INSERT_HEAD(&head, n1, entries);
763 n2 = malloc(sizeof(struct entry)); /* Insert after. */
764 LIST_INSERT_AFTER(n1, n2, entries);
766 n3 = malloc(sizeof(struct entry)); /* Insert before. */
767 LIST_INSERT_BEFORE(n2, n3, entries);
769 LIST_REMOVE(n2, entries); /* Deletion. */
771 /* Forward traversal. */
772 LIST_FOREACH(np, &head, entries)
775 /* Safe forward traversal. */
776 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
779 LIST_REMOVE(np, entries);
783 while (!LIST_EMPTY(&head)) { /* List Deletion. */
784 n1 = LIST_FIRST(&head);
785 LIST_REMOVE(n1, entries);
789 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
791 n2 = LIST_NEXT(n1, entries);
798 A tail queue is headed by a structure defined by the
801 This structure contains a pair of pointers,
802 one to the first element in the tail queue and the other to
803 the last element in the tail queue.
804 The elements are doubly linked so that an arbitrary element can be
805 removed without traversing the tail queue.
806 New elements can be added to the tail queue after an existing element,
807 before an existing element, at the head of the tail queue,
808 or at the end of the tail queue.
811 structure is declared as follows:
812 .Bd -literal -offset indent
813 TAILQ_HEAD(HEADNAME, TYPE) head;
818 is the name of the structure to be defined, and
820 is the type of the elements to be linked into the tail queue.
821 A pointer to the head of the tail queue can later be declared as:
822 .Bd -literal -offset indent
823 struct HEADNAME *headp;
830 are user selectable.)
833 .Nm TAILQ_HEAD_INITIALIZER
834 evaluates to an initializer for the tail queue
839 concatenates the tail queue headed by
841 onto the end of the one headed by
843 removing all entries from the former.
847 evaluates to true if there are no items on the tail queue.
851 declares a structure that connects the elements in
856 returns the first item on the tail queue or NULL if the tail queue
861 traverses the tail queue referenced by
863 in the forward direction, assigning each element in turn to
868 if the loop completes normally, or if there were no elements.
871 .Nm TAILQ_FOREACH_REVERSE
872 traverses the tail queue referenced by
874 in the reverse direction, assigning each element in turn to
878 .Nm TAILQ_FOREACH_SAFE
880 .Nm TAILQ_FOREACH_REVERSE_SAFE
881 traverse the list referenced by
883 in the forward or reverse direction respectively,
884 assigning each element in turn to
886 However, unlike their unsafe counterparts,
889 .Nm TAILQ_FOREACH_REVERSE
890 permit to both remove
892 as well as free it from within the loop safely without interfering with the
897 initializes the tail queue referenced by
901 .Nm TAILQ_INSERT_HEAD
902 inserts the new element
904 at the head of the tail queue.
907 .Nm TAILQ_INSERT_TAIL
908 inserts the new element
910 at the end of the tail queue.
913 .Nm TAILQ_INSERT_AFTER
914 inserts the new element
920 .Nm TAILQ_INSERT_BEFORE
921 inserts the new element
928 returns the last item on the tail queue.
929 If the tail queue is empty the return value is
934 returns the next item on the tail queue, or NULL if this item is the last.
938 returns the previous item on the tail queue, or NULL if this item
946 .Sh TAIL QUEUE EXAMPLE
948 TAILQ_HEAD(tailhead, entry) head =
949 TAILQ_HEAD_INITIALIZER(head);
950 struct tailhead *headp; /* Tail queue head. */
953 TAILQ_ENTRY(entry) entries; /* Tail queue. */
955 } *n1, *n2, *n3, *np;
957 TAILQ_INIT(&head); /* Initialize the queue. */
959 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
960 TAILQ_INSERT_HEAD(&head, n1, entries);
962 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
963 TAILQ_INSERT_TAIL(&head, n1, entries);
965 n2 = malloc(sizeof(struct entry)); /* Insert after. */
966 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
968 n3 = malloc(sizeof(struct entry)); /* Insert before. */
969 TAILQ_INSERT_BEFORE(n2, n3, entries);
971 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
973 /* Forward traversal. */
974 TAILQ_FOREACH(np, &head, entries)
976 /* Safe forward traversal. */
977 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
980 TAILQ_REMOVE(&head, np, entries);
983 /* Reverse traversal. */
984 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
986 /* TailQ Deletion. */
987 while (!TAILQ_EMPTY(&head)) {
988 n1 = TAILQ_FIRST(&head);
989 TAILQ_REMOVE(&head, n1, entries);
992 /* Faster TailQ Deletion. */
993 n1 = TAILQ_FIRST(&head);
995 n2 = TAILQ_NEXT(n1, entries);
1004 functions first appeared in