1 .\" $NetBSD: queue.3,v 1.41 2009/03/11 08:29:56 wiz Exp $
3 .\" Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 .\" POSSIBILITY OF SUCH DAMAGE.
27 .\" Copyright (c) 1993 The Regents of the University of California.
28 .\" All rights reserved.
30 .\" Redistribution and use in source and binary forms, with or without
31 .\" modification, are permitted provided that the following conditions
33 .\" 1. Redistributions of source code must retain the above copyright
34 .\" notice, this list of conditions and the following disclaimer.
35 .\" 2. Redistributions in binary form must reproduce the above copyright
36 .\" notice, this list of conditions and the following disclaimer in the
37 .\" documentation and/or other materials provided with the distribution.
38 .\" 3. Neither the name of the University nor the names of its contributors
39 .\" may be used to endorse or promote products derived from this software
40 .\" without specific prior written permission.
42 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 .\" @(#)queue.3 8.1 (Berkeley) 12/13/93
61 .Nm SLIST_HEAD_INITIALIZER ,
64 .Nm SLIST_INSERT_AFTER ,
65 .Nm SLIST_INSERT_HEAD ,
66 .Nm SLIST_REMOVE_HEAD ,
69 .Nm SLIST_FOREACH_SAFE ,
74 .Nm SIMPLEQ_HEAD_INITIALIZER ,
77 .Nm SIMPLEQ_INSERT_HEAD ,
78 .Nm SIMPLEQ_INSERT_TAIL ,
79 .Nm SIMPLEQ_INSERT_AFTER ,
80 .Nm SIMPLEQ_REMOVE_HEAD ,
83 .Nm SIMPLEQ_FOREACH_SAFE ,
90 .Nm STAILQ_HEAD_INITIALIZER ,
93 .Nm STAILQ_INSERT_HEAD ,
94 .Nm STAILQ_INSERT_TAIL ,
95 .Nm STAILQ_INSERT_AFTER ,
96 .Nm STAILQ_REMOVE_HEAD ,
99 .Nm STAILQ_FOREACH_SAFE ,
106 .Nm LIST_HEAD_INITIALIZER ,
109 .Nm LIST_INSERT_AFTER ,
110 .Nm LIST_INSERT_BEFORE ,
111 .Nm LIST_INSERT_HEAD ,
118 .Nm TAILQ_HEAD_INITIALIZER ,
121 .Nm TAILQ_INSERT_HEAD ,
122 .Nm TAILQ_INSERT_TAIL ,
123 .Nm TAILQ_INSERT_AFTER ,
124 .Nm TAILQ_INSERT_BEFORE ,
127 .Nm TAILQ_FOREACH_SAFE ,
128 .Nm TAILQ_FOREACH_REVERSE ,
129 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
137 .Nm CIRCLEQ_HEAD_INITIALIZER ,
140 .Nm CIRCLEQ_INSERT_AFTER ,
141 .Nm CIRCLEQ_INSERT_BEFORE ,
142 .Nm CIRCLEQ_INSERT_HEAD ,
143 .Nm CIRCLEQ_INSERT_TAIL ,
145 .Nm CIRCLEQ_FOREACH ,
146 .Nm CIRCLEQ_FOREACH_REVERSE ,
152 .Nm CIRCLEQ_LOOP_NEXT ,
153 .Nm CIRCLEQ_LOOP_PREV
154 .Nd "implementations of singly-linked lists, simple queues, lists, tail queues, and circular queues"
158 .Fn SLIST_HEAD "HEADNAME" "TYPE"
159 .Fn SLIST_HEAD_INITIALIZER "head"
160 .Fn SLIST_ENTRY "TYPE"
161 .Fn SLIST_INIT "SLIST_HEAD *head"
162 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
163 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
164 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
165 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
166 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
167 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *tmp"
169 .Fn SLIST_EMPTY "SLIST_HEAD *head"
171 .Fn SLIST_FIRST "SLIST_HEAD *head"
173 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
175 .Fn SIMPLEQ_HEAD "HEADNAME" "TYPE"
176 .Fn SIMPLEQ_HEAD_INITIALIZER "head"
177 .Fn SIMPLEQ_ENTRY "TYPE"
178 .Fn SIMPLEQ_INIT "SIMPLEQ_HEAD *head"
179 .Fn SIMPLEQ_INSERT_HEAD "SIMPLEQ_HEAD *head" "TYPE *elm" "SIMPLEQ_ENTRY NAME"
180 .Fn SIMPLEQ_INSERT_TAIL "SIMPLEQ_HEAD *head" "TYPE *elm" "SIMPLEQ_ENTRY NAME"
181 .Fn SIMPLEQ_INSERT_AFTER "SIMPLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "SIMPLEQ_ENTRY NAME"
182 .Fn SIMPLEQ_REMOVE_HEAD "SIMPLEQ_HEAD *head" "SIMPLEQ_ENTRY NAME"
183 .Fn SIMPLEQ_REMOVE "SIMPLEQ_HEAD *head" "TYPE *elm" "TYPE" "SIMPLEQ_ENTRY NAME"
184 .Fn SIMPLEQ_FOREACH "TYPE *var" "SIMPLEQ_HEAD *head" "SIMPLEQ_ENTRY NAME"
185 .Fn SIMPLEQ_FOREACH_SAFE "TYPE *var" "SIMPLEQ_HEAD *head" "SIMPLEQ_ENTRY NAME" "TYPE *tmp"
187 .Fn SIMPLEQ_EMPTY "SIMPLEQ_HEAD *head"
189 .Fn SIMPLEQ_FIRST "SIMPLEQ_HEAD *head"
191 .Fn SIMPLEQ_NEXT "TYPE *elm" "SIMPLEQ_ENTRY NAME"
193 .Fn SIMPLEQ_LAST "SIMPLEQ_HEAD *head" "TYPE *elm" "SIMPLEQ_ENTRY NAME"
194 .Fn SIMPLEQ_CONCAT "SIMPLEQ_HEAD *head1" "SIMPLEQ_HEAD *head2"
196 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
197 .Fn STAILQ_HEAD_INITIALIZER "head"
198 .Fn STAILQ_ENTRY "TYPE"
199 .Fn STAILQ_INIT "STAILQ_HEAD *head"
200 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
201 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
202 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
203 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
204 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
205 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
206 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *tmp"
208 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
210 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
212 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
214 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
215 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
217 .Fn LIST_HEAD "HEADNAME" "TYPE"
218 .Fn LIST_HEAD_INITIALIZER "head"
219 .Fn LIST_ENTRY "TYPE"
220 .Fn LIST_INIT "LIST_HEAD *head"
221 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
222 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
223 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
224 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
225 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
227 .Fn LIST_EMPTY "LIST_HEAD *head"
229 .Fn LIST_FIRST "LIST_HEAD *head"
231 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
233 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
234 .Fn TAILQ_HEAD_INITIALIZER "head"
235 .Fn TAILQ_ENTRY "TYPE"
236 .Fn TAILQ_INIT "TAILQ_HEAD *head"
237 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
238 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
239 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
240 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
241 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
242 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
243 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *tmp"
244 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
245 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *tmp"
247 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
249 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
251 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
253 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
255 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
256 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
258 .Fn CIRCLEQ_HEAD "HEADNAME" "TYPE"
259 .Fn CIRCLEQ_HEAD_INITIALIZER "head"
260 .Fn CIRCLEQ_ENTRY "TYPE"
261 .Fn CIRCLEQ_INIT "CIRCLEQ_HEAD *head"
262 .Fn CIRCLEQ_INSERT_AFTER "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
263 .Fn CIRCLEQ_INSERT_BEFORE "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
264 .Fn CIRCLEQ_INSERT_HEAD "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
265 .Fn CIRCLEQ_INSERT_TAIL "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
266 .Fn CIRCLEQ_REMOVE "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
267 .Fn CIRCLEQ_FOREACH "TYPE *var" "CIRCLEQ_HEAD *head" "CIRCLEQ_ENTRY NAME"
268 .Fn CIRCLEQ_FOREACH_REVERSE "TYPE *var" "CIRCLEQ_HEAD *head" "CIRCLEQ_ENTRY NAME"
270 .Fn CIRCLEQ_EMPTY "CIRCLEQ_HEAD *head"
272 .Fn CIRCLEQ_FIRST "CIRCLEQ_HEAD *head"
274 .Fn CIRCLEQ_LAST "CIRCLEQ_HEAD *head"
276 .Fn CIRCLEQ_NEXT "TYPE *elm" "CIRCLEQ_ENTRY NAME"
278 .Fn CIRCLEQ_PREV "TYPE *elm" "CIRCLEQ_ENTRY NAME"
280 .Fn CIRCLEQ_LOOP_NEXT "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
282 .Fn CIRCLEQ_LOOP_PREV "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
284 These macros define and operate on five types of data structures:
285 singly-linked lists, simple queues, lists, tail queues, and circular queues.
286 All five structures support the following functionality:
287 .Bl -enum -compact -offset indent
289 Insertion of a new entry at the head of the list.
291 Insertion of a new entry before or after any element in the list.
293 Removal of any entry in the list.
295 Forward traversal through the list.
298 Singly-linked lists are the simplest of the five data structures and
299 support only the above functionality.
300 Singly-linked lists are ideal for applications with large datasets and
302 or for implementing a LIFO queue.
304 Simple queues add the following functionality:
305 .Bl -enum -compact -offset indent
307 Entries can be added at the end of a list.
309 They may be concatenated.
312 .Bl -enum -compact -offset indent
314 Entries may not be added before any element in the list.
316 All list insertions and removals must specify the head of the list.
318 Each head entry requires two pointers rather than one.
321 Simple queues are ideal for applications with large datasets and few or
322 no removals, or for implementing a FIFO queue.
324 All doubly linked types of data structures (lists, tail queues, and circle
325 queues) additionally allow:
326 .Bl -enum -compact -offset indent
328 Insertion of a new entry before any element in the list.
330 O(1) removal of any entry in the list.
333 .Bl -enum -compact -offset indent
335 Each element requires two pointers rather than one.
337 Code size and execution time of operations (except for removal) is about
338 twice that of the singly-linked data-structures.
341 Linked lists are the simplest of the doubly linked data structures and
342 support only the above functionality over singly-linked lists.
344 Tail queues add the following functionality:
345 .Bl -enum -compact -offset indent
347 Entries can be added at the end of a list.
349 They may be concatenated.
352 .Bl -enum -compact -offset indent
354 All list insertions and removals, except insertion before another element, must
355 specify the head of the list.
357 Each head entry requires two pointers rather than one.
359 Code size is about 15% greater and operations run about 20% slower
363 Circular queues add the following functionality:
364 .Bl -enum -compact -offset indent
366 Entries can be added at the end of a list.
368 They may be traversed backwards, from tail to head.
371 .Bl -enum -compact -offset indent
373 All list insertions and removals must specify the head of the list.
375 Each head entry requires two pointers rather than one.
377 The termination condition for traversal is more complex.
379 Code size is about 40% greater and operations run about 45% slower
383 In the macro definitions,
385 is the name of a user defined structure,
386 that must contain a field of type
397 is the name of a user defined structure that must be declared
405 See the examples below for further explanation of how these
407 .Ss Summary of Operations
408 The following table summarizes the supported macros for each type
413 l | c | c | c | c | c | c
414 l | c | c | c | c | c | c
415 l | c | c | c | c | c | c
416 l | c | c | c | c | c | c
417 l | c | c | c | c | c | c
418 l | c | c | c | c | c | c.
419 :SLIST:LIST:SIMPLEQ:STAILQ:TAILQ:CIRCLEQ
424 _FOREACH_REVERSE:-:-:-:-:+:+
425 _INSERT_AFTER:+:+:+:+:+:+
426 _INSERT_BEFORE:-:+:-:-:+:+
427 _INSERT_HEAD:+:+:+:+:+:+
428 _INSERT_TAIL:-:-:+:+:+:+
430 _LOOP_NEXT:-:-:-:-:-:+
431 _LOOP_PREV:-:-:-:-:-:+
435 _REMOVE_HEAD:+:-:+:+:-:-
438 .Sh SINGLY-LINKED LISTS
439 A singly-linked list is headed by a structure defined by the
442 This structure contains a single pointer to the first element
444 The elements are singly linked for minimum space and pointer manipulation
445 overhead at the expense of O(n) removal for arbitrary elements.
446 New elements can be added to the list after an existing element or
447 at the head of the list.
450 structure is declared as follows:
451 .Bd -literal -offset indent
452 SLIST_HEAD(HEADNAME, TYPE) head;
457 is the name of the structure to be defined, and
459 is the type of the elements to be linked into the list.
460 A pointer to the head of the list can later be declared as:
461 .Bd -literal -offset indent
462 struct HEADNAME *headp;
469 are user selectable.)
472 .Nm SLIST_HEAD_INITIALIZER
473 evaluates to an initializer for the list
478 evaluates to true if there are no elements in the list.
482 declares a structure that connects the elements in
487 returns the first element in the list or NULL if the list is empty.
491 traverses the list referenced by
493 in the forward direction, assigning each element in
497 The SAFE versions uses
499 to hold the next element, so
501 may be freed or removed from the list.
505 initializes the list referenced by
509 .Nm SLIST_INSERT_HEAD
510 inserts the new element
512 at the head of the list.
515 .Nm SLIST_INSERT_AFTER
516 inserts the new element
523 returns the next element in the list.
532 .Nm SLIST_REMOVE_HEAD
533 removes the first element from the head of the list.
534 For optimum efficiency,
535 elements being removed from the head of the list should explicitly use
536 this macro instead of the generic
539 .Sh SINGLY-LINKED LIST EXAMPLE
541 SLIST_HEAD(slisthead, entry) head =
542 SLIST_HEAD_INITIALIZER(head);
543 struct slisthead *headp; /* Singly-linked List head. */
546 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
548 } *n1, *n2, *n3, *np;
550 SLIST_INIT(\*[Am]head); /* Initialize the list. */
552 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
553 SLIST_INSERT_HEAD(\*[Am]head, n1, entries);
555 n2 = malloc(sizeof(struct entry)); /* Insert after. */
556 SLIST_INSERT_AFTER(n1, n2, entries);
558 SLIST_REMOVE(\*[Am]head, n2, entry, entries);/* Deletion. */
561 n3 = SLIST_FIRST(\*[Am]head);
562 SLIST_REMOVE_HEAD(\*[Am]head, entries); /* Deletion from the head. */
564 /* Forward traversal. */
565 SLIST_FOREACH(np, \*[Am]head, entries)
568 while (!SLIST_EMPTY(\*[Am]head)) { /* List Deletion. */
569 n1 = SLIST_FIRST(\*[Am]head);
570 SLIST_REMOVE_HEAD(\*[Am]head, entries);
575 A simple queue is headed by a structure defined by the
578 This structure contains a pair of pointers,
579 one to the first element in the simple queue and the other to
580 the last element in the simple queue.
581 The elements are singly linked for minimum space and pointer manipulation
582 overhead at the expense of O(n) removal for arbitrary elements.
583 New elements can be added to the queue after an existing element,
584 at the head of the queue, or at the end of the queue.
587 structure is declared as follows:
588 .Bd -literal -offset indent
589 SIMPLEQ_HEAD(HEADNAME, TYPE) head;
594 is the name of the structure to be defined, and
596 is the type of the elements to be linked into the simple queue.
597 A pointer to the head of the simple queue can later be declared as:
598 .Bd -literal -offset indent
599 struct HEADNAME *headp;
606 are user selectable.)
610 declares a structure that connects the elements in
614 .Nm SIMPLEQ_HEAD_INITIALIZER
615 provides a value which can be used to initialize a simple queue head at
616 compile time, and is used at the point that the simple queue head
617 variable is declared, like:
618 .Bd -literal -offset indent
619 struct HEADNAME head = SIMPLEQ_HEAD_INITIALIZER(head);
624 initializes the simple queue referenced by
628 .Nm SIMPLEQ_INSERT_HEAD
629 inserts the new element
631 at the head of the simple queue.
634 .Nm SIMPLEQ_INSERT_TAIL
635 inserts the new element
637 at the end of the simple queue.
640 .Nm SIMPLEQ_INSERT_AFTER
641 inserts the new element
650 from the simple queue.
653 .Nm SIMPLEQ_REMOVE_HEAD
654 removes the first element from the head of the simple queue.
655 For optimum efficiency,
656 elements being removed from the head of the queue should explicitly use
657 this macro instead of the generic
663 return true if the simple queue
669 returns the first element of the simple queue
675 .Nm SIMPLEQ_FOREACH_SAFE
676 traverse the tail queue referenced by
678 in the forward direction, assigning each element
682 The SAFE versions uses
684 to hold the next element, so
686 may be freed or removed from the list.
690 returns the element after the element
695 returns the last item on the tail queue.
696 If the tail queue is empty the return value is
701 concatenates the tail queue headed by
703 onto the end of the one headed by
705 removing all entries from the former.
707 The macros prefixed with
710 .Nm STAILQ_HEAD_INITIALIZER ,
713 .Nm STAILQ_INSERT_HEAD ,
714 .Nm STAILQ_INSERT_TAIL ,
715 .Nm STAILQ_INSERT_AFTER ,
716 .Nm STAILQ_REMOVE_HEAD ,
719 .Nm STAILQ_FOREACH_SAFE ,
726 are functionally identical to these simple queue functions,
727 and are provided for compatibility with
729 .Sh SIMPLE QUEUE EXAMPLE
731 SIMPLEQ_HEAD(simplehead, entry) head;
732 struct simplehead *headp; /* Simple queue head. */
735 SIMPLEQ_ENTRY(entry) entries; /* Simple queue. */
739 SIMPLEQ_INIT(\*[Am]head); /* Initialize the queue. */
741 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
742 SIMPLEQ_INSERT_HEAD(\*[Am]head, n1, entries);
744 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
745 SIMPLEQ_INSERT_TAIL(\*[Am]head, n1, entries);
747 n2 = malloc(sizeof(struct entry)); /* Insert after. */
748 SIMPLEQ_INSERT_AFTER(\*[Am]head, n1, n2, entries);
749 /* Forward traversal. */
750 SIMPLEQ_FOREACH(np, \*[Am]head, entries)
753 while (SIMPLEQ_FIRST(\*[Am]head) != NULL)
754 SIMPLEQ_REMOVE_HEAD(\*[Am]head, entries);
755 if (SIMPLEQ_EMPTY(\*[Am]head)) /* Test for emptiness. */
756 printf("nothing to do\\n");
759 A list is headed by a structure defined by the
762 This structure contains a single pointer to the first element
764 The elements are doubly linked so that an arbitrary element can be
765 removed without traversing the list.
766 New elements can be added to the list after an existing element,
767 before an existing element, or at the head of the list.
770 structure is declared as follows:
771 .Bd -literal -offset indent
772 LIST_HEAD(HEADNAME, TYPE) head;
777 is the name of the structure to be defined, and
779 is the type of the elements to be linked into the list.
780 A pointer to the head of the list can later be declared as:
781 .Bd -literal -offset indent
782 struct HEADNAME *headp;
789 are user selectable.)
793 declares a structure that connects the elements in
797 .Nm LIST_HEAD_INITIALIZER
798 provides a value which can be used to initialize a list head at
799 compile time, and is used at the point that the list head
800 variable is declared, like:
801 .Bd -literal -offset indent
802 struct HEADNAME head = LIST_HEAD_INITIALIZER(head);
807 initializes the list referenced by
812 inserts the new element
814 at the head of the list.
817 .Nm LIST_INSERT_AFTER
818 inserts the new element
824 .Nm LIST_INSERT_BEFORE
825 inserts the new element
838 return true if the list
844 returns the first element of the list
849 traverses the list referenced by
851 in the forward direction, assigning each element in turn to
856 returns the element after the element
860 LIST_HEAD(listhead, entry) head;
861 struct listhead *headp; /* List head. */
864 LIST_ENTRY(entry) entries; /* List. */
868 LIST_INIT(\*[Am]head); /* Initialize the list. */
870 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
871 LIST_INSERT_HEAD(\*[Am]head, n1, entries);
873 n2 = malloc(sizeof(struct entry)); /* Insert after. */
874 LIST_INSERT_AFTER(n1, n2, entries);
876 n2 = malloc(sizeof(struct entry)); /* Insert before. */
877 LIST_INSERT_BEFORE(n1, n2, entries);
878 /* Forward traversal. */
879 LIST_FOREACH(np, \*[Am]head, entries)
882 while (LIST_FIRST(\*[Am]head) != NULL)
883 LIST_REMOVE(LIST_FIRST(\*[Am]head), entries);
884 if (LIST_EMPTY(\*[Am]head)) /* Test for emptiness. */
885 printf("nothing to do\\n");
888 A tail queue is headed by a structure defined by the
891 This structure contains a pair of pointers,
892 one to the first element in the tail queue and the other to
893 the last element in the tail queue.
894 The elements are doubly linked so that an arbitrary element can be
895 removed without traversing the tail queue.
896 New elements can be added to the queue after an existing element,
897 before an existing element, at the head of the queue, or at the end
901 structure is declared as follows:
902 .Bd -literal -offset indent
903 TAILQ_HEAD(HEADNAME, TYPE) head;
908 is the name of the structure to be defined, and
910 is the type of the elements to be linked into the tail queue.
911 A pointer to the head of the tail queue can later be declared as:
912 .Bd -literal -offset indent
913 struct HEADNAME *headp;
920 are user selectable.)
924 declares a structure that connects the elements in
928 .Nm TAILQ_HEAD_INITIALIZER
929 provides a value which can be used to initialize a tail queue head at
930 compile time, and is used at the point that the tail queue head
931 variable is declared, like:
932 .Bd -literal -offset indent
933 struct HEADNAME head = TAILQ_HEAD_INITIALIZER(head);
938 initializes the tail queue referenced by
942 .Nm TAILQ_INSERT_HEAD
943 inserts the new element
945 at the head of the tail queue.
948 .Nm TAILQ_INSERT_TAIL
949 inserts the new element
951 at the end of the tail queue.
954 .Nm TAILQ_INSERT_AFTER
955 inserts the new element
961 .Nm TAILQ_INSERT_BEFORE
962 inserts the new element
975 return true if the tail queue
981 returns the first element of the tail queue
986 .Nm TAILQ_FOREACH_REVERSE ,
987 .Nm TAILQ_FOREACH_SAFE ,
989 .Nm TAILQ_FOREACH_REVERSE_SAFE
990 traverse the tail queue referenced by
992 in the forward or reverse direction direction, assigning each element in turn to
995 The SAFE versions uses
997 to hold the next element, so
999 may be freed or removed from the list.
1003 returns the element after the element
1008 concatenates the tail queue headed by
1010 onto the end of the one headed by
1012 removing all entries from the former.
1013 .Sh TAIL QUEUE EXAMPLE
1015 TAILQ_HEAD(tailhead, entry) head;
1016 struct tailhead *headp; /* Tail queue head. */
1019 TAILQ_ENTRY(entry) entries; /* Tail queue. */
1023 TAILQ_INIT(\*[Am]head); /* Initialize the queue. */
1025 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1026 TAILQ_INSERT_HEAD(\*[Am]head, n1, entries);
1028 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1029 TAILQ_INSERT_TAIL(\*[Am]head, n1, entries);
1031 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1032 TAILQ_INSERT_AFTER(\*[Am]head, n1, n2, entries);
1034 n2 = malloc(sizeof(struct entry)); /* Insert before. */
1035 TAILQ_INSERT_BEFORE(n1, n2, entries);
1036 /* Forward traversal. */
1037 TAILQ_FOREACH(np, \*[Am]head, entries)
1039 /* Reverse traversal. */
1040 TAILQ_FOREACH_REVERSE(np, \*[Am]head, tailhead, entries)
1043 while (TAILQ_FIRST(\*[Am]head) != NULL)
1044 TAILQ_REMOVE(\*[Am]head, TAILQ_FIRST(\*[Am]head), entries);
1045 if (TAILQ_EMPTY(\*[Am]head)) /* Test for emptiness. */
1046 printf("nothing to do\\n");
1049 A circular queue is headed by a structure defined by the
1052 This structure contains a pair of pointers,
1053 one to the first element in the circular queue and the other to the
1054 last element in the circular queue.
1055 The elements are doubly linked so that an arbitrary element can be
1056 removed without traversing the queue.
1057 New elements can be added to the queue after an existing element,
1058 before an existing element, at the head of the queue, or at the end
1062 structure is declared as follows:
1063 .Bd -literal -offset indent
1064 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
1069 is the name of the structure to be defined, and
1071 is the type of the elements to be linked into the circular queue.
1072 A pointer to the head of the circular queue can later be declared as:
1073 .Bd -literal -offset indent
1074 struct HEADNAME *headp;
1081 are user selectable.)
1085 declares a structure that connects the elements in
1089 .Nm CIRCLEQ_HEAD_INITIALIZER
1090 provides a value which can be used to initialize a circular queue head at
1091 compile time, and is used at the point that the circular queue head
1092 variable is declared, like:
1093 .Bd -literal -offset indent
1094 struct HEADNAME head = CIRCLEQ_HEAD_INITIALIZER(head);
1099 initializes the circular queue referenced by
1103 .Nm CIRCLEQ_INSERT_HEAD
1104 inserts the new element
1106 at the head of the circular queue.
1109 .Nm CIRCLEQ_INSERT_TAIL
1110 inserts the new element
1112 at the end of the circular queue.
1115 .Nm CIRCLEQ_INSERT_AFTER
1116 inserts the new element
1122 .Nm CIRCLEQ_INSERT_BEFORE
1123 inserts the new element
1132 from the circular queue.
1136 return true if the circular queue
1142 returns the first element of the circular queue
1147 traverses the circle queue referenced by
1149 in the forward direction, assigning each element in turn to
1151 Each element is assigned exactly once.
1154 .Nm CIRCLEQ_FOREACH_REVERSE
1155 traverses the circle queue referenced by
1157 in the reverse direction, assigning each element in turn to
1159 Each element is assigned exactly once.
1163 returns the last element of the circular queue
1168 returns the element after the element
1173 returns the element before the element
1177 .Nm CIRCLEQ_LOOP_NEXT
1178 returns the element after the element
1182 was the last element in the queue, the first element is returned.
1185 .Nm CIRCLEQ_LOOP_PREV
1186 returns the element before the element
1190 was the first element in the queue, the last element is returned.
1191 .Sh CIRCULAR QUEUE EXAMPLE
1193 CIRCLEQ_HEAD(circleq, entry) head;
1194 struct circleq *headp; /* Circular queue head. */
1197 CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
1201 CIRCLEQ_INIT(\*[Am]head); /* Initialize the circular queue. */
1203 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
1204 CIRCLEQ_INSERT_HEAD(\*[Am]head, n1, entries);
1206 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
1207 CIRCLEQ_INSERT_TAIL(\*[Am]head, n1, entries);
1209 n2 = malloc(sizeof(struct entry)); /* Insert after. */
1210 CIRCLEQ_INSERT_AFTER(\*[Am]head, n1, n2, entries);
1212 n2 = malloc(sizeof(struct entry)); /* Insert before. */
1213 CIRCLEQ_INSERT_BEFORE(\*[Am]head, n1, n2, entries);
1214 /* Forward traversal. */
1215 CIRCLEQ_FOREACH(np, \*[Am]head, entries)
1217 /* Reverse traversal. */
1218 CIRCLEQ_FOREACH_REVERSE(np, \*[Am]head, entries)
1221 while (CIRCLEQ_FIRST(\*[Am]head) != (void *)\*[Am]head)
1222 CIRCLEQ_REMOVE(\*[Am]head, CIRCLEQ_FIRST(\*[Am]head), entries);
1223 if (CIRCLEQ_EMPTY(\*[Am]head)) /* Test for emptiness. */
1224 printf("nothing to do\\n");
1227 Some of these macros or functions perform no error checking,
1228 and invalid usage leads to undefined behaviour.
1229 In the case of macros or functions that expect their arguments
1230 to be elements that are present in the list or queue, passing an element
1231 that is not present is invalid.
1235 functions first appeared in
1239 functions first appeared in
1245 functions first appeared in
1249 functions first appeared in