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. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
39 .Nm SLIST_FOREACH_FROM ,
40 .Nm SLIST_FOREACH_SAFE ,
41 .Nm SLIST_FOREACH_FROM_SAFE ,
43 .Nm SLIST_HEAD_INITIALIZER ,
45 .Nm SLIST_INSERT_AFTER ,
46 .Nm SLIST_INSERT_HEAD ,
48 .Nm SLIST_REMOVE_AFTER ,
49 .Nm SLIST_REMOVE_HEAD ,
57 .Nm STAILQ_FOREACH_FROM ,
58 .Nm STAILQ_FOREACH_SAFE ,
59 .Nm STAILQ_FOREACH_FROM_SAFE ,
61 .Nm STAILQ_HEAD_INITIALIZER ,
63 .Nm STAILQ_INSERT_AFTER ,
64 .Nm STAILQ_INSERT_HEAD ,
65 .Nm STAILQ_INSERT_TAIL ,
68 .Nm STAILQ_REMOVE_AFTER ,
69 .Nm STAILQ_REMOVE_HEAD ,
76 .Nm LIST_FOREACH_FROM ,
77 .Nm LIST_FOREACH_SAFE ,
78 .Nm LIST_FOREACH_FROM_SAFE ,
80 .Nm LIST_HEAD_INITIALIZER ,
82 .Nm LIST_INSERT_AFTER ,
83 .Nm LIST_INSERT_BEFORE ,
84 .Nm LIST_INSERT_HEAD ,
94 .Nm TAILQ_FOREACH_FROM ,
95 .Nm TAILQ_FOREACH_SAFE ,
96 .Nm TAILQ_FOREACH_FROM_SAFE ,
97 .Nm TAILQ_FOREACH_REVERSE ,
98 .Nm TAILQ_FOREACH_REVERSE_FROM ,
99 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
100 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
102 .Nm TAILQ_HEAD_INITIALIZER ,
104 .Nm TAILQ_INSERT_AFTER ,
105 .Nm TAILQ_INSERT_BEFORE ,
106 .Nm TAILQ_INSERT_HEAD ,
107 .Nm TAILQ_INSERT_TAIL ,
113 .Nd implementations of singly-linked lists, singly-linked tail queues,
114 lists and tail queues
118 .Fn SLIST_EMPTY "SLIST_HEAD *head"
119 .Fn SLIST_ENTRY "TYPE"
120 .Fn SLIST_FIRST "SLIST_HEAD *head"
121 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
122 .Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
123 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
124 .Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
125 .Fn SLIST_HEAD "HEADNAME" "TYPE"
126 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
127 .Fn SLIST_INIT "SLIST_HEAD *head"
128 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
129 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
130 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
131 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
132 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
133 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
134 .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
136 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
137 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
138 .Fn STAILQ_ENTRY "TYPE"
139 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
140 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
141 .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
142 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
143 .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
144 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
145 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
146 .Fn STAILQ_INIT "STAILQ_HEAD *head"
147 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
148 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
149 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
150 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
151 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
152 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
153 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
154 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
155 .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
157 .Fn LIST_EMPTY "LIST_HEAD *head"
158 .Fn LIST_ENTRY "TYPE"
159 .Fn LIST_FIRST "LIST_HEAD *head"
160 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
161 .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
162 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
163 .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
164 .Fn LIST_HEAD "HEADNAME" "TYPE"
165 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
166 .Fn LIST_INIT "LIST_HEAD *head"
167 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
168 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
169 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
170 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
171 .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
172 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
173 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
175 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
176 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
177 .Fn TAILQ_ENTRY "TYPE"
178 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
179 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
180 .Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
181 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
182 .Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
183 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
184 .Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
185 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
186 .Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
187 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
188 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
189 .Fn TAILQ_INIT "TAILQ_HEAD *head"
190 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
191 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
192 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
193 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
194 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
195 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
196 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
197 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
198 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
201 These macros define and operate on four types of data structures:
202 singly-linked lists, singly-linked tail queues, lists, and tail queues.
203 All four structures support the following functionality:
204 .Bl -enum -compact -offset indent
206 Insertion of a new entry at the head of the list.
208 Insertion of a new entry after any element in the list.
210 O(1) removal of an entry from the head of the list.
212 Forward traversal through the list.
214 Swapping the contents of two lists.
217 Singly-linked lists are the simplest of the four data structures
218 and support only the above functionality.
219 Singly-linked lists are ideal for applications with large datasets
220 and few or no removals,
221 or for implementing a LIFO queue.
222 Singly-linked lists add the following functionality:
223 .Bl -enum -compact -offset indent
225 O(n) removal of any entry in the list.
228 Singly-linked tail queues add the following functionality:
229 .Bl -enum -compact -offset indent
231 Entries can be added at the end of a list.
233 O(n) removal of any entry in the list.
235 They may be concatenated.
238 .Bl -enum -compact -offset indent
240 All list insertions must specify the head of the list.
242 Each head entry requires two pointers rather than one.
244 Code size is about 15% greater and operations run about 20% slower
245 than singly-linked lists.
248 Singly-linked tail queues are ideal for applications with large datasets and
250 or for implementing a FIFO queue.
252 All doubly linked types of data structures (lists and tail queues)
254 .Bl -enum -compact -offset indent
256 Insertion of a new entry before any element in the list.
258 O(1) removal of any entry in the list.
261 .Bl -enum -compact -offset indent
263 Each element requires two pointers rather than one.
265 Code size and execution time of operations (except for removal) is about
266 twice that of the singly-linked data-structures.
269 Linked lists are the simplest of the doubly linked data structures.
270 They add the following functionality over the above:
271 .Bl -enum -compact -offset indent
273 They may be traversed backwards.
276 .Bl -enum -compact -offset indent
278 To traverse backwards, an entry to begin the traversal and the list in
279 which it is contained must be specified.
282 Tail queues add the following functionality:
283 .Bl -enum -compact -offset indent
285 Entries can be added at the end of a list.
287 They may be traversed backwards, from tail to head.
289 They may be concatenated.
292 .Bl -enum -compact -offset indent
294 All list insertions and removals must specify the head of the list.
296 Each head entry requires two pointers rather than one.
298 Code size is about 15% greater and operations run about 20% slower
299 than singly-linked lists.
302 In the macro definitions,
304 is the name of a user defined structure,
305 that must contain a field of type
315 is the name of a user defined structure that must be declared
322 See the examples below for further explanation of how these
324 .Sh SINGLY-LINKED LISTS
325 A singly-linked list is headed by a structure defined by the
328 This structure contains a single pointer to the first element
330 The elements are singly linked for minimum space and pointer manipulation
331 overhead at the expense of O(n) removal for arbitrary elements.
332 New elements can be added to the list after an existing element or
333 at the head of the list.
336 structure is declared as follows:
337 .Bd -literal -offset indent
338 SLIST_HEAD(HEADNAME, TYPE) head;
343 is the name of the structure to be defined, and
345 is the type of the elements to be linked into the list.
346 A pointer to the head of the list can later be declared as:
347 .Bd -literal -offset indent
348 struct HEADNAME *headp;
355 are user selectable.)
358 .Nm SLIST_HEAD_INITIALIZER
359 evaluates to an initializer for the list
364 evaluates to true if there are no elements in the list.
368 declares a structure that connects the elements in
373 returns the first element in the list or NULL if the list is empty.
377 traverses the list referenced by
379 in the forward direction, assigning each element in
384 .Nm SLIST_FOREACH_FROM
385 behaves identically to
389 is NULL, else it treats
391 as a previously found SLIST element and begins the loop at
393 instead of the first element in the SLIST referenced by
397 .Nm SLIST_FOREACH_SAFE
398 traverses the list referenced by
400 in the forward direction, assigning each element in
405 here it is permitted to both remove
407 as well as free it from within the loop safely without interfering with the
411 .Nm SLIST_FOREACH_FROM_SAFE
412 behaves identically to
413 .Nm SLIST_FOREACH_SAFE
416 is NULL, else it treats
418 as a previously found SLIST element and begins the loop at
420 instead of the first element in the SLIST referenced by
425 initializes the list referenced by
429 .Nm SLIST_INSERT_HEAD
430 inserts the new element
432 at the head of the list.
435 .Nm SLIST_INSERT_AFTER
436 inserts the new element
443 returns the next element in the list.
446 .Nm SLIST_REMOVE_AFTER
447 removes the element after
452 this macro does not traverse the entire list.
455 .Nm SLIST_REMOVE_HEAD
458 from the head of the list.
459 For optimum efficiency,
460 elements being removed from the head of the list should explicitly use
461 this macro instead of the generic
473 swaps the contents of
477 .Sh SINGLY-LINKED LIST EXAMPLE
479 SLIST_HEAD(slisthead, entry) head =
480 SLIST_HEAD_INITIALIZER(head);
481 struct slisthead *headp; /* Singly-linked List head. */
484 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
486 } *n1, *n2, *n3, *np;
488 SLIST_INIT(&head); /* Initialize the list. */
490 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
491 SLIST_INSERT_HEAD(&head, n1, entries);
493 n2 = malloc(sizeof(struct entry)); /* Insert after. */
494 SLIST_INSERT_AFTER(n1, n2, entries);
496 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
499 n3 = SLIST_FIRST(&head);
500 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
502 /* Forward traversal. */
503 SLIST_FOREACH(np, &head, entries)
505 /* Safe forward traversal. */
506 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
509 SLIST_REMOVE(&head, np, entry, entries);
513 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
514 n1 = SLIST_FIRST(&head);
515 SLIST_REMOVE_HEAD(&head, entries);
519 .Sh SINGLY-LINKED TAIL QUEUES
520 A singly-linked tail queue is headed by a structure defined by the
523 This structure contains a pair of pointers,
524 one to the first element in the tail queue and the other to
525 the last element in the tail queue.
526 The elements are singly linked for minimum space and pointer
527 manipulation overhead at the expense of O(n) removal for arbitrary
529 New elements can be added to the tail queue after an existing element,
530 at the head of the tail queue, or at the end of the tail queue.
533 structure is declared as follows:
534 .Bd -literal -offset indent
535 STAILQ_HEAD(HEADNAME, TYPE) head;
540 is the name of the structure to be defined, and
542 is the type of the elements to be linked into the tail queue.
543 A pointer to the head of the tail queue can later be declared as:
544 .Bd -literal -offset indent
545 struct HEADNAME *headp;
552 are user selectable.)
555 .Nm STAILQ_HEAD_INITIALIZER
556 evaluates to an initializer for the tail queue
561 concatenates the tail queue headed by
563 onto the end of the one headed by
565 removing all entries from the former.
569 evaluates to true if there are no items on the tail queue.
573 declares a structure that connects the elements in
578 returns the first item on the tail queue or NULL if the tail queue
583 traverses the tail queue referenced by
585 in the forward direction, assigning each element
590 .Nm STAILQ_FOREACH_FROM
591 behaves identically to
595 is NULL, else it treats
597 as a previously found STAILQ element and begins the loop at
599 instead of the first element in the STAILQ referenced by
603 .Nm STAILQ_FOREACH_SAFE
604 traverses the tail queue referenced by
606 in the forward direction, assigning each element
611 here it is permitted to both remove
613 as well as free it from within the loop safely without interfering with the
617 .Nm STAILQ_FOREACH_FROM_SAFE
618 behaves identically to
619 .Nm STAILQ_FOREACH_SAFE
622 is NULL, else it treats
624 as a previously found STAILQ element and begins the loop at
626 instead of the first element in the STAILQ referenced by
631 initializes the tail queue referenced by
635 .Nm STAILQ_INSERT_HEAD
636 inserts the new element
638 at the head of the tail queue.
641 .Nm STAILQ_INSERT_TAIL
642 inserts the new element
644 at the end of the tail queue.
647 .Nm STAILQ_INSERT_AFTER
648 inserts the new element
655 returns the last item on the tail queue.
656 If the tail queue is empty the return value is
661 returns the next item on the tail queue, or NULL this item is the last.
664 .Nm STAILQ_REMOVE_AFTER
665 removes the element after
670 this macro does not traverse the entire tail queue.
673 .Nm STAILQ_REMOVE_HEAD
674 removes the element at the head of the tail queue.
675 For optimum efficiency,
676 elements being removed from the head of the tail queue should
677 use this macro explicitly rather than the generic
689 swaps the contents of
693 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
695 STAILQ_HEAD(stailhead, entry) head =
696 STAILQ_HEAD_INITIALIZER(head);
697 struct stailhead *headp; /* Singly-linked tail queue head. */
700 STAILQ_ENTRY(entry) entries; /* Tail queue. */
702 } *n1, *n2, *n3, *np;
704 STAILQ_INIT(&head); /* Initialize the queue. */
706 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
707 STAILQ_INSERT_HEAD(&head, n1, entries);
709 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
710 STAILQ_INSERT_TAIL(&head, n1, entries);
712 n2 = malloc(sizeof(struct entry)); /* Insert after. */
713 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
715 STAILQ_REMOVE(&head, n2, entry, entries);
717 /* Deletion from the head. */
718 n3 = STAILQ_FIRST(&head);
719 STAILQ_REMOVE_HEAD(&head, entries);
721 /* Forward traversal. */
722 STAILQ_FOREACH(np, &head, entries)
724 /* Safe forward traversal. */
725 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
728 STAILQ_REMOVE(&head, np, entry, entries);
731 /* TailQ Deletion. */
732 while (!STAILQ_EMPTY(&head)) {
733 n1 = STAILQ_FIRST(&head);
734 STAILQ_REMOVE_HEAD(&head, entries);
737 /* Faster TailQ Deletion. */
738 n1 = STAILQ_FIRST(&head);
740 n2 = STAILQ_NEXT(n1, entries);
747 A list is headed by a structure defined by the
750 This structure contains a single pointer to the first element
752 The elements are doubly linked so that an arbitrary element can be
753 removed without traversing the list.
754 New elements can be added to the list after an existing element,
755 before an existing element, or at the head of the list.
758 structure is declared as follows:
759 .Bd -literal -offset indent
760 LIST_HEAD(HEADNAME, TYPE) head;
765 is the name of the structure to be defined, and
767 is the type of the elements to be linked into the list.
768 A pointer to the head of the list can later be declared as:
769 .Bd -literal -offset indent
770 struct HEADNAME *headp;
777 are user selectable.)
780 .Nm LIST_HEAD_INITIALIZER
781 evaluates to an initializer for the list
786 evaluates to true if there are no elements in the list.
790 declares a structure that connects the elements in
795 returns the first element in the list or NULL if the list
800 traverses the list referenced by
802 in the forward direction, assigning each element in turn to
806 .Nm LIST_FOREACH_FROM
807 behaves identically to
811 is NULL, else it treats
813 as a previously found LIST element and begins the loop at
815 instead of the first element in the LIST referenced by
819 .Nm LIST_FOREACH_SAFE
820 traverses the list referenced by
822 in the forward direction, assigning each element in turn to
826 here it is permitted to both remove
828 as well as free it from within the loop safely without interfering with the
832 .Nm LIST_FOREACH_FROM_SAFE
833 behaves identically to
834 .Nm LIST_FOREACH_SAFE
837 is NULL, else it treats
839 as a previously found LIST element and begins the loop at
841 instead of the first element in the LIST referenced by
846 initializes the list referenced by
851 inserts the new element
853 at the head of the list.
856 .Nm LIST_INSERT_AFTER
857 inserts the new element
863 .Nm LIST_INSERT_BEFORE
864 inserts the new element
871 returns the next element in the list, or NULL if this is the last.
875 returns the previous element in the list, or NULL if this is the first.
889 swaps the contents of
895 LIST_HEAD(listhead, entry) head =
896 LIST_HEAD_INITIALIZER(head);
897 struct listhead *headp; /* List head. */
900 LIST_ENTRY(entry) entries; /* List. */
902 } *n1, *n2, *n3, *np, *np_temp;
904 LIST_INIT(&head); /* Initialize the list. */
906 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
907 LIST_INSERT_HEAD(&head, n1, entries);
909 n2 = malloc(sizeof(struct entry)); /* Insert after. */
910 LIST_INSERT_AFTER(n1, n2, entries);
912 n3 = malloc(sizeof(struct entry)); /* Insert before. */
913 LIST_INSERT_BEFORE(n2, n3, entries);
915 LIST_REMOVE(n2, entries); /* Deletion. */
917 /* Forward traversal. */
918 LIST_FOREACH(np, &head, entries)
921 /* Safe forward traversal. */
922 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
925 LIST_REMOVE(np, entries);
929 while (!LIST_EMPTY(&head)) { /* List Deletion. */
930 n1 = LIST_FIRST(&head);
931 LIST_REMOVE(n1, entries);
935 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
937 n2 = LIST_NEXT(n1, entries);
944 A tail queue is headed by a structure defined by the
947 This structure contains a pair of pointers,
948 one to the first element in the tail queue and the other to
949 the last element in the tail queue.
950 The elements are doubly linked so that an arbitrary element can be
951 removed without traversing the tail queue.
952 New elements can be added to the tail queue after an existing element,
953 before an existing element, at the head of the tail queue,
954 or at the end of the tail queue.
957 structure is declared as follows:
958 .Bd -literal -offset indent
959 TAILQ_HEAD(HEADNAME, TYPE) head;
964 is the name of the structure to be defined, and
966 is the type of the elements to be linked into the tail queue.
967 A pointer to the head of the tail queue can later be declared as:
968 .Bd -literal -offset indent
969 struct HEADNAME *headp;
976 are user selectable.)
979 .Nm TAILQ_HEAD_INITIALIZER
980 evaluates to an initializer for the tail queue
985 concatenates the tail queue headed by
987 onto the end of the one headed by
989 removing all entries from the former.
993 evaluates to true if there are no items on the tail queue.
997 declares a structure that connects the elements in
1002 returns the first item on the tail queue or NULL if the tail queue
1007 traverses the tail queue referenced by
1009 in the forward direction, assigning each element in turn to
1014 if the loop completes normally, or if there were no elements.
1017 .Nm TAILQ_FOREACH_FROM
1018 behaves identically to
1022 is NULL, else it treats
1024 as a previously found TAILQ element and begins the loop at
1026 instead of the first element in the TAILQ referenced by
1030 .Nm TAILQ_FOREACH_REVERSE
1031 traverses the tail queue referenced by
1033 in the reverse direction, assigning each element in turn to
1037 .Nm TAILQ_FOREACH_REVERSE_FROM
1038 behaves identically to
1039 .Nm TAILQ_FOREACH_REVERSE
1042 is NULL, else it treats
1044 as a previously found TAILQ element and begins the reverse loop at
1046 instead of the last element in the TAILQ referenced by
1050 .Nm TAILQ_FOREACH_SAFE
1052 .Nm TAILQ_FOREACH_REVERSE_SAFE
1053 traverse the list referenced by
1055 in the forward or reverse direction respectively,
1056 assigning each element in turn to
1058 However, unlike their unsafe counterparts,
1061 .Nm TAILQ_FOREACH_REVERSE
1062 permit to both remove
1064 as well as free it from within the loop safely without interfering with the
1068 .Nm TAILQ_FOREACH_FROM_SAFE
1069 behaves identically to
1070 .Nm TAILQ_FOREACH_SAFE
1073 is NULL, else it treats
1075 as a previously found TAILQ element and begins the loop at
1077 instead of the first element in the TAILQ referenced by
1081 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1082 behaves identically to
1083 .Nm TAILQ_FOREACH_REVERSE_SAFE
1086 is NULL, else it treats
1088 as a previously found TAILQ element and begins the reverse loop at
1090 instead of the last element in the TAILQ referenced by
1095 initializes the tail queue referenced by
1099 .Nm TAILQ_INSERT_HEAD
1100 inserts the new element
1102 at the head of the tail queue.
1105 .Nm TAILQ_INSERT_TAIL
1106 inserts the new element
1108 at the end of the tail queue.
1111 .Nm TAILQ_INSERT_AFTER
1112 inserts the new element
1118 .Nm TAILQ_INSERT_BEFORE
1119 inserts the new element
1126 returns the last item on the tail queue.
1127 If the tail queue is empty the return value is
1132 returns the next item on the tail queue, or NULL if this item is the last.
1136 returns the previous item on the tail queue, or NULL if this item
1143 from the tail queue.
1147 swaps the contents of
1151 .Sh TAIL QUEUE EXAMPLE
1153 TAILQ_HEAD(tailhead, entry) head =
1154 TAILQ_HEAD_INITIALIZER(head);
1155 struct tailhead *headp; /* Tail queue head. */
1158 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1160 } *n1, *n2, *n3, *np;
1162 TAILQ_INIT(&head); /* Initialize the queue. */
1164 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1165 TAILQ_INSERT_HEAD(&head, n1, entries);
1167 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1168 TAILQ_INSERT_TAIL(&head, n1, entries);
1170 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1171 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1173 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1174 TAILQ_INSERT_BEFORE(n2, n3, entries);
1176 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1178 /* Forward traversal. */
1179 TAILQ_FOREACH(np, &head, entries)
1181 /* Safe forward traversal. */
1182 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1185 TAILQ_REMOVE(&head, np, entries);
1188 /* Reverse traversal. */
1189 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1191 /* TailQ Deletion. */
1192 while (!TAILQ_EMPTY(&head)) {
1193 n1 = TAILQ_FIRST(&head);
1194 TAILQ_REMOVE(&head, n1, entries);
1197 /* Faster TailQ Deletion. */
1198 n1 = TAILQ_FIRST(&head);
1199 while (n1 != NULL) {
1200 n2 = TAILQ_NEXT(n1, entries);
1211 functions first appeared in