2 .\" The Regents of the University of California. All rights reserved.
4 .\" %%%LICENSE_START(BSD_3_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. Neither the name of the University nor the names of its contributors
14 .\" may be used to endorse or promote products derived from this software
15 .\" without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
41 .\" .Nm SLIST_FOREACH_FROM ,
42 .\" .Nm SLIST_FOREACH_SAFE ,
43 .\" .Nm SLIST_FOREACH_FROM_SAFE ,
45 .Nm SLIST_HEAD_INITIALIZER ,
47 .Nm SLIST_INSERT_AFTER ,
48 .Nm SLIST_INSERT_HEAD ,
50 .\" .Nm SLIST_REMOVE_AFTER ,
51 .Nm SLIST_REMOVE_HEAD ,
59 .\" .Nm STAILQ_FOREACH_FROM ,
60 .\" .Nm STAILQ_FOREACH_SAFE ,
61 .\" .Nm STAILQ_FOREACH_FROM_SAFE ,
63 .Nm STAILQ_HEAD_INITIALIZER ,
65 .Nm STAILQ_INSERT_AFTER ,
66 .Nm STAILQ_INSERT_HEAD ,
67 .Nm STAILQ_INSERT_TAIL ,
70 .\" .Nm STAILQ_REMOVE_AFTER ,
71 .Nm STAILQ_REMOVE_HEAD ,
78 .\" .Nm LIST_FOREACH_FROM ,
79 .\" .Nm LIST_FOREACH_SAFE ,
80 .\" .Nm LIST_FOREACH_FROM_SAFE ,
82 .Nm LIST_HEAD_INITIALIZER ,
84 .Nm LIST_INSERT_AFTER ,
85 .Nm LIST_INSERT_BEFORE ,
86 .Nm LIST_INSERT_HEAD ,
96 .\" .Nm TAILQ_FOREACH_FROM ,
97 .\" .Nm TAILQ_FOREACH_SAFE ,
98 .\" .Nm TAILQ_FOREACH_FROM_SAFE ,
99 .Nm TAILQ_FOREACH_REVERSE ,
100 .\" .Nm TAILQ_FOREACH_REVERSE_FROM ,
101 .\" .Nm TAILQ_FOREACH_REVERSE_SAFE ,
102 .\" .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
104 .Nm TAILQ_HEAD_INITIALIZER ,
106 .Nm TAILQ_INSERT_AFTER ,
107 .Nm TAILQ_INSERT_BEFORE ,
108 .Nm TAILQ_INSERT_HEAD ,
109 .Nm TAILQ_INSERT_TAIL ,
115 .Nd implementations of singly-linked lists, singly-linked tail queues,
116 lists and tail queues
120 .Fn SLIST_EMPTY "SLIST_HEAD *head"
121 .Fn SLIST_ENTRY "TYPE"
122 .Fn SLIST_FIRST "SLIST_HEAD *head"
123 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
124 .\" .Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
125 .\" .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
126 .\" .Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
127 .Fn SLIST_HEAD "HEADNAME" "TYPE"
128 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
129 .Fn SLIST_INIT "SLIST_HEAD *head"
130 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
131 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
132 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
133 .\" .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
134 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
135 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
136 .\" .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
138 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
139 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
140 .Fn STAILQ_ENTRY "TYPE"
141 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
142 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
143 .\" .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
144 .\" .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
145 .\" .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
146 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
147 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
148 .Fn STAILQ_INIT "STAILQ_HEAD *head"
149 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
150 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
151 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
152 .\" .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
153 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
154 .\" .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
155 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
156 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
157 .\" .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
159 .Fn LIST_EMPTY "LIST_HEAD *head"
160 .Fn LIST_ENTRY "TYPE"
161 .Fn LIST_FIRST "LIST_HEAD *head"
162 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
163 .\" .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
164 .\" .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
165 .\" .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
166 .Fn LIST_HEAD "HEADNAME" "TYPE"
167 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
168 .Fn LIST_INIT "LIST_HEAD *head"
169 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
170 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
171 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
172 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
173 .\" .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "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 Swapping 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 tail queues 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.
272 They add the following functionality over the above:
273 .Bl -enum -compact -offset indent
275 They may be traversed backwards.
278 .Bl -enum -compact -offset indent
280 To traverse backwards, an entry to begin the traversal and the list in
281 which it is contained must be specified.
284 Tail queues add the following functionality:
285 .Bl -enum -compact -offset indent
287 Entries can be added at the end of a list.
289 They may be traversed backwards, from tail to head.
291 They may be concatenated.
294 .Bl -enum -compact -offset indent
296 All list insertions and removals must specify the head of the list.
298 Each head entry requires two pointers rather than one.
300 Code size is about 15% greater and operations run about 20% slower
301 than singly-linked lists.
304 In the macro definitions,
306 is the name of a user defined structure,
307 that must contain a field of type
317 is the name of a user defined structure that must be declared
324 See the examples below for further explanation of how these
326 .Ss Singly-linked lists
327 A singly-linked list is headed by a structure defined by the
330 This structure contains a single pointer to the first element
332 The elements are singly linked for minimum space and pointer manipulation
333 overhead at the expense of O(n) removal for arbitrary elements.
334 New elements can be added to the list after an existing element or
335 at the head of the list.
338 structure is declared as follows:
339 .Bd -literal -offset indent
340 SLIST_HEAD(HEADNAME, TYPE) head;
345 is the name of the structure to be defined, and
347 is the type of the elements to be linked into the list.
348 A pointer to the head of the list can later be declared as:
349 .Bd -literal -offset indent
350 struct HEADNAME *headp;
357 are user selectable.)
360 .Nm SLIST_HEAD_INITIALIZER
361 evaluates to an initializer for the list
366 evaluates to true if there are no elements in the list.
370 declares a structure that connects the elements in
375 returns the first element in the list or NULL if the list is empty.
379 traverses the list referenced by
381 in the forward direction, assigning each element in
386 .\" .Nm SLIST_FOREACH_FROM
387 .\" behaves identically to
388 .\" .Nm SLIST_FOREACH
391 .\" is NULL, else it treats
393 .\" as a previously found SLIST element and begins the loop at
395 .\" instead of the first element in the SLIST referenced by
399 .\" .Nm SLIST_FOREACH_SAFE
400 .\" traverses the list referenced by
402 .\" in the forward direction, assigning each element in
406 .\" .Fn SLIST_FOREACH
407 .\" here it is permitted to both remove
409 .\" as well as free it from within the loop safely without interfering with the
413 .\" .Nm SLIST_FOREACH_FROM_SAFE
414 .\" behaves identically to
415 .\" .Nm SLIST_FOREACH_SAFE
418 .\" is NULL, else it treats
420 .\" as a previously found SLIST element and begins the loop at
422 .\" instead of the first element in the SLIST referenced by
427 initializes the list referenced by
431 .Nm SLIST_INSERT_HEAD
432 inserts the new element
434 at the head of the list.
437 .Nm SLIST_INSERT_AFTER
438 inserts the new element
445 returns the next element in the list.
448 .\" .Nm SLIST_REMOVE_AFTER
449 .\" removes the element after
453 .\" .Fa SLIST_REMOVE ,
454 .\" this macro does not traverse the entire list.
457 .Nm SLIST_REMOVE_HEAD
460 from the head of the list.
461 For optimum efficiency,
462 elements being removed from the head of the list should explicitly use
463 this macro instead of the generic
475 .\" swaps the contents of
479 .Ss Singly-linked list example
481 SLIST_HEAD(slisthead, entry) head =
482 SLIST_HEAD_INITIALIZER(head);
483 struct slisthead *headp; /* Singly-linked List head. */
486 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
488 } *n1, *n2, *n3, *np;
490 SLIST_INIT(&head); /* Initialize the list. */
492 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
493 SLIST_INSERT_HEAD(&head, n1, entries);
495 n2 = malloc(sizeof(struct entry)); /* Insert after. */
496 SLIST_INSERT_AFTER(n1, n2, entries);
498 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
501 n3 = SLIST_FIRST(&head);
502 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
504 /* Forward traversal. */
505 SLIST_FOREACH(np, &head, entries)
507 .\" /* Safe forward traversal. */
508 .\"SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
511 .\" SLIST_REMOVE(&head, np, entry, entries);
515 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
516 n1 = SLIST_FIRST(&head);
517 SLIST_REMOVE_HEAD(&head, entries);
521 .Ss Singly-linked tail queues
522 A singly-linked tail queue is headed by a structure defined by the
525 This structure contains a pair of pointers,
526 one to the first element in the tail queue and the other to
527 the last element in the tail queue.
528 The elements are singly linked for minimum space and pointer
529 manipulation overhead at the expense of O(n) removal for arbitrary
531 New elements can be added to the tail queue after an existing element,
532 at the head of the tail queue, or at the end of the tail queue.
535 structure is declared as follows:
536 .Bd -literal -offset indent
537 STAILQ_HEAD(HEADNAME, TYPE) head;
542 is the name of the structure to be defined, and
544 is the type of the elements to be linked into the tail queue.
545 A pointer to the head of the tail queue can later be declared as:
546 .Bd -literal -offset indent
547 struct HEADNAME *headp;
554 are user selectable.)
557 .Nm STAILQ_HEAD_INITIALIZER
558 evaluates to an initializer for the tail queue
563 concatenates the tail queue headed by
565 onto the end of the one headed by
567 removing all entries from the former.
571 evaluates to true if there are no items on the tail queue.
575 declares a structure that connects the elements in
580 returns the first item on the tail queue or NULL if the tail queue
585 traverses the tail queue referenced by
587 in the forward direction, assigning each element
592 .\" .Nm STAILQ_FOREACH_FROM
593 .\" behaves identically to
594 .\" .Nm STAILQ_FOREACH
597 .\" is NULL, else it treats
599 .\" as a previously found STAILQ element and begins the loop at
601 .\" instead of the first element in the STAILQ referenced by
605 .\" .Nm STAILQ_FOREACH_SAFE
606 .\" traverses the tail queue referenced by
608 .\" in the forward direction, assigning each element
612 .\" .Fn STAILQ_FOREACH
613 .\" here it is permitted to both remove
615 .\" as well as free it from within the loop safely without interfering with the
619 .\" .Nm STAILQ_FOREACH_FROM_SAFE
620 .\" behaves identically to
621 .\" .Nm STAILQ_FOREACH_SAFE
624 .\" is NULL, else it treats
626 .\" as a previously found STAILQ element and begins the loop at
628 .\" instead of the first element in the STAILQ referenced by
633 initializes the tail queue referenced by
637 .Nm STAILQ_INSERT_HEAD
638 inserts the new element
640 at the head of the tail queue.
643 .Nm STAILQ_INSERT_TAIL
644 inserts the new element
646 at the end of the tail queue.
649 .Nm STAILQ_INSERT_AFTER
650 inserts the new element
657 .\" returns the last item on the tail queue.
658 .\" If the tail queue is empty the return value is
663 returns the next item on the tail queue, or NULL this item is the last.
666 .\" .Nm STAILQ_REMOVE_AFTER
667 .\" removes the element after
669 .\" from the tail queue.
671 .\" .Fa STAILQ_REMOVE ,
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 .Ss 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.
880 .\" must contain element
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
1021 .\" .Nm TAILQ_FOREACH
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,
1061 .\" .Nm TAILQ_FOREACH
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 .Ss 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) {
1185 .\" np\->do_stuff();
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);
1208 n2 = malloc(sizeof(struct entry)); /* Insert before. */
1209 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
1210 /* Forward traversal. */
1211 for (np = head.cqh_first; np != (void *)&head;
1212 np = np\->entries.cqe_next)
1214 /* Reverse traversal. */
1215 for (np = head.cqh_last; np != (void *)&head; np = np\->entries.cqe_prev)
1218 while (head.cqh_first != (void *)&head)
1219 CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
1222 Not in POSIX.1, POSIX.1-2001 or POSIX.1-2008.
1223 Present on the BSDs.
1225 functions first appeared in