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_FROM ,
44 .Nm SLIST_FOREACH_SAFE ,
45 .Nm SLIST_FOREACH_FROM_SAFE ,
47 .Nm SLIST_HEAD_INITIALIZER ,
49 .Nm SLIST_INSERT_AFTER ,
50 .Nm SLIST_INSERT_HEAD ,
52 .Nm SLIST_REMOVE_AFTER ,
53 .Nm SLIST_REMOVE_HEAD ,
61 .Nm STAILQ_FOREACH_FROM ,
62 .Nm STAILQ_FOREACH_SAFE ,
63 .Nm STAILQ_FOREACH_FROM_SAFE ,
65 .Nm STAILQ_HEAD_INITIALIZER ,
67 .Nm STAILQ_INSERT_AFTER ,
68 .Nm STAILQ_INSERT_HEAD ,
69 .Nm STAILQ_INSERT_TAIL ,
72 .Nm STAILQ_REMOVE_AFTER ,
73 .Nm STAILQ_REMOVE_HEAD ,
80 .Nm LIST_FOREACH_FROM ,
81 .Nm LIST_FOREACH_SAFE ,
82 .Nm LIST_FOREACH_FROM_SAFE ,
84 .Nm LIST_HEAD_INITIALIZER ,
86 .Nm LIST_INSERT_AFTER ,
87 .Nm LIST_INSERT_BEFORE ,
88 .Nm LIST_INSERT_HEAD ,
97 .Nm TAILQ_FOREACH_FROM ,
98 .Nm TAILQ_FOREACH_SAFE ,
99 .Nm TAILQ_FOREACH_FROM_SAFE ,
100 .Nm TAILQ_FOREACH_REVERSE ,
101 .Nm TAILQ_FOREACH_REVERSE_FROM ,
102 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
103 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
105 .Nm TAILQ_HEAD_INITIALIZER ,
107 .Nm TAILQ_INSERT_AFTER ,
108 .Nm TAILQ_INSERT_BEFORE ,
109 .Nm TAILQ_INSERT_HEAD ,
110 .Nm TAILQ_INSERT_TAIL ,
116 .Nd implementations of singly-linked lists, singly-linked tail queues,
117 lists and tail queues
121 .Fn SLIST_EMPTY "SLIST_HEAD *head"
122 .Fn SLIST_ENTRY "TYPE"
123 .Fn SLIST_FIRST "SLIST_HEAD *head"
124 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
125 .Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
126 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
127 .Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
128 .Fn SLIST_HEAD "HEADNAME" "TYPE"
129 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
130 .Fn SLIST_INIT "SLIST_HEAD *head"
131 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
132 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
133 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
134 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
135 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
136 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
137 .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
139 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
140 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
141 .Fn STAILQ_ENTRY "TYPE"
142 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
143 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
144 .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
145 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
146 .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
147 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
148 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
149 .Fn STAILQ_INIT "STAILQ_HEAD *head"
150 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
151 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
152 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
153 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
154 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
155 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
156 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
157 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
158 .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
160 .Fn LIST_EMPTY "LIST_HEAD *head"
161 .Fn LIST_ENTRY "TYPE"
162 .Fn LIST_FIRST "LIST_HEAD *head"
163 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
164 .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
165 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
166 .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
167 .Fn LIST_HEAD "HEADNAME" "TYPE"
168 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
169 .Fn LIST_INIT "LIST_HEAD *head"
170 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
171 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
172 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
173 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
174 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
175 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
177 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
178 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
179 .Fn TAILQ_ENTRY "TYPE"
180 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
181 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
182 .Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
183 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
184 .Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
185 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
186 .Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
187 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
188 .Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
189 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
190 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
191 .Fn TAILQ_INIT "TAILQ_HEAD *head"
192 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
193 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
194 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
195 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
196 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
197 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
198 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
199 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
200 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
203 These macros define and operate on four types of data structures:
204 singly-linked lists, singly-linked tail queues, lists, and tail queues.
205 All four structures support the following functionality:
206 .Bl -enum -compact -offset indent
208 Insertion of a new entry at the head of the list.
210 Insertion of a new entry after any element in the list.
212 O(1) removal of an entry from the head of the list.
214 Forward traversal through the list.
216 Swawpping the contents of two lists.
219 Singly-linked lists are the simplest of the four data structures
220 and support only the above functionality.
221 Singly-linked lists are ideal for applications with large datasets
222 and few or no removals,
223 or for implementing a LIFO queue.
224 Singly-linked lists add the following functionality:
225 .Bl -enum -compact -offset indent
227 O(n) removal of any entry in the list.
230 Singly-linked tail queues add the following functionality:
231 .Bl -enum -compact -offset indent
233 Entries can be added at the end of a list.
235 O(n) removal of any entry in the list.
237 They may be concatenated.
240 .Bl -enum -compact -offset indent
242 All list insertions must specify the head of the list.
244 Each head entry requires two pointers rather than one.
246 Code size is about 15% greater and operations run about 20% slower
247 than singly-linked lists.
250 Singly-linked tailqs are ideal for applications with large datasets and
252 or for implementing a FIFO queue.
254 All doubly linked types of data structures (lists and tail queues)
256 .Bl -enum -compact -offset indent
258 Insertion of a new entry before any element in the list.
260 O(1) removal of any entry in the list.
263 .Bl -enum -compact -offset indent
265 Each element requires two pointers rather than one.
267 Code size and execution time of operations (except for removal) is about
268 twice that of the singly-linked data-structures.
271 Linked lists are the simplest of the doubly linked data structures and support
272 only the above functionality over singly-linked lists.
274 Tail queues add the following functionality:
275 .Bl -enum -compact -offset indent
277 Entries can be added at the end of a list.
279 They may be traversed backwards, from tail to head.
281 They may be concatenated.
284 .Bl -enum -compact -offset indent
286 All list insertions and removals must specify the head of the list.
288 Each head entry requires two pointers rather than one.
290 Code size is about 15% greater and operations run about 20% slower
291 than singly-linked lists.
294 In the macro definitions,
296 is the name of a user defined structure,
297 that must contain a field of type
307 is the name of a user defined structure that must be declared
314 See the examples below for further explanation of how these
316 .Sh SINGLY-LINKED LISTS
317 A singly-linked list is headed by a structure defined by the
320 This structure contains a single pointer to the first element
322 The elements are singly linked for minimum space and pointer manipulation
323 overhead at the expense of O(n) removal for arbitrary elements.
324 New elements can be added to the list after an existing element or
325 at the head of the list.
328 structure is declared as follows:
329 .Bd -literal -offset indent
330 SLIST_HEAD(HEADNAME, TYPE) head;
335 is the name of the structure to be defined, and
337 is the type of the elements to be linked into the list.
338 A pointer to the head of the list can later be declared as:
339 .Bd -literal -offset indent
340 struct HEADNAME *headp;
347 are user selectable.)
350 .Nm SLIST_HEAD_INITIALIZER
351 evaluates to an initializer for the list
356 evaluates to true if there are no elements in the list.
360 declares a structure that connects the elements in
365 returns the first element in the list or NULL if the list is empty.
369 traverses the list referenced by
371 in the forward direction, assigning each element in
376 .Nm SLIST_FOREACH_FROM
377 behaves identically to
381 is NULL, else it treats
383 as a previously found SLIST element and begins the loop at
385 instead of the first element in the SLIST referenced by
389 .Nm SLIST_FOREACH_SAFE
390 traverses the list referenced by
392 in the forward direction, assigning each element in
397 here it is permitted to both remove
399 as well as free it from within the loop safely without interfering with the
403 .Nm SLIST_FOREACH_FROM_SAFE
404 behaves identically to
405 .Nm SLIST_FOREACH_SAFE
408 is NULL, else it treats
410 as a previously found SLIST element and begins the loop at
412 instead of the first element in the SLIST referenced by
417 initializes the list referenced by
421 .Nm SLIST_INSERT_HEAD
422 inserts the new element
424 at the head of the list.
427 .Nm SLIST_INSERT_AFTER
428 inserts the new element
435 returns the next element in the list.
438 .Nm SLIST_REMOVE_AFTER
439 removes the element after
441 from the list. Unlike
443 this macro does not traverse the entire list.
446 .Nm SLIST_REMOVE_HEAD
449 from the head of the list.
450 For optimum efficiency,
451 elements being removed from the head of the list should explicitly use
452 this macro instead of the generic
464 swaps the contents of
468 .Sh SINGLY-LINKED LIST EXAMPLE
470 SLIST_HEAD(slisthead, entry) head =
471 SLIST_HEAD_INITIALIZER(head);
472 struct slisthead *headp; /* Singly-linked List head. */
475 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
477 } *n1, *n2, *n3, *np;
479 SLIST_INIT(&head); /* Initialize the list. */
481 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
482 SLIST_INSERT_HEAD(&head, n1, entries);
484 n2 = malloc(sizeof(struct entry)); /* Insert after. */
485 SLIST_INSERT_AFTER(n1, n2, entries);
487 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
490 n3 = SLIST_FIRST(&head);
491 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
493 /* Forward traversal. */
494 SLIST_FOREACH(np, &head, entries)
496 /* Safe forward traversal. */
497 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
500 SLIST_REMOVE(&head, np, entry, entries);
504 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
505 n1 = SLIST_FIRST(&head);
506 SLIST_REMOVE_HEAD(&head, entries);
510 .Sh SINGLY-LINKED TAIL QUEUES
511 A singly-linked tail queue is headed by a structure defined by the
514 This structure contains a pair of pointers,
515 one to the first element in the tail queue and the other to
516 the last element in the tail queue.
517 The elements are singly linked for minimum space and pointer
518 manipulation overhead at the expense of O(n) removal for arbitrary
520 New elements can be added to the tail queue after an existing element,
521 at the head of the tail queue, or at the end of the tail queue.
524 structure is declared as follows:
525 .Bd -literal -offset indent
526 STAILQ_HEAD(HEADNAME, TYPE) head;
531 is the name of the structure to be defined, and
533 is the type of the elements to be linked into the tail queue.
534 A pointer to the head of the tail queue can later be declared as:
535 .Bd -literal -offset indent
536 struct HEADNAME *headp;
543 are user selectable.)
546 .Nm STAILQ_HEAD_INITIALIZER
547 evaluates to an initializer for the tail queue
552 concatenates the tail queue headed by
554 onto the end of the one headed by
556 removing all entries from the former.
560 evaluates to true if there are no items on the tail queue.
564 declares a structure that connects the elements in
569 returns the first item on the tail queue or NULL if the tail queue
574 traverses the tail queue referenced by
576 in the forward direction, assigning each element
581 .Nm STAILQ_FOREACH_FROM
582 behaves identically to
586 is NULL, else it treats
588 as a previously found STAILQ element and begins the loop at
590 instead of the first element in the STAILQ referenced by
594 .Nm STAILQ_FOREACH_SAFE
595 traverses the tail queue referenced by
597 in the forward direction, assigning each element
602 here it is permitted to both remove
604 as well as free it from within the loop safely without interfering with the
608 .Nm STAILQ_FOREACH_FROM_SAFE
609 behaves identically to
610 .Nm STAILQ_FOREACH_SAFE
613 is NULL, else it treats
615 as a previously found STAILQ element and begins the loop at
617 instead of the first element in the STAILQ referenced by
622 initializes the tail queue referenced by
626 .Nm STAILQ_INSERT_HEAD
627 inserts the new element
629 at the head of the tail queue.
632 .Nm STAILQ_INSERT_TAIL
633 inserts the new element
635 at the end of the tail queue.
638 .Nm STAILQ_INSERT_AFTER
639 inserts the new element
646 returns the last item on the tail queue.
647 If the tail queue is empty the return value is
652 returns the next item on the tail queue, or NULL this item is the last.
655 .Nm STAILQ_REMOVE_AFTER
656 removes the element after
658 from the tail queue. Unlike
660 this macro does not traverse the entire tail queue.
663 .Nm STAILQ_REMOVE_HEAD
664 removes the element at the head of the tail queue.
665 For optimum efficiency,
666 elements being removed from the head of the tail queue should
667 use this macro explicitly rather than the generic
679 swaps the contents of
683 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
685 STAILQ_HEAD(stailhead, entry) head =
686 STAILQ_HEAD_INITIALIZER(head);
687 struct stailhead *headp; /* Singly-linked tail queue head. */
690 STAILQ_ENTRY(entry) entries; /* Tail queue. */
692 } *n1, *n2, *n3, *np;
694 STAILQ_INIT(&head); /* Initialize the queue. */
696 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
697 STAILQ_INSERT_HEAD(&head, n1, entries);
699 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
700 STAILQ_INSERT_TAIL(&head, n1, entries);
702 n2 = malloc(sizeof(struct entry)); /* Insert after. */
703 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
705 STAILQ_REMOVE(&head, n2, entry, entries);
707 /* Deletion from the head. */
708 n3 = STAILQ_FIRST(&head);
709 STAILQ_REMOVE_HEAD(&head, entries);
711 /* Forward traversal. */
712 STAILQ_FOREACH(np, &head, entries)
714 /* Safe forward traversal. */
715 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
718 STAILQ_REMOVE(&head, np, entry, entries);
721 /* TailQ Deletion. */
722 while (!STAILQ_EMPTY(&head)) {
723 n1 = STAILQ_FIRST(&head);
724 STAILQ_REMOVE_HEAD(&head, entries);
727 /* Faster TailQ Deletion. */
728 n1 = STAILQ_FIRST(&head);
730 n2 = STAILQ_NEXT(n1, entries);
737 A list is headed by a structure defined by the
740 This structure contains a single pointer to the first element
742 The elements are doubly linked so that an arbitrary element can be
743 removed without traversing the list.
744 New elements can be added to the list after an existing element,
745 before an existing element, or at the head of the list.
748 structure is declared as follows:
749 .Bd -literal -offset indent
750 LIST_HEAD(HEADNAME, TYPE) head;
755 is the name of the structure to be defined, and
757 is the type of the elements to be linked into the list.
758 A pointer to the head of the list can later be declared as:
759 .Bd -literal -offset indent
760 struct HEADNAME *headp;
767 are user selectable.)
770 .Nm LIST_HEAD_INITIALIZER
771 evaluates to an initializer for the list
776 evaluates to true if there are no elements in the list.
780 declares a structure that connects the elements in
785 returns the first element in the list or NULL if the list
790 traverses the list referenced by
792 in the forward direction, assigning each element in turn to
796 .Nm LIST_FOREACH_FROM
797 behaves identically to
801 is NULL, else it treats
803 as a previously found LIST element and begins the loop at
805 instead of the first element in the LIST referenced by
809 .Nm LIST_FOREACH_SAFE
810 traverses the list referenced by
812 in the forward direction, assigning each element in turn to
816 here it is permitted to both remove
818 as well as free it from within the loop safely without interfering with the
822 .Nm LIST_FOREACH_FROM_SAFE
823 behaves identically to
824 .Nm LIST_FOREACH_SAFE
827 is NULL, else it treats
829 as a previously found LIST element and begins the loop at
831 instead of the first element in the LIST referenced by
836 initializes the list referenced by
841 inserts the new element
843 at the head of the list.
846 .Nm LIST_INSERT_AFTER
847 inserts the new element
853 .Nm LIST_INSERT_BEFORE
854 inserts the new element
861 returns the next element in the list, or NULL if this is the last.
871 swaps the contents of
877 LIST_HEAD(listhead, entry) head =
878 LIST_HEAD_INITIALIZER(head);
879 struct listhead *headp; /* List head. */
882 LIST_ENTRY(entry) entries; /* List. */
884 } *n1, *n2, *n3, *np, *np_temp;
886 LIST_INIT(&head); /* Initialize the list. */
888 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
889 LIST_INSERT_HEAD(&head, n1, entries);
891 n2 = malloc(sizeof(struct entry)); /* Insert after. */
892 LIST_INSERT_AFTER(n1, n2, entries);
894 n3 = malloc(sizeof(struct entry)); /* Insert before. */
895 LIST_INSERT_BEFORE(n2, n3, entries);
897 LIST_REMOVE(n2, entries); /* Deletion. */
899 /* Forward traversal. */
900 LIST_FOREACH(np, &head, entries)
903 /* Safe forward traversal. */
904 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
907 LIST_REMOVE(np, entries);
911 while (!LIST_EMPTY(&head)) { /* List Deletion. */
912 n1 = LIST_FIRST(&head);
913 LIST_REMOVE(n1, entries);
917 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
919 n2 = LIST_NEXT(n1, entries);
926 A tail queue is headed by a structure defined by the
929 This structure contains a pair of pointers,
930 one to the first element in the tail queue and the other to
931 the last element in the tail queue.
932 The elements are doubly linked so that an arbitrary element can be
933 removed without traversing the tail queue.
934 New elements can be added to the tail queue after an existing element,
935 before an existing element, at the head of the tail queue,
936 or at the end of the tail queue.
939 structure is declared as follows:
940 .Bd -literal -offset indent
941 TAILQ_HEAD(HEADNAME, TYPE) head;
946 is the name of the structure to be defined, and
948 is the type of the elements to be linked into the tail queue.
949 A pointer to the head of the tail queue can later be declared as:
950 .Bd -literal -offset indent
951 struct HEADNAME *headp;
958 are user selectable.)
961 .Nm TAILQ_HEAD_INITIALIZER
962 evaluates to an initializer for the tail queue
967 concatenates the tail queue headed by
969 onto the end of the one headed by
971 removing all entries from the former.
975 evaluates to true if there are no items on the tail queue.
979 declares a structure that connects the elements in
984 returns the first item on the tail queue or NULL if the tail queue
989 traverses the tail queue referenced by
991 in the forward direction, assigning each element in turn to
996 if the loop completes normally, or if there were no elements.
999 .Nm TAILQ_FOREACH_FROM
1000 behaves identically to
1004 is NULL, else it treats
1006 as a previously found TAILQ element and begins the loop at
1008 instead of the first element in the TAILQ referenced by
1012 .Nm TAILQ_FOREACH_REVERSE
1013 traverses the tail queue referenced by
1015 in the reverse direction, assigning each element in turn to
1019 .Nm TAILQ_FOREACH_REVERSE_FROM
1020 behaves identically to
1021 .Nm TAILQ_FOREACH_REVERSE
1024 is NULL, else it treats
1026 as a previously found TAILQ element and begins the reverse loop at
1028 instead of the last element in the TAILQ referenced by
1032 .Nm TAILQ_FOREACH_SAFE
1034 .Nm TAILQ_FOREACH_REVERSE_SAFE
1035 traverse the list referenced by
1037 in the forward or reverse direction respectively,
1038 assigning each element in turn to
1040 However, unlike their unsafe counterparts,
1043 .Nm TAILQ_FOREACH_REVERSE
1044 permit to both remove
1046 as well as free it from within the loop safely without interfering with the
1050 .Nm TAILQ_FOREACH_FROM_SAFE
1051 behaves identically to
1052 .Nm TAILQ_FOREACH_SAFE
1055 is NULL, else it treats
1057 as a previously found TAILQ element and begins the loop at
1059 instead of the first element in the TAILQ referenced by
1063 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1064 behaves identically to
1065 .Nm TAILQ_FOREACH_REVERSE_SAFE
1068 is NULL, else it treats
1070 as a previously found TAILQ element and begins the reverse loop at
1072 instead of the last element in the TAILQ referenced by
1077 initializes the tail queue referenced by
1081 .Nm TAILQ_INSERT_HEAD
1082 inserts the new element
1084 at the head of the tail queue.
1087 .Nm TAILQ_INSERT_TAIL
1088 inserts the new element
1090 at the end of the tail queue.
1093 .Nm TAILQ_INSERT_AFTER
1094 inserts the new element
1100 .Nm TAILQ_INSERT_BEFORE
1101 inserts the new element
1108 returns the last item on the tail queue.
1109 If the tail queue is empty the return value is
1114 returns the next item on the tail queue, or NULL if this item is the last.
1118 returns the previous item on the tail queue, or NULL if this item
1125 from the tail queue.
1129 swaps the contents of
1133 .Sh TAIL QUEUE EXAMPLE
1135 TAILQ_HEAD(tailhead, entry) head =
1136 TAILQ_HEAD_INITIALIZER(head);
1137 struct tailhead *headp; /* Tail queue head. */
1140 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1142 } *n1, *n2, *n3, *np;
1144 TAILQ_INIT(&head); /* Initialize the queue. */
1146 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1147 TAILQ_INSERT_HEAD(&head, n1, entries);
1149 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1150 TAILQ_INSERT_TAIL(&head, n1, entries);
1152 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1153 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1155 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1156 TAILQ_INSERT_BEFORE(n2, n3, entries);
1158 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1160 /* Forward traversal. */
1161 TAILQ_FOREACH(np, &head, entries)
1163 /* Safe forward traversal. */
1164 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1167 TAILQ_REMOVE(&head, np, entries);
1170 /* Reverse traversal. */
1171 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1173 /* TailQ Deletion. */
1174 while (!TAILQ_EMPTY(&head)) {
1175 n1 = TAILQ_FIRST(&head);
1176 TAILQ_REMOVE(&head, n1, entries);
1179 /* Faster TailQ Deletion. */
1180 n1 = TAILQ_FIRST(&head);
1181 while (n1 != NULL) {
1182 n2 = TAILQ_NEXT(n1, entries);
1191 functions first appeared in