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 ,
98 .Nm TAILQ_FOREACH_FROM ,
99 .Nm TAILQ_FOREACH_SAFE ,
100 .Nm TAILQ_FOREACH_FROM_SAFE ,
101 .Nm TAILQ_FOREACH_REVERSE ,
102 .Nm TAILQ_FOREACH_REVERSE_FROM ,
103 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
104 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
106 .Nm TAILQ_HEAD_INITIALIZER ,
108 .Nm TAILQ_INSERT_AFTER ,
109 .Nm TAILQ_INSERT_BEFORE ,
110 .Nm TAILQ_INSERT_HEAD ,
111 .Nm TAILQ_INSERT_TAIL ,
117 .Nd implementations of singly-linked lists, singly-linked tail queues,
118 lists and tail queues
122 .Fn SLIST_EMPTY "SLIST_HEAD *head"
123 .Fn SLIST_ENTRY "TYPE"
124 .Fn SLIST_FIRST "SLIST_HEAD *head"
125 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
126 .Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
127 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
128 .Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
129 .Fn SLIST_HEAD "HEADNAME" "TYPE"
130 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
131 .Fn SLIST_INIT "SLIST_HEAD *head"
132 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
133 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
134 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
135 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
136 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
137 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
138 .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
140 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
141 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
142 .Fn STAILQ_ENTRY "TYPE"
143 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
144 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
145 .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
146 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
147 .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
148 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
149 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
150 .Fn STAILQ_INIT "STAILQ_HEAD *head"
151 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
152 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
153 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
154 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
155 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
156 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
157 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
158 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
159 .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
161 .Fn LIST_EMPTY "LIST_HEAD *head"
162 .Fn LIST_ENTRY "TYPE"
163 .Fn LIST_FIRST "LIST_HEAD *head"
164 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
165 .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
166 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
167 .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
168 .Fn LIST_HEAD "HEADNAME" "TYPE"
169 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
170 .Fn LIST_INIT "LIST_HEAD *head"
171 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
172 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
173 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
174 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
175 .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
176 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
177 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
179 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
180 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
181 .Fn TAILQ_ENTRY "TYPE"
182 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
183 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
184 .Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
185 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
186 .Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
187 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
188 .Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
189 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
190 .Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
191 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
192 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
193 .Fn TAILQ_INIT "TAILQ_HEAD *head"
194 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
195 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
196 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
197 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
198 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
199 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
200 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
201 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
202 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
205 These macros define and operate on four types of data structures:
206 singly-linked lists, singly-linked tail queues, lists, and tail queues.
207 All four structures support the following functionality:
208 .Bl -enum -compact -offset indent
210 Insertion of a new entry at the head of the list.
212 Insertion of a new entry after any element in the list.
214 O(1) removal of an entry from the head of the list.
216 Forward traversal through the list.
218 Swapping the contents of two lists.
221 Singly-linked lists are the simplest of the four data structures
222 and support only the above functionality.
223 Singly-linked lists are ideal for applications with large datasets
224 and few or no removals,
225 or for implementing a LIFO queue.
226 Singly-linked lists add the following functionality:
227 .Bl -enum -compact -offset indent
229 O(n) removal of any entry in the list.
232 Singly-linked tail queues add the following functionality:
233 .Bl -enum -compact -offset indent
235 Entries can be added at the end of a list.
237 O(n) removal of any entry in the list.
239 They may be concatenated.
242 .Bl -enum -compact -offset indent
244 All list insertions must specify the head of the list.
246 Each head entry requires two pointers rather than one.
248 Code size is about 15% greater and operations run about 20% slower
249 than singly-linked lists.
252 Singly-linked tail queues are ideal for applications with large datasets and
254 or for implementing a FIFO queue.
256 All doubly linked types of data structures (lists and tail queues)
258 .Bl -enum -compact -offset indent
260 Insertion of a new entry before any element in the list.
262 O(1) removal of any entry in the list.
265 .Bl -enum -compact -offset indent
267 Each element requires two pointers rather than one.
269 Code size and execution time of operations (except for removal) is about
270 twice that of the singly-linked data-structures.
273 Linked lists are the simplest of the doubly linked data structures.
274 They add the following functionality over the above:
275 .Bl -enum -compact -offset indent
277 They may be traversed backwards.
280 .Bl -enum -compact -offset indent
282 To traverse backwards, an entry to begin the traversal and the list in
283 which it is contained must be specified.
286 Tail queues add the following functionality:
287 .Bl -enum -compact -offset indent
289 Entries can be added at the end of a list.
291 They may be traversed backwards, from tail to head.
293 They may be concatenated.
296 .Bl -enum -compact -offset indent
298 All list insertions and removals must specify the head of the list.
300 Each head entry requires two pointers rather than one.
302 Code size is about 15% greater and operations run about 20% slower
303 than singly-linked lists.
306 In the macro definitions,
308 is the name of a user defined structure,
309 that must contain a field of type
319 is the name of a user defined structure that must be declared
326 See the examples below for further explanation of how these
328 .Sh SINGLY-LINKED LISTS
329 A singly-linked list is headed by a structure defined by the
332 This structure contains a single pointer to the first element
334 The elements are singly linked for minimum space and pointer manipulation
335 overhead at the expense of O(n) removal for arbitrary elements.
336 New elements can be added to the list after an existing element or
337 at the head of the list.
340 structure is declared as follows:
341 .Bd -literal -offset indent
342 SLIST_HEAD(HEADNAME, TYPE) head;
347 is the name of the structure to be defined, and
349 is the type of the elements to be linked into the list.
350 A pointer to the head of the list can later be declared as:
351 .Bd -literal -offset indent
352 struct HEADNAME *headp;
359 are user selectable.)
362 .Nm SLIST_HEAD_INITIALIZER
363 evaluates to an initializer for the list
368 evaluates to true if there are no elements in the list.
372 declares a structure that connects the elements in
377 returns the first element in the list or NULL if the list is empty.
381 traverses the list referenced by
383 in the forward direction, assigning each element in
388 .Nm SLIST_FOREACH_FROM
389 behaves identically to
393 is NULL, else it treats
395 as a previously found SLIST element and begins the loop at
397 instead of the first element in the SLIST referenced by
401 .Nm SLIST_FOREACH_SAFE
402 traverses the list referenced by
404 in the forward direction, assigning each element in
409 here it is permitted to both remove
411 as well as free it from within the loop safely without interfering with the
415 .Nm SLIST_FOREACH_FROM_SAFE
416 behaves identically to
417 .Nm SLIST_FOREACH_SAFE
420 is NULL, else it treats
422 as a previously found SLIST element and begins the loop at
424 instead of the first element in the SLIST referenced by
429 initializes the list referenced by
433 .Nm SLIST_INSERT_HEAD
434 inserts the new element
436 at the head of the list.
439 .Nm SLIST_INSERT_AFTER
440 inserts the new element
447 returns the next element in the list.
450 .Nm SLIST_REMOVE_AFTER
451 removes the element after
453 from the list. Unlike
455 this macro does not traverse the entire list.
458 .Nm SLIST_REMOVE_HEAD
461 from the head of the list.
462 For optimum efficiency,
463 elements being removed from the head of the list should explicitly use
464 this macro instead of the generic
476 swaps the contents of
480 .Sh SINGLY-LINKED LIST EXAMPLE
482 SLIST_HEAD(slisthead, entry) head =
483 SLIST_HEAD_INITIALIZER(head);
484 struct slisthead *headp; /* Singly-linked List head. */
487 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
489 } *n1, *n2, *n3, *np;
491 SLIST_INIT(&head); /* Initialize the list. */
493 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
494 SLIST_INSERT_HEAD(&head, n1, entries);
496 n2 = malloc(sizeof(struct entry)); /* Insert after. */
497 SLIST_INSERT_AFTER(n1, n2, entries);
499 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
502 n3 = SLIST_FIRST(&head);
503 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
505 /* Forward traversal. */
506 SLIST_FOREACH(np, &head, entries)
508 /* Safe forward traversal. */
509 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
512 SLIST_REMOVE(&head, np, entry, entries);
516 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
517 n1 = SLIST_FIRST(&head);
518 SLIST_REMOVE_HEAD(&head, entries);
522 .Sh SINGLY-LINKED TAIL QUEUES
523 A singly-linked tail queue is headed by a structure defined by the
526 This structure contains a pair of pointers,
527 one to the first element in the tail queue and the other to
528 the last element in the tail queue.
529 The elements are singly linked for minimum space and pointer
530 manipulation overhead at the expense of O(n) removal for arbitrary
532 New elements can be added to the tail queue after an existing element,
533 at the head of the tail queue, or at the end of the tail queue.
536 structure is declared as follows:
537 .Bd -literal -offset indent
538 STAILQ_HEAD(HEADNAME, TYPE) head;
543 is the name of the structure to be defined, and
545 is the type of the elements to be linked into the tail queue.
546 A pointer to the head of the tail queue can later be declared as:
547 .Bd -literal -offset indent
548 struct HEADNAME *headp;
555 are user selectable.)
558 .Nm STAILQ_HEAD_INITIALIZER
559 evaluates to an initializer for the tail queue
564 concatenates the tail queue headed by
566 onto the end of the one headed by
568 removing all entries from the former.
572 evaluates to true if there are no items on the tail queue.
576 declares a structure that connects the elements in
581 returns the first item on the tail queue or NULL if the tail queue
586 traverses the tail queue referenced by
588 in the forward direction, assigning each element
593 .Nm STAILQ_FOREACH_FROM
594 behaves identically to
598 is NULL, else it treats
600 as a previously found STAILQ element and begins the loop at
602 instead of the first element in the STAILQ referenced by
606 .Nm STAILQ_FOREACH_SAFE
607 traverses the tail queue referenced by
609 in the forward direction, assigning each element
614 here it is permitted to both remove
616 as well as free it from within the loop safely without interfering with the
620 .Nm STAILQ_FOREACH_FROM_SAFE
621 behaves identically to
622 .Nm STAILQ_FOREACH_SAFE
625 is NULL, else it treats
627 as a previously found STAILQ element and begins the loop at
629 instead of the first element in the STAILQ referenced by
634 initializes the tail queue referenced by
638 .Nm STAILQ_INSERT_HEAD
639 inserts the new element
641 at the head of the tail queue.
644 .Nm STAILQ_INSERT_TAIL
645 inserts the new element
647 at the end of the tail queue.
650 .Nm STAILQ_INSERT_AFTER
651 inserts the new element
658 returns the last item on the tail queue.
659 If the tail queue is empty the return value is
664 returns the next item on the tail queue, or NULL this item is the last.
667 .Nm STAILQ_REMOVE_AFTER
668 removes the element after
670 from the tail queue. Unlike
672 this macro does not traverse the entire tail queue.
675 .Nm STAILQ_REMOVE_HEAD
676 removes the element at the head of the tail queue.
677 For optimum efficiency,
678 elements being removed from the head of the tail queue should
679 use this macro explicitly rather than the generic
691 swaps the contents of
695 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
697 STAILQ_HEAD(stailhead, entry) head =
698 STAILQ_HEAD_INITIALIZER(head);
699 struct stailhead *headp; /* Singly-linked tail queue head. */
702 STAILQ_ENTRY(entry) entries; /* Tail queue. */
704 } *n1, *n2, *n3, *np;
706 STAILQ_INIT(&head); /* Initialize the queue. */
708 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
709 STAILQ_INSERT_HEAD(&head, n1, entries);
711 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
712 STAILQ_INSERT_TAIL(&head, n1, entries);
714 n2 = malloc(sizeof(struct entry)); /* Insert after. */
715 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
717 STAILQ_REMOVE(&head, n2, entry, entries);
719 /* Deletion from the head. */
720 n3 = STAILQ_FIRST(&head);
721 STAILQ_REMOVE_HEAD(&head, entries);
723 /* Forward traversal. */
724 STAILQ_FOREACH(np, &head, entries)
726 /* Safe forward traversal. */
727 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
730 STAILQ_REMOVE(&head, np, entry, entries);
733 /* TailQ Deletion. */
734 while (!STAILQ_EMPTY(&head)) {
735 n1 = STAILQ_FIRST(&head);
736 STAILQ_REMOVE_HEAD(&head, entries);
739 /* Faster TailQ Deletion. */
740 n1 = STAILQ_FIRST(&head);
742 n2 = STAILQ_NEXT(n1, entries);
749 A list is headed by a structure defined by the
752 This structure contains a single pointer to the first element
754 The elements are doubly linked so that an arbitrary element can be
755 removed without traversing the list.
756 New elements can be added to the list after an existing element,
757 before an existing element, or at the head of the list.
760 structure is declared as follows:
761 .Bd -literal -offset indent
762 LIST_HEAD(HEADNAME, TYPE) head;
767 is the name of the structure to be defined, and
769 is the type of the elements to be linked into the list.
770 A pointer to the head of the list can later be declared as:
771 .Bd -literal -offset indent
772 struct HEADNAME *headp;
779 are user selectable.)
782 .Nm LIST_HEAD_INITIALIZER
783 evaluates to an initializer for the list
788 evaluates to true if there are no elements in the list.
792 declares a structure that connects the elements in
797 returns the first element in the list or NULL if the list
802 traverses the list referenced by
804 in the forward direction, assigning each element in turn to
808 .Nm LIST_FOREACH_FROM
809 behaves identically to
813 is NULL, else it treats
815 as a previously found LIST element and begins the loop at
817 instead of the first element in the LIST referenced by
821 .Nm LIST_FOREACH_SAFE
822 traverses the list referenced by
824 in the forward direction, assigning each element in turn to
828 here it is permitted to both remove
830 as well as free it from within the loop safely without interfering with the
834 .Nm LIST_FOREACH_FROM_SAFE
835 behaves identically to
836 .Nm LIST_FOREACH_SAFE
839 is NULL, else it treats
841 as a previously found LIST element and begins the loop at
843 instead of the first element in the LIST referenced by
848 initializes the list referenced by
853 inserts the new element
855 at the head of the list.
858 .Nm LIST_INSERT_AFTER
859 inserts the new element
865 .Nm LIST_INSERT_BEFORE
866 inserts the new element
873 returns the next element in the list, or NULL if this is the last.
877 returns the previous element in the list, or NULL if this is the first.
891 swaps the contents of
897 LIST_HEAD(listhead, entry) head =
898 LIST_HEAD_INITIALIZER(head);
899 struct listhead *headp; /* List head. */
902 LIST_ENTRY(entry) entries; /* List. */
904 } *n1, *n2, *n3, *np, *np_temp;
906 LIST_INIT(&head); /* Initialize the list. */
908 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
909 LIST_INSERT_HEAD(&head, n1, entries);
911 n2 = malloc(sizeof(struct entry)); /* Insert after. */
912 LIST_INSERT_AFTER(n1, n2, entries);
914 n3 = malloc(sizeof(struct entry)); /* Insert before. */
915 LIST_INSERT_BEFORE(n2, n3, entries);
917 LIST_REMOVE(n2, entries); /* Deletion. */
919 /* Forward traversal. */
920 LIST_FOREACH(np, &head, entries)
923 /* Safe forward traversal. */
924 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
927 LIST_REMOVE(np, entries);
931 while (!LIST_EMPTY(&head)) { /* List Deletion. */
932 n1 = LIST_FIRST(&head);
933 LIST_REMOVE(n1, entries);
937 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
939 n2 = LIST_NEXT(n1, entries);
946 A tail queue is headed by a structure defined by the
949 This structure contains a pair of pointers,
950 one to the first element in the tail queue and the other to
951 the last element in the tail queue.
952 The elements are doubly linked so that an arbitrary element can be
953 removed without traversing the tail queue.
954 New elements can be added to the tail queue after an existing element,
955 before an existing element, at the head of the tail queue,
956 or at the end of the tail queue.
959 structure is declared as follows:
960 .Bd -literal -offset indent
961 TAILQ_HEAD(HEADNAME, TYPE) head;
966 is the name of the structure to be defined, and
968 is the type of the elements to be linked into the tail queue.
969 A pointer to the head of the tail queue can later be declared as:
970 .Bd -literal -offset indent
971 struct HEADNAME *headp;
978 are user selectable.)
981 .Nm TAILQ_HEAD_INITIALIZER
982 evaluates to an initializer for the tail queue
987 concatenates the tail queue headed by
989 onto the end of the one headed by
991 removing all entries from the former.
995 evaluates to true if there are no items on the tail queue.
999 declares a structure that connects the elements in
1004 returns the first item on the tail queue or NULL if the tail queue
1009 traverses the tail queue referenced by
1011 in the forward direction, assigning each element in turn to
1016 if the loop completes normally, or if there were no elements.
1019 .Nm TAILQ_FOREACH_FROM
1020 behaves identically to
1024 is NULL, else it treats
1026 as a previously found TAILQ element and begins the loop at
1028 instead of the first element in the TAILQ referenced by
1032 .Nm TAILQ_FOREACH_REVERSE
1033 traverses the tail queue referenced by
1035 in the reverse direction, assigning each element in turn to
1039 .Nm TAILQ_FOREACH_REVERSE_FROM
1040 behaves identically to
1041 .Nm TAILQ_FOREACH_REVERSE
1044 is NULL, else it treats
1046 as a previously found TAILQ element and begins the reverse loop at
1048 instead of the last element in the TAILQ referenced by
1052 .Nm TAILQ_FOREACH_SAFE
1054 .Nm TAILQ_FOREACH_REVERSE_SAFE
1055 traverse the list referenced by
1057 in the forward or reverse direction respectively,
1058 assigning each element in turn to
1060 However, unlike their unsafe counterparts,
1063 .Nm TAILQ_FOREACH_REVERSE
1064 permit to both remove
1066 as well as free it from within the loop safely without interfering with the
1070 .Nm TAILQ_FOREACH_FROM_SAFE
1071 behaves identically to
1072 .Nm TAILQ_FOREACH_SAFE
1075 is NULL, else it treats
1077 as a previously found TAILQ element and begins the loop at
1079 instead of the first element in the TAILQ referenced by
1083 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1084 behaves identically to
1085 .Nm TAILQ_FOREACH_REVERSE_SAFE
1088 is NULL, else it treats
1090 as a previously found TAILQ element and begins the reverse loop at
1092 instead of the last element in the TAILQ referenced by
1097 initializes the tail queue referenced by
1101 .Nm TAILQ_INSERT_HEAD
1102 inserts the new element
1104 at the head of the tail queue.
1107 .Nm TAILQ_INSERT_TAIL
1108 inserts the new element
1110 at the end of the tail queue.
1113 .Nm TAILQ_INSERT_AFTER
1114 inserts the new element
1120 .Nm TAILQ_INSERT_BEFORE
1121 inserts the new element
1128 returns the last item on the tail queue.
1129 If the tail queue is empty the return value is
1134 returns the next item on the tail queue, or NULL if this item is the last.
1138 returns the previous item on the tail queue, or NULL if this item
1145 from the tail queue.
1149 swaps the contents of
1153 .Sh TAIL QUEUE EXAMPLE
1155 TAILQ_HEAD(tailhead, entry) head =
1156 TAILQ_HEAD_INITIALIZER(head);
1157 struct tailhead *headp; /* Tail queue head. */
1160 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1162 } *n1, *n2, *n3, *np;
1164 TAILQ_INIT(&head); /* Initialize the queue. */
1166 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1167 TAILQ_INSERT_HEAD(&head, n1, entries);
1169 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1170 TAILQ_INSERT_TAIL(&head, n1, entries);
1172 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1173 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1175 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1176 TAILQ_INSERT_BEFORE(n2, n3, entries);
1178 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1180 /* Forward traversal. */
1181 TAILQ_FOREACH(np, &head, entries)
1183 /* Safe forward traversal. */
1184 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1187 TAILQ_REMOVE(&head, np, entries);
1190 /* Reverse traversal. */
1191 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1193 /* TailQ Deletion. */
1194 while (!TAILQ_EMPTY(&head)) {
1195 n1 = TAILQ_FIRST(&head);
1196 TAILQ_REMOVE(&head, n1, entries);
1199 /* Faster TailQ Deletion. */
1200 n1 = TAILQ_FIRST(&head);
1201 while (n1 != NULL) {
1202 n2 = TAILQ_NEXT(n1, entries);
1213 functions first appeared in