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 $
44 .Nm SLIST_HEAD_INITIALIZER ,
46 .Nm SLIST_INSERT_AFTER ,
47 .Nm SLIST_INSERT_HEAD ,
49 .Nm SLIST_REMOVE_HEAD ,
56 .Nm STAILQ_HEAD_INITIALIZER ,
58 .Nm STAILQ_INSERT_AFTER ,
59 .Nm STAILQ_INSERT_HEAD ,
60 .Nm STAILQ_INSERT_TAIL ,
63 .Nm STAILQ_REMOVE_HEAD ,
70 .Nm LIST_HEAD_INITIALIZER ,
72 .Nm LIST_INSERT_AFTER ,
73 .Nm LIST_INSERT_BEFORE ,
74 .Nm LIST_INSERT_HEAD ,
81 .Nm TAILQ_FOREACH_REVERSE ,
83 .Nm TAILQ_HEAD_INITIALIZER ,
85 .Nm TAILQ_INSERT_AFTER ,
86 .Nm TAILQ_INSERT_BEFORE ,
87 .Nm TAILQ_INSERT_HEAD ,
88 .Nm TAILQ_INSERT_TAIL ,
97 .Nm CIRCLEQ_FOREACH_REVERSE ,
99 .Nm CIRCLEQ_HEAD_INITIALIZER ,
101 .Nm CIRCLEQ_INSERT_AFTER ,
102 .Nm CIRCLEQ_INSERT_BEFORE ,
103 .Nm CIRCLEQ_INSERT_HEAD ,
104 .Nm CIRCLEQ_INSERT_TAIL ,
109 .Nd implementations of singly-linked lists, singly-linked tail queues,
110 lists, tail queues, and circular queues
114 .Fn SLIST_EMPTY "SLIST_HEAD *head"
115 .Fn SLIST_ENTRY "TYPE"
116 .Fn SLIST_FIRST "SLIST_HEAD *head"
117 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
118 .Fn SLIST_HEAD "HEADNAME" "TYPE"
119 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
120 .Fn SLIST_INIT "SLIST_HEAD *head"
121 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
122 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
123 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
124 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
125 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
127 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
128 .Fn STAILQ_ENTRY "TYPE"
129 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
130 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
131 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
132 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
133 .Fn STAILQ_INIT "STAILQ_HEAD *head"
134 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
135 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
136 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
137 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
138 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
139 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
140 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
142 .Fn LIST_EMPTY "LIST_HEAD *head"
143 .Fn LIST_ENTRY "TYPE"
144 .Fn LIST_FIRST "LIST_HEAD *head"
145 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
146 .Fn LIST_HEAD "HEADNAME" "TYPE"
147 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
148 .Fn LIST_INIT "LIST_HEAD *head"
149 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
150 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
151 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
152 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
153 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
155 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
156 .Fn TAILQ_ENTRY "TYPE"
157 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
158 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
159 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
160 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
161 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
162 .Fn TAILQ_INIT "TAILQ_HEAD *head"
163 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
164 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
165 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
166 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
167 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
168 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
169 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
170 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
172 .Fn CIRCLEQ_EMPTY "CIRCLEQ_HEAD *head"
173 .Fn CIRCLEQ_ENTRY "TYPE"
174 .Fn CIRCLEQ_FIRST "CIRCLEQ_HEAD *head"
175 .Fn CIRCLEQ_FOREACH "TYPE *var" "CIRCLEQ_HEAD *head" "CIRCLEQ_ENTRY NAME"
176 .Fn CIRCLEQ_FOREACH_REVERSE "TYPE *var" "CIRCLEQ_HEAD *head" "CIRCLEQ_ENTRY NAME"
177 .Fn CIRCLEQ_HEAD "HEADNAME" "TYPE"
178 .Fn CIRCLEQ_HEAD_INITIALIZER "CIRCLEQ_HEAD head"
179 .Fn CIRCLEQ_INIT "CIRCLEQ_HEAD *head"
180 .Fn CIRCLEQ_INSERT_AFTER "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
181 .Fn CIRCLEQ_INSERT_BEFORE "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
182 .Fn CIRCLEQ_INSERT_HEAD "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
183 .Fn CIRCLEQ_INSERT_TAIL "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
184 .Fn CIRCLEQ_LAST "CIRCLEQ_HEAD *head"
185 .Fn CIRCLEQ_NEXT "TYPE *elm" "CIRCLEQ_ENTRY NAME"
186 .Fn CIRCLE_PREV "TYPE *elm" "CIRCLEQ_ENTRY NAME"
187 .Fn CIRCLEQ_REMOVE "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
189 These macros define and operate on five types of data structures:
190 singly-linked lists, singly-linked tail queues, lists, tail queues,
192 All five structures support the following functionality:
193 .Bl -enum -compact -offset indent
195 Insertion of a new entry at the head of the list.
197 Insertion of a new entry after any element in the list.
199 O(1) removal of an entry from the head of the list.
201 O(n) removal of any entry in the list.
203 Forward traversal through the list.
206 Singly-linked lists are the simplest of the five data structures
207 and support only the above functionality.
208 Singly-linked lists are ideal for applications with large datasets
209 and few or no removals,
210 or for implementing a LIFO queue.
212 Singly-linked tail queues add the following functionality:
213 .Bl -enum -compact -offset indent
215 Entries can be added at the end of a list.
218 .Bl -enum -compact -offset indent
220 All list insertions must specify the head of the list.
222 Each head entry requires two pointers rather than one.
224 Code size is about 15% greater and operations run about 20% slower
225 than singly-linked lists.
228 Singly-linked tailqs are ideal for applications with large datasets and
230 or for implementing a FIFO queue.
232 All doubly linked types of data structures (lists, tail queues, and circle
233 queues) additionally allow:
234 .Bl -enum -compact -offset indent
236 Insertion of a new entry before any element in the list.
238 O(1) removal of any entry in the list.
241 .Bl -enum -compact -offset indent
243 Each elements requires two pointers rather than one.
245 Code size and execution time of operations (except for removal) is about
246 twice that of the singly-linked data-structures.
249 Linked lists are the simplest of the doubly linked data structures and support
250 only the above functionality over singly-linked lists.
252 Tail queues add the following functionality:
253 .Bl -enum -compact -offset indent
255 Entries can be added at the end of a list.
257 They may be traversed backwards, from tail to head.
260 .Bl -enum -compact -offset indent
262 All list insertions and removals must specify the head of the list.
264 Each head entry requires two pointers rather than one.
266 Code size is about 15% greater and operations run about 20% slower
267 than singly-linked lists.
270 Circular queues add the following functionality:
271 .Bl -enum -compact -offset indent
273 Entries can be added at the end of a list.
275 They may be traversed backwards, from tail to head.
278 .Bl -enum -compact -offset indent
280 All list insertions and removals must specify the head of the list.
282 Each head entry requires two pointers rather than one.
284 The termination condition for traversal is more complex.
286 Code size is about 40% greater and operations run about 45% slower
290 In the macro definitions,
292 is the name of a user defined structure,
293 that must contain a field of type
304 is the name of a user defined structure that must be declared
312 See the examples below for further explanation of how these
314 .Sh SINGLY-LINKED LISTS
315 A singly-linked list is headed by a structure defined by the
318 This structure contains a single pointer to the first element
320 The elements are singly linked for minimum space and pointer manipulation
321 overhead at the expense of O(n) removal for arbitrary elements.
322 New elements can be added to the list after an existing element or
323 at the head of the list.
326 structure is declared as follows:
327 .Bd -literal -offset indent
328 SLIST_HEAD(HEADNAME, TYPE) head;
333 is the name of the structure to be defined, and
335 is the type of the elements to be linked into the list.
336 A pointer to the head of the list can later be declared as:
337 .Bd -literal -offset indent
338 struct HEADNAME *headp;
345 are user selectable.)
348 .Nm SLIST_HEAD_INITIALIZER
349 evaluates to an initializer for the list
354 evaluates to true if there are no elements in the list.
358 declares a structure that connects the elements in
363 returns the first element in the list or NULL if the list is empty.
367 traverses the list referenced by
369 in the forward direction, assigning each element in
375 initializes the list referenced by
379 .Nm SLIST_INSERT_HEAD
380 inserts the new element
382 at the head of the list.
385 .Nm SLIST_INSERT_AFTER
386 inserts the new element
393 returns the next element in the list.
396 .Nm SLIST_REMOVE_HEAD
399 from the head of the list.
400 For optimum efficiency,
401 elements being removed from the head of the list should explicitly use
402 this macro instead of the generic
411 .Sh SINGLY-LINKED LIST EXAMPLE
413 SLIST_HEAD(slisthead, entry) head =
414 SLIST_HEAD_INITIALIZER(head);
415 struct slisthead *headp; /* Singly-linked List head. */
418 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
420 } *n1, *n2, *n3, *np;
422 SLIST_INIT(&head); /* Initialize the list. */
424 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
425 SLIST_INSERT_HEAD(&head, n1, entries);
427 n2 = malloc(sizeof(struct entry)); /* Insert after. */
428 SLIST_INSERT_AFTER(n1, n2, entries);
430 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
433 n3 = SLIST_FIRST(&head);
434 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
436 /* Forward traversal. */
437 SLIST_FOREACH(np, &head, entries)
440 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
441 n1 = SLIST_FIRST(&head);
442 SLIST_REMOVE_HEAD(&head, entries);
446 .Sh SINGLY-LINKED TAIL QUEUES
447 A singly-linked tail queue is headed by a structure defined by the
450 This structure contains a pair of pointers,
451 one to the first element in the tail queue and the other to
452 the last element in the tail queue.
453 The elements are singly linked for minimum space and pointer
454 manipulation overhead at the expense of O(n) removal for arbitrary
456 New elements can be added to the tail queue after an existing element,
457 at the head of the tail queue, or at the end of the tail queue.
460 structure is declared as follows:
461 .Bd -literal -offset indent
462 STAILQ_HEAD(HEADNAME, TYPE) head;
467 is the name of the structure to be defined, and
469 is the type of the elements to be linked into the tail queue.
470 A pointer to the head of the tail queue can later be declared as:
471 .Bd -literal -offset indent
472 struct HEADNAME *headp;
479 are user selectable.)
482 .Nm STAILQ_HEAD_INITIALIZER
483 evaluates to an initializer for the tail queue
488 evaluates to true if there are no items on the tail queue.
492 declares a structure that connects the elements in
497 returns the first item on the tail queue or NULL if the tail queue
502 traverses the tail queue referenced by
504 in the forward direction, assigning each element
510 initializes the tail queue referenced by
514 .Nm STAILQ_INSERT_HEAD
515 inserts the new element
517 at the head of the tail queue.
520 .Nm STAILQ_INSERT_TAIL
521 inserts the new element
523 at the end of the tail queue.
526 .Nm STAILQ_INSERT_AFTER
527 inserts the new element
534 returns the last item on the tail queue.
535 If the tail queue is empty the return value is undefined.
539 returns the next item on the tail queue, or NULL this item is the last.
542 .Nm STAILQ_REMOVE_HEAD
543 removes the element at the head of the tail queue.
544 For optimum efficiency,
545 elements being removed from the head of the tail queue should
546 use this macro explicitly rather than the generic
555 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
557 STAILQ_HEAD(stailhead, entry) head =
558 STAILQ_HEAD_INITIALIZER(head);
559 struct stailhead *headp; /* Singly-linked tail queue head. */
562 STAILQ_ENTRY(entry) entries; /* Tail queue. */
564 } *n1, *n2, *n3, *np;
566 STAILQ_INIT(&head); /* Initialize the queue. */
568 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
569 STAILQ_INSERT_HEAD(&head, n1, entries);
571 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
572 STAILQ_INSERT_TAIL(&head, n1, entries);
574 n2 = malloc(sizeof(struct entry)); /* Insert after. */
575 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
577 STAILQ_REMOVE(&head, n2, entry, entries);
579 /* Deletion from the head. */
580 n3 = STAILQ_FIRST(&head);
581 STAILQ_REMOVE_HEAD(&head, entries);
583 /* Forward traversal. */
584 STAILQ_FOREACH(np, &head, entries)
586 /* TailQ Deletion. */
587 while (!STAILQ_EMPTY(&head)) {
588 n1 = STAILQ_FIRST(&head);
589 STAILQ_REMOVE_HEAD(&head, entries);
592 /* Faster TailQ Deletion. */
593 n1 = STAILQ_FIRST(&head);
595 n2 = STAILQ_NEXT(n1, entries);
602 A list is headed by a structure defined by the
605 This structure contains a single pointer to the first element
607 The elements are doubly linked so that an arbitrary element can be
608 removed without traversing the list.
609 New elements can be added to the list after an existing element,
610 before an existing element, or at the head of the list.
613 structure is declared as follows:
614 .Bd -literal -offset indent
615 LIST_HEAD(HEADNAME, TYPE) head;
620 is the name of the structure to be defined, and
622 is the type of the elements to be linked into the list.
623 A pointer to the head of the list can later be declared as:
624 .Bd -literal -offset indent
625 struct HEADNAME *headp;
632 are user selectable.)
635 .Nm LIST_HEAD_INITIALIZER
636 evaluates to an initializer for the list
641 evaluates to true if their are no elements in the list.
645 declares a structure that connects the elements in
650 returns the first element in the list or NULL if the list
655 traverses the list referenced by
657 in the forward direction, assigning each element in turn to
662 initializes the list referenced by
667 inserts the new element
669 at the head of the list.
672 .Nm LIST_INSERT_AFTER
673 inserts the new element
679 .Nm LIST_INSERT_BEFORE
680 inserts the new element
687 returns the next element in the list, or NULL if this is the last.
696 LIST_HEAD(listhead, entry) head =
697 LIST_HEAD_INITIALIZER(head);
698 struct listhead *headp; /* List head. */
701 LIST_ENTRY(entry) entries; /* List. */
703 } *n1, *n2, *n3, *np;
705 LIST_INIT(&head); /* Initialize the list. */
707 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
708 LIST_INSERT_HEAD(&head, n1, entries);
710 n2 = malloc(sizeof(struct entry)); /* Insert after. */
711 LIST_INSERT_AFTER(n1, n2, entries);
713 n3 = malloc(sizeof(struct entry)); /* Insert before. */
714 LIST_INSERT_BEFORE(n2, n3, entries);
716 LIST_REMOVE(n2, entries); /* Deletion. */
718 /* Forward traversal. */
719 LIST_FOREACH(np, &head, entries)
722 while (!LIST_EMPTY(&head)) { /* List Deletion. */
723 n1 = LIST_FIRST(&head);
724 LIST_REMOVE(n1, entries);
728 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
730 n2 = LIST_NEXT(n1, entries);
737 A tail queue is headed by a structure defined by the
740 This structure contains a pair of pointers,
741 one to the first element in the tail queue and the other to
742 the last element in the tail queue.
743 The elements are doubly linked so that an arbitrary element can be
744 removed without traversing the tail queue.
745 New elements can be added to the tail queue after an existing element,
746 before an existing element, at the head of the tail queue,
747 or at the end of the tail queue.
750 structure is declared as follows:
751 .Bd -literal -offset indent
752 TAILQ_HEAD(HEADNAME, TYPE) head;
757 is the name of the structure to be defined, and
759 is the type of the elements to be linked into the tail queue.
760 A pointer to the head of the tail queue can later be declared as:
761 .Bd -literal -offset indent
762 struct HEADNAME *headp;
769 are user selectable.)
772 .Nm TAILQ_HEAD_INITIALIZER
773 evaluates to an initializer for the tail queue
778 evaluates to true if there are no items on the tail queue.
782 declares a structure that connects the elements in
787 returns the first item on the tail queue or NULL if the tail queue
792 traverses the tail queue referenced by
794 in the forward direction, assigning each element in turn to
798 .Nm TAILQ_FOREACH_REVERSE
799 traverses the tail queue referenced by
801 in the reverse direction, assigning each element in turn to
806 initializes the tail queue referenced by
810 .Nm TAILQ_INSERT_HEAD
811 inserts the new element
813 at the head of the tail queue.
816 .Nm TAILQ_INSERT_TAIL
817 inserts the new element
819 at the end of the tail queue.
822 .Nm TAILQ_INSERT_AFTER
823 inserts the new element
829 .Nm TAILQ_INSERT_BEFORE
830 inserts the new element
837 returns the last item on the tail queue.
838 If the tail queue is empty the return value is undefined.
842 returns the next item on the tail queue, or NULL if this item is the last.
846 returns the previous item on the tail queue, or NULL if this item
854 .Sh TAIL QUEUE EXAMPLE
856 TAILQ_HEAD(tailhead, entry) head =
857 TAILQ_HEAD_INITIALIZER(head);
858 struct tailhead *headp; /* Tail queue head. */
861 TAILQ_ENTRY(entry) entries; /* Tail queue. */
863 } *n1, *n2, *n3, *np;
865 TAILQ_INIT(&head); /* Initialize the queue. */
867 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
868 TAILQ_INSERT_HEAD(&head, n1, entries);
870 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
871 TAILQ_INSERT_TAIL(&head, n1, entries);
873 n2 = malloc(sizeof(struct entry)); /* Insert after. */
874 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
876 n3 = malloc(sizeof(struct entry)); /* Insert before. */
877 TAILQ_INSERT_BEFORE(n2, n3, entries);
879 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
881 /* Forward traversal. */
882 TAILQ_FOREACH(np, &head, entries)
884 /* Reverse traversal. */
885 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
887 /* TailQ Deletion. */
888 while (!TAILQ_EMPTY(&head)) {
889 n1 = TAILQ_FIRST(&head);
890 TAILQ_REMOVE(&head, n1, entries);
893 /* Faster TailQ Deletion. */
894 n1 = TAILQ_FIRST(&head);
896 n2 = TAILQ_NEXT(n1, entries);
903 A circular queue is headed by a structure defined by the
906 This structure contains a pair of pointers,
907 one to the first element in the circular queue and the other to the
908 last element in the circular queue.
909 The elements are doubly linked so that an arbitrary element can be
910 removed without traversing the queue.
911 New elements can be added to the queue after an existing element,
912 before an existing element, at the head of the queue, or at the end
916 structure is declared as follows:
917 .Bd -literal -offset indent
918 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
923 is the name of the structure to be defined, and
925 is the type of the elements to be linked into the circular queue.
926 A pointer to the head of the circular queue can later be declared as:
927 .Bd -literal -offset indent
928 struct HEADNAME *headp;
935 are user selectable.)
938 .Nm CIRCLEQ_HEAD_INITIALIZER
939 evaluates to an initializer for the circle queue
944 evaluates to true if there are no items on the circle queue.
948 declares a structure that connects the elements in
953 returns the first item on the circle queue.
957 traverses the circle queue referenced by
959 in the forward direction, assigning each element in turn to
963 .Nm CICRLEQ_FOREACH_REVERSE
964 traverses the circle queue referenced by
966 in the reverse direction, assigning each element in turn to
971 initializes the circular queue referenced by
975 .Nm CIRCLEQ_INSERT_HEAD
976 inserts the new element
978 at the head of the circular queue.
981 .Nm CIRCLEQ_INSERT_TAIL
982 inserts the new element
984 at the end of the circular queue.
987 .Nm CIRCLEQ_INSERT_AFTER
988 inserts the new element
994 .Nm CIRCLEQ_INSERT_BEFORE
995 inserts the new element
1002 returns the last item on the circle queue.
1006 returns the next item on the circle queue.
1010 returns the previous item on the circle queue.
1016 from the circular queue.
1017 .Sh CIRCULAR QUEUE EXAMPLE
1019 CIRCLEQ_HEAD(circlehead, entry) head =
1020 CIRCLEQ_HEAD_INITIALIZER(head);
1021 struct circlehead *headp; /* Circular queue head. */
1024 CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
1028 CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
1030 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1031 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
1033 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1034 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
1036 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1037 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
1039 n2 = malloc(sizeof(struct entry)); /* Insert before. */
1040 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
1042 CIRCLEQ_REMOVE(&head, n1, entries); /* Deletion. */
1044 /* Forward traversal. */
1045 CIRCLEQ_FOREACH(np, &head, entries)
1047 /* Reverse traversal. */
1048 CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
1050 /* CircleQ Deletion. */
1051 while (CIRCLEQ_FIRST(&head) != (void *)&head) {
1052 n1 = CIRCLEQ_HEAD(&head);
1053 CIRCLEQ_REMOVE(&head, n1, entries);
1056 /* Faster CircleQ Deletion. */
1057 n1 = CIRCLEQ_FIRST(&head);
1058 while (n1 != (void *)&head) {
1059 n2 = CIRCLEQ_NEXT(n1, entries);
1063 CIRCLEQ_INIT(&head);
1068 functions first appeared in