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
32 .Nm SLIST_CLASS_ENTRY ,
33 .Nm SLIST_CLASS_HEAD ,
39 .Nm SLIST_FOREACH_FROM ,
40 .Nm SLIST_FOREACH_FROM_SAFE ,
41 .Nm SLIST_FOREACH_SAFE ,
43 .Nm SLIST_HEAD_INITIALIZER ,
45 .Nm SLIST_INSERT_AFTER ,
46 .Nm SLIST_INSERT_HEAD ,
49 .Nm SLIST_REMOVE_AFTER ,
50 .Nm SLIST_REMOVE_HEAD ,
52 .Nm STAILQ_CLASS_ENTRY ,
53 .Nm STAILQ_CLASS_HEAD ,
59 .Nm STAILQ_FOREACH_FROM ,
60 .Nm STAILQ_FOREACH_FROM_SAFE ,
61 .Nm STAILQ_FOREACH_SAFE ,
63 .Nm STAILQ_HEAD_INITIALIZER ,
65 .Nm STAILQ_INSERT_AFTER ,
66 .Nm STAILQ_INSERT_HEAD ,
67 .Nm STAILQ_INSERT_TAIL ,
71 .Nm STAILQ_REMOVE_AFTER ,
72 .Nm STAILQ_REMOVE_HEAD ,
74 .Nm LIST_CLASS_ENTRY ,
81 .Nm LIST_FOREACH_FROM ,
82 .Nm LIST_FOREACH_FROM_SAFE ,
83 .Nm LIST_FOREACH_SAFE ,
85 .Nm LIST_HEAD_INITIALIZER ,
87 .Nm LIST_INSERT_AFTER ,
88 .Nm LIST_INSERT_BEFORE ,
89 .Nm LIST_INSERT_HEAD ,
94 .Nm TAILQ_CLASS_ENTRY ,
95 .Nm TAILQ_CLASS_HEAD ,
101 .Nm TAILQ_FOREACH_FROM ,
102 .Nm TAILQ_FOREACH_FROM_SAFE ,
103 .Nm TAILQ_FOREACH_REVERSE ,
104 .Nm TAILQ_FOREACH_REVERSE_FROM ,
105 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
106 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
107 .Nm TAILQ_FOREACH_SAFE ,
109 .Nm TAILQ_HEAD_INITIALIZER ,
111 .Nm TAILQ_INSERT_AFTER ,
112 .Nm TAILQ_INSERT_BEFORE ,
113 .Nm TAILQ_INSERT_HEAD ,
114 .Nm TAILQ_INSERT_TAIL ,
120 .Nd implementations of singly-linked lists, singly-linked tail queues,
121 lists and tail queues
125 .Fn SLIST_CLASS_ENTRY "CLASSTYPE"
126 .Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
127 .Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME"
128 .Fn SLIST_EMPTY "SLIST_HEAD *head"
129 .Fn SLIST_ENTRY "TYPE"
130 .Fn SLIST_FIRST "SLIST_HEAD *head"
131 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
132 .Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
133 .Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
134 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
135 .Fn SLIST_HEAD "HEADNAME" "TYPE"
136 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
137 .Fn SLIST_INIT "SLIST_HEAD *head"
138 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
139 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
140 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
141 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
142 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
143 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
144 .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE"
146 .Fn STAILQ_CLASS_ENTRY "CLASSTYPE"
147 .Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
148 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
149 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
150 .Fn STAILQ_ENTRY "TYPE"
151 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
152 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
153 .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
154 .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
155 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
156 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
157 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
158 .Fn STAILQ_INIT "STAILQ_HEAD *head"
159 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
160 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
161 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
162 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
163 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
164 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
165 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
166 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
167 .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE"
169 .Fn LIST_CLASS_ENTRY "CLASSTYPE"
170 .Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
171 .Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
172 .Fn LIST_EMPTY "LIST_HEAD *head"
173 .Fn LIST_ENTRY "TYPE"
174 .Fn LIST_FIRST "LIST_HEAD *head"
175 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
176 .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
177 .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
178 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
179 .Fn LIST_HEAD "HEADNAME" "TYPE"
180 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
181 .Fn LIST_INIT "LIST_HEAD *head"
182 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
183 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
184 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
185 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
186 .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
187 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
188 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
190 .Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
191 .Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
192 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
193 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
194 .Fn TAILQ_ENTRY "TYPE"
195 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
196 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
197 .Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
198 .Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
199 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
200 .Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
201 .Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
202 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
203 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
204 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
205 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
206 .Fn TAILQ_INIT "TAILQ_HEAD *head"
207 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
208 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
209 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
210 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
211 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
212 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
213 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
214 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
215 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
218 These macros define and operate on four types of data structures which
219 can be used in both C and C++ source code:
220 .Bl -enum -compact -offset indent
226 Singly-linked tail queues
230 All four structures support the following functionality:
231 .Bl -enum -compact -offset indent
233 Insertion of a new entry at the head of the list.
235 Insertion of a new entry after any element in the list.
237 O(1) removal of an entry from the head of the list.
239 Forward traversal through the list.
241 Swapping the contents of two lists.
244 Singly-linked lists are the simplest of the four data structures
245 and support only the above functionality.
246 Singly-linked lists are ideal for applications with large datasets
247 and few or no removals,
248 or for implementing a LIFO queue.
249 Singly-linked lists add the following functionality:
250 .Bl -enum -compact -offset indent
252 O(n) removal of any entry in the list.
254 O(n) concatenation of two lists.
257 Singly-linked tail queues add the following functionality:
258 .Bl -enum -compact -offset indent
260 Entries can be added at the end of a list.
262 O(n) removal of any entry in the list.
264 They may be concatenated.
267 .Bl -enum -compact -offset indent
269 All list insertions must specify the head of the list.
271 Each head entry requires two pointers rather than one.
273 Code size is about 15% greater and operations run about 20% slower
274 than singly-linked lists.
277 Singly-linked tail queues are ideal for applications with large datasets and
279 or for implementing a FIFO queue.
281 All doubly linked types of data structures (lists and tail queues)
283 .Bl -enum -compact -offset indent
285 Insertion of a new entry before any element in the list.
287 O(1) removal of any entry in the list.
290 .Bl -enum -compact -offset indent
292 Each element requires two pointers rather than one.
294 Code size and execution time of operations (except for removal) is about
295 twice that of the singly-linked data-structures.
298 Linked lists are the simplest of the doubly linked data structures.
299 They add the following functionality over the above:
300 .Bl -enum -compact -offset indent
302 O(n) concatenation of two lists.
304 They may be traversed backwards.
307 .Bl -enum -compact -offset indent
309 To traverse backwards, an entry to begin the traversal and the list in
310 which it is contained must be specified.
313 Tail queues add the following functionality:
314 .Bl -enum -compact -offset indent
316 Entries can be added at the end of a list.
318 They may be traversed backwards, from tail to head.
320 They may be concatenated.
323 .Bl -enum -compact -offset indent
325 All list insertions and removals must specify the head of the list.
327 Each head entry requires two pointers rather than one.
329 Code size is about 15% greater and operations run about 20% slower
330 than singly-linked lists.
333 In the macro definitions,
335 is the name of a user defined structure.
336 The structure must contain a field called
344 In the macro definitions,
346 is the name of a user defined class.
347 The class must contain a field called
350 .Li SLIST_CLASS_ENTRY ,
351 .Li STAILQ_CLASS_ENTRY ,
352 .Li LIST_CLASS_ENTRY ,
354 .Li TAILQ_CLASS_ENTRY .
357 is the name of a user defined structure that must be declared
360 .Li SLIST_CLASS_HEAD ,
362 .Li STAILQ_CLASS_HEAD ,
364 .Li LIST_CLASS_HEAD ,
367 .Li TAILQ_CLASS_HEAD .
368 See the examples below for further explanation of how these
370 .Sh SINGLY-LINKED LISTS
371 A singly-linked list is headed by a structure defined by the
374 This structure contains a single pointer to the first element
376 The elements are singly linked for minimum space and pointer manipulation
377 overhead at the expense of O(n) removal for arbitrary elements.
378 New elements can be added to the list after an existing element or
379 at the head of the list.
382 structure is declared as follows:
383 .Bd -literal -offset indent
384 SLIST_HEAD(HEADNAME, TYPE) head;
389 is the name of the structure to be defined, and
391 is the type of the elements to be linked into the list.
392 A pointer to the head of the list can later be declared as:
393 .Bd -literal -offset indent
394 struct HEADNAME *headp;
401 are user selectable.)
404 .Nm SLIST_HEAD_INITIALIZER
405 evaluates to an initializer for the list
410 concatenates the list headed by
412 onto the end of the one headed by
414 removing all entries from the former.
415 Use of this macro should be avoided as it traverses the entirety of the
418 A singly-linked tail queue should be used if this macro is needed in
419 high-usage code paths or to operate on long lists.
423 evaluates to true if there are no elements in the list.
427 declares a structure that connects the elements in
432 returns the first element in the list or NULL if the list is empty.
436 traverses the list referenced by
438 in the forward direction, assigning each element in
443 .Nm SLIST_FOREACH_FROM
444 behaves identically to
448 is NULL, else it treats
450 as a previously found SLIST element and begins the loop at
452 instead of the first element in the SLIST referenced by
456 .Nm SLIST_FOREACH_SAFE
457 traverses the list referenced by
459 in the forward direction, assigning each element in
464 here it is permitted to both remove
466 as well as free it from within the loop safely without interfering with the
470 .Nm SLIST_FOREACH_FROM_SAFE
471 behaves identically to
472 .Nm SLIST_FOREACH_SAFE
475 is NULL, else it treats
477 as a previously found SLIST element and begins the loop at
479 instead of the first element in the SLIST referenced by
484 initializes the list referenced by
488 .Nm SLIST_INSERT_HEAD
489 inserts the new element
491 at the head of the list.
494 .Nm SLIST_INSERT_AFTER
495 inserts the new element
502 returns the next element in the list.
505 .Nm SLIST_REMOVE_AFTER
506 removes the element after
511 this macro does not traverse the entire list.
514 .Nm SLIST_REMOVE_HEAD
517 from the head of the list.
518 For optimum efficiency,
519 elements being removed from the head of the list should explicitly use
520 this macro instead of the generic
529 Use of this macro should be avoided as it traverses the entire list.
530 A doubly-linked list should be used if this macro is needed in
531 high-usage code paths or to operate on long lists.
535 swaps the contents of
539 .Sh SINGLY-LINKED LIST EXAMPLE
541 SLIST_HEAD(slisthead, entry) head =
542 SLIST_HEAD_INITIALIZER(head);
543 struct slisthead *headp; /* Singly-linked List head. */
546 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
548 } *n1, *n2, *n3, *np;
550 SLIST_INIT(&head); /* Initialize the list. */
552 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
553 SLIST_INSERT_HEAD(&head, n1, entries);
555 n2 = malloc(sizeof(struct entry)); /* Insert after. */
556 SLIST_INSERT_AFTER(n1, n2, entries);
558 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
561 n3 = SLIST_FIRST(&head);
562 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
564 /* Forward traversal. */
565 SLIST_FOREACH(np, &head, entries)
567 /* Safe forward traversal. */
568 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
571 SLIST_REMOVE(&head, np, entry, entries);
575 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
576 n1 = SLIST_FIRST(&head);
577 SLIST_REMOVE_HEAD(&head, entries);
581 .Sh SINGLY-LINKED TAIL QUEUES
582 A singly-linked tail queue is headed by a structure defined by the
585 This structure contains a pair of pointers,
586 one to the first element in the tail queue and the other to
587 the last element in the tail queue.
588 The elements are singly linked for minimum space and pointer
589 manipulation overhead at the expense of O(n) removal for arbitrary
591 New elements can be added to the tail queue after an existing element,
592 at the head of the tail queue, or at the end of the tail queue.
595 structure is declared as follows:
596 .Bd -literal -offset indent
597 STAILQ_HEAD(HEADNAME, TYPE) head;
602 is the name of the structure to be defined, and
604 is the type of the elements to be linked into the tail queue.
605 A pointer to the head of the tail queue can later be declared as:
606 .Bd -literal -offset indent
607 struct HEADNAME *headp;
614 are user selectable.)
617 .Nm STAILQ_HEAD_INITIALIZER
618 evaluates to an initializer for the tail queue
623 concatenates the tail queue headed by
625 onto the end of the one headed by
627 removing all entries from the former.
631 evaluates to true if there are no items on the tail queue.
635 declares a structure that connects the elements in
640 returns the first item on the tail queue or NULL if the tail queue
645 traverses the tail queue referenced by
647 in the forward direction, assigning each element
652 .Nm STAILQ_FOREACH_FROM
653 behaves identically to
657 is NULL, else it treats
659 as a previously found STAILQ element and begins the loop at
661 instead of the first element in the STAILQ referenced by
665 .Nm STAILQ_FOREACH_SAFE
666 traverses the tail queue referenced by
668 in the forward direction, assigning each element
673 here it is permitted to both remove
675 as well as free it from within the loop safely without interfering with the
679 .Nm STAILQ_FOREACH_FROM_SAFE
680 behaves identically to
681 .Nm STAILQ_FOREACH_SAFE
684 is NULL, else it treats
686 as a previously found STAILQ element and begins the loop at
688 instead of the first element in the STAILQ referenced by
693 initializes the tail queue referenced by
697 .Nm STAILQ_INSERT_HEAD
698 inserts the new element
700 at the head of the tail queue.
703 .Nm STAILQ_INSERT_TAIL
704 inserts the new element
706 at the end of the tail queue.
709 .Nm STAILQ_INSERT_AFTER
710 inserts the new element
717 returns the last item on the tail queue.
718 If the tail queue is empty the return value is
723 returns the next item on the tail queue, or NULL this item is the last.
726 .Nm STAILQ_REMOVE_AFTER
727 removes the element after
732 this macro does not traverse the entire tail queue.
735 .Nm STAILQ_REMOVE_HEAD
736 removes the element at the head of the tail queue.
737 For optimum efficiency,
738 elements being removed from the head of the tail queue should
739 use this macro explicitly rather than the generic
748 Use of this macro should be avoided as it traverses the entire list.
749 A doubly-linked tail queue should be used if this macro is needed in
750 high-usage code paths or to operate on long tail queues.
754 swaps the contents of
758 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
760 STAILQ_HEAD(stailhead, entry) head =
761 STAILQ_HEAD_INITIALIZER(head);
762 struct stailhead *headp; /* Singly-linked tail queue head. */
765 STAILQ_ENTRY(entry) entries; /* Tail queue. */
767 } *n1, *n2, *n3, *np;
769 STAILQ_INIT(&head); /* Initialize the queue. */
771 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
772 STAILQ_INSERT_HEAD(&head, n1, entries);
774 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
775 STAILQ_INSERT_TAIL(&head, n1, entries);
777 n2 = malloc(sizeof(struct entry)); /* Insert after. */
778 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
780 STAILQ_REMOVE(&head, n2, entry, entries);
782 /* Deletion from the head. */
783 n3 = STAILQ_FIRST(&head);
784 STAILQ_REMOVE_HEAD(&head, entries);
786 /* Forward traversal. */
787 STAILQ_FOREACH(np, &head, entries)
789 /* Safe forward traversal. */
790 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
793 STAILQ_REMOVE(&head, np, entry, entries);
796 /* TailQ Deletion. */
797 while (!STAILQ_EMPTY(&head)) {
798 n1 = STAILQ_FIRST(&head);
799 STAILQ_REMOVE_HEAD(&head, entries);
802 /* Faster TailQ Deletion. */
803 n1 = STAILQ_FIRST(&head);
805 n2 = STAILQ_NEXT(n1, entries);
812 A list is headed by a structure defined by the
815 This structure contains a single pointer to the first element
817 The elements are doubly linked so that an arbitrary element can be
818 removed without traversing the list.
819 New elements can be added to the list after an existing element,
820 before an existing element, or at the head of the list.
823 structure is declared as follows:
824 .Bd -literal -offset indent
825 LIST_HEAD(HEADNAME, TYPE) head;
830 is the name of the structure to be defined, and
832 is the type of the elements to be linked into the list.
833 A pointer to the head of the list can later be declared as:
834 .Bd -literal -offset indent
835 struct HEADNAME *headp;
842 are user selectable.)
845 .Nm LIST_HEAD_INITIALIZER
846 evaluates to an initializer for the list
851 concatenates the list headed by
853 onto the end of the one headed by
855 removing all entries from the former.
856 Use of this macro should be avoided as it traverses the entirety of the
859 A tail queue should be used if this macro is needed in
860 high-usage code paths or to operate on long lists.
864 evaluates to true if there are no elements in the list.
868 declares a structure that connects the elements in
873 returns the first element in the list or NULL if the list
878 traverses the list referenced by
880 in the forward direction, assigning each element in turn to
884 .Nm LIST_FOREACH_FROM
885 behaves identically to
889 is NULL, else it treats
891 as a previously found LIST element and begins the loop at
893 instead of the first element in the LIST referenced by
897 .Nm LIST_FOREACH_SAFE
898 traverses the list referenced by
900 in the forward direction, assigning each element in turn to
904 here it is permitted to both remove
906 as well as free it from within the loop safely without interfering with the
910 .Nm LIST_FOREACH_FROM_SAFE
911 behaves identically to
912 .Nm LIST_FOREACH_SAFE
915 is NULL, else it treats
917 as a previously found LIST element and begins the loop at
919 instead of the first element in the LIST referenced by
924 initializes the list referenced by
929 inserts the new element
931 at the head of the list.
934 .Nm LIST_INSERT_AFTER
935 inserts the new element
941 .Nm LIST_INSERT_BEFORE
942 inserts the new element
949 returns the next element in the list, or NULL if this is the last.
953 returns the previous element in the list, or NULL if this is the first.
967 swaps the contents of
973 LIST_HEAD(listhead, entry) head =
974 LIST_HEAD_INITIALIZER(head);
975 struct listhead *headp; /* List head. */
978 LIST_ENTRY(entry) entries; /* List. */
980 } *n1, *n2, *n3, *np, *np_temp;
982 LIST_INIT(&head); /* Initialize the list. */
984 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
985 LIST_INSERT_HEAD(&head, n1, entries);
987 n2 = malloc(sizeof(struct entry)); /* Insert after. */
988 LIST_INSERT_AFTER(n1, n2, entries);
990 n3 = malloc(sizeof(struct entry)); /* Insert before. */
991 LIST_INSERT_BEFORE(n2, n3, entries);
993 LIST_REMOVE(n2, entries); /* Deletion. */
995 /* Forward traversal. */
996 LIST_FOREACH(np, &head, entries)
999 /* Safe forward traversal. */
1000 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
1003 LIST_REMOVE(np, entries);
1007 while (!LIST_EMPTY(&head)) { /* List Deletion. */
1008 n1 = LIST_FIRST(&head);
1009 LIST_REMOVE(n1, entries);
1013 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
1014 while (n1 != NULL) {
1015 n2 = LIST_NEXT(n1, entries);
1022 A tail queue is headed by a structure defined by the
1025 This structure contains a pair of pointers,
1026 one to the first element in the tail queue and the other to
1027 the last element in the tail queue.
1028 The elements are doubly linked so that an arbitrary element can be
1029 removed without traversing the tail queue.
1030 New elements can be added to the tail queue after an existing element,
1031 before an existing element, at the head of the tail queue,
1032 or at the end of the tail queue.
1035 structure is declared as follows:
1036 .Bd -literal -offset indent
1037 TAILQ_HEAD(HEADNAME, TYPE) head;
1042 is the name of the structure to be defined, and
1044 is the type of the elements to be linked into the tail queue.
1045 A pointer to the head of the tail queue can later be declared as:
1046 .Bd -literal -offset indent
1047 struct HEADNAME *headp;
1054 are user selectable.)
1057 .Nm TAILQ_HEAD_INITIALIZER
1058 evaluates to an initializer for the tail queue
1063 concatenates the tail queue headed by
1065 onto the end of the one headed by
1067 removing all entries from the former.
1071 evaluates to true if there are no items on the tail queue.
1075 declares a structure that connects the elements in
1080 returns the first item on the tail queue or NULL if the tail queue
1085 traverses the tail queue referenced by
1087 in the forward direction, assigning each element in turn to
1092 if the loop completes normally, or if there were no elements.
1095 .Nm TAILQ_FOREACH_FROM
1096 behaves identically to
1100 is NULL, else it treats
1102 as a previously found TAILQ element and begins the loop at
1104 instead of the first element in the TAILQ referenced by
1108 .Nm TAILQ_FOREACH_REVERSE
1109 traverses the tail queue referenced by
1111 in the reverse direction, assigning each element in turn to
1115 .Nm TAILQ_FOREACH_REVERSE_FROM
1116 behaves identically to
1117 .Nm TAILQ_FOREACH_REVERSE
1120 is NULL, else it treats
1122 as a previously found TAILQ element and begins the reverse loop at
1124 instead of the last element in the TAILQ referenced by
1128 .Nm TAILQ_FOREACH_SAFE
1130 .Nm TAILQ_FOREACH_REVERSE_SAFE
1131 traverse the list referenced by
1133 in the forward or reverse direction respectively,
1134 assigning each element in turn to
1136 However, unlike their unsafe counterparts,
1139 .Nm TAILQ_FOREACH_REVERSE
1140 permit to both remove
1142 as well as free it from within the loop safely without interfering with the
1146 .Nm TAILQ_FOREACH_FROM_SAFE
1147 behaves identically to
1148 .Nm TAILQ_FOREACH_SAFE
1151 is NULL, else it treats
1153 as a previously found TAILQ element and begins the loop at
1155 instead of the first element in the TAILQ referenced by
1159 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1160 behaves identically to
1161 .Nm TAILQ_FOREACH_REVERSE_SAFE
1164 is NULL, else it treats
1166 as a previously found TAILQ element and begins the reverse loop at
1168 instead of the last element in the TAILQ referenced by
1173 initializes the tail queue referenced by
1177 .Nm TAILQ_INSERT_HEAD
1178 inserts the new element
1180 at the head of the tail queue.
1183 .Nm TAILQ_INSERT_TAIL
1184 inserts the new element
1186 at the end of the tail queue.
1189 .Nm TAILQ_INSERT_AFTER
1190 inserts the new element
1196 .Nm TAILQ_INSERT_BEFORE
1197 inserts the new element
1204 returns the last item on the tail queue.
1205 If the tail queue is empty the return value is
1210 returns the next item on the tail queue, or NULL if this item is the last.
1214 returns the previous item on the tail queue, or NULL if this item
1221 from the tail queue.
1225 swaps the contents of
1229 .Sh TAIL QUEUE EXAMPLE
1231 TAILQ_HEAD(tailhead, entry) head =
1232 TAILQ_HEAD_INITIALIZER(head);
1233 struct tailhead *headp; /* Tail queue head. */
1236 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1238 } *n1, *n2, *n3, *np;
1240 TAILQ_INIT(&head); /* Initialize the queue. */
1242 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1243 TAILQ_INSERT_HEAD(&head, n1, entries);
1245 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1246 TAILQ_INSERT_TAIL(&head, n1, entries);
1248 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1249 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1251 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1252 TAILQ_INSERT_BEFORE(n2, n3, entries);
1254 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1256 /* Forward traversal. */
1257 TAILQ_FOREACH(np, &head, entries)
1259 /* Safe forward traversal. */
1260 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1263 TAILQ_REMOVE(&head, np, entries);
1266 /* Reverse traversal. */
1267 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1269 /* TailQ Deletion. */
1270 while (!TAILQ_EMPTY(&head)) {
1271 n1 = TAILQ_FIRST(&head);
1272 TAILQ_REMOVE(&head, n1, entries);
1275 /* Faster TailQ Deletion. */
1276 n1 = TAILQ_FIRST(&head);
1277 while (n1 != NULL) {
1278 n2 = TAILQ_NEXT(n1, entries);
1287 it can be useful to trace queue changes.
1288 To enable tracing, define the macro
1289 .Va QUEUE_MACRO_DEBUG_TRACE
1292 It can also be useful to trash pointers that have been unlinked from a queue,
1293 to detect use after removal.
1294 To enable pointer trashing, define the macro
1295 .Va QUEUE_MACRO_DEBUG_TRASH
1298 .Fn QMD_IS_TRASHED "void *ptr"
1301 has been trashed by the
1302 .Va QUEUE_MACRO_DEBUG_TRASH
1308 .Fn SLIST_REMOVE_PREVPTR
1309 macro is available to aid debugging:
1310 .Bl -hang -offset indent
1311 .It Fn SLIST_REMOVE_PREVPTR "TYPE **prev" "TYPE *elm" "SLIST_ENTRY NAME"
1315 which must directly follow the element whose
1320 This macro validates that
1334 functions first appeared in