2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
39 .Nm SLIST_CLASS_ENTRY ,
40 .Nm SLIST_CLASS_HEAD ,
45 .Nm SLIST_FOREACH_FROM ,
46 .Nm SLIST_FOREACH_FROM_SAFE ,
47 .Nm SLIST_FOREACH_SAFE ,
49 .Nm SLIST_HEAD_INITIALIZER ,
51 .Nm SLIST_INSERT_AFTER ,
52 .Nm SLIST_INSERT_HEAD ,
55 .Nm SLIST_REMOVE_AFTER ,
56 .Nm SLIST_REMOVE_HEAD ,
58 .Nm STAILQ_CLASS_ENTRY ,
59 .Nm STAILQ_CLASS_HEAD ,
65 .Nm STAILQ_FOREACH_FROM ,
66 .Nm STAILQ_FOREACH_FROM_SAFE ,
67 .Nm STAILQ_FOREACH_SAFE ,
69 .Nm STAILQ_HEAD_INITIALIZER ,
71 .Nm STAILQ_INSERT_AFTER ,
72 .Nm STAILQ_INSERT_HEAD ,
73 .Nm STAILQ_INSERT_TAIL ,
77 .Nm STAILQ_REMOVE_AFTER ,
78 .Nm STAILQ_REMOVE_HEAD ,
80 .Nm LIST_CLASS_ENTRY ,
86 .Nm LIST_FOREACH_FROM ,
87 .Nm LIST_FOREACH_FROM_SAFE ,
88 .Nm LIST_FOREACH_SAFE ,
90 .Nm LIST_HEAD_INITIALIZER ,
92 .Nm LIST_INSERT_AFTER ,
93 .Nm LIST_INSERT_BEFORE ,
94 .Nm LIST_INSERT_HEAD ,
99 .Nm TAILQ_CLASS_ENTRY ,
100 .Nm TAILQ_CLASS_HEAD ,
106 .Nm TAILQ_FOREACH_FROM ,
107 .Nm TAILQ_FOREACH_FROM_SAFE ,
108 .Nm TAILQ_FOREACH_REVERSE ,
109 .Nm TAILQ_FOREACH_REVERSE_FROM ,
110 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
111 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
112 .Nm TAILQ_FOREACH_SAFE ,
114 .Nm TAILQ_HEAD_INITIALIZER ,
116 .Nm TAILQ_INSERT_AFTER ,
117 .Nm TAILQ_INSERT_BEFORE ,
118 .Nm TAILQ_INSERT_HEAD ,
119 .Nm TAILQ_INSERT_TAIL ,
125 .Nd implementations of singly-linked lists, singly-linked tail queues,
126 lists and tail queues
130 .Fn SLIST_CLASS_ENTRY "CLASSTYPE"
131 .Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
132 .Fn SLIST_EMPTY "SLIST_HEAD *head"
133 .Fn SLIST_ENTRY "TYPE"
134 .Fn SLIST_FIRST "SLIST_HEAD *head"
135 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
136 .Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
137 .Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
138 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
139 .Fn SLIST_HEAD "HEADNAME" "TYPE"
140 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
141 .Fn SLIST_INIT "SLIST_HEAD *head"
142 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
143 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
144 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
145 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
146 .Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
147 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
148 .Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
150 .Fn STAILQ_CLASS_ENTRY "CLASSTYPE"
151 .Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
152 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
153 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
154 .Fn STAILQ_ENTRY "TYPE"
155 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
156 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
157 .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
158 .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
159 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
160 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
161 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
162 .Fn STAILQ_INIT "STAILQ_HEAD *head"
163 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
164 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
165 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
166 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
167 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
168 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
169 .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
170 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
171 .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
173 .Fn LIST_CLASS_ENTRY "CLASSTYPE"
174 .Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
175 .Fn LIST_EMPTY "LIST_HEAD *head"
176 .Fn LIST_ENTRY "TYPE"
177 .Fn LIST_FIRST "LIST_HEAD *head"
178 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
179 .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
180 .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
181 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
182 .Fn LIST_HEAD "HEADNAME" "TYPE"
183 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
184 .Fn LIST_INIT "LIST_HEAD *head"
185 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
186 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
187 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
188 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
189 .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
190 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
191 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
193 .Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
194 .Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
195 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
196 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
197 .Fn TAILQ_ENTRY "TYPE"
198 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
199 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
200 .Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
201 .Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
202 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
203 .Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
204 .Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
205 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
206 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
207 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
208 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
209 .Fn TAILQ_INIT "TAILQ_HEAD *head"
210 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
211 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
212 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
213 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
214 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
215 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
216 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
217 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
218 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
221 These macros define and operate on four types of data structures which
222 can be used in both C and C++ source code:
223 .Bl -enum -compact -offset indent
229 Singly-linked tail queues
233 All four structures support the following functionality:
234 .Bl -enum -compact -offset indent
236 Insertion of a new entry at the head of the list.
238 Insertion of a new entry after any element in the list.
240 O(1) removal of an entry from the head of the list.
242 Forward traversal through the list.
244 Swapping the contents of two lists.
247 Singly-linked lists are the simplest of the four data structures
248 and support only the above functionality.
249 Singly-linked lists are ideal for applications with large datasets
250 and few or no removals,
251 or for implementing a LIFO queue.
252 Singly-linked lists add the following functionality:
253 .Bl -enum -compact -offset indent
255 O(n) removal of any entry in the list.
258 Singly-linked tail queues add the following functionality:
259 .Bl -enum -compact -offset indent
261 Entries can be added at the end of a list.
263 O(n) removal of any entry in the list.
265 They may be concatenated.
268 .Bl -enum -compact -offset indent
270 All list insertions must specify the head of the list.
272 Each head entry requires two pointers rather than one.
274 Code size is about 15% greater and operations run about 20% slower
275 than singly-linked lists.
278 Singly-linked tail queues are ideal for applications with large datasets and
280 or for implementing a FIFO queue.
282 All doubly linked types of data structures (lists and tail queues)
284 .Bl -enum -compact -offset indent
286 Insertion of a new entry before any element in the list.
288 O(1) removal of any entry in the list.
291 .Bl -enum -compact -offset indent
293 Each element requires two pointers rather than one.
295 Code size and execution time of operations (except for removal) is about
296 twice that of the singly-linked data-structures.
299 Linked lists are the simplest of the doubly linked data structures.
300 They add the following functionality over the above:
301 .Bl -enum -compact -offset indent
303 They may be traversed backwards.
306 .Bl -enum -compact -offset indent
308 To traverse backwards, an entry to begin the traversal and the list in
309 which it is contained must be specified.
312 Tail queues add the following functionality:
313 .Bl -enum -compact -offset indent
315 Entries can be added at the end of a list.
317 They may be traversed backwards, from tail to head.
319 They may be concatenated.
322 .Bl -enum -compact -offset indent
324 All list insertions and removals must specify the head of the list.
326 Each head entry requires two pointers rather than one.
328 Code size is about 15% greater and operations run about 20% slower
329 than singly-linked lists.
332 In the macro definitions,
334 is the name of a user defined structure.
335 The structure must contain a field called
343 In the macro definitions,
345 is the name of a user defined class.
346 The class must contain a field called
349 .Li SLIST_CLASS_ENTRY ,
350 .Li STAILQ_CLASS_ENTRY ,
351 .Li LIST_CLASS_ENTRY ,
353 .Li TAILQ_CLASS_ENTRY .
356 is the name of a user defined structure that must be declared
359 .Li SLIST_CLASS_HEAD ,
361 .Li STAILQ_CLASS_HEAD ,
363 .Li LIST_CLASS_HEAD ,
366 .Li TAILQ_CLASS_HEAD .
367 See the examples below for further explanation of how these
369 .Sh SINGLY-LINKED LISTS
370 A singly-linked list is headed by a structure defined by the
373 This structure contains a single pointer to the first element
375 The elements are singly linked for minimum space and pointer manipulation
376 overhead at the expense of O(n) removal for arbitrary elements.
377 New elements can be added to the list after an existing element or
378 at the head of the list.
381 structure is declared as follows:
382 .Bd -literal -offset indent
383 SLIST_HEAD(HEADNAME, TYPE) head;
388 is the name of the structure to be defined, and
390 is the type of the elements to be linked into the list.
391 A pointer to the head of the list can later be declared as:
392 .Bd -literal -offset indent
393 struct HEADNAME *headp;
400 are user selectable.)
403 .Nm SLIST_HEAD_INITIALIZER
404 evaluates to an initializer for the list
409 evaluates to true if there are no elements in the list.
413 declares a structure that connects the elements in
418 returns the first element in the list or NULL if the list is empty.
422 traverses the list referenced by
424 in the forward direction, assigning each element in
429 .Nm SLIST_FOREACH_FROM
430 behaves identically to
434 is NULL, else it treats
436 as a previously found SLIST element and begins the loop at
438 instead of the first element in the SLIST referenced by
442 .Nm SLIST_FOREACH_SAFE
443 traverses the list referenced by
445 in the forward direction, assigning each element in
450 here it is permitted to both remove
452 as well as free it from within the loop safely without interfering with the
456 .Nm SLIST_FOREACH_FROM_SAFE
457 behaves identically to
458 .Nm SLIST_FOREACH_SAFE
461 is NULL, else it treats
463 as a previously found SLIST element and begins the loop at
465 instead of the first element in the SLIST referenced by
470 initializes the list referenced by
474 .Nm SLIST_INSERT_HEAD
475 inserts the new element
477 at the head of the list.
480 .Nm SLIST_INSERT_AFTER
481 inserts the new element
488 returns the next element in the list.
491 .Nm SLIST_REMOVE_AFTER
492 removes the element after
494 from the list. Unlike
496 this macro does not traverse the entire list.
499 .Nm SLIST_REMOVE_HEAD
502 from the head of the list.
503 For optimum efficiency,
504 elements being removed from the head of the list should explicitly use
505 this macro instead of the generic
517 swaps the contents of
521 .Sh SINGLY-LINKED LIST EXAMPLE
523 SLIST_HEAD(slisthead, entry) head =
524 SLIST_HEAD_INITIALIZER(head);
525 struct slisthead *headp; /* Singly-linked List head. */
528 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
530 } *n1, *n2, *n3, *np;
532 SLIST_INIT(&head); /* Initialize the list. */
534 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
535 SLIST_INSERT_HEAD(&head, n1, entries);
537 n2 = malloc(sizeof(struct entry)); /* Insert after. */
538 SLIST_INSERT_AFTER(n1, n2, entries);
540 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
543 n3 = SLIST_FIRST(&head);
544 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
546 /* Forward traversal. */
547 SLIST_FOREACH(np, &head, entries)
549 /* Safe forward traversal. */
550 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
553 SLIST_REMOVE(&head, np, entry, entries);
557 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
558 n1 = SLIST_FIRST(&head);
559 SLIST_REMOVE_HEAD(&head, entries);
563 .Sh SINGLY-LINKED TAIL QUEUES
564 A singly-linked tail queue is headed by a structure defined by the
567 This structure contains a pair of pointers,
568 one to the first element in the tail queue and the other to
569 the last element in the tail queue.
570 The elements are singly linked for minimum space and pointer
571 manipulation overhead at the expense of O(n) removal for arbitrary
573 New elements can be added to the tail queue after an existing element,
574 at the head of the tail queue, or at the end of the tail queue.
577 structure is declared as follows:
578 .Bd -literal -offset indent
579 STAILQ_HEAD(HEADNAME, TYPE) head;
584 is the name of the structure to be defined, and
586 is the type of the elements to be linked into the tail queue.
587 A pointer to the head of the tail queue can later be declared as:
588 .Bd -literal -offset indent
589 struct HEADNAME *headp;
596 are user selectable.)
599 .Nm STAILQ_HEAD_INITIALIZER
600 evaluates to an initializer for the tail queue
605 concatenates the tail queue headed by
607 onto the end of the one headed by
609 removing all entries from the former.
613 evaluates to true if there are no items on the tail queue.
617 declares a structure that connects the elements in
622 returns the first item on the tail queue or NULL if the tail queue
627 traverses the tail queue referenced by
629 in the forward direction, assigning each element
634 .Nm STAILQ_FOREACH_FROM
635 behaves identically to
639 is NULL, else it treats
641 as a previously found STAILQ element and begins the loop at
643 instead of the first element in the STAILQ referenced by
647 .Nm STAILQ_FOREACH_SAFE
648 traverses the tail queue referenced by
650 in the forward direction, assigning each element
655 here it is permitted to both remove
657 as well as free it from within the loop safely without interfering with the
661 .Nm STAILQ_FOREACH_FROM_SAFE
662 behaves identically to
663 .Nm STAILQ_FOREACH_SAFE
666 is NULL, else it treats
668 as a previously found STAILQ element and begins the loop at
670 instead of the first element in the STAILQ referenced by
675 initializes the tail queue referenced by
679 .Nm STAILQ_INSERT_HEAD
680 inserts the new element
682 at the head of the tail queue.
685 .Nm STAILQ_INSERT_TAIL
686 inserts the new element
688 at the end of the tail queue.
691 .Nm STAILQ_INSERT_AFTER
692 inserts the new element
699 returns the last item on the tail queue.
700 If the tail queue is empty the return value is
705 returns the next item on the tail queue, or NULL this item is the last.
708 .Nm STAILQ_REMOVE_AFTER
709 removes the element after
711 from the tail queue. Unlike
713 this macro does not traverse the entire tail queue.
716 .Nm STAILQ_REMOVE_HEAD
717 removes the element at the head of the tail queue.
718 For optimum efficiency,
719 elements being removed from the head of the tail queue should
720 use this macro explicitly rather than the generic
732 swaps the contents of
736 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
738 STAILQ_HEAD(stailhead, entry) head =
739 STAILQ_HEAD_INITIALIZER(head);
740 struct stailhead *headp; /* Singly-linked tail queue head. */
743 STAILQ_ENTRY(entry) entries; /* Tail queue. */
745 } *n1, *n2, *n3, *np;
747 STAILQ_INIT(&head); /* Initialize the queue. */
749 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
750 STAILQ_INSERT_HEAD(&head, n1, entries);
752 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
753 STAILQ_INSERT_TAIL(&head, n1, entries);
755 n2 = malloc(sizeof(struct entry)); /* Insert after. */
756 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
758 STAILQ_REMOVE(&head, n2, entry, entries);
760 /* Deletion from the head. */
761 n3 = STAILQ_FIRST(&head);
762 STAILQ_REMOVE_HEAD(&head, entries);
764 /* Forward traversal. */
765 STAILQ_FOREACH(np, &head, entries)
767 /* Safe forward traversal. */
768 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
771 STAILQ_REMOVE(&head, np, entry, entries);
774 /* TailQ Deletion. */
775 while (!STAILQ_EMPTY(&head)) {
776 n1 = STAILQ_FIRST(&head);
777 STAILQ_REMOVE_HEAD(&head, entries);
780 /* Faster TailQ Deletion. */
781 n1 = STAILQ_FIRST(&head);
783 n2 = STAILQ_NEXT(n1, entries);
790 A list is headed by a structure defined by the
793 This structure contains a single pointer to the first element
795 The elements are doubly linked so that an arbitrary element can be
796 removed without traversing the list.
797 New elements can be added to the list after an existing element,
798 before an existing element, or at the head of the list.
801 structure is declared as follows:
802 .Bd -literal -offset indent
803 LIST_HEAD(HEADNAME, TYPE) head;
808 is the name of the structure to be defined, and
810 is the type of the elements to be linked into the list.
811 A pointer to the head of the list can later be declared as:
812 .Bd -literal -offset indent
813 struct HEADNAME *headp;
820 are user selectable.)
823 .Nm LIST_HEAD_INITIALIZER
824 evaluates to an initializer for the list
829 evaluates to true if there are no elements in the list.
833 declares a structure that connects the elements in
838 returns the first element in the list or NULL if the list
843 traverses the list referenced by
845 in the forward direction, assigning each element in turn to
849 .Nm LIST_FOREACH_FROM
850 behaves identically to
854 is NULL, else it treats
856 as a previously found LIST element and begins the loop at
858 instead of the first element in the LIST referenced by
862 .Nm LIST_FOREACH_SAFE
863 traverses the list referenced by
865 in the forward direction, assigning each element in turn to
869 here it is permitted to both remove
871 as well as free it from within the loop safely without interfering with the
875 .Nm LIST_FOREACH_FROM_SAFE
876 behaves identically to
877 .Nm LIST_FOREACH_SAFE
880 is NULL, else it treats
882 as a previously found LIST element and begins the loop at
884 instead of the first element in the LIST referenced by
889 initializes the list referenced by
894 inserts the new element
896 at the head of the list.
899 .Nm LIST_INSERT_AFTER
900 inserts the new element
906 .Nm LIST_INSERT_BEFORE
907 inserts the new element
914 returns the next element in the list, or NULL if this is the last.
918 returns the previous element in the list, or NULL if this is the first.
932 swaps the contents of
938 LIST_HEAD(listhead, entry) head =
939 LIST_HEAD_INITIALIZER(head);
940 struct listhead *headp; /* List head. */
943 LIST_ENTRY(entry) entries; /* List. */
945 } *n1, *n2, *n3, *np, *np_temp;
947 LIST_INIT(&head); /* Initialize the list. */
949 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
950 LIST_INSERT_HEAD(&head, n1, entries);
952 n2 = malloc(sizeof(struct entry)); /* Insert after. */
953 LIST_INSERT_AFTER(n1, n2, entries);
955 n3 = malloc(sizeof(struct entry)); /* Insert before. */
956 LIST_INSERT_BEFORE(n2, n3, entries);
958 LIST_REMOVE(n2, entries); /* Deletion. */
960 /* Forward traversal. */
961 LIST_FOREACH(np, &head, entries)
964 /* Safe forward traversal. */
965 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
968 LIST_REMOVE(np, entries);
972 while (!LIST_EMPTY(&head)) { /* List Deletion. */
973 n1 = LIST_FIRST(&head);
974 LIST_REMOVE(n1, entries);
978 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
980 n2 = LIST_NEXT(n1, entries);
987 A tail queue is headed by a structure defined by the
990 This structure contains a pair of pointers,
991 one to the first element in the tail queue and the other to
992 the last element in the tail queue.
993 The elements are doubly linked so that an arbitrary element can be
994 removed without traversing the tail queue.
995 New elements can be added to the tail queue after an existing element,
996 before an existing element, at the head of the tail queue,
997 or at the end of the tail queue.
1000 structure is declared as follows:
1001 .Bd -literal -offset indent
1002 TAILQ_HEAD(HEADNAME, TYPE) head;
1007 is the name of the structure to be defined, and
1009 is the type of the elements to be linked into the tail queue.
1010 A pointer to the head of the tail queue can later be declared as:
1011 .Bd -literal -offset indent
1012 struct HEADNAME *headp;
1019 are user selectable.)
1022 .Nm TAILQ_HEAD_INITIALIZER
1023 evaluates to an initializer for the tail queue
1028 concatenates the tail queue headed by
1030 onto the end of the one headed by
1032 removing all entries from the former.
1036 evaluates to true if there are no items on the tail queue.
1040 declares a structure that connects the elements in
1045 returns the first item on the tail queue or NULL if the tail queue
1050 traverses the tail queue referenced by
1052 in the forward direction, assigning each element in turn to
1057 if the loop completes normally, or if there were no elements.
1060 .Nm TAILQ_FOREACH_FROM
1061 behaves identically to
1065 is NULL, else it treats
1067 as a previously found TAILQ element and begins the loop at
1069 instead of the first element in the TAILQ referenced by
1073 .Nm TAILQ_FOREACH_REVERSE
1074 traverses the tail queue referenced by
1076 in the reverse direction, assigning each element in turn to
1080 .Nm TAILQ_FOREACH_REVERSE_FROM
1081 behaves identically to
1082 .Nm TAILQ_FOREACH_REVERSE
1085 is NULL, else it treats
1087 as a previously found TAILQ element and begins the reverse loop at
1089 instead of the last element in the TAILQ referenced by
1093 .Nm TAILQ_FOREACH_SAFE
1095 .Nm TAILQ_FOREACH_REVERSE_SAFE
1096 traverse the list referenced by
1098 in the forward or reverse direction respectively,
1099 assigning each element in turn to
1101 However, unlike their unsafe counterparts,
1104 .Nm TAILQ_FOREACH_REVERSE
1105 permit to both remove
1107 as well as free it from within the loop safely without interfering with the
1111 .Nm TAILQ_FOREACH_FROM_SAFE
1112 behaves identically to
1113 .Nm TAILQ_FOREACH_SAFE
1116 is NULL, else it treats
1118 as a previously found TAILQ element and begins the loop at
1120 instead of the first element in the TAILQ referenced by
1124 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1125 behaves identically to
1126 .Nm TAILQ_FOREACH_REVERSE_SAFE
1129 is NULL, else it treats
1131 as a previously found TAILQ element and begins the reverse loop at
1133 instead of the last element in the TAILQ referenced by
1138 initializes the tail queue referenced by
1142 .Nm TAILQ_INSERT_HEAD
1143 inserts the new element
1145 at the head of the tail queue.
1148 .Nm TAILQ_INSERT_TAIL
1149 inserts the new element
1151 at the end of the tail queue.
1154 .Nm TAILQ_INSERT_AFTER
1155 inserts the new element
1161 .Nm TAILQ_INSERT_BEFORE
1162 inserts the new element
1169 returns the last item on the tail queue.
1170 If the tail queue is empty the return value is
1175 returns the next item on the tail queue, or NULL if this item is the last.
1179 returns the previous item on the tail queue, or NULL if this item
1186 from the tail queue.
1190 swaps the contents of
1194 .Sh TAIL QUEUE EXAMPLE
1196 TAILQ_HEAD(tailhead, entry) head =
1197 TAILQ_HEAD_INITIALIZER(head);
1198 struct tailhead *headp; /* Tail queue head. */
1201 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1203 } *n1, *n2, *n3, *np;
1205 TAILQ_INIT(&head); /* Initialize the queue. */
1207 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1208 TAILQ_INSERT_HEAD(&head, n1, entries);
1210 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1211 TAILQ_INSERT_TAIL(&head, n1, entries);
1213 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1214 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1216 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1217 TAILQ_INSERT_BEFORE(n2, n3, entries);
1219 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1221 /* Forward traversal. */
1222 TAILQ_FOREACH(np, &head, entries)
1224 /* Safe forward traversal. */
1225 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1228 TAILQ_REMOVE(&head, np, entries);
1231 /* Reverse traversal. */
1232 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1234 /* TailQ Deletion. */
1235 while (!TAILQ_EMPTY(&head)) {
1236 n1 = TAILQ_FIRST(&head);
1237 TAILQ_REMOVE(&head, n1, entries);
1240 /* Faster TailQ Deletion. */
1241 n1 = TAILQ_FIRST(&head);
1242 while (n1 != NULL) {
1243 n2 = TAILQ_NEXT(n1, entries);
1254 functions first appeared in