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
35 .Nm SLIST_CLASS_ENTRY ,
36 .Nm SLIST_CLASS_HEAD ,
41 .Nm SLIST_FOREACH_FROM ,
42 .Nm SLIST_FOREACH_FROM_SAFE ,
43 .Nm SLIST_FOREACH_SAFE ,
45 .Nm SLIST_HEAD_INITIALIZER ,
47 .Nm SLIST_INSERT_AFTER ,
48 .Nm SLIST_INSERT_HEAD ,
51 .Nm SLIST_REMOVE_AFTER ,
52 .Nm SLIST_REMOVE_HEAD ,
54 .Nm STAILQ_CLASS_ENTRY ,
55 .Nm STAILQ_CLASS_HEAD ,
61 .Nm STAILQ_FOREACH_FROM ,
62 .Nm STAILQ_FOREACH_FROM_SAFE ,
63 .Nm STAILQ_FOREACH_SAFE ,
65 .Nm STAILQ_HEAD_INITIALIZER ,
67 .Nm STAILQ_INSERT_AFTER ,
68 .Nm STAILQ_INSERT_HEAD ,
69 .Nm STAILQ_INSERT_TAIL ,
73 .Nm STAILQ_REMOVE_AFTER ,
74 .Nm STAILQ_REMOVE_HEAD ,
76 .Nm LIST_CLASS_ENTRY ,
82 .Nm LIST_FOREACH_FROM ,
83 .Nm LIST_FOREACH_FROM_SAFE ,
84 .Nm LIST_FOREACH_SAFE ,
86 .Nm LIST_HEAD_INITIALIZER ,
88 .Nm LIST_INSERT_AFTER ,
89 .Nm LIST_INSERT_BEFORE ,
90 .Nm LIST_INSERT_HEAD ,
95 .Nm TAILQ_CLASS_ENTRY ,
96 .Nm TAILQ_CLASS_HEAD ,
102 .Nm TAILQ_FOREACH_FROM ,
103 .Nm TAILQ_FOREACH_FROM_SAFE ,
104 .Nm TAILQ_FOREACH_REVERSE ,
105 .Nm TAILQ_FOREACH_REVERSE_FROM ,
106 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
107 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
108 .Nm TAILQ_FOREACH_SAFE ,
110 .Nm TAILQ_HEAD_INITIALIZER ,
112 .Nm TAILQ_INSERT_AFTER ,
113 .Nm TAILQ_INSERT_BEFORE ,
114 .Nm TAILQ_INSERT_HEAD ,
115 .Nm TAILQ_INSERT_TAIL ,
121 .Nd implementations of singly-linked lists, singly-linked tail queues,
122 lists and tail queues
126 .Fn SLIST_CLASS_ENTRY "CLASSTYPE"
127 .Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
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" "SLIST_ENTRY NAME"
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" "STAILQ_ENTRY NAME"
169 .Fn LIST_CLASS_ENTRY "CLASSTYPE"
170 .Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
171 .Fn LIST_EMPTY "LIST_HEAD *head"
172 .Fn LIST_ENTRY "TYPE"
173 .Fn LIST_FIRST "LIST_HEAD *head"
174 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
175 .Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
176 .Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
177 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
178 .Fn LIST_HEAD "HEADNAME" "TYPE"
179 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
180 .Fn LIST_INIT "LIST_HEAD *head"
181 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
182 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
183 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
184 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
185 .Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
186 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
187 .Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
189 .Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
190 .Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
191 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
192 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
193 .Fn TAILQ_ENTRY "TYPE"
194 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
195 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
196 .Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
197 .Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
198 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
199 .Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
200 .Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
201 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
202 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
203 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
204 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
205 .Fn TAILQ_INIT "TAILQ_HEAD *head"
206 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
207 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
208 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
209 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
210 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
211 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
212 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
213 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
214 .Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
217 These macros define and operate on four types of data structures which
218 can be used in both C and C++ source code:
219 .Bl -enum -compact -offset indent
225 Singly-linked tail queues
229 All four structures support the following functionality:
230 .Bl -enum -compact -offset indent
232 Insertion of a new entry at the head of the list.
234 Insertion of a new entry after any element in the list.
236 O(1) removal of an entry from the head of the list.
238 Forward traversal through the list.
240 Swapping the contents of two lists.
243 Singly-linked lists are the simplest of the four data structures
244 and support only the above functionality.
245 Singly-linked lists are ideal for applications with large datasets
246 and few or no removals,
247 or for implementing a LIFO queue.
248 Singly-linked lists add the following functionality:
249 .Bl -enum -compact -offset indent
251 O(n) removal of any entry in the list.
254 Singly-linked tail queues add the following functionality:
255 .Bl -enum -compact -offset indent
257 Entries can be added at the end of a list.
259 O(n) removal of any entry in the list.
261 They may be concatenated.
264 .Bl -enum -compact -offset indent
266 All list insertions must specify the head of the list.
268 Each head entry requires two pointers rather than one.
270 Code size is about 15% greater and operations run about 20% slower
271 than singly-linked lists.
274 Singly-linked tail queues are ideal for applications with large datasets and
276 or for implementing a FIFO queue.
278 All doubly linked types of data structures (lists and tail queues)
280 .Bl -enum -compact -offset indent
282 Insertion of a new entry before any element in the list.
284 O(1) removal of any entry in the list.
287 .Bl -enum -compact -offset indent
289 Each element requires two pointers rather than one.
291 Code size and execution time of operations (except for removal) is about
292 twice that of the singly-linked data-structures.
295 Linked lists are the simplest of the doubly linked data structures.
296 They add the following functionality over the above:
297 .Bl -enum -compact -offset indent
299 They may be traversed backwards.
302 .Bl -enum -compact -offset indent
304 To traverse backwards, an entry to begin the traversal and the list in
305 which it is contained must be specified.
308 Tail queues add the following functionality:
309 .Bl -enum -compact -offset indent
311 Entries can be added at the end of a list.
313 They may be traversed backwards, from tail to head.
315 They may be concatenated.
318 .Bl -enum -compact -offset indent
320 All list insertions and removals must specify the head of the list.
322 Each head entry requires two pointers rather than one.
324 Code size is about 15% greater and operations run about 20% slower
325 than singly-linked lists.
328 In the macro definitions,
330 is the name of a user defined structure.
331 The structure must contain a field called
339 In the macro definitions,
341 is the name of a user defined class.
342 The class must contain a field called
345 .Li SLIST_CLASS_ENTRY ,
346 .Li STAILQ_CLASS_ENTRY ,
347 .Li LIST_CLASS_ENTRY ,
349 .Li TAILQ_CLASS_ENTRY .
352 is the name of a user defined structure that must be declared
355 .Li SLIST_CLASS_HEAD ,
357 .Li STAILQ_CLASS_HEAD ,
359 .Li LIST_CLASS_HEAD ,
362 .Li TAILQ_CLASS_HEAD .
363 See the examples below for further explanation of how these
365 .Sh SINGLY-LINKED LISTS
366 A singly-linked list is headed by a structure defined by the
369 This structure contains a single pointer to the first element
371 The elements are singly linked for minimum space and pointer manipulation
372 overhead at the expense of O(n) removal for arbitrary elements.
373 New elements can be added to the list after an existing element or
374 at the head of the list.
377 structure is declared as follows:
378 .Bd -literal -offset indent
379 SLIST_HEAD(HEADNAME, TYPE) head;
384 is the name of the structure to be defined, and
386 is the type of the elements to be linked into the list.
387 A pointer to the head of the list can later be declared as:
388 .Bd -literal -offset indent
389 struct HEADNAME *headp;
396 are user selectable.)
399 .Nm SLIST_HEAD_INITIALIZER
400 evaluates to an initializer for the list
405 evaluates to true if there are no elements in the list.
409 declares a structure that connects the elements in
414 returns the first element in the list or NULL if the list is empty.
418 traverses the list referenced by
420 in the forward direction, assigning each element in
425 .Nm SLIST_FOREACH_FROM
426 behaves identically to
430 is NULL, else it treats
432 as a previously found SLIST element and begins the loop at
434 instead of the first element in the SLIST referenced by
438 .Nm SLIST_FOREACH_SAFE
439 traverses the list referenced by
441 in the forward direction, assigning each element in
446 here it is permitted to both remove
448 as well as free it from within the loop safely without interfering with the
452 .Nm SLIST_FOREACH_FROM_SAFE
453 behaves identically to
454 .Nm SLIST_FOREACH_SAFE
457 is NULL, else it treats
459 as a previously found SLIST element and begins the loop at
461 instead of the first element in the SLIST referenced by
466 initializes the list referenced by
470 .Nm SLIST_INSERT_HEAD
471 inserts the new element
473 at the head of the list.
476 .Nm SLIST_INSERT_AFTER
477 inserts the new element
484 returns the next element in the list.
487 .Nm SLIST_REMOVE_AFTER
488 removes the element after
493 this macro does not traverse the entire list.
496 .Nm SLIST_REMOVE_HEAD
499 from the head of the list.
500 For optimum efficiency,
501 elements being removed from the head of the list should explicitly use
502 this macro instead of the generic
514 swaps the contents of
518 .Sh SINGLY-LINKED LIST EXAMPLE
520 SLIST_HEAD(slisthead, entry) head =
521 SLIST_HEAD_INITIALIZER(head);
522 struct slisthead *headp; /* Singly-linked List head. */
525 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
527 } *n1, *n2, *n3, *np;
529 SLIST_INIT(&head); /* Initialize the list. */
531 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
532 SLIST_INSERT_HEAD(&head, n1, entries);
534 n2 = malloc(sizeof(struct entry)); /* Insert after. */
535 SLIST_INSERT_AFTER(n1, n2, entries);
537 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
540 n3 = SLIST_FIRST(&head);
541 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
543 /* Forward traversal. */
544 SLIST_FOREACH(np, &head, entries)
546 /* Safe forward traversal. */
547 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
550 SLIST_REMOVE(&head, np, entry, entries);
554 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
555 n1 = SLIST_FIRST(&head);
556 SLIST_REMOVE_HEAD(&head, entries);
560 .Sh SINGLY-LINKED TAIL QUEUES
561 A singly-linked tail queue is headed by a structure defined by the
564 This structure contains a pair of pointers,
565 one to the first element in the tail queue and the other to
566 the last element in the tail queue.
567 The elements are singly linked for minimum space and pointer
568 manipulation overhead at the expense of O(n) removal for arbitrary
570 New elements can be added to the tail queue after an existing element,
571 at the head of the tail queue, or at the end of the tail queue.
574 structure is declared as follows:
575 .Bd -literal -offset indent
576 STAILQ_HEAD(HEADNAME, TYPE) head;
581 is the name of the structure to be defined, and
583 is the type of the elements to be linked into the tail queue.
584 A pointer to the head of the tail queue can later be declared as:
585 .Bd -literal -offset indent
586 struct HEADNAME *headp;
593 are user selectable.)
596 .Nm STAILQ_HEAD_INITIALIZER
597 evaluates to an initializer for the tail queue
602 concatenates the tail queue headed by
604 onto the end of the one headed by
606 removing all entries from the former.
610 evaluates to true if there are no items on the tail queue.
614 declares a structure that connects the elements in
619 returns the first item on the tail queue or NULL if the tail queue
624 traverses the tail queue referenced by
626 in the forward direction, assigning each element
631 .Nm STAILQ_FOREACH_FROM
632 behaves identically to
636 is NULL, else it treats
638 as a previously found STAILQ element and begins the loop at
640 instead of the first element in the STAILQ referenced by
644 .Nm STAILQ_FOREACH_SAFE
645 traverses the tail queue referenced by
647 in the forward direction, assigning each element
652 here it is permitted to both remove
654 as well as free it from within the loop safely without interfering with the
658 .Nm STAILQ_FOREACH_FROM_SAFE
659 behaves identically to
660 .Nm STAILQ_FOREACH_SAFE
663 is NULL, else it treats
665 as a previously found STAILQ element and begins the loop at
667 instead of the first element in the STAILQ referenced by
672 initializes the tail queue referenced by
676 .Nm STAILQ_INSERT_HEAD
677 inserts the new element
679 at the head of the tail queue.
682 .Nm STAILQ_INSERT_TAIL
683 inserts the new element
685 at the end of the tail queue.
688 .Nm STAILQ_INSERT_AFTER
689 inserts the new element
696 returns the last item on the tail queue.
697 If the tail queue is empty the return value is
702 returns the next item on the tail queue, or NULL this item is the last.
705 .Nm STAILQ_REMOVE_AFTER
706 removes the element after
711 this macro does not traverse the entire tail queue.
714 .Nm STAILQ_REMOVE_HEAD
715 removes the element at the head of the tail queue.
716 For optimum efficiency,
717 elements being removed from the head of the tail queue should
718 use this macro explicitly rather than the generic
730 swaps the contents of
734 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
736 STAILQ_HEAD(stailhead, entry) head =
737 STAILQ_HEAD_INITIALIZER(head);
738 struct stailhead *headp; /* Singly-linked tail queue head. */
741 STAILQ_ENTRY(entry) entries; /* Tail queue. */
743 } *n1, *n2, *n3, *np;
745 STAILQ_INIT(&head); /* Initialize the queue. */
747 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
748 STAILQ_INSERT_HEAD(&head, n1, entries);
750 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
751 STAILQ_INSERT_TAIL(&head, n1, entries);
753 n2 = malloc(sizeof(struct entry)); /* Insert after. */
754 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
756 STAILQ_REMOVE(&head, n2, entry, entries);
758 /* Deletion from the head. */
759 n3 = STAILQ_FIRST(&head);
760 STAILQ_REMOVE_HEAD(&head, entries);
762 /* Forward traversal. */
763 STAILQ_FOREACH(np, &head, entries)
765 /* Safe forward traversal. */
766 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
769 STAILQ_REMOVE(&head, np, entry, entries);
772 /* TailQ Deletion. */
773 while (!STAILQ_EMPTY(&head)) {
774 n1 = STAILQ_FIRST(&head);
775 STAILQ_REMOVE_HEAD(&head, entries);
778 /* Faster TailQ Deletion. */
779 n1 = STAILQ_FIRST(&head);
781 n2 = STAILQ_NEXT(n1, entries);
788 A list is headed by a structure defined by the
791 This structure contains a single pointer to the first element
793 The elements are doubly linked so that an arbitrary element can be
794 removed without traversing the list.
795 New elements can be added to the list after an existing element,
796 before an existing element, or at the head of the list.
799 structure is declared as follows:
800 .Bd -literal -offset indent
801 LIST_HEAD(HEADNAME, TYPE) head;
806 is the name of the structure to be defined, and
808 is the type of the elements to be linked into the list.
809 A pointer to the head of the list can later be declared as:
810 .Bd -literal -offset indent
811 struct HEADNAME *headp;
818 are user selectable.)
821 .Nm LIST_HEAD_INITIALIZER
822 evaluates to an initializer for the list
827 evaluates to true if there are no elements in the list.
831 declares a structure that connects the elements in
836 returns the first element in the list or NULL if the list
841 traverses the list referenced by
843 in the forward direction, assigning each element in turn to
847 .Nm LIST_FOREACH_FROM
848 behaves identically to
852 is NULL, else it treats
854 as a previously found LIST element and begins the loop at
856 instead of the first element in the LIST referenced by
860 .Nm LIST_FOREACH_SAFE
861 traverses the list referenced by
863 in the forward direction, assigning each element in turn to
867 here it is permitted to both remove
869 as well as free it from within the loop safely without interfering with the
873 .Nm LIST_FOREACH_FROM_SAFE
874 behaves identically to
875 .Nm LIST_FOREACH_SAFE
878 is NULL, else it treats
880 as a previously found LIST element and begins the loop at
882 instead of the first element in the LIST referenced by
887 initializes the list referenced by
892 inserts the new element
894 at the head of the list.
897 .Nm LIST_INSERT_AFTER
898 inserts the new element
904 .Nm LIST_INSERT_BEFORE
905 inserts the new element
912 returns the next element in the list, or NULL if this is the last.
916 returns the previous element in the list, or NULL if this is the first.
930 swaps the contents of
936 LIST_HEAD(listhead, entry) head =
937 LIST_HEAD_INITIALIZER(head);
938 struct listhead *headp; /* List head. */
941 LIST_ENTRY(entry) entries; /* List. */
943 } *n1, *n2, *n3, *np, *np_temp;
945 LIST_INIT(&head); /* Initialize the list. */
947 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
948 LIST_INSERT_HEAD(&head, n1, entries);
950 n2 = malloc(sizeof(struct entry)); /* Insert after. */
951 LIST_INSERT_AFTER(n1, n2, entries);
953 n3 = malloc(sizeof(struct entry)); /* Insert before. */
954 LIST_INSERT_BEFORE(n2, n3, entries);
956 LIST_REMOVE(n2, entries); /* Deletion. */
958 /* Forward traversal. */
959 LIST_FOREACH(np, &head, entries)
962 /* Safe forward traversal. */
963 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
966 LIST_REMOVE(np, entries);
970 while (!LIST_EMPTY(&head)) { /* List Deletion. */
971 n1 = LIST_FIRST(&head);
972 LIST_REMOVE(n1, entries);
976 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
978 n2 = LIST_NEXT(n1, entries);
985 A tail queue is headed by a structure defined by the
988 This structure contains a pair of pointers,
989 one to the first element in the tail queue and the other to
990 the last element in the tail queue.
991 The elements are doubly linked so that an arbitrary element can be
992 removed without traversing the tail queue.
993 New elements can be added to the tail queue after an existing element,
994 before an existing element, at the head of the tail queue,
995 or at the end of the tail queue.
998 structure is declared as follows:
999 .Bd -literal -offset indent
1000 TAILQ_HEAD(HEADNAME, TYPE) head;
1005 is the name of the structure to be defined, and
1007 is the type of the elements to be linked into the tail queue.
1008 A pointer to the head of the tail queue can later be declared as:
1009 .Bd -literal -offset indent
1010 struct HEADNAME *headp;
1017 are user selectable.)
1020 .Nm TAILQ_HEAD_INITIALIZER
1021 evaluates to an initializer for the tail queue
1026 concatenates the tail queue headed by
1028 onto the end of the one headed by
1030 removing all entries from the former.
1034 evaluates to true if there are no items on the tail queue.
1038 declares a structure that connects the elements in
1043 returns the first item on the tail queue or NULL if the tail queue
1048 traverses the tail queue referenced by
1050 in the forward direction, assigning each element in turn to
1055 if the loop completes normally, or if there were no elements.
1058 .Nm TAILQ_FOREACH_FROM
1059 behaves identically to
1063 is NULL, else it treats
1065 as a previously found TAILQ element and begins the loop at
1067 instead of the first element in the TAILQ referenced by
1071 .Nm TAILQ_FOREACH_REVERSE
1072 traverses the tail queue referenced by
1074 in the reverse direction, assigning each element in turn to
1078 .Nm TAILQ_FOREACH_REVERSE_FROM
1079 behaves identically to
1080 .Nm TAILQ_FOREACH_REVERSE
1083 is NULL, else it treats
1085 as a previously found TAILQ element and begins the reverse loop at
1087 instead of the last element in the TAILQ referenced by
1091 .Nm TAILQ_FOREACH_SAFE
1093 .Nm TAILQ_FOREACH_REVERSE_SAFE
1094 traverse the list referenced by
1096 in the forward or reverse direction respectively,
1097 assigning each element in turn to
1099 However, unlike their unsafe counterparts,
1102 .Nm TAILQ_FOREACH_REVERSE
1103 permit to both remove
1105 as well as free it from within the loop safely without interfering with the
1109 .Nm TAILQ_FOREACH_FROM_SAFE
1110 behaves identically to
1111 .Nm TAILQ_FOREACH_SAFE
1114 is NULL, else it treats
1116 as a previously found TAILQ element and begins the loop at
1118 instead of the first element in the TAILQ referenced by
1122 .Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1123 behaves identically to
1124 .Nm TAILQ_FOREACH_REVERSE_SAFE
1127 is NULL, else it treats
1129 as a previously found TAILQ element and begins the reverse loop at
1131 instead of the last element in the TAILQ referenced by
1136 initializes the tail queue referenced by
1140 .Nm TAILQ_INSERT_HEAD
1141 inserts the new element
1143 at the head of the tail queue.
1146 .Nm TAILQ_INSERT_TAIL
1147 inserts the new element
1149 at the end of the tail queue.
1152 .Nm TAILQ_INSERT_AFTER
1153 inserts the new element
1159 .Nm TAILQ_INSERT_BEFORE
1160 inserts the new element
1167 returns the last item on the tail queue.
1168 If the tail queue is empty the return value is
1173 returns the next item on the tail queue, or NULL if this item is the last.
1177 returns the previous item on the tail queue, or NULL if this item
1184 from the tail queue.
1188 swaps the contents of
1192 .Sh TAIL QUEUE EXAMPLE
1194 TAILQ_HEAD(tailhead, entry) head =
1195 TAILQ_HEAD_INITIALIZER(head);
1196 struct tailhead *headp; /* Tail queue head. */
1199 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1201 } *n1, *n2, *n3, *np;
1203 TAILQ_INIT(&head); /* Initialize the queue. */
1205 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1206 TAILQ_INSERT_HEAD(&head, n1, entries);
1208 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1209 TAILQ_INSERT_TAIL(&head, n1, entries);
1211 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1212 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1214 n3 = malloc(sizeof(struct entry)); /* Insert before. */
1215 TAILQ_INSERT_BEFORE(n2, n3, entries);
1217 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
1219 /* Forward traversal. */
1220 TAILQ_FOREACH(np, &head, entries)
1222 /* Safe forward traversal. */
1223 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1226 TAILQ_REMOVE(&head, np, entries);
1229 /* Reverse traversal. */
1230 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1232 /* TailQ Deletion. */
1233 while (!TAILQ_EMPTY(&head)) {
1234 n1 = TAILQ_FIRST(&head);
1235 TAILQ_REMOVE(&head, n1, entries);
1238 /* Faster TailQ Deletion. */
1239 n1 = TAILQ_FIRST(&head);
1240 while (n1 != NULL) {
1241 n2 = TAILQ_NEXT(n1, entries);
1252 functions first appeared in