1 .\" $OpenBSD: queue.3,v 1.68 2020/12/30 13:33:38 millert Exp $
2 .\" $NetBSD: queue.3,v 1.4 1995/07/03 00:25:36 mycroft Exp $
4 .\" Copyright (c) 1993 The Regents of the University of California.
5 .\" All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. Neither the name of the University nor the names of its contributors
16 .\" may be used to endorse or promote products derived from this software
17 .\" without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" @(#)queue.3 8.1 (Berkeley) 12/13/93
33 .Dd $Mdocdate: December 30 2020 $
39 .Nm SLIST_HEAD_INITIALIZER ,
44 .Nm SLIST_FOREACH_SAFE ,
46 .Nm SLIST_INSERT_AFTER ,
47 .Nm SLIST_INSERT_HEAD ,
48 .Nm SLIST_REMOVE_AFTER ,
49 .Nm SLIST_REMOVE_HEAD ,
53 .Nm LIST_HEAD_INITIALIZER ,
58 .Nm LIST_FOREACH_SAFE ,
60 .Nm LIST_INSERT_AFTER ,
61 .Nm LIST_INSERT_BEFORE ,
62 .Nm LIST_INSERT_HEAD ,
67 .Nm SIMPLEQ_HEAD_INITIALIZER ,
72 .Nm SIMPLEQ_FOREACH_SAFE ,
74 .Nm SIMPLEQ_INSERT_AFTER ,
75 .Nm SIMPLEQ_INSERT_HEAD ,
76 .Nm SIMPLEQ_INSERT_TAIL ,
77 .Nm SIMPLEQ_REMOVE_AFTER ,
78 .Nm SIMPLEQ_REMOVE_HEAD ,
82 .Nm STAILQ_HEAD_INITIALIZER ,
88 .Nm STAILQ_FOREACH_SAFE ,
90 .Nm STAILQ_INSERT_AFTER ,
91 .Nm STAILQ_INSERT_HEAD ,
92 .Nm STAILQ_INSERT_TAIL ,
94 .Nm STAILQ_REMOVE_AFTER ,
95 .Nm STAILQ_REMOVE_HEAD ,
99 .Nm TAILQ_HEAD_INITIALIZER ,
106 .Nm TAILQ_FOREACH_SAFE ,
107 .Nm TAILQ_FOREACH_REVERSE ,
108 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
110 .Nm TAILQ_INSERT_AFTER ,
111 .Nm TAILQ_INSERT_BEFORE ,
112 .Nm TAILQ_INSERT_HEAD ,
113 .Nm TAILQ_INSERT_TAIL ,
117 .Nd intrusive singly-linked and doubly-linked lists, simple queues, singly-linked and doubly-linked tail queues
121 .Fn SLIST_ENTRY "TYPE"
122 .Fn SLIST_HEAD "HEADNAME" "TYPE"
123 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
125 .Fn SLIST_FIRST "SLIST_HEAD *head"
127 .Fn SLIST_NEXT "struct TYPE *listelm" "FIELDNAME"
129 .Fn SLIST_EMPTY "SLIST_HEAD *head"
130 .Fn SLIST_FOREACH "VARNAME" "SLIST_HEAD *head" "FIELDNAME"
131 .Fn SLIST_FOREACH_SAFE "VARNAME" "SLIST_HEAD *head" "FIELDNAME" "TEMP_VARNAME"
133 .Fn SLIST_INIT "SLIST_HEAD *head"
135 .Fn SLIST_INSERT_AFTER "struct TYPE *listelm" "struct TYPE *elm" "FIELDNAME"
137 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "struct TYPE *elm" "FIELDNAME"
139 .Fn SLIST_REMOVE_AFTER "struct TYPE *elm" "FIELDNAME"
141 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "FIELDNAME"
143 .Fn SLIST_REMOVE "SLIST_HEAD *head" "struct TYPE *elm" "TYPE" "FIELDNAME"
145 .Fn LIST_ENTRY "TYPE"
146 .Fn LIST_HEAD "HEADNAME" "TYPE"
147 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
149 .Fn LIST_FIRST "LIST_HEAD *head"
151 .Fn LIST_NEXT "struct TYPE *listelm" "FIELDNAME"
153 .Fn LIST_EMPTY "LIST_HEAD *head"
154 .Fn LIST_FOREACH "VARNAME" "LIST_HEAD *head" "FIELDNAME"
155 .Fn LIST_FOREACH_SAFE "VARNAME" "LIST_HEAD *head" "FIELDNAME" "TEMP_VARNAME"
157 .Fn LIST_INIT "LIST_HEAD *head"
159 .Fn LIST_INSERT_AFTER "struct TYPE *listelm" "struct TYPE *elm" "FIELDNAME"
161 .Fn LIST_INSERT_BEFORE "struct TYPE *listelm" "struct TYPE *elm" "FIELDNAME"
163 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "struct TYPE *elm" "FIELDNAME"
165 .Fn LIST_REMOVE "struct TYPE *elm" "FIELDNAME"
167 .Fn LIST_REPLACE "struct TYPE *elm" "struct TYPE *elm2" "FIELDNAME"
169 .Fn SIMPLEQ_ENTRY "TYPE"
170 .Fn SIMPLEQ_HEAD "HEADNAME" "TYPE"
171 .Fn SIMPLEQ_HEAD_INITIALIZER "SIMPLEQ_HEAD head"
173 .Fn SIMPLEQ_FIRST "SIMPLEQ_HEAD *head"
175 .Fn SIMPLEQ_NEXT "struct TYPE *listelm" "FIELDNAME"
177 .Fn SIMPLEQ_EMPTY "SIMPLEQ_HEAD *head"
178 .Fn SIMPLEQ_FOREACH "VARNAME" "SIMPLEQ_HEAD *head" "FIELDNAME"
179 .Fn SIMPLEQ_FOREACH_SAFE "VARNAME" "SIMPLEQ_HEAD *head" "FIELDNAME" "TEMP_VARNAME"
181 .Fn SIMPLEQ_INIT "SIMPLEQ_HEAD *head"
183 .Fn SIMPLEQ_INSERT_AFTER "SIMPLEQ_HEAD *head" "struct TYPE *listelm" "struct TYPE *elm" "FIELDNAME"
185 .Fn SIMPLEQ_INSERT_HEAD "SIMPLEQ_HEAD *head" "struct TYPE *elm" "FIELDNAME"
187 .Fn SIMPLEQ_INSERT_TAIL "SIMPLEQ_HEAD *head" "struct TYPE *elm" "FIELDNAME"
189 .Fn SIMPLEQ_REMOVE_AFTER "SIMPLEQ_HEAD *head" "struct TYPE *elm" "FIELDNAME"
191 .Fn SIMPLEQ_REMOVE_HEAD "SIMPLEQ_HEAD *head" "FIELDNAME"
192 .Fn SIMPLEQ_CONCAT "SIMPLEQ_HEAD *head1" "SIMPLEQ_HEAD *head2"
194 .Fn STAILQ_ENTRY "TYPE"
195 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
196 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
197 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
198 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
199 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
200 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
201 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
202 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
203 .Fn STAILQ_INIT "STAILQ_HEAD *head"
204 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
205 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
206 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
207 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
208 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
209 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
210 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
212 .Fn TAILQ_ENTRY "TYPE"
213 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
214 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
216 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
218 .Fn TAILQ_NEXT "struct TYPE *listelm" "FIELDNAME"
220 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
222 .Fn TAILQ_PREV "struct TYPE *listelm" "HEADNAME" "FIELDNAME"
224 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
225 .Fn TAILQ_FOREACH "VARNAME" "TAILQ_HEAD *head" "FIELDNAME"
226 .Fn TAILQ_FOREACH_SAFE "VARNAME" "TAILQ_HEAD *head" "FIELDNAME" "TEMP_VARNAME"
227 .Fn TAILQ_FOREACH_REVERSE "VARNAME" "TAILQ_HEAD *head" "HEADNAME" "FIELDNAME"
228 .Fn TAILQ_FOREACH_REVERSE_SAFE "VARNAME" "TAILQ_HEAD *head" "HEADNAME" "FIELDNAME" "TEMP_VARNAME"
230 .Fn TAILQ_INIT "TAILQ_HEAD *head"
232 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "struct TYPE *listelm" "struct TYPE *elm" "FIELDNAME"
234 .Fn TAILQ_INSERT_BEFORE "struct TYPE *listelm" "struct TYPE *elm" "FIELDNAME"
236 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "struct TYPE *elm" "FIELDNAME"
238 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "struct TYPE *elm" "FIELDNAME"
240 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "struct TYPE *elm" "FIELDNAME"
242 .Fn TAILQ_REPLACE "TAILQ_HEAD *head" "struct TYPE *elm" "struct TYPE *elm2" "FIELDNAME"
243 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "FIELDNAME"
245 These macros define and operate on five types of data structures:
246 singly-linked lists, simple queues, lists, singly-linked tail queues,
248 All five structures support the following functionality:
250 .Bl -enum -compact -offset indent
252 Insertion of a new entry at the head of the list.
254 Insertion of a new entry after any element in the list.
256 Removal of an entry from the head of the list.
258 Forward traversal through the list.
261 The following table provides a quick overview
262 of which types support which additional macros:
263 .Bl -column -offset 6n "LAST, PREV, FOREACH_REVERSE" SLIST LIST SIMPLEQ STAILQ TAILQ
264 .It LAST, PREV, FOREACH_REVERSE Ta - Ta - Ta - Ta - Ta TAILQ
265 .It INSERT_BEFORE, REPLACE Ta - Ta LIST Ta - Ta - Ta TAILQ
266 .It INSERT_TAIL, CONCAT Ta - Ta - Ta SIMPLEQ Ta STAILQ Ta TAILQ
267 .It REMOVE_AFTER, REMOVE_HEAD Ta SLIST Ta - Ta SIMPLEQ Ta STAILQ Ta -
268 .It REMOVE Ta SLIST Ta LIST Ta - Ta STAILQ Ta TAILQ
271 Singly-linked lists are the simplest of the five data structures
272 and support only the above functionality.
273 Singly-linked lists are ideal for applications with large datasets
274 and few or no removals, or for implementing a LIFO queue.
276 Simple queues and singly-linked tail queues add the following functionality:
278 .Bl -enum -compact -offset indent
280 Entries can be added at the end of a list.
285 .Bl -enum -compact -offset indent
287 All list insertions must specify the head of the list.
289 Each head entry requires two pointers rather than one.
291 Code size is about 15% greater and operations run about 20% slower
292 than singly-linked lists.
295 Simple queues and singly-linked tail queues are ideal for applications with
296 large datasets and few or no removals, or for implementing a FIFO queue.
298 All doubly linked types of data structures (lists and tail queues)
301 .Bl -enum -compact -offset indent
303 Insertion of a new entry before any element in the list.
305 Removal of any entry in the list.
310 .Bl -enum -compact -offset indent
312 Each element requires two pointers rather than one.
314 Code size and execution time of operations (except for removal) is about
315 twice that of the singly-linked data-structures.
318 Lists are the simplest of the doubly linked data structures and support
319 only the above functionality over singly-linked lists.
321 Tail queues add the following functionality:
323 .Bl -enum -compact -offset indent
325 Entries can be added at the end of a list.
327 They may be traversed backwards, at a cost.
332 .Bl -enum -compact -offset indent
334 All list insertions and removals must specify the head of the list.
336 Each head entry requires two pointers rather than one.
338 Code size is about 15% greater and operations run about 20% slower
339 than singly-linked lists.
342 An additional type of data structure, circular queues, violated the C
343 language aliasing rules and were miscompiled as a result.
344 All code using them should be converted to another structure;
345 tail queues are usually the easiest to convert to.
347 All these lists and queues are intrusive: they link together user
348 defined structures containing a field of type
355 In the macro definitions,
357 is the name tag of the user defined structure and
362 If an instance of the user defined structure needs to be a member of
363 multiple lists at the same time, the structure requires multiple
365 fields, one for each list.
369 is the name tag of a user defined structure that must be declared
377 See the examples below for further explanation of how these macros are used.
378 .Sh SINGLY-LINKED LISTS
379 A singly-linked list is headed by a structure defined by the
382 This structure contains a single pointer to the first element on the list.
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 struct
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.)
412 facility is often not used, leading to the following bizarre code:
413 .Bd -literal -offset indent
414 SLIST_HEAD(, TYPE) head, *headp;
419 macro declares a structure that connects the elements in the list.
423 macro initializes the list referenced by
426 The list can also be initialized statically by using the
427 .Fn SLIST_HEAD_INITIALIZER
429 .Bd -literal -offset indent
430 SLIST_HEAD(HEADNAME, TYPE) head = SLIST_HEAD_INITIALIZER(head);
434 .Fn SLIST_INSERT_HEAD
435 macro inserts the new element
437 at the head of the list.
440 .Fn SLIST_INSERT_AFTER
441 macro inserts the new element
447 .Fn SLIST_REMOVE_HEAD
448 macro removes the first element of the list pointed by
452 .Fn SLIST_REMOVE_AFTER
453 macro removes the list element immediately following
458 macro removes the element
460 of the list pointed by
467 macros can be used to traverse the list:
468 .Bd -literal -offset indent
469 for (np = SLIST_FIRST(&head); np != NULL; np = SLIST_NEXT(np, FIELDNAME))
472 Or, for simplicity, one can use the
475 .Bd -literal -offset indent
476 SLIST_FOREACH(np, head, FIELDNAME)
480 .Fn SLIST_FOREACH_SAFE
481 traverses the list referenced by head in a
482 forward direction, assigning each element in turn to var.
485 it is permitted to remove var as well
486 as free it from within the loop safely without interfering with the traversal.
490 macro should be used to check whether a simple list is empty.
491 .Sh SINGLY-LINKED LIST EXAMPLE
493 SLIST_HEAD(listhead, entry) head;
496 SLIST_ENTRY(entry) entries; /* Simple list. */
500 SLIST_INIT(&head); /* Initialize simple list. */
502 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
503 SLIST_INSERT_HEAD(&head, n1, entries);
505 n2 = malloc(sizeof(struct entry)); /* Insert after. */
506 SLIST_INSERT_AFTER(n1, n2, entries);
508 SLIST_FOREACH(np, &head, entries) /* Forward traversal. */
511 while (!SLIST_EMPTY(&head)) { /* Delete. */
512 n1 = SLIST_FIRST(&head);
513 SLIST_REMOVE_HEAD(&head, entries);
519 A list is headed by a structure defined by the
522 This structure contains a single pointer to the first element on the list.
523 The elements are doubly linked so that an arbitrary element can be
524 removed without traversing the list.
525 New elements can be added to the list after an existing element,
526 before an existing element, or at the head of the list.
529 structure is declared as follows:
530 .Bd -literal -offset indent
531 LIST_HEAD(HEADNAME, TYPE) head;
536 is the name of the structure to be defined, and struct
538 is the type of the elements to be linked into the list.
539 A pointer to the head of the list can later be declared as:
540 .Bd -literal -offset indent
541 struct HEADNAME *headp;
548 are user selectable.)
552 facility is often not used, leading to the following bizarre code:
553 .Bd -literal -offset indent
554 LIST_HEAD(, TYPE) head, *headp;
559 macro declares a structure that connects the elements in the list.
563 macro initializes the list referenced by
566 The list can also be initialized statically by using the
567 .Fn LIST_HEAD_INITIALIZER
569 .Bd -literal -offset indent
570 LIST_HEAD(HEADNAME, TYPE) head = LIST_HEAD_INITIALIZER(head);
575 macro inserts the new element
577 at the head of the list.
580 .Fn LIST_INSERT_AFTER
581 macro inserts the new element
587 .Fn LIST_INSERT_BEFORE
588 macro inserts the new element
595 macro removes the element
601 macro replaces the list element
610 macros can be used to traverse the list:
611 .Bd -literal -offset indent
612 for (np = LIST_FIRST(&head); np != NULL; np = LIST_NEXT(np, FIELDNAME))
615 Or, for simplicity, one can use the
618 .Bd -literal -offset indent
619 LIST_FOREACH(np, head, FIELDNAME)
623 .Fn LIST_FOREACH_SAFE
624 traverses the list referenced by head in a
625 forward direction, assigning each element in turn to var.
628 it is permitted to remove var as well
629 as free it from within the loop safely without interfering with the traversal.
633 macro should be used to check whether a list is empty.
636 LIST_HEAD(listhead, entry) head;
639 LIST_ENTRY(entry) entries; /* List. */
643 LIST_INIT(&head); /* Initialize list. */
645 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
646 LIST_INSERT_HEAD(&head, n1, entries);
648 n2 = malloc(sizeof(struct entry)); /* Insert after. */
649 LIST_INSERT_AFTER(n1, n2, entries);
651 n2 = malloc(sizeof(struct entry)); /* Insert before. */
652 LIST_INSERT_BEFORE(n1, n2, entries);
653 /* Forward traversal. */
654 LIST_FOREACH(np, &head, entries)
657 while (!LIST_EMPTY(&head)) { /* Delete. */
658 n1 = LIST_FIRST(&head);
659 LIST_REMOVE(n1, entries);
664 A simple queue is headed by a structure defined by the
667 This structure contains a pair of pointers, one to the first element in the
668 simple queue and the other to the last element in the simple queue.
669 The elements are singly linked.
670 New elements can be added to the queue after an existing element,
671 at the head of the queue or at the tail of the queue.
674 structure is declared as follows:
675 .Bd -literal -offset indent
676 SIMPLEQ_HEAD(HEADNAME, TYPE) head;
681 is the name of the structure to be defined, and struct
683 is the type of the elements to be linked into the queue.
684 A pointer to the head of the queue can later be declared as:
685 .Bd -literal -offset indent
686 struct HEADNAME *headp;
693 are user selectable.)
697 macro declares a structure that connects the elements in
702 macro initializes the queue referenced by
705 The queue can also be initialized statically by using the
706 .Fn SIMPLEQ_HEAD_INITIALIZER
708 .Bd -literal -offset indent
709 SIMPLEQ_HEAD(HEADNAME, TYPE) head = SIMPLEQ_HEAD_INITIALIZER(head);
713 .Fn SIMPLEQ_INSERT_AFTER
714 macro inserts the new element
720 .Fn SIMPLEQ_INSERT_HEAD
721 macro inserts the new element
723 at the head of the queue.
726 .Fn SIMPLEQ_INSERT_TAIL
727 macro inserts the new element
729 at the end of the queue.
732 .Fn SIMPLEQ_REMOVE_AFTER
733 macro removes the queue element immediately following
737 .Fn SIMPLEQ_REMOVE_HEAD
738 macro removes the first element
743 macro concatenates all the elements of the queue referenced by
745 to the end of the queue referenced by
750 This is more efficient than removing and inserting the individual elements
751 as it does not actually traverse
758 macros can be used to traverse the queue.
761 is used for queue traversal:
762 .Bd -literal -offset indent
763 SIMPLEQ_FOREACH(np, head, FIELDNAME)
767 .Fn SIMPLEQ_FOREACH_SAFE
768 traverses the queue referenced by head in a
769 forward direction, assigning each element in turn to var.
772 it is permitted to remove var as well
773 as free it from within the loop safely without interfering with the traversal.
777 macro should be used to check whether a list is empty.
778 .Sh SIMPLE QUEUE EXAMPLE
780 SIMPLEQ_HEAD(listhead, entry) head = SIMPLEQ_HEAD_INITIALIZER(head);
783 SIMPLEQ_ENTRY(entry) entries; /* Simple queue. */
787 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
788 SIMPLEQ_INSERT_HEAD(&head, n1, entries);
790 n2 = malloc(sizeof(struct entry)); /* Insert after. */
791 SIMPLEQ_INSERT_AFTER(&head, n1, n2, entries);
793 n2 = malloc(sizeof(struct entry)); /* Insert at the tail. */
794 SIMPLEQ_INSERT_TAIL(&head, n2, entries);
795 /* Forward traversal. */
796 SIMPLEQ_FOREACH(np, &head, entries)
799 while (!SIMPLEQ_EMPTY(&head)) {
800 n1 = SIMPLEQ_FIRST(&head);
801 SIMPLEQ_REMOVE_HEAD(&head, entries);
805 .Sh SINGLY-LINKED TAIL QUEUES
806 A singly-linked tail queue is headed by a structure defined by the
809 This structure contains a pair of pointers, one to the first element in
810 the tail queue and the other to the last element in the tail queue.
811 The elements are singly linked for minimum space and pointer manipulation
812 overhead at the expense of O(n) removal for arbitrary elements.
813 New elements can be added to the tail queue after an existing element,
814 at the head of the tail queue or at the end of the tail queue.
817 structure is declared as follows:
818 .Bd -literal -offset indent
819 STAILQ_HEAD(HEADNAME, TYPE) head;
824 is the name of the structure to be defined, and struct
826 is the type of the elements to be linked into the tail queue.
827 A pointer to the head of the tail queue can later be declared as:
828 .Bd -literal -offset indent
829 struct HEADNAME *headp;
836 are user selectable.)
840 macro declares a structure that connects the elements in
845 macro initializes the tail queue referenced by
848 The tail queue can also be initialized statically by using the
849 .Fn STAILQ_HEAD_INITIALIZER
851 .Bd -literal -offset indent
852 STAILQ_HEAD(HEADNAME, TYPE) head = STAILQ_HEAD_INITIALIZER(head);
856 .Fn STAILQ_INSERT_AFTER
857 macro inserts the new element
863 .Fn STAILQ_INSERT_HEAD
864 macro inserts the new element
866 at the head of the tail queue.
869 .Fn STAILQ_INSERT_TAIL
870 macro inserts the new element
872 at the end of the tail queue.
875 .Fn STAILQ_REMOVE_AFTER
876 macro removes the queue element immediately following
880 this macro does not traverse the entire tail queue.
883 .Fn STAILQ_REMOVE_HEAD
884 macro removes the first element
886 For optimum efficiency,
887 elements being removed from the head of the tail queue should
888 use this macro explicitly rather than the generic
894 macro removes the element
897 Use of this macro should be avoided as it traverses the entire list.
898 A doubly-linked tail queue should be used if this macro is needed in
899 high-usage code paths or to operate on long tail queues.
903 macro concatenates all the elements of the tail queue referenced by
905 to the end of the tail queue referenced by
910 This is more efficient than removing and inserting the individual elements
911 as it does not actually traverse
916 is used for queue traversal:
917 .Bd -literal -offset indent
918 STAILQ_FOREACH(np, head, FIELDNAME)
922 .Fn STAILQ_FOREACH_SAFE
923 traverses the queue referenced by head in a
924 forward direction, assigning each element in turn to var.
927 it is permitted to remove var as well
928 as free it from within the loop safely without interfering with the traversal.
935 macros can be used to manually traverse a tail queue or an arbitrary part of
939 macro should be used to check whether a tail queue is empty.
940 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
942 STAILQ_HEAD(listhead, entry) head = STAILQ_HEAD_INITIALIZER(head);
945 STAILQ_ENTRY(entry) entries; /* Singly-linked tail queue. */
949 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
950 STAILQ_INSERT_HEAD(&head, n1, entries);
952 n2 = malloc(sizeof(struct entry)); /* Insert at the tail. */
953 STAILQ_INSERT_TAIL(&head, n2, entries);
955 n2 = malloc(sizeof(struct entry)); /* Insert after. */
956 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
959 STAILQ_REMOVE(&head, n2, entry, entries);
961 /* Deletion from the head. */
962 n3 = STAILQ_FIRST(&head);
963 STAILQ_REMOVE_HEAD(&head, entries);
965 /* Forward traversal. */
966 STAILQ_FOREACH(np, &head, entries)
968 /* Safe forward traversal. */
969 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
971 STAILQ_REMOVE(&head, np, entry, entries);
975 while (!STAILQ_EMPTY(&head)) {
976 n1 = STAILQ_FIRST(&head);
977 STAILQ_REMOVE_HEAD(&head, entries);
982 A tail queue is headed by a structure defined by the
985 This structure contains a pair of pointers,
986 one to the first element in the tail queue and the other to
987 the last element in the tail queue.
988 The elements are doubly linked so that an arbitrary element can be
989 removed without traversing the tail queue.
990 New elements can be added to the queue after an existing element,
991 before an existing element, at the head of the queue, or at the end
995 structure is declared as follows:
996 .Bd -literal -offset indent
997 TAILQ_HEAD(HEADNAME, TYPE) head;
1002 is the name of the structure to be defined, and struct
1004 is the type of the elements to be linked into the tail queue.
1005 A pointer to the head of the tail queue can later be declared as:
1006 .Bd -literal -offset indent
1007 struct HEADNAME *headp;
1014 are user selectable.)
1018 macro declares a structure that connects the elements in
1023 macro initializes the tail queue referenced by
1026 The tail queue can also be initialized statically by using the
1027 .Fn TAILQ_HEAD_INITIALIZER
1031 .Fn TAILQ_INSERT_HEAD
1032 macro inserts the new element
1034 at the head of the tail queue.
1037 .Fn TAILQ_INSERT_TAIL
1038 macro inserts the new element
1040 at the end of the tail queue.
1043 .Fn TAILQ_INSERT_AFTER
1044 macro inserts the new element
1050 .Fn TAILQ_INSERT_BEFORE
1051 macro inserts the new element
1058 macro removes the element
1060 from the tail queue.
1064 macro replaces the list element
1066 with the new element
1071 macro concatenates all the elements of the tail queue referenced by
1073 to the end of the tail queue referenced by
1078 This is more efficient than removing and inserting the individual elements
1079 as it does not actually traverse
1084 .Fn TAILQ_FOREACH_REVERSE
1085 are used for traversing a tail queue.
1087 starts at the first element and proceeds towards the last.
1088 .Fn TAILQ_FOREACH_REVERSE
1089 starts at the last element and proceeds towards the first.
1090 .Bd -literal -offset indent
1091 TAILQ_FOREACH(np, &head, FIELDNAME)
1092 TAILQ_FOREACH_REVERSE(np, &head, HEADNAME, FIELDNAME)
1096 .Fn TAILQ_FOREACH_SAFE
1098 .Fn TAILQ_FOREACH_REVERSE_SAFE
1099 traverse the list referenced by head
1100 in a forward or reverse direction respectively,
1101 assigning each element in turn to var.
1102 However, unlike their unsafe counterparts,
1103 they permit both the removal of var
1104 as well as freeing it from within the loop safely
1105 without interfering with the traversal.
1113 macros can be used to manually traverse a tail queue or an arbitrary part of
1118 macro should be used to check whether a tail queue is empty.
1119 .Sh TAIL QUEUE EXAMPLE
1121 TAILQ_HEAD(tailhead, entry) head;
1124 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1128 TAILQ_INIT(&head); /* Initialize queue. */
1130 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1131 TAILQ_INSERT_HEAD(&head, n1, entries);
1133 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1134 TAILQ_INSERT_TAIL(&head, n1, entries);
1136 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1137 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1139 n2 = malloc(sizeof(struct entry)); /* Insert before. */
1140 TAILQ_INSERT_BEFORE(n1, n2, entries);
1141 /* Forward traversal. */
1142 TAILQ_FOREACH(np, &head, entries)
1144 /* Manual forward traversal. */
1145 for (np = n2; np != NULL; np = TAILQ_NEXT(np, entries))
1148 while ((np = TAILQ_FIRST(&head))) {
1149 TAILQ_REMOVE(&head, np, entries);
1157 It is an error to assume the next and previous fields are preserved
1158 after an element has been removed from a list or queue.
1159 Using any macro (except the various forms of insertion) on an element
1160 removed from a list or queue is incorrect.
1161 An example of erroneous usage is removing the same element twice.
1170 macros are deprecated; they provided symmetry with the historical
1175 Trying to free a list in the following way is a common error:
1176 .Bd -literal -offset indent
1177 LIST_FOREACH(var, head, entry)
1184 is free'd, the FOREACH macros refer to a pointer that may have been
1185 reallocated already.
1186 A similar situation occurs when the current element is deleted
1188 In cases like these the data structure's FOREACH_SAFE macros should be used
1193 functions first appeared in
1195 The historical circle queue macros were deprecated in