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_AFTER ,
51 .Nm SLIST_REMOVE_HEAD ,
59 .Nm STAILQ_FOREACH_SAFE ,
61 .Nm STAILQ_HEAD_INITIALIZER ,
63 .Nm STAILQ_INSERT_AFTER ,
64 .Nm STAILQ_INSERT_HEAD ,
65 .Nm STAILQ_INSERT_TAIL ,
68 .Nm STAILQ_REMOVE_AFTER ,
69 .Nm STAILQ_REMOVE_HEAD ,
76 .Nm LIST_FOREACH_SAFE ,
78 .Nm LIST_HEAD_INITIALIZER ,
80 .Nm LIST_INSERT_AFTER ,
81 .Nm LIST_INSERT_BEFORE ,
82 .Nm LIST_INSERT_HEAD ,
92 .Nm TAILQ_FOREACH_SAFE ,
93 .Nm TAILQ_FOREACH_REVERSE ,
94 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
96 .Nm TAILQ_HEAD_INITIALIZER ,
98 .Nm TAILQ_INSERT_AFTER ,
99 .Nm TAILQ_INSERT_BEFORE ,
100 .Nm TAILQ_INSERT_HEAD ,
101 .Nm TAILQ_INSERT_TAIL ,
107 .Nd implementations of singly-linked lists, singly-linked tail queues,
108 lists and tail queues
112 .Fn SLIST_EMPTY "SLIST_HEAD *head"
113 .Fn SLIST_ENTRY "TYPE"
114 .Fn SLIST_FIRST "SLIST_HEAD *head"
115 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
116 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
117 .Fn SLIST_HEAD "HEADNAME" "TYPE"
118 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
119 .Fn SLIST_INIT "SLIST_HEAD *head"
120 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
121 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
122 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
123 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
124 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
125 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
126 .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
128 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
129 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
130 .Fn STAILQ_ENTRY "TYPE"
131 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
132 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
133 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
134 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
135 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
136 .Fn STAILQ_INIT "STAILQ_HEAD *head"
137 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
138 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
139 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
140 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
141 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
142 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
143 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
144 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
145 .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
147 .Fn LIST_EMPTY "LIST_HEAD *head"
148 .Fn LIST_ENTRY "TYPE"
149 .Fn LIST_FIRST "LIST_HEAD *head"
150 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
151 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
152 .Fn LIST_HEAD "HEADNAME" "TYPE"
153 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
154 .Fn LIST_INIT "LIST_HEAD *head"
155 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
156 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
157 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
158 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
159 .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
160 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
161 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
163 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
164 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
165 .Fn TAILQ_ENTRY "TYPE"
166 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
167 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
168 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
169 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
170 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
171 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
172 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
173 .Fn TAILQ_INIT "TAILQ_HEAD *head"
174 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
175 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
176 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
177 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
178 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
179 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
180 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
181 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
182 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
185 These macros define and operate on four types of data structures:
186 singly-linked lists, singly-linked tail queues, lists, and tail queues.
187 All four structures support the following functionality:
188 .Bl -enum -compact -offset indent
190 Insertion of a new entry at the head of the list.
192 Insertion of a new entry after any element in the list.
194 O(1) removal of an entry from the head of the list.
196 Forward traversal through the list.
198 Swapping the contents of two lists.
201 Singly-linked lists are the simplest of the four data structures
202 and support only the above functionality.
203 Singly-linked lists are ideal for applications with large datasets
204 and few or no removals,
205 or for implementing a LIFO queue.
206 Singly-linked lists add the following functionality:
207 .Bl -enum -compact -offset indent
209 O(n) removal of any entry in the list.
212 Singly-linked tail queues add the following functionality:
213 .Bl -enum -compact -offset indent
215 Entries can be added at the end of a list.
217 O(n) removal of any entry in the list.
219 They may be concatenated.
222 .Bl -enum -compact -offset indent
224 All list insertions must specify the head of the list.
226 Each head entry requires two pointers rather than one.
228 Code size is about 15% greater and operations run about 20% slower
229 than singly-linked lists.
232 Singly-linked tail queues are ideal for applications with large datasets and
234 or for implementing a FIFO queue.
236 All doubly linked types of data structures (lists and tail queues)
238 .Bl -enum -compact -offset indent
240 Insertion of a new entry before any element in the list.
242 O(1) removal of any entry in the list.
245 .Bl -enum -compact -offset indent
247 Each element requires two pointers rather than one.
249 Code size and execution time of operations (except for removal) is about
250 twice that of the singly-linked data-structures.
253 Linked lists are the simplest of the doubly linked data structures.
254 They add the following functionality over the above:
255 .Bl -enum -compact -offset indent
257 They may be traversed backwards.
260 .Bl -enum -compact -offset indent
262 To traverse backwards, an entry to begin the traversal and the list in
263 which it is contained must be specified.
266 Tail queues add the following functionality:
267 .Bl -enum -compact -offset indent
269 Entries can be added at the end of a list.
271 They may be traversed backwards, from tail to head.
273 They may be concatenated.
276 .Bl -enum -compact -offset indent
278 All list insertions and removals must specify the head of the list.
280 Each head entry requires two pointers rather than one.
282 Code size is about 15% greater and operations run about 20% slower
283 than singly-linked lists.
286 In the macro definitions,
288 is the name of a user defined structure,
289 that must contain a field of type
299 is the name of a user defined structure that must be declared
306 See the examples below for further explanation of how these
308 .Sh SINGLY-LINKED LISTS
309 A singly-linked list is headed by a structure defined by the
312 This structure contains a single pointer to the first element
314 The elements are singly linked for minimum space and pointer manipulation
315 overhead at the expense of O(n) removal for arbitrary elements.
316 New elements can be added to the list after an existing element or
317 at the head of the list.
320 structure is declared as follows:
321 .Bd -literal -offset indent
322 SLIST_HEAD(HEADNAME, TYPE) head;
327 is the name of the structure to be defined, and
329 is the type of the elements to be linked into the list.
330 A pointer to the head of the list can later be declared as:
331 .Bd -literal -offset indent
332 struct HEADNAME *headp;
339 are user selectable.)
342 .Nm SLIST_HEAD_INITIALIZER
343 evaluates to an initializer for the list
348 evaluates to true if there are no elements in the list.
352 declares a structure that connects the elements in
357 returns the first element in the list or NULL if the list is empty.
361 traverses the list referenced by
363 in the forward direction, assigning each element in
368 .Nm SLIST_FOREACH_SAFE
369 traverses the list referenced by
371 in the forward direction, assigning each element in
376 here it is permitted to both remove
378 as well as free it from within the loop safely without interfering with the
383 initializes the list referenced by
387 .Nm SLIST_INSERT_HEAD
388 inserts the new element
390 at the head of the list.
393 .Nm SLIST_INSERT_AFTER
394 inserts the new element
401 returns the next element in the list.
404 .Nm SLIST_REMOVE_AFTER
405 removes the element after
407 from the list. Unlike
409 this macro does not traverse the entire list.
412 .Nm SLIST_REMOVE_HEAD
415 from the head of the list.
416 For optimum efficiency,
417 elements being removed from the head of the list should explicitly use
418 this macro instead of the generic
430 swaps the contents of
434 .Sh SINGLY-LINKED LIST EXAMPLE
436 SLIST_HEAD(slisthead, entry) head =
437 SLIST_HEAD_INITIALIZER(head);
438 struct slisthead *headp; /* Singly-linked List head. */
441 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
443 } *n1, *n2, *n3, *np;
445 SLIST_INIT(&head); /* Initialize the list. */
447 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
448 SLIST_INSERT_HEAD(&head, n1, entries);
450 n2 = malloc(sizeof(struct entry)); /* Insert after. */
451 SLIST_INSERT_AFTER(n1, n2, entries);
453 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
456 n3 = SLIST_FIRST(&head);
457 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
459 /* Forward traversal. */
460 SLIST_FOREACH(np, &head, entries)
462 /* Safe forward traversal. */
463 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
466 SLIST_REMOVE(&head, np, entry, entries);
470 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
471 n1 = SLIST_FIRST(&head);
472 SLIST_REMOVE_HEAD(&head, entries);
476 .Sh SINGLY-LINKED TAIL QUEUES
477 A singly-linked tail queue is headed by a structure defined by the
480 This structure contains a pair of pointers,
481 one to the first element in the tail queue and the other to
482 the last element in the tail queue.
483 The elements are singly linked for minimum space and pointer
484 manipulation overhead at the expense of O(n) removal for arbitrary
486 New elements can be added to the tail queue after an existing element,
487 at the head of the tail queue, or at the end of the tail queue.
490 structure is declared as follows:
491 .Bd -literal -offset indent
492 STAILQ_HEAD(HEADNAME, TYPE) head;
497 is the name of the structure to be defined, and
499 is the type of the elements to be linked into the tail queue.
500 A pointer to the head of the tail queue can later be declared as:
501 .Bd -literal -offset indent
502 struct HEADNAME *headp;
509 are user selectable.)
512 .Nm STAILQ_HEAD_INITIALIZER
513 evaluates to an initializer for the tail queue
518 concatenates the tail queue headed by
520 onto the end of the one headed by
522 removing all entries from the former.
526 evaluates to true if there are no items on the tail queue.
530 declares a structure that connects the elements in
535 returns the first item on the tail queue or NULL if the tail queue
540 traverses the tail queue referenced by
542 in the forward direction, assigning each element
547 .Nm STAILQ_FOREACH_SAFE
548 traverses the tail queue referenced by
550 in the forward direction, assigning each element
555 here it is permitted to both remove
557 as well as free it from within the loop safely without interfering with the
562 initializes the tail queue referenced by
566 .Nm STAILQ_INSERT_HEAD
567 inserts the new element
569 at the head of the tail queue.
572 .Nm STAILQ_INSERT_TAIL
573 inserts the new element
575 at the end of the tail queue.
578 .Nm STAILQ_INSERT_AFTER
579 inserts the new element
586 returns the last item on the tail queue.
587 If the tail queue is empty the return value is
592 returns the next item on the tail queue, or NULL this item is the last.
595 .Nm STAILQ_REMOVE_AFTER
596 removes the element after
598 from the tail queue. Unlike
600 this macro does not traverse the entire tail queue.
603 .Nm STAILQ_REMOVE_HEAD
604 removes the element at the head of the tail queue.
605 For optimum efficiency,
606 elements being removed from the head of the tail queue should
607 use this macro explicitly rather than the generic
619 swaps the contents of
623 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
625 STAILQ_HEAD(stailhead, entry) head =
626 STAILQ_HEAD_INITIALIZER(head);
627 struct stailhead *headp; /* Singly-linked tail queue head. */
630 STAILQ_ENTRY(entry) entries; /* Tail queue. */
632 } *n1, *n2, *n3, *np;
634 STAILQ_INIT(&head); /* Initialize the queue. */
636 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
637 STAILQ_INSERT_HEAD(&head, n1, entries);
639 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
640 STAILQ_INSERT_TAIL(&head, n1, entries);
642 n2 = malloc(sizeof(struct entry)); /* Insert after. */
643 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
645 STAILQ_REMOVE(&head, n2, entry, entries);
647 /* Deletion from the head. */
648 n3 = STAILQ_FIRST(&head);
649 STAILQ_REMOVE_HEAD(&head, entries);
651 /* Forward traversal. */
652 STAILQ_FOREACH(np, &head, entries)
654 /* Safe forward traversal. */
655 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
658 STAILQ_REMOVE(&head, np, entry, entries);
661 /* TailQ Deletion. */
662 while (!STAILQ_EMPTY(&head)) {
663 n1 = STAILQ_FIRST(&head);
664 STAILQ_REMOVE_HEAD(&head, entries);
667 /* Faster TailQ Deletion. */
668 n1 = STAILQ_FIRST(&head);
670 n2 = STAILQ_NEXT(n1, entries);
677 A list is headed by a structure defined by the
680 This structure contains a single pointer to the first element
682 The elements are doubly linked so that an arbitrary element can be
683 removed without traversing the list.
684 New elements can be added to the list after an existing element,
685 before an existing element, or at the head of the list.
688 structure is declared as follows:
689 .Bd -literal -offset indent
690 LIST_HEAD(HEADNAME, TYPE) head;
695 is the name of the structure to be defined, and
697 is the type of the elements to be linked into the list.
698 A pointer to the head of the list can later be declared as:
699 .Bd -literal -offset indent
700 struct HEADNAME *headp;
707 are user selectable.)
710 .Nm LIST_HEAD_INITIALIZER
711 evaluates to an initializer for the list
716 evaluates to true if there are no elements in the list.
720 declares a structure that connects the elements in
725 returns the first element in the list or NULL if the list
730 traverses the list referenced by
732 in the forward direction, assigning each element in turn to
736 .Nm LIST_FOREACH_SAFE
737 traverses the list referenced by
739 in the forward direction, assigning each element in turn to
743 here it is permitted to both remove
745 as well as free it from within the loop safely without interfering with the
750 initializes the list referenced by
755 inserts the new element
757 at the head of the list.
760 .Nm LIST_INSERT_AFTER
761 inserts the new element
767 .Nm LIST_INSERT_BEFORE
768 inserts the new element
775 returns the next element in the list, or NULL if this is the last.
779 returns the previous element in the list, or NULL if this is the first.
793 swaps the contents of
799 LIST_HEAD(listhead, entry) head =
800 LIST_HEAD_INITIALIZER(head);
801 struct listhead *headp; /* List head. */
804 LIST_ENTRY(entry) entries; /* List. */
806 } *n1, *n2, *n3, *np, *np_temp;
808 LIST_INIT(&head); /* Initialize the list. */
810 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
811 LIST_INSERT_HEAD(&head, n1, entries);
813 n2 = malloc(sizeof(struct entry)); /* Insert after. */
814 LIST_INSERT_AFTER(n1, n2, entries);
816 n3 = malloc(sizeof(struct entry)); /* Insert before. */
817 LIST_INSERT_BEFORE(n2, n3, entries);
819 LIST_REMOVE(n2, entries); /* Deletion. */
821 /* Forward traversal. */
822 LIST_FOREACH(np, &head, entries)
825 /* Safe forward traversal. */
826 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
829 LIST_REMOVE(np, entries);
833 while (!LIST_EMPTY(&head)) { /* List Deletion. */
834 n1 = LIST_FIRST(&head);
835 LIST_REMOVE(n1, entries);
839 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
841 n2 = LIST_NEXT(n1, entries);
848 A tail queue is headed by a structure defined by the
851 This structure contains a pair of pointers,
852 one to the first element in the tail queue and the other to
853 the last element in the tail queue.
854 The elements are doubly linked so that an arbitrary element can be
855 removed without traversing the tail queue.
856 New elements can be added to the tail queue after an existing element,
857 before an existing element, at the head of the tail queue,
858 or at the end of the tail queue.
861 structure is declared as follows:
862 .Bd -literal -offset indent
863 TAILQ_HEAD(HEADNAME, TYPE) head;
868 is the name of the structure to be defined, and
870 is the type of the elements to be linked into the tail queue.
871 A pointer to the head of the tail queue can later be declared as:
872 .Bd -literal -offset indent
873 struct HEADNAME *headp;
880 are user selectable.)
883 .Nm TAILQ_HEAD_INITIALIZER
884 evaluates to an initializer for the tail queue
889 concatenates the tail queue headed by
891 onto the end of the one headed by
893 removing all entries from the former.
897 evaluates to true if there are no items on the tail queue.
901 declares a structure that connects the elements in
906 returns the first item on the tail queue or NULL if the tail queue
911 traverses the tail queue referenced by
913 in the forward direction, assigning each element in turn to
918 if the loop completes normally, or if there were no elements.
921 .Nm TAILQ_FOREACH_REVERSE
922 traverses the tail queue referenced by
924 in the reverse direction, assigning each element in turn to
928 .Nm TAILQ_FOREACH_SAFE
930 .Nm TAILQ_FOREACH_REVERSE_SAFE
931 traverse the list referenced by
933 in the forward or reverse direction respectively,
934 assigning each element in turn to
936 However, unlike their unsafe counterparts,
939 .Nm TAILQ_FOREACH_REVERSE
940 permit to both remove
942 as well as free it from within the loop safely without interfering with the
947 initializes the tail queue referenced by
951 .Nm TAILQ_INSERT_HEAD
952 inserts the new element
954 at the head of the tail queue.
957 .Nm TAILQ_INSERT_TAIL
958 inserts the new element
960 at the end of the tail queue.
963 .Nm TAILQ_INSERT_AFTER
964 inserts the new element
970 .Nm TAILQ_INSERT_BEFORE
971 inserts the new element
978 returns the last item on the tail queue.
979 If the tail queue is empty the return value is
984 returns the next item on the tail queue, or NULL if this item is the last.
988 returns the previous item on the tail queue, or NULL if this item
999 swaps the contents of
1003 .Sh TAIL QUEUE EXAMPLE
1005 TAILQ_HEAD(tailhead, entry) head =
1006 TAILQ_HEAD_INITIALIZER(head);
1007 struct tailhead *headp; /* Tail queue head. */
1010 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1012 } *n1, *n2, *n3, *np;
1014 TAILQ_INIT(&head); /* Initialize the queue. */
1016 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1017 TAILQ_INSERT_HEAD(&head, n1, entries);
1019 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1020 TAILQ_INSERT_TAIL(&head, n1, entries);
1022 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1023 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1025 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1026 TAILQ_INSERT_BEFORE(n2, n3, entries);
1028 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1030 /* Forward traversal. */
1031 TAILQ_FOREACH(np, &head, entries)
1033 /* Safe forward traversal. */
1034 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1037 TAILQ_REMOVE(&head, np, entries);
1040 /* Reverse traversal. */
1041 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1043 /* TailQ Deletion. */
1044 while (!TAILQ_EMPTY(&head)) {
1045 n1 = TAILQ_FIRST(&head);
1046 TAILQ_REMOVE(&head, n1, entries);
1049 /* Faster TailQ Deletion. */
1050 n1 = TAILQ_FIRST(&head);
1051 while (n1 != NULL) {
1052 n2 = TAILQ_NEXT(n1, entries);
1063 functions first appeared in