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
33 .\" $FreeBSD: src/share/man/man3/queue.3,v 1.15.2.7 2001/12/18 10:09:02 ru Exp $
34 .\" $DragonFly: src/share/man/man3/queue.3,v 1.6 2008/07/24 01:24:24 swildner Exp $
45 .Nm SLIST_HEAD_INITIALIZER ,
47 .Nm SLIST_INSERT_AFTER ,
48 .Nm SLIST_INSERT_HEAD ,
50 .Nm SLIST_REMOVE_HEAD ,
58 .Nm STAILQ_HEAD_INITIALIZER ,
60 .Nm STAILQ_INSERT_AFTER ,
61 .Nm STAILQ_INSERT_HEAD ,
62 .Nm STAILQ_INSERT_TAIL ,
65 .Nm STAILQ_REMOVE_HEAD ,
71 .Nm LIST_FOREACH_MUTABLE ,
73 .Nm LIST_HEAD_INITIALIZER ,
75 .Nm LIST_INSERT_AFTER ,
76 .Nm LIST_INSERT_BEFORE ,
77 .Nm LIST_INSERT_HEAD ,
85 .Nm TAILQ_FOREACH_SAFE ,
86 .Nm TAILQ_FOREACH_MUTABLE ,
87 .Nm TAILQ_FOREACH_REVERSE ,
89 .Nm TAILQ_HEAD_INITIALIZER ,
91 .Nm TAILQ_INSERT_AFTER ,
92 .Nm TAILQ_INSERT_BEFORE ,
93 .Nm TAILQ_INSERT_HEAD ,
94 .Nm TAILQ_INSERT_TAIL ,
102 .Nm CIRCLEQ_FOREACH ,
103 .Nm CIRCLEQ_FOREACH_REVERSE ,
105 .Nm CIRCLEQ_HEAD_INITIALIZER ,
107 .Nm CIRCLEQ_INSERT_AFTER ,
108 .Nm CIRCLEQ_INSERT_BEFORE ,
109 .Nm CIRCLEQ_INSERT_HEAD ,
110 .Nm CIRCLEQ_INSERT_TAIL ,
115 .Nd implementations of singly-linked lists, singly-linked tail queues,
116 lists, tail queues, and circular queues
120 .Fn SLIST_EMPTY "SLIST_HEAD *head"
121 .Fn SLIST_ENTRY "TYPE"
122 .Fn SLIST_FIRST "SLIST_HEAD *head"
123 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
124 .Fn SLIST_HEAD "HEADNAME" "TYPE"
125 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
126 .Fn SLIST_INIT "SLIST_HEAD *head"
127 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
128 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
129 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
130 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
131 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
133 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
134 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
135 .Fn STAILQ_ENTRY "TYPE"
136 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
137 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
138 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
139 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
140 .Fn STAILQ_INIT "STAILQ_HEAD *head"
141 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
142 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
143 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
144 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
145 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
146 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
147 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
149 .Fn LIST_EMPTY "LIST_HEAD *head"
150 .Fn LIST_ENTRY "TYPE"
151 .Fn LIST_FIRST "LIST_HEAD *head"
152 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
153 .Fn LIST_FOREACH_MUTABLE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
154 .Fn LIST_HEAD "HEADNAME" "TYPE"
155 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
156 .Fn LIST_INIT "LIST_HEAD *head"
157 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
158 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
159 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
160 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
161 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
163 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
164 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
165 .Fn TAILQ_ENTRY "TYPE"
166 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
167 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
168 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
169 .Fn TAILQ_FOREACH_MUTABLE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
170 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
171 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
172 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
173 .Fn TAILQ_INIT "TAILQ_HEAD *head"
174 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
175 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
176 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
177 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
178 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
179 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
180 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
181 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
183 .Fn CIRCLEQ_EMPTY "CIRCLEQ_HEAD *head"
184 .Fn CIRCLEQ_ENTRY "TYPE"
185 .Fn CIRCLEQ_FIRST "CIRCLEQ_HEAD *head"
186 .Fn CIRCLEQ_FOREACH "TYPE *var" "CIRCLEQ_HEAD *head" "CIRCLEQ_ENTRY NAME"
187 .Fn CIRCLEQ_FOREACH_REVERSE "TYPE *var" "CIRCLEQ_HEAD *head" "CIRCLEQ_ENTRY NAME"
188 .Fn CIRCLEQ_HEAD "HEADNAME" "TYPE"
189 .Fn CIRCLEQ_HEAD_INITIALIZER "CIRCLEQ_HEAD head"
190 .Fn CIRCLEQ_INIT "CIRCLEQ_HEAD *head"
191 .Fn CIRCLEQ_INSERT_AFTER "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
192 .Fn CIRCLEQ_INSERT_BEFORE "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
193 .Fn CIRCLEQ_INSERT_HEAD "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
194 .Fn CIRCLEQ_INSERT_TAIL "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
195 .Fn CIRCLEQ_LAST "CIRCLEQ_HEAD *head"
196 .Fn CIRCLEQ_NEXT "TYPE *elm" "CIRCLEQ_ENTRY NAME"
197 .Fn CIRCLEQ_PREV "TYPE *elm" "CIRCLEQ_ENTRY NAME"
198 .Fn CIRCLEQ_REMOVE "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
200 These macros define and operate on five types of data structures:
201 singly-linked lists, singly-linked tail queues, lists, tail queues,
203 All five 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 O(n) removal of any entry in the list.
214 Forward traversal through the list.
217 Singly-linked lists are the simplest of the five 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.
223 Singly-linked tail queues add the following functionality:
224 .Bl -enum -compact -offset indent
226 Entries can be added at the end of a list.
229 .Bl -enum -compact -offset indent
231 All list insertions must specify the head of the list.
233 Each head entry requires two pointers rather than one.
235 Code size is about 15% greater and operations run about 20% slower
236 than singly-linked lists.
239 Singly-linked tailqs are ideal for applications with large datasets and
241 or for implementing a FIFO queue.
243 All doubly linked types of data structures (lists, tail queues, and circle
244 queues) additionally allow:
245 .Bl -enum -compact -offset indent
247 Insertion of a new entry before any element in the list.
249 O(1) removal of any entry in the list.
252 .Bl -enum -compact -offset indent
254 Each elements requires two pointers rather than one.
256 Code size and execution time of operations (except for removal) is about
257 twice that of the singly-linked data-structures.
260 Linked lists are the simplest of the doubly linked data structures and support
261 only the above functionality over singly-linked lists.
263 Tail queues add the following functionality:
264 .Bl -enum -compact -offset indent
266 Entries can be added at the end of a list.
268 They may be traversed backwards, from tail to head.
271 .Bl -enum -compact -offset indent
273 All list insertions and removals must specify the head of the list.
275 Each head entry requires two pointers rather than one.
277 Code size is about 15% greater and operations run about 20% slower
278 than singly-linked lists.
281 Circular queues add the following functionality:
282 .Bl -enum -compact -offset indent
284 Entries can be added at the end of a list.
286 They may be traversed backwards, from tail to head.
289 .Bl -enum -compact -offset indent
291 All list insertions and removals must specify the head of the list.
293 Each head entry requires two pointers rather than one.
295 The termination condition for traversal is more complex.
297 Code size is about 40% greater and operations run about 45% slower
301 In the macro definitions,
303 is the name of a user defined structure,
304 that must contain a field of type
315 is the name of a user defined structure that must be declared
323 See the examples below for further explanation of how these
325 .Sh SINGLY-LINKED LISTS
326 A singly-linked list is headed by a structure defined by the
329 This structure contains a single pointer to the first element
331 The elements are singly linked for minimum space and pointer manipulation
332 overhead at the expense of O(n) removal for arbitrary elements.
333 New elements can be added to the list after an existing element or
334 at the head of the list.
337 structure is declared as follows:
338 .Bd -literal -offset indent
339 SLIST_HEAD(HEADNAME, TYPE) head;
344 is the name of the structure to be defined, and
346 is the type of the elements to be linked into the list.
347 A pointer to the head of the list can later be declared as:
348 .Bd -literal -offset indent
349 struct HEADNAME *headp;
356 are user selectable.)
359 .Nm SLIST_HEAD_INITIALIZER
360 evaluates to an initializer for the list
365 evaluates to true if there are no elements in the list.
369 declares a structure that connects the elements in
374 returns the first element in the list or NULL if the list is empty.
378 traverses the list referenced by
380 in the forward direction, assigning each element in
386 initializes the list referenced by
390 .Nm SLIST_INSERT_HEAD
391 inserts the new element
393 at the head of the list.
396 .Nm SLIST_INSERT_AFTER
397 inserts the new element
404 returns the next element in the list.
407 .Nm SLIST_REMOVE_HEAD
410 from the head of the list.
411 For optimum efficiency,
412 elements being removed from the head of the list should explicitly use
413 this macro instead of the generic
422 .Sh SINGLY-LINKED LIST EXAMPLE
424 SLIST_HEAD(slisthead, entry) head =
425 SLIST_HEAD_INITIALIZER(head);
426 struct slisthead *headp; /* Singly-linked List head. */
429 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
431 } *n1, *n2, *n3, *np;
433 SLIST_INIT(&head); /* Initialize the list. */
435 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
436 SLIST_INSERT_HEAD(&head, n1, entries);
438 n2 = malloc(sizeof(struct entry)); /* Insert after. */
439 SLIST_INSERT_AFTER(n1, n2, entries);
441 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
444 n3 = SLIST_FIRST(&head);
445 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
447 /* Forward traversal. */
448 SLIST_FOREACH(np, &head, entries)
451 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
452 n1 = SLIST_FIRST(&head);
453 SLIST_REMOVE_HEAD(&head, entries);
457 .Sh SINGLY-LINKED TAIL QUEUES
458 A singly-linked tail queue is headed by a structure defined by the
461 This structure contains a pair of pointers,
462 one to the first element in the tail queue and the other to
463 the last element in the tail queue.
464 The elements are singly linked for minimum space and pointer
465 manipulation overhead at the expense of O(n) removal for arbitrary
467 New elements can be added to the tail queue after an existing element,
468 at the head of the tail queue, or at the end of the tail queue.
471 structure is declared as follows:
472 .Bd -literal -offset indent
473 STAILQ_HEAD(HEADNAME, TYPE) head;
478 is the name of the structure to be defined, and
480 is the type of the elements to be linked into the tail queue.
481 A pointer to the head of the tail queue can later be declared as:
482 .Bd -literal -offset indent
483 struct HEADNAME *headp;
490 are user selectable.)
493 .Nm STAILQ_HEAD_INITIALIZER
494 evaluates to an initializer for the tail queue
499 concatenates the tail queue headed by
501 onto the end of the one headed by
503 removing all entries from the former.
507 evaluates to true if there are no items on the tail queue.
511 declares a structure that connects the elements in
516 returns the first item on the tail queue or NULL if the tail queue
521 traverses the tail queue referenced by
523 in the forward direction, assigning each element
529 initializes the tail queue referenced by
533 .Nm STAILQ_INSERT_HEAD
534 inserts the new element
536 at the head of the tail queue.
539 .Nm STAILQ_INSERT_TAIL
540 inserts the new element
542 at the end of the tail queue.
545 .Nm STAILQ_INSERT_AFTER
546 inserts the new element
553 returns the last item on the tail queue.
554 If the tail queue is empty the return value is undefined.
558 returns the next item on the tail queue, or NULL this item is the last.
561 .Nm STAILQ_REMOVE_HEAD
562 removes the element at the head of the tail queue.
563 For optimum efficiency,
564 elements being removed from the head of the tail queue should
565 use this macro explicitly rather than the generic
574 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
576 STAILQ_HEAD(stailhead, entry) head =
577 STAILQ_HEAD_INITIALIZER(head);
578 struct stailhead *headp; /* Singly-linked tail queue head. */
581 STAILQ_ENTRY(entry) entries; /* Tail queue. */
583 } *n1, *n2, *n3, *np;
585 STAILQ_INIT(&head); /* Initialize the queue. */
587 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
588 STAILQ_INSERT_HEAD(&head, n1, entries);
590 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
591 STAILQ_INSERT_TAIL(&head, n1, entries);
593 n2 = malloc(sizeof(struct entry)); /* Insert after. */
594 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
596 STAILQ_REMOVE(&head, n2, entry, entries);
598 /* Deletion from the head. */
599 n3 = STAILQ_FIRST(&head);
600 STAILQ_REMOVE_HEAD(&head, entries);
602 /* Forward traversal. */
603 STAILQ_FOREACH(np, &head, entries)
605 /* TailQ Deletion. */
606 while (!STAILQ_EMPTY(&head)) {
607 n1 = STAILQ_FIRST(&head);
608 STAILQ_REMOVE_HEAD(&head, entries);
611 /* Faster TailQ Deletion. */
612 n1 = STAILQ_FIRST(&head);
614 n2 = STAILQ_NEXT(n1, entries);
621 A list is headed by a structure defined by the
624 This structure contains a single pointer to the first element
626 The elements are doubly linked so that an arbitrary element can be
627 removed without traversing the list.
628 New elements can be added to the list after an existing element,
629 before an existing element, or at the head of the list.
632 structure is declared as follows:
633 .Bd -literal -offset indent
634 LIST_HEAD(HEADNAME, TYPE) head;
639 is the name of the structure to be defined, and
641 is the type of the elements to be linked into the list.
642 A pointer to the head of the list can later be declared as:
643 .Bd -literal -offset indent
644 struct HEADNAME *headp;
651 are user selectable.)
654 .Nm LIST_HEAD_INITIALIZER
655 evaluates to an initializer for the list
660 evaluates to true if their are no elements in the list.
664 declares a structure that connects the elements in
669 returns the first element in the list or NULL if the list
674 traverses the list referenced by
676 in the forward direction, assigning each element in turn to
680 .Nm LIST_FOREACH_MUTABLE
681 traverses the list referenced by
683 in the forward direction, assigning each element in turn to
687 it is permitted to remove
689 from the list without interfering with the traversal.
693 initializes the list referenced by
698 inserts the new element
700 at the head of the list.
703 .Nm LIST_INSERT_AFTER
704 inserts the new element
710 .Nm LIST_INSERT_BEFORE
711 inserts the new element
718 returns the next element in the list, or NULL if this is the last.
727 LIST_HEAD(listhead, entry) head =
728 LIST_HEAD_INITIALIZER(head);
729 struct listhead *headp; /* List head. */
732 LIST_ENTRY(entry) entries; /* List. */
734 } *n1, *n2, *n3, *np;
736 LIST_INIT(&head); /* Initialize the list. */
738 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
739 LIST_INSERT_HEAD(&head, n1, entries);
741 n2 = malloc(sizeof(struct entry)); /* Insert after. */
742 LIST_INSERT_AFTER(n1, n2, entries);
744 n3 = malloc(sizeof(struct entry)); /* Insert before. */
745 LIST_INSERT_BEFORE(n2, n3, entries);
747 LIST_REMOVE(n2, entries); /* Deletion. */
749 /* Forward traversal. */
750 LIST_FOREACH(np, &head, entries)
753 while (!LIST_EMPTY(&head)) { /* List Deletion. */
754 n1 = LIST_FIRST(&head);
755 LIST_REMOVE(n1, entries);
759 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
761 n2 = LIST_NEXT(n1, entries);
768 A tail queue is headed by a structure defined by the
771 This structure contains a pair of pointers,
772 one to the first element in the tail queue and the other to
773 the last element in the tail queue.
774 The elements are doubly linked so that an arbitrary element can be
775 removed without traversing the tail queue.
776 New elements can be added to the tail queue after an existing element,
777 before an existing element, at the head of the tail queue,
778 or at the end of the tail queue.
781 structure is declared as follows:
782 .Bd -literal -offset indent
783 TAILQ_HEAD(HEADNAME, TYPE) head;
788 is the name of the structure to be defined, and
790 is the type of the elements to be linked into the tail queue.
791 A pointer to the head of the tail queue can later be declared as:
792 .Bd -literal -offset indent
793 struct HEADNAME *headp;
800 are user selectable.)
803 .Nm TAILQ_HEAD_INITIALIZER
804 evaluates to an initializer for the tail queue
809 concatenates the tail queue headed by
811 onto the end of the one headed by
813 removing all entries from the former.
817 evaluates to true if there are no items on the tail queue.
821 declares a structure that connects the elements in
826 returns the first item on the tail queue or NULL if the tail queue
831 traverses the tail queue referenced by
833 in the forward direction, assigning each element in turn to
837 .Nm TAILQ_FOREACH_MUTABLE
838 traverses the tail queue referenced by
840 in the forward direction, assigning each element in turn to
844 it is permitted to remove
846 from the tail queue without interfering with the traversal.
849 .Nm TAILQ_FOREACH_REVERSE
850 traverses the tail queue referenced by
852 in the reverse direction, assigning each element in turn to
856 .Nm TAILQ_FOREACH_SAFE
857 traverses the list referenced by
859 in the forward direction,
860 assigning each element in turn to
862 However, unlike its unsafe counterpart,
863 .Nm TAILQ_FOREACH_SAVE
864 permits to both remove
866 as well as free it from within the loop safely without interfering with the
871 initializes the tail queue referenced by
875 .Nm TAILQ_INSERT_HEAD
876 inserts the new element
878 at the head of the tail queue.
881 .Nm TAILQ_INSERT_TAIL
882 inserts the new element
884 at the end of the tail queue.
887 .Nm TAILQ_INSERT_AFTER
888 inserts the new element
894 .Nm TAILQ_INSERT_BEFORE
895 inserts the new element
902 returns the last item on the tail queue.
903 If the tail queue is empty the return value is undefined.
907 returns the next item on the tail queue, or NULL if this item is the last.
911 returns the previous item on the tail queue, or NULL if this item
919 .Sh TAIL QUEUE EXAMPLE
921 TAILQ_HEAD(tailhead, entry) head =
922 TAILQ_HEAD_INITIALIZER(head);
923 struct tailhead *headp; /* Tail queue head. */
926 TAILQ_ENTRY(entry) entries; /* Tail queue. */
928 } *n1, *n2, *n3, *np;
930 TAILQ_INIT(&head); /* Initialize the queue. */
932 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
933 TAILQ_INSERT_HEAD(&head, n1, entries);
935 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
936 TAILQ_INSERT_TAIL(&head, n1, entries);
938 n2 = malloc(sizeof(struct entry)); /* Insert after. */
939 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
941 n3 = malloc(sizeof(struct entry)); /* Insert before. */
942 TAILQ_INSERT_BEFORE(n2, n3, entries);
944 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
946 /* Forward traversal. */
947 TAILQ_FOREACH(np, &head, entries)
949 /* Safe forward traversal. */
950 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
953 TAILQ_REMOVE(&head, np, entries);
956 /* Reverse traversal. */
957 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
959 /* TailQ Deletion. */
960 while (!TAILQ_EMPTY(&head)) {
961 n1 = TAILQ_FIRST(&head);
962 TAILQ_REMOVE(&head, n1, entries);
965 /* Faster TailQ Deletion. */
966 n1 = TAILQ_FIRST(&head);
968 n2 = TAILQ_NEXT(n1, entries);
975 A circular queue is headed by a structure defined by the
978 This structure contains a pair of pointers,
979 one to the first element in the circular queue and the other to the
980 last element in the circular queue.
981 The elements are doubly linked so that an arbitrary element can be
982 removed without traversing the queue.
983 New elements can be added to the queue after an existing element,
984 before an existing element, at the head of the queue, or at the end
988 structure is declared as follows:
989 .Bd -literal -offset indent
990 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
995 is the name of the structure to be defined, and
997 is the type of the elements to be linked into the circular queue.
998 A pointer to the head of the circular queue can later be declared as:
999 .Bd -literal -offset indent
1000 struct HEADNAME *headp;
1007 are user selectable.)
1010 .Nm CIRCLEQ_HEAD_INITIALIZER
1011 evaluates to an initializer for the circle queue
1016 evaluates to true if there are no items on the circle queue.
1020 declares a structure that connects the elements in
1025 returns the first item on the circle queue.
1029 traverses the circle queue referenced by
1031 in the forward direction, assigning each element in turn to
1035 .Nm CICRLEQ_FOREACH_REVERSE
1036 traverses the circle queue referenced by
1038 in the reverse direction, assigning each element in turn to
1043 initializes the circular queue referenced by
1047 .Nm CIRCLEQ_INSERT_HEAD
1048 inserts the new element
1050 at the head of the circular queue.
1053 .Nm CIRCLEQ_INSERT_TAIL
1054 inserts the new element
1056 at the end of the circular queue.
1059 .Nm CIRCLEQ_INSERT_AFTER
1060 inserts the new element
1066 .Nm CIRCLEQ_INSERT_BEFORE
1067 inserts the new element
1074 returns the last item on the circle queue.
1078 returns the next item on the circle queue.
1082 returns the previous item on the circle queue.
1088 from the circular queue.
1089 .Sh CIRCULAR QUEUE EXAMPLE
1091 CIRCLEQ_HEAD(circlehead, entry) head =
1092 CIRCLEQ_HEAD_INITIALIZER(head);
1093 struct circlehead *headp; /* Circular queue head. */
1096 CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
1100 CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
1102 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1103 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
1105 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1106 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
1108 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1109 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
1111 n2 = malloc(sizeof(struct entry)); /* Insert before. */
1112 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
1114 CIRCLEQ_REMOVE(&head, n1, entries); /* Deletion. */
1116 /* Forward traversal. */
1117 CIRCLEQ_FOREACH(np, &head, entries)
1119 /* Reverse traversal. */
1120 CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
1122 /* CircleQ Deletion. */
1123 while (CIRCLEQ_FIRST(&head) != (void *)&head) {
1124 n1 = CIRCLEQ_HEAD(&head);
1125 CIRCLEQ_REMOVE(&head, n1, entries);
1128 /* Faster CircleQ Deletion. */
1129 n1 = CIRCLEQ_FIRST(&head);
1130 while (n1 != (void *)&head) {
1131 n2 = CIRCLEQ_NEXT(n1, entries);
1135 CIRCLEQ_INIT(&head);
1140 functions first appeared in