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
39 .Nm SLIST_CLASS_ENTRY ,
40 .Nm SLIST_CLASS_HEAD ,
46 .Nm SLIST_FOREACH_FROM ,
47 .Nm SLIST_FOREACH_FROM_SAFE ,
48 .Nm SLIST_FOREACH_SAFE ,
50 .Nm SLIST_HEAD_INITIALIZER ,
52 .Nm SLIST_INSERT_AFTER ,
53 .Nm SLIST_INSERT_HEAD ,
56 .Nm SLIST_REMOVE_AFTER ,
57 .Nm SLIST_REMOVE_HEAD ,
59 .Nm STAILQ_CLASS_ENTRY ,
60 .Nm STAILQ_CLASS_HEAD ,
66 .Nm STAILQ_FOREACH_FROM ,
67 .Nm STAILQ_FOREACH_FROM_SAFE ,
68 .Nm STAILQ_FOREACH_SAFE ,
70 .Nm STAILQ_HEAD_INITIALIZER ,
72 .Nm STAILQ_INSERT_AFTER ,
73 .Nm STAILQ_INSERT_HEAD ,
74 .Nm STAILQ_INSERT_TAIL ,
78 .Nm STAILQ_REMOVE_AFTER ,
79 .Nm STAILQ_REMOVE_HEAD ,
81 .Nm LIST_CLASS_ENTRY ,
88 .Nm LIST_FOREACH_FROM ,
89 .Nm LIST_FOREACH_FROM_SAFE ,
90 .Nm LIST_FOREACH_SAFE ,
92 .Nm LIST_HEAD_INITIALIZER ,
94 .Nm LIST_INSERT_AFTER ,
95 .Nm LIST_INSERT_BEFORE ,
96 .Nm LIST_INSERT_HEAD ,
101 .Nm TAILQ_CLASS_ENTRY ,
102 .Nm TAILQ_CLASS_HEAD ,
108 .Nm TAILQ_FOREACH_FROM ,
109 .Nm TAILQ_FOREACH_FROM_SAFE ,
110 .Nm TAILQ_FOREACH_REVERSE ,
111 .Nm TAILQ_FOREACH_REVERSE_FROM ,
112 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
113 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
114 .Nm TAILQ_FOREACH_SAFE ,
116 .Nm TAILQ_HEAD_INITIALIZER ,
118 .Nm TAILQ_INSERT_AFTER ,
119 .Nm TAILQ_INSERT_BEFORE ,
120 .Nm TAILQ_INSERT_HEAD ,
121 .Nm TAILQ_INSERT_TAIL ,
127 .Nd implementations of singly-linked lists, singly-linked tail queues,
128 lists and tail queues
132 .Fn SLIST_CLASS_ENTRY "CLASSTYPE"
133 .Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
134 .Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME"
135 .Fn SLIST_EMPTY "SLIST_HEAD *head"
136 .Fn SLIST_ENTRY "TYPE"
137 .Fn SLIST_FIRST "SLIST_HEAD *head"
138 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
139 .Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
140 .Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
141 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
142 .Fn SLIST_HEAD "HEADNAME" "TYPE"
143 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
144 .Fn SLIST_INIT "SLIST_HEAD *head"
145 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
146 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
147 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
148 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
149 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
150 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
151 .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
153 .Fn STAILQ_CLASS_ENTRY "CLASSTYPE"
154 .Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
155 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
156 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
157 .Fn STAILQ_ENTRY "TYPE"
158 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
159 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
160 .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
161 .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
162 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
163 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
164 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
165 .Fn STAILQ_INIT "STAILQ_HEAD *head"
166 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
167 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
168 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
169 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
170 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
171 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
172 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
173 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
174 .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
176 .Fn LIST_CLASS_ENTRY "CLASSTYPE"
177 .Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
178 .Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
179 .Fn LIST_EMPTY "LIST_HEAD *head"
180 .Fn LIST_ENTRY "TYPE"
181 .Fn LIST_FIRST "LIST_HEAD *head"
182 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
183 .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
184 .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
185 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
186 .Fn LIST_HEAD "HEADNAME" "TYPE"
187 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
188 .Fn LIST_INIT "LIST_HEAD *head"
189 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
190 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
191 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
192 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
193 .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
194 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
195 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
197 .Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
198 .Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
199 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
200 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
201 .Fn TAILQ_ENTRY "TYPE"
202 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
203 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
204 .Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
205 .Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
206 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
207 .Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
208 .Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
209 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
210 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
211 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
212 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
213 .Fn TAILQ_INIT "TAILQ_HEAD *head"
214 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
215 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
216 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
217 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
218 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
219 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
220 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
221 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
222 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
225 These macros define and operate on four types of data structures which
226 can be used in both C and C++ source code:
227 .Bl -enum -compact -offset indent
233 Singly-linked tail queues
237 All four structures support the following functionality:
238 .Bl -enum -compact -offset indent
240 Insertion of a new entry at the head of the list.
242 Insertion of a new entry after any element in the list.
244 O(1) removal of an entry from the head of the list.
246 Forward traversal through the list.
248 Swapping the contents of two lists.
251 Singly-linked lists are the simplest of the four data structures
252 and support only the above functionality.
253 Singly-linked lists are ideal for applications with large datasets
254 and few or no removals,
255 or for implementing a LIFO queue.
256 Singly-linked lists add the following functionality:
257 .Bl -enum -compact -offset indent
259 O(n) removal of any entry in the list.
261 O(n) concatenation of two lists.
264 Singly-linked tail queues add the following functionality:
265 .Bl -enum -compact -offset indent
267 Entries can be added at the end of a list.
269 O(n) removal of any entry in the list.
271 They may be concatenated.
274 .Bl -enum -compact -offset indent
276 All list insertions must specify the head of the list.
278 Each head entry requires two pointers rather than one.
280 Code size is about 15% greater and operations run about 20% slower
281 than singly-linked lists.
284 Singly-linked tail queues are ideal for applications with large datasets and
286 or for implementing a FIFO queue.
288 All doubly linked types of data structures (lists and tail queues)
290 .Bl -enum -compact -offset indent
292 Insertion of a new entry before any element in the list.
294 O(1) removal of any entry in the list.
297 .Bl -enum -compact -offset indent
299 Each element requires two pointers rather than one.
301 Code size and execution time of operations (except for removal) is about
302 twice that of the singly-linked data-structures.
305 Linked lists are the simplest of the doubly linked data structures.
306 They add the following functionality over the above:
307 .Bl -enum -compact -offset indent
309 O(n) concatenation of two lists.
311 They may be traversed backwards.
314 .Bl -enum -compact -offset indent
316 To traverse backwards, an entry to begin the traversal and the list in
317 which it is contained must be specified.
320 Tail queues add the following functionality:
321 .Bl -enum -compact -offset indent
323 Entries can be added at the end of a list.
325 They may be traversed backwards, from tail to head.
327 They may be concatenated.
330 .Bl -enum -compact -offset indent
332 All list insertions and removals must specify the head of the list.
334 Each head entry requires two pointers rather than one.
336 Code size is about 15% greater and operations run about 20% slower
337 than singly-linked lists.
340 In the macro definitions,
342 is the name of a user defined structure.
343 The structure must contain a field called
351 In the macro definitions,
353 is the name of a user defined class.
354 The class must contain a field called
357 .Li SLIST_CLASS_ENTRY ,
358 .Li STAILQ_CLASS_ENTRY ,
359 .Li LIST_CLASS_ENTRY ,
361 .Li TAILQ_CLASS_ENTRY .
364 is the name of a user defined structure that must be declared
367 .Li SLIST_CLASS_HEAD ,
369 .Li STAILQ_CLASS_HEAD ,
371 .Li LIST_CLASS_HEAD ,
374 .Li TAILQ_CLASS_HEAD .
375 See the examples below for further explanation of how these
377 .Sh SINGLY-LINKED LISTS
378 A singly-linked list is headed by a structure defined by the
381 This structure contains a single pointer to the first element
383 The elements are singly linked for minimum space and pointer manipulation
384 overhead at the expense of O(n) removal for arbitrary elements.
385 New elements can be added to the list after an existing element or
386 at the head of the list.
389 structure is declared as follows:
390 .Bd -literal -offset indent
391 SLIST_HEAD(HEADNAME, TYPE) head;
396 is the name of the structure to be defined, and
398 is the type of the elements to be linked into the list.
399 A pointer to the head of the list can later be declared as:
400 .Bd -literal -offset indent
401 struct HEADNAME *headp;
408 are user selectable.)
411 .Nm SLIST_HEAD_INITIALIZER
412 evaluates to an initializer for the list
417 concatenates the list headed by
419 onto the end of the one headed by
421 removing all entries from the former.
422 Use of this macro should be avoided as it traverses the entirety of the
425 A singly-linked tail queue should be used if this macro is needed in
426 high-usage code paths or to operate on long lists.
430 evaluates to true if there are no elements in the list.
434 declares a structure that connects the elements in
439 returns the first element in the list or NULL if the list is empty.
443 traverses the list referenced by
445 in the forward direction, assigning each element in
450 .Nm SLIST_FOREACH_FROM
451 behaves identically to
455 is NULL, else it treats
457 as a previously found SLIST element and begins the loop at
459 instead of the first element in the SLIST referenced by
463 .Nm SLIST_FOREACH_SAFE
464 traverses the list referenced by
466 in the forward direction, assigning each element in
471 here it is permitted to both remove
473 as well as free it from within the loop safely without interfering with the
477 .Nm SLIST_FOREACH_FROM_SAFE
478 behaves identically to
479 .Nm SLIST_FOREACH_SAFE
482 is NULL, else it treats
484 as a previously found SLIST element and begins the loop at
486 instead of the first element in the SLIST referenced by
491 initializes the list referenced by
495 .Nm SLIST_INSERT_HEAD
496 inserts the new element
498 at the head of the list.
501 .Nm SLIST_INSERT_AFTER
502 inserts the new element
509 returns the next element in the list.
512 .Nm SLIST_REMOVE_AFTER
513 removes the element after
518 this macro does not traverse the entire list.
521 .Nm SLIST_REMOVE_HEAD
524 from the head of the list.
525 For optimum efficiency,
526 elements being removed from the head of the list should explicitly use
527 this macro instead of the generic
536 Use of this macro should be avoided as it traverses the entire list.
537 A doubly-linked list should be used if this macro is needed in
538 high-usage code paths or to operate on long lists.
542 swaps the contents of
546 .Sh SINGLY-LINKED LIST EXAMPLE
548 SLIST_HEAD(slisthead, entry) head =
549 SLIST_HEAD_INITIALIZER(head);
550 struct slisthead *headp; /* Singly-linked List head. */
553 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
555 } *n1, *n2, *n3, *np;
557 SLIST_INIT(&head); /* Initialize the list. */
559 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
560 SLIST_INSERT_HEAD(&head, n1, entries);
562 n2 = malloc(sizeof(struct entry)); /* Insert after. */
563 SLIST_INSERT_AFTER(n1, n2, entries);
565 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
568 n3 = SLIST_FIRST(&head);
569 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
571 /* Forward traversal. */
572 SLIST_FOREACH(np, &head, entries)
574 /* Safe forward traversal. */
575 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
578 SLIST_REMOVE(&head, np, entry, entries);
582 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
583 n1 = SLIST_FIRST(&head);
584 SLIST_REMOVE_HEAD(&head, entries);
588 .Sh SINGLY-LINKED TAIL QUEUES
589 A singly-linked tail queue is headed by a structure defined by the
592 This structure contains a pair of pointers,
593 one to the first element in the tail queue and the other to
594 the last element in the tail queue.
595 The elements are singly linked for minimum space and pointer
596 manipulation overhead at the expense of O(n) removal for arbitrary
598 New elements can be added to the tail queue after an existing element,
599 at the head of the tail queue, or at the end of the tail queue.
602 structure is declared as follows:
603 .Bd -literal -offset indent
604 STAILQ_HEAD(HEADNAME, TYPE) head;
609 is the name of the structure to be defined, and
611 is the type of the elements to be linked into the tail queue.
612 A pointer to the head of the tail queue can later be declared as:
613 .Bd -literal -offset indent
614 struct HEADNAME *headp;
621 are user selectable.)
624 .Nm STAILQ_HEAD_INITIALIZER
625 evaluates to an initializer for the tail queue
630 concatenates the tail queue headed by
632 onto the end of the one headed by
634 removing all entries from the former.
638 evaluates to true if there are no items on the tail queue.
642 declares a structure that connects the elements in
647 returns the first item on the tail queue or NULL if the tail queue
652 traverses the tail queue referenced by
654 in the forward direction, assigning each element
659 .Nm STAILQ_FOREACH_FROM
660 behaves identically to
664 is NULL, else it treats
666 as a previously found STAILQ element and begins the loop at
668 instead of the first element in the STAILQ referenced by
672 .Nm STAILQ_FOREACH_SAFE
673 traverses the tail queue referenced by
675 in the forward direction, assigning each element
680 here it is permitted to both remove
682 as well as free it from within the loop safely without interfering with the
686 .Nm STAILQ_FOREACH_FROM_SAFE
687 behaves identically to
688 .Nm STAILQ_FOREACH_SAFE
691 is NULL, else it treats
693 as a previously found STAILQ element and begins the loop at
695 instead of the first element in the STAILQ referenced by
700 initializes the tail queue referenced by
704 .Nm STAILQ_INSERT_HEAD
705 inserts the new element
707 at the head of the tail queue.
710 .Nm STAILQ_INSERT_TAIL
711 inserts the new element
713 at the end of the tail queue.
716 .Nm STAILQ_INSERT_AFTER
717 inserts the new element
724 returns the last item on the tail queue.
725 If the tail queue is empty the return value is
730 returns the next item on the tail queue, or NULL this item is the last.
733 .Nm STAILQ_REMOVE_AFTER
734 removes the element after
739 this macro does not traverse the entire tail queue.
742 .Nm STAILQ_REMOVE_HEAD
743 removes the element at the head of the tail queue.
744 For optimum efficiency,
745 elements being removed from the head of the tail queue should
746 use this macro explicitly rather than the generic
755 Use of this macro should be avoided as it traverses the entire list.
756 A doubly-linked tail queue should be used if this macro is needed in
757 high-usage code paths or to operate on long tail queues.
761 swaps the contents of
765 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
767 STAILQ_HEAD(stailhead, entry) head =
768 STAILQ_HEAD_INITIALIZER(head);
769 struct stailhead *headp; /* Singly-linked tail queue head. */
772 STAILQ_ENTRY(entry) entries; /* Tail queue. */
774 } *n1, *n2, *n3, *np;
776 STAILQ_INIT(&head); /* Initialize the queue. */
778 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
779 STAILQ_INSERT_HEAD(&head, n1, entries);
781 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
782 STAILQ_INSERT_TAIL(&head, n1, entries);
784 n2 = malloc(sizeof(struct entry)); /* Insert after. */
785 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
787 STAILQ_REMOVE(&head, n2, entry, entries);
789 /* Deletion from the head. */
790 n3 = STAILQ_FIRST(&head);
791 STAILQ_REMOVE_HEAD(&head, entries);
793 /* Forward traversal. */
794 STAILQ_FOREACH(np, &head, entries)
796 /* Safe forward traversal. */
797 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
800 STAILQ_REMOVE(&head, np, entry, entries);
803 /* TailQ Deletion. */
804 while (!STAILQ_EMPTY(&head)) {
805 n1 = STAILQ_FIRST(&head);
806 STAILQ_REMOVE_HEAD(&head, entries);
809 /* Faster TailQ Deletion. */
810 n1 = STAILQ_FIRST(&head);
812 n2 = STAILQ_NEXT(n1, entries);
819 A list is headed by a structure defined by the
822 This structure contains a single pointer to the first element
824 The elements are doubly linked so that an arbitrary element can be
825 removed without traversing the list.
826 New elements can be added to the list after an existing element,
827 before an existing element, or at the head of the list.
830 structure is declared as follows:
831 .Bd -literal -offset indent
832 LIST_HEAD(HEADNAME, TYPE) head;
837 is the name of the structure to be defined, and
839 is the type of the elements to be linked into the list.
840 A pointer to the head of the list can later be declared as:
841 .Bd -literal -offset indent
842 struct HEADNAME *headp;
849 are user selectable.)
852 .Nm LIST_HEAD_INITIALIZER
853 evaluates to an initializer for the list
858 concatenates the list headed by
860 onto the end of the one headed by
862 removing all entries from the former.
863 Use of this macro should be avoided as it traverses the entirety of the
866 A tail queue should be used if this macro is needed in
867 high-usage code paths or to operate on long lists.
871 evaluates to true if there are no elements in the list.
875 declares a structure that connects the elements in
880 returns the first element in the list or NULL if the list
885 traverses the list referenced by
887 in the forward direction, assigning each element in turn to
891 .Nm LIST_FOREACH_FROM
892 behaves identically to
896 is NULL, else it treats
898 as a previously found LIST element and begins the loop at
900 instead of the first element in the LIST referenced by
904 .Nm LIST_FOREACH_SAFE
905 traverses the list referenced by
907 in the forward direction, assigning each element in turn to
911 here it is permitted to both remove
913 as well as free it from within the loop safely without interfering with the
917 .Nm LIST_FOREACH_FROM_SAFE
918 behaves identically to
919 .Nm LIST_FOREACH_SAFE
922 is NULL, else it treats
924 as a previously found LIST element and begins the loop at
926 instead of the first element in the LIST referenced by
931 initializes the list referenced by
936 inserts the new element
938 at the head of the list.
941 .Nm LIST_INSERT_AFTER
942 inserts the new element
948 .Nm LIST_INSERT_BEFORE
949 inserts the new element
956 returns the next element in the list, or NULL if this is the last.
960 returns the previous element in the list, or NULL if this is the first.
974 swaps the contents of
980 LIST_HEAD(listhead, entry) head =
981 LIST_HEAD_INITIALIZER(head);
982 struct listhead *headp; /* List head. */
985 LIST_ENTRY(entry) entries; /* List. */
987 } *n1, *n2, *n3, *np, *np_temp;
989 LIST_INIT(&head); /* Initialize the list. */
991 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
992 LIST_INSERT_HEAD(&head, n1, entries);
994 n2 = malloc(sizeof(struct entry)); /* Insert after. */
995 LIST_INSERT_AFTER(n1, n2, entries);
997 n3 = malloc(sizeof(struct entry)); /* Insert before. */
998 LIST_INSERT_BEFORE(n2, n3, entries);
1000 LIST_REMOVE(n2, entries); /* Deletion. */
1002 /* Forward traversal. */
1003 LIST_FOREACH(np, &head, entries)
1006 /* Safe forward traversal. */
1007 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
1010 LIST_REMOVE(np, entries);
1014 while (!LIST_EMPTY(&head)) { /* List Deletion. */
1015 n1 = LIST_FIRST(&head);
1016 LIST_REMOVE(n1, entries);
1020 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
1021 while (n1 != NULL) {
1022 n2 = LIST_NEXT(n1, entries);
1029 A tail queue is headed by a structure defined by the
1032 This structure contains a pair of pointers,
1033 one to the first element in the tail queue and the other to
1034 the last element in the tail queue.
1035 The elements are doubly linked so that an arbitrary element can be
1036 removed without traversing the tail queue.
1037 New elements can be added to the tail queue after an existing element,
1038 before an existing element, at the head of the tail queue,
1039 or at the end of the tail queue.
1042 structure is declared as follows:
1043 .Bd -literal -offset indent
1044 TAILQ_HEAD(HEADNAME, TYPE) head;
1049 is the name of the structure to be defined, and
1051 is the type of the elements to be linked into the tail queue.
1052 A pointer to the head of the tail queue can later be declared as:
1053 .Bd -literal -offset indent
1054 struct HEADNAME *headp;
1061 are user selectable.)
1064 .Nm TAILQ_HEAD_INITIALIZER
1065 evaluates to an initializer for the tail queue
1070 concatenates the tail queue headed by
1072 onto the end of the one headed by
1074 removing all entries from the former.
1078 evaluates to true if there are no items on the tail queue.
1082 declares a structure that connects the elements in
1087 returns the first item on the tail queue or NULL if the tail queue
1092 traverses the tail queue referenced by
1094 in the forward direction, assigning each element in turn to
1099 if the loop completes normally, or if there were no elements.
1102 .Nm TAILQ_FOREACH_FROM
1103 behaves identically to
1107 is NULL, else it treats
1109 as a previously found TAILQ element and begins the loop at
1111 instead of the first element in the TAILQ referenced by
1115 .Nm TAILQ_FOREACH_REVERSE
1116 traverses the tail queue referenced by
1118 in the reverse direction, assigning each element in turn to
1122 .Nm TAILQ_FOREACH_REVERSE_FROM
1123 behaves identically to
1124 .Nm TAILQ_FOREACH_REVERSE
1127 is NULL, else it treats
1129 as a previously found TAILQ element and begins the reverse loop at
1131 instead of the last element in the TAILQ referenced by
1135 .Nm TAILQ_FOREACH_SAFE
1137 .Nm TAILQ_FOREACH_REVERSE_SAFE
1138 traverse the list referenced by
1140 in the forward or reverse direction respectively,
1141 assigning each element in turn to
1143 However, unlike their unsafe counterparts,
1146 .Nm TAILQ_FOREACH_REVERSE
1147 permit to both remove
1149 as well as free it from within the loop safely without interfering with the
1153 .Nm TAILQ_FOREACH_FROM_SAFE
1154 behaves identically to
1155 .Nm TAILQ_FOREACH_SAFE
1158 is NULL, else it treats
1160 as a previously found TAILQ element and begins the loop at
1162 instead of the first element in the TAILQ referenced by
1166 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1167 behaves identically to
1168 .Nm TAILQ_FOREACH_REVERSE_SAFE
1171 is NULL, else it treats
1173 as a previously found TAILQ element and begins the reverse loop at
1175 instead of the last element in the TAILQ referenced by
1180 initializes the tail queue referenced by
1184 .Nm TAILQ_INSERT_HEAD
1185 inserts the new element
1187 at the head of the tail queue.
1190 .Nm TAILQ_INSERT_TAIL
1191 inserts the new element
1193 at the end of the tail queue.
1196 .Nm TAILQ_INSERT_AFTER
1197 inserts the new element
1203 .Nm TAILQ_INSERT_BEFORE
1204 inserts the new element
1211 returns the last item on the tail queue.
1212 If the tail queue is empty the return value is
1217 returns the next item on the tail queue, or NULL if this item is the last.
1221 returns the previous item on the tail queue, or NULL if this item
1228 from the tail queue.
1232 swaps the contents of
1236 .Sh TAIL QUEUE EXAMPLE
1238 TAILQ_HEAD(tailhead, entry) head =
1239 TAILQ_HEAD_INITIALIZER(head);
1240 struct tailhead *headp; /* Tail queue head. */
1243 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1245 } *n1, *n2, *n3, *np;
1247 TAILQ_INIT(&head); /* Initialize the queue. */
1249 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1250 TAILQ_INSERT_HEAD(&head, n1, entries);
1252 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1253 TAILQ_INSERT_TAIL(&head, n1, entries);
1255 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1256 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1258 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1259 TAILQ_INSERT_BEFORE(n2, n3, entries);
1261 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1263 /* Forward traversal. */
1264 TAILQ_FOREACH(np, &head, entries)
1266 /* Safe forward traversal. */
1267 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1270 TAILQ_REMOVE(&head, np, entries);
1273 /* Reverse traversal. */
1274 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1276 /* TailQ Deletion. */
1277 while (!TAILQ_EMPTY(&head)) {
1278 n1 = TAILQ_FIRST(&head);
1279 TAILQ_REMOVE(&head, n1, entries);
1282 /* Faster TailQ Deletion. */
1283 n1 = TAILQ_FIRST(&head);
1284 while (n1 != NULL) {
1285 n2 = TAILQ_NEXT(n1, entries);
1296 functions first appeared in