xref: /freebsd/share/man/man3/queue.3 (revision 325151a3)
1.\" Copyright (c) 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
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.
15.\"
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
26.\" SUCH DAMAGE.
27.\"
28.\"	@(#)queue.3	8.2 (Berkeley) 1/24/94
29.\" $FreeBSD$
30.\"
31.Dd June 24, 2015
32.Dt QUEUE 3
33.Os
34.Sh NAME
35.Nm SLIST_CLASS_ENTRY ,
36.Nm SLIST_CLASS_HEAD ,
37.Nm SLIST_EMPTY ,
38.Nm SLIST_ENTRY ,
39.Nm SLIST_FIRST ,
40.Nm SLIST_FOREACH ,
41.Nm SLIST_FOREACH_FROM ,
42.Nm SLIST_FOREACH_FROM_SAFE ,
43.Nm SLIST_FOREACH_SAFE ,
44.Nm SLIST_HEAD ,
45.Nm SLIST_HEAD_INITIALIZER ,
46.Nm SLIST_INIT ,
47.Nm SLIST_INSERT_AFTER ,
48.Nm SLIST_INSERT_HEAD ,
49.Nm SLIST_NEXT ,
50.Nm SLIST_REMOVE ,
51.Nm SLIST_REMOVE_AFTER ,
52.Nm SLIST_REMOVE_HEAD ,
53.Nm SLIST_SWAP ,
54.Nm STAILQ_CLASS_ENTRY ,
55.Nm STAILQ_CLASS_HEAD ,
56.Nm STAILQ_CONCAT ,
57.Nm STAILQ_EMPTY ,
58.Nm STAILQ_ENTRY ,
59.Nm STAILQ_FIRST ,
60.Nm STAILQ_FOREACH ,
61.Nm STAILQ_FOREACH_FROM ,
62.Nm STAILQ_FOREACH_FROM_SAFE ,
63.Nm STAILQ_FOREACH_SAFE ,
64.Nm STAILQ_HEAD ,
65.Nm STAILQ_HEAD_INITIALIZER ,
66.Nm STAILQ_INIT ,
67.Nm STAILQ_INSERT_AFTER ,
68.Nm STAILQ_INSERT_HEAD ,
69.Nm STAILQ_INSERT_TAIL ,
70.Nm STAILQ_LAST ,
71.Nm STAILQ_NEXT ,
72.Nm STAILQ_REMOVE ,
73.Nm STAILQ_REMOVE_AFTER ,
74.Nm STAILQ_REMOVE_HEAD ,
75.Nm STAILQ_SWAP ,
76.Nm LIST_CLASS_ENTRY ,
77.Nm LIST_CLASS_HEAD ,
78.Nm LIST_EMPTY ,
79.Nm LIST_ENTRY ,
80.Nm LIST_FIRST ,
81.Nm LIST_FOREACH ,
82.Nm LIST_FOREACH_FROM ,
83.Nm LIST_FOREACH_FROM_SAFE ,
84.Nm LIST_FOREACH_SAFE ,
85.Nm LIST_HEAD ,
86.Nm LIST_HEAD_INITIALIZER ,
87.Nm LIST_INIT ,
88.Nm LIST_INSERT_AFTER ,
89.Nm LIST_INSERT_BEFORE ,
90.Nm LIST_INSERT_HEAD ,
91.Nm LIST_NEXT ,
92.Nm LIST_PREV ,
93.Nm LIST_REMOVE ,
94.Nm LIST_SWAP ,
95.Nm TAILQ_CLASS_ENTRY ,
96.Nm TAILQ_CLASS_HEAD ,
97.Nm TAILQ_CONCAT ,
98.Nm TAILQ_EMPTY ,
99.Nm TAILQ_ENTRY ,
100.Nm TAILQ_FIRST ,
101.Nm TAILQ_FOREACH ,
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 ,
109.Nm TAILQ_HEAD ,
110.Nm TAILQ_HEAD_INITIALIZER ,
111.Nm TAILQ_INIT ,
112.Nm TAILQ_INSERT_AFTER ,
113.Nm TAILQ_INSERT_BEFORE ,
114.Nm TAILQ_INSERT_HEAD ,
115.Nm TAILQ_INSERT_TAIL ,
116.Nm TAILQ_LAST ,
117.Nm TAILQ_NEXT ,
118.Nm TAILQ_PREV ,
119.Nm TAILQ_REMOVE ,
120.Nm TAILQ_SWAP
121.Nd implementations of singly-linked lists, singly-linked tail queues,
122lists and tail queues
123.Sh SYNOPSIS
124.In sys/queue.h
125.\"
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"
145.\"
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"
168.\"
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"
188.\"
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"
215.\"
216.Sh DESCRIPTION
217These macros define and operate on four types of data structures which
218can be used in both C and C++ source code:
219.Bl -enum -compact -offset indent
220.It
221Lists
222.It
223Singly-linked lists
224.It
225Singly-linked tail queues
226.It
227Tail queues
228.El
229All four structures support the following functionality:
230.Bl -enum -compact -offset indent
231.It
232Insertion of a new entry at the head of the list.
233.It
234Insertion of a new entry after any element in the list.
235.It
236O(1) removal of an entry from the head of the list.
237.It
238Forward traversal through the list.
239.It
240Swapping the contents of two lists.
241.El
242.Pp
243Singly-linked lists are the simplest of the four data structures
244and support only the above functionality.
245Singly-linked lists are ideal for applications with large datasets
246and few or no removals,
247or for implementing a LIFO queue.
248Singly-linked lists add the following functionality:
249.Bl -enum -compact -offset indent
250.It
251O(n) removal of any entry in the list.
252.El
253.Pp
254Singly-linked tail queues add the following functionality:
255.Bl -enum -compact -offset indent
256.It
257Entries can be added at the end of a list.
258.It
259O(n) removal of any entry in the list.
260.It
261They may be concatenated.
262.El
263However:
264.Bl -enum -compact -offset indent
265.It
266All list insertions must specify the head of the list.
267.It
268Each head entry requires two pointers rather than one.
269.It
270Code size is about 15% greater and operations run about 20% slower
271than singly-linked lists.
272.El
273.Pp
274Singly-linked tail queues are ideal for applications with large datasets and
275few or no removals,
276or for implementing a FIFO queue.
277.Pp
278All doubly linked types of data structures (lists and tail queues)
279additionally allow:
280.Bl -enum -compact -offset indent
281.It
282Insertion of a new entry before any element in the list.
283.It
284O(1) removal of any entry in the list.
285.El
286However:
287.Bl -enum -compact -offset indent
288.It
289Each element requires two pointers rather than one.
290.It
291Code size and execution time of operations (except for removal) is about
292twice that of the singly-linked data-structures.
293.El
294.Pp
295Linked lists are the simplest of the doubly linked data structures.
296They add the following functionality over the above:
297.Bl -enum -compact -offset indent
298.It
299They may be traversed backwards.
300.El
301However:
302.Bl -enum -compact -offset indent
303.It
304To traverse backwards, an entry to begin the traversal and the list in
305which it is contained must be specified.
306.El
307.Pp
308Tail queues add the following functionality:
309.Bl -enum -compact -offset indent
310.It
311Entries can be added at the end of a list.
312.It
313They may be traversed backwards, from tail to head.
314.It
315They may be concatenated.
316.El
317However:
318.Bl -enum -compact -offset indent
319.It
320All list insertions and removals must specify the head of the list.
321.It
322Each head entry requires two pointers rather than one.
323.It
324Code size is about 15% greater and operations run about 20% slower
325than singly-linked lists.
326.El
327.Pp
328In the macro definitions,
329.Fa TYPE
330is the name of a user defined structure.
331The structure must contain a field called
332.Fa NAME
333which is of type
334.Li SLIST_ENTRY ,
335.Li STAILQ_ENTRY ,
336.Li LIST_ENTRY ,
337or
338.Li TAILQ_ENTRY .
339In the macro definitions,
340.Fa CLASSTYPE
341is the name of a user defined class.
342The class must contain a field called
343.Fa NAME
344which is of type
345.Li SLIST_CLASS_ENTRY ,
346.Li STAILQ_CLASS_ENTRY ,
347.Li LIST_CLASS_ENTRY ,
348or
349.Li TAILQ_CLASS_ENTRY .
350The argument
351.Fa HEADNAME
352is the name of a user defined structure that must be declared
353using the macros
354.Li SLIST_HEAD ,
355.Li SLIST_CLASS_HEAD ,
356.Li STAILQ_HEAD ,
357.Li STAILQ_CLASS_HEAD ,
358.Li LIST_HEAD ,
359.Li LIST_CLASS_HEAD ,
360.Li TAILQ_HEAD ,
361or
362.Li TAILQ_CLASS_HEAD .
363See the examples below for further explanation of how these
364macros are used.
365.Sh SINGLY-LINKED LISTS
366A singly-linked list is headed by a structure defined by the
367.Nm SLIST_HEAD
368macro.
369This structure contains a single pointer to the first element
370on the list.
371The elements are singly linked for minimum space and pointer manipulation
372overhead at the expense of O(n) removal for arbitrary elements.
373New elements can be added to the list after an existing element or
374at the head of the list.
375An
376.Fa SLIST_HEAD
377structure is declared as follows:
378.Bd -literal -offset indent
379SLIST_HEAD(HEADNAME, TYPE) head;
380.Ed
381.Pp
382where
383.Fa HEADNAME
384is the name of the structure to be defined, and
385.Fa TYPE
386is the type of the elements to be linked into the list.
387A pointer to the head of the list can later be declared as:
388.Bd -literal -offset indent
389struct HEADNAME *headp;
390.Ed
391.Pp
392(The names
393.Li head
394and
395.Li headp
396are user selectable.)
397.Pp
398The macro
399.Nm SLIST_HEAD_INITIALIZER
400evaluates to an initializer for the list
401.Fa head .
402.Pp
403The macro
404.Nm SLIST_EMPTY
405evaluates to true if there are no elements in the list.
406.Pp
407The macro
408.Nm SLIST_ENTRY
409declares a structure that connects the elements in
410the list.
411.Pp
412The macro
413.Nm SLIST_FIRST
414returns the first element in the list or NULL if the list is empty.
415.Pp
416The macro
417.Nm SLIST_FOREACH
418traverses the list referenced by
419.Fa head
420in the forward direction, assigning each element in
421turn to
422.Fa var .
423.Pp
424The macro
425.Nm SLIST_FOREACH_FROM
426behaves identically to
427.Nm SLIST_FOREACH
428when
429.Fa var
430is NULL, else it treats
431.Fa var
432as a previously found SLIST element and begins the loop at
433.Fa var
434instead of the first element in the SLIST referenced by
435.Fa head .
436.Pp
437The macro
438.Nm SLIST_FOREACH_SAFE
439traverses the list referenced by
440.Fa head
441in the forward direction, assigning each element in
442turn to
443.Fa var .
444However, unlike
445.Fn SLIST_FOREACH
446here it is permitted to both remove
447.Fa var
448as well as free it from within the loop safely without interfering with the
449traversal.
450.Pp
451The macro
452.Nm SLIST_FOREACH_FROM_SAFE
453behaves identically to
454.Nm SLIST_FOREACH_SAFE
455when
456.Fa var
457is NULL, else it treats
458.Fa var
459as a previously found SLIST element and begins the loop at
460.Fa var
461instead of the first element in the SLIST referenced by
462.Fa head .
463.Pp
464The macro
465.Nm SLIST_INIT
466initializes the list referenced by
467.Fa head .
468.Pp
469The macro
470.Nm SLIST_INSERT_HEAD
471inserts the new element
472.Fa elm
473at the head of the list.
474.Pp
475The macro
476.Nm SLIST_INSERT_AFTER
477inserts the new element
478.Fa elm
479after the element
480.Fa listelm .
481.Pp
482The macro
483.Nm SLIST_NEXT
484returns the next element in the list.
485.Pp
486The macro
487.Nm SLIST_REMOVE_AFTER
488removes the element after
489.Fa elm
490from the list.
491Unlike
492.Fa SLIST_REMOVE ,
493this macro does not traverse the entire list.
494.Pp
495The macro
496.Nm SLIST_REMOVE_HEAD
497removes the element
498.Fa elm
499from the head of the list.
500For optimum efficiency,
501elements being removed from the head of the list should explicitly use
502this macro instead of the generic
503.Fa SLIST_REMOVE
504macro.
505.Pp
506The macro
507.Nm SLIST_REMOVE
508removes the element
509.Fa elm
510from the list.
511.Pp
512The macro
513.Nm SLIST_SWAP
514swaps the contents of
515.Fa head1
516and
517.Fa head2 .
518.Sh SINGLY-LINKED LIST EXAMPLE
519.Bd -literal
520SLIST_HEAD(slisthead, entry) head =
521    SLIST_HEAD_INITIALIZER(head);
522struct slisthead *headp;		/* Singly-linked List head. */
523struct entry {
524	...
525	SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
526	...
527} *n1, *n2, *n3, *np;
528
529SLIST_INIT(&head);			/* Initialize the list. */
530
531n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
532SLIST_INSERT_HEAD(&head, n1, entries);
533
534n2 = malloc(sizeof(struct entry));	/* Insert after. */
535SLIST_INSERT_AFTER(n1, n2, entries);
536
537SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
538free(n2);
539
540n3 = SLIST_FIRST(&head);
541SLIST_REMOVE_HEAD(&head, entries);	/* Deletion from the head. */
542free(n3);
543					/* Forward traversal. */
544SLIST_FOREACH(np, &head, entries)
545	np-> ...
546					/* Safe forward traversal. */
547SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
548	np->do_stuff();
549	...
550	SLIST_REMOVE(&head, np, entry, entries);
551	free(np);
552}
553
554while (!SLIST_EMPTY(&head)) {		/* List Deletion. */
555	n1 = SLIST_FIRST(&head);
556	SLIST_REMOVE_HEAD(&head, entries);
557	free(n1);
558}
559.Ed
560.Sh SINGLY-LINKED TAIL QUEUES
561A singly-linked tail queue is headed by a structure defined by the
562.Nm STAILQ_HEAD
563macro.
564This structure contains a pair of pointers,
565one to the first element in the tail queue and the other to
566the last element in the tail queue.
567The elements are singly linked for minimum space and pointer
568manipulation overhead at the expense of O(n) removal for arbitrary
569elements.
570New elements can be added to the tail queue after an existing element,
571at the head of the tail queue, or at the end of the tail queue.
572A
573.Fa STAILQ_HEAD
574structure is declared as follows:
575.Bd -literal -offset indent
576STAILQ_HEAD(HEADNAME, TYPE) head;
577.Ed
578.Pp
579where
580.Li HEADNAME
581is the name of the structure to be defined, and
582.Li TYPE
583is the type of the elements to be linked into the tail queue.
584A pointer to the head of the tail queue can later be declared as:
585.Bd -literal -offset indent
586struct HEADNAME *headp;
587.Ed
588.Pp
589(The names
590.Li head
591and
592.Li headp
593are user selectable.)
594.Pp
595The macro
596.Nm STAILQ_HEAD_INITIALIZER
597evaluates to an initializer for the tail queue
598.Fa head .
599.Pp
600The macro
601.Nm STAILQ_CONCAT
602concatenates the tail queue headed by
603.Fa head2
604onto the end of the one headed by
605.Fa head1
606removing all entries from the former.
607.Pp
608The macro
609.Nm STAILQ_EMPTY
610evaluates to true if there are no items on the tail queue.
611.Pp
612The macro
613.Nm STAILQ_ENTRY
614declares a structure that connects the elements in
615the tail queue.
616.Pp
617The macro
618.Nm STAILQ_FIRST
619returns the first item on the tail queue or NULL if the tail queue
620is empty.
621.Pp
622The macro
623.Nm STAILQ_FOREACH
624traverses the tail queue referenced by
625.Fa head
626in the forward direction, assigning each element
627in turn to
628.Fa var .
629.Pp
630The macro
631.Nm STAILQ_FOREACH_FROM
632behaves identically to
633.Nm STAILQ_FOREACH
634when
635.Fa var
636is NULL, else it treats
637.Fa var
638as a previously found STAILQ element and begins the loop at
639.Fa var
640instead of the first element in the STAILQ referenced by
641.Fa head .
642.Pp
643The macro
644.Nm STAILQ_FOREACH_SAFE
645traverses the tail queue referenced by
646.Fa head
647in the forward direction, assigning each element
648in turn to
649.Fa var .
650However, unlike
651.Fn STAILQ_FOREACH
652here it is permitted to both remove
653.Fa var
654as well as free it from within the loop safely without interfering with the
655traversal.
656.Pp
657The macro
658.Nm STAILQ_FOREACH_FROM_SAFE
659behaves identically to
660.Nm STAILQ_FOREACH_SAFE
661when
662.Fa var
663is NULL, else it treats
664.Fa var
665as a previously found STAILQ element and begins the loop at
666.Fa var
667instead of the first element in the STAILQ referenced by
668.Fa head .
669.Pp
670The macro
671.Nm STAILQ_INIT
672initializes the tail queue referenced by
673.Fa head .
674.Pp
675The macro
676.Nm STAILQ_INSERT_HEAD
677inserts the new element
678.Fa elm
679at the head of the tail queue.
680.Pp
681The macro
682.Nm STAILQ_INSERT_TAIL
683inserts the new element
684.Fa elm
685at the end of the tail queue.
686.Pp
687The macro
688.Nm STAILQ_INSERT_AFTER
689inserts the new element
690.Fa elm
691after the element
692.Fa listelm .
693.Pp
694The macro
695.Nm STAILQ_LAST
696returns the last item on the tail queue.
697If the tail queue is empty the return value is
698.Dv NULL .
699.Pp
700The macro
701.Nm STAILQ_NEXT
702returns the next item on the tail queue, or NULL this item is the last.
703.Pp
704The macro
705.Nm STAILQ_REMOVE_AFTER
706removes the element after
707.Fa elm
708from the tail queue.
709Unlike
710.Fa STAILQ_REMOVE ,
711this macro does not traverse the entire tail queue.
712.Pp
713The macro
714.Nm STAILQ_REMOVE_HEAD
715removes the element at the head of the tail queue.
716For optimum efficiency,
717elements being removed from the head of the tail queue should
718use this macro explicitly rather than the generic
719.Fa STAILQ_REMOVE
720macro.
721.Pp
722The macro
723.Nm STAILQ_REMOVE
724removes the element
725.Fa elm
726from the tail queue.
727.Pp
728The macro
729.Nm STAILQ_SWAP
730swaps the contents of
731.Fa head1
732and
733.Fa head2 .
734.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
735.Bd -literal
736STAILQ_HEAD(stailhead, entry) head =
737    STAILQ_HEAD_INITIALIZER(head);
738struct stailhead *headp;		/* Singly-linked tail queue head. */
739struct entry {
740	...
741	STAILQ_ENTRY(entry) entries;	/* Tail queue. */
742	...
743} *n1, *n2, *n3, *np;
744
745STAILQ_INIT(&head);			/* Initialize the queue. */
746
747n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
748STAILQ_INSERT_HEAD(&head, n1, entries);
749
750n1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
751STAILQ_INSERT_TAIL(&head, n1, entries);
752
753n2 = malloc(sizeof(struct entry));	/* Insert after. */
754STAILQ_INSERT_AFTER(&head, n1, n2, entries);
755					/* Deletion. */
756STAILQ_REMOVE(&head, n2, entry, entries);
757free(n2);
758					/* Deletion from the head. */
759n3 = STAILQ_FIRST(&head);
760STAILQ_REMOVE_HEAD(&head, entries);
761free(n3);
762					/* Forward traversal. */
763STAILQ_FOREACH(np, &head, entries)
764	np-> ...
765					/* Safe forward traversal. */
766STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
767	np->do_stuff();
768	...
769	STAILQ_REMOVE(&head, np, entry, entries);
770	free(np);
771}
772					/* TailQ Deletion. */
773while (!STAILQ_EMPTY(&head)) {
774	n1 = STAILQ_FIRST(&head);
775	STAILQ_REMOVE_HEAD(&head, entries);
776	free(n1);
777}
778					/* Faster TailQ Deletion. */
779n1 = STAILQ_FIRST(&head);
780while (n1 != NULL) {
781	n2 = STAILQ_NEXT(n1, entries);
782	free(n1);
783	n1 = n2;
784}
785STAILQ_INIT(&head);
786.Ed
787.Sh LISTS
788A list is headed by a structure defined by the
789.Nm LIST_HEAD
790macro.
791This structure contains a single pointer to the first element
792on the list.
793The elements are doubly linked so that an arbitrary element can be
794removed without traversing the list.
795New elements can be added to the list after an existing element,
796before an existing element, or at the head of the list.
797A
798.Fa LIST_HEAD
799structure is declared as follows:
800.Bd -literal -offset indent
801LIST_HEAD(HEADNAME, TYPE) head;
802.Ed
803.Pp
804where
805.Fa HEADNAME
806is the name of the structure to be defined, and
807.Fa TYPE
808is the type of the elements to be linked into the list.
809A pointer to the head of the list can later be declared as:
810.Bd -literal -offset indent
811struct HEADNAME *headp;
812.Ed
813.Pp
814(The names
815.Li head
816and
817.Li headp
818are user selectable.)
819.Pp
820The macro
821.Nm LIST_HEAD_INITIALIZER
822evaluates to an initializer for the list
823.Fa head .
824.Pp
825The macro
826.Nm LIST_EMPTY
827evaluates to true if there are no elements in the list.
828.Pp
829The macro
830.Nm LIST_ENTRY
831declares a structure that connects the elements in
832the list.
833.Pp
834The macro
835.Nm LIST_FIRST
836returns the first element in the list or NULL if the list
837is empty.
838.Pp
839The macro
840.Nm LIST_FOREACH
841traverses the list referenced by
842.Fa head
843in the forward direction, assigning each element in turn to
844.Fa var .
845.Pp
846The macro
847.Nm LIST_FOREACH_FROM
848behaves identically to
849.Nm LIST_FOREACH
850when
851.Fa var
852is NULL, else it treats
853.Fa var
854as a previously found LIST element and begins the loop at
855.Fa var
856instead of the first element in the LIST referenced by
857.Fa head .
858.Pp
859The macro
860.Nm LIST_FOREACH_SAFE
861traverses the list referenced by
862.Fa head
863in the forward direction, assigning each element in turn to
864.Fa var .
865However, unlike
866.Fn LIST_FOREACH
867here it is permitted to both remove
868.Fa var
869as well as free it from within the loop safely without interfering with the
870traversal.
871.Pp
872The macro
873.Nm LIST_FOREACH_FROM_SAFE
874behaves identically to
875.Nm LIST_FOREACH_SAFE
876when
877.Fa var
878is NULL, else it treats
879.Fa var
880as a previously found LIST element and begins the loop at
881.Fa var
882instead of the first element in the LIST referenced by
883.Fa head .
884.Pp
885The macro
886.Nm LIST_INIT
887initializes the list referenced by
888.Fa head .
889.Pp
890The macro
891.Nm LIST_INSERT_HEAD
892inserts the new element
893.Fa elm
894at the head of the list.
895.Pp
896The macro
897.Nm LIST_INSERT_AFTER
898inserts the new element
899.Fa elm
900after the element
901.Fa listelm .
902.Pp
903The macro
904.Nm LIST_INSERT_BEFORE
905inserts the new element
906.Fa elm
907before the element
908.Fa listelm .
909.Pp
910The macro
911.Nm LIST_NEXT
912returns the next element in the list, or NULL if this is the last.
913.Pp
914The macro
915.Nm LIST_PREV
916returns the previous element in the list, or NULL if this is the first.
917List
918.Fa head
919must contain element
920.Fa elm .
921.Pp
922The macro
923.Nm LIST_REMOVE
924removes the element
925.Fa elm
926from the list.
927.Pp
928The macro
929.Nm LIST_SWAP
930swaps the contents of
931.Fa head1
932and
933.Fa head2 .
934.Sh LIST EXAMPLE
935.Bd -literal
936LIST_HEAD(listhead, entry) head =
937    LIST_HEAD_INITIALIZER(head);
938struct listhead *headp;			/* List head. */
939struct entry {
940	...
941	LIST_ENTRY(entry) entries;	/* List. */
942	...
943} *n1, *n2, *n3, *np, *np_temp;
944
945LIST_INIT(&head);			/* Initialize the list. */
946
947n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
948LIST_INSERT_HEAD(&head, n1, entries);
949
950n2 = malloc(sizeof(struct entry));	/* Insert after. */
951LIST_INSERT_AFTER(n1, n2, entries);
952
953n3 = malloc(sizeof(struct entry));	/* Insert before. */
954LIST_INSERT_BEFORE(n2, n3, entries);
955
956LIST_REMOVE(n2, entries);		/* Deletion. */
957free(n2);
958					/* Forward traversal. */
959LIST_FOREACH(np, &head, entries)
960	np-> ...
961
962					/* Safe forward traversal. */
963LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
964	np->do_stuff();
965	...
966	LIST_REMOVE(np, entries);
967	free(np);
968}
969
970while (!LIST_EMPTY(&head)) {		/* List Deletion. */
971	n1 = LIST_FIRST(&head);
972	LIST_REMOVE(n1, entries);
973	free(n1);
974}
975
976n1 = LIST_FIRST(&head);			/* Faster List Deletion. */
977while (n1 != NULL) {
978	n2 = LIST_NEXT(n1, entries);
979	free(n1);
980	n1 = n2;
981}
982LIST_INIT(&head);
983.Ed
984.Sh TAIL QUEUES
985A tail queue is headed by a structure defined by the
986.Nm TAILQ_HEAD
987macro.
988This structure contains a pair of pointers,
989one to the first element in the tail queue and the other to
990the last element in the tail queue.
991The elements are doubly linked so that an arbitrary element can be
992removed without traversing the tail queue.
993New elements can be added to the tail queue after an existing element,
994before an existing element, at the head of the tail queue,
995or at the end of the tail queue.
996A
997.Fa TAILQ_HEAD
998structure is declared as follows:
999.Bd -literal -offset indent
1000TAILQ_HEAD(HEADNAME, TYPE) head;
1001.Ed
1002.Pp
1003where
1004.Li HEADNAME
1005is the name of the structure to be defined, and
1006.Li TYPE
1007is the type of the elements to be linked into the tail queue.
1008A pointer to the head of the tail queue can later be declared as:
1009.Bd -literal -offset indent
1010struct HEADNAME *headp;
1011.Ed
1012.Pp
1013(The names
1014.Li head
1015and
1016.Li headp
1017are user selectable.)
1018.Pp
1019The macro
1020.Nm TAILQ_HEAD_INITIALIZER
1021evaluates to an initializer for the tail queue
1022.Fa head .
1023.Pp
1024The macro
1025.Nm TAILQ_CONCAT
1026concatenates the tail queue headed by
1027.Fa head2
1028onto the end of the one headed by
1029.Fa head1
1030removing all entries from the former.
1031.Pp
1032The macro
1033.Nm TAILQ_EMPTY
1034evaluates to true if there are no items on the tail queue.
1035.Pp
1036The macro
1037.Nm TAILQ_ENTRY
1038declares a structure that connects the elements in
1039the tail queue.
1040.Pp
1041The macro
1042.Nm TAILQ_FIRST
1043returns the first item on the tail queue or NULL if the tail queue
1044is empty.
1045.Pp
1046The macro
1047.Nm TAILQ_FOREACH
1048traverses the tail queue referenced by
1049.Fa head
1050in the forward direction, assigning each element in turn to
1051.Fa var .
1052.Fa var
1053is set to
1054.Dv NULL
1055if the loop completes normally, or if there were no elements.
1056.Pp
1057The macro
1058.Nm TAILQ_FOREACH_FROM
1059behaves identically to
1060.Nm TAILQ_FOREACH
1061when
1062.Fa var
1063is NULL, else it treats
1064.Fa var
1065as a previously found TAILQ element and begins the loop at
1066.Fa var
1067instead of the first element in the TAILQ referenced by
1068.Fa head .
1069.Pp
1070The macro
1071.Nm TAILQ_FOREACH_REVERSE
1072traverses the tail queue referenced by
1073.Fa head
1074in the reverse direction, assigning each element in turn to
1075.Fa var .
1076.Pp
1077The macro
1078.Nm TAILQ_FOREACH_REVERSE_FROM
1079behaves identically to
1080.Nm TAILQ_FOREACH_REVERSE
1081when
1082.Fa var
1083is NULL, else it treats
1084.Fa var
1085as a previously found TAILQ element and begins the reverse loop at
1086.Fa var
1087instead of the last element in the TAILQ referenced by
1088.Fa head .
1089.Pp
1090The macros
1091.Nm TAILQ_FOREACH_SAFE
1092and
1093.Nm TAILQ_FOREACH_REVERSE_SAFE
1094traverse the list referenced by
1095.Fa head
1096in the forward or reverse direction respectively,
1097assigning each element in turn to
1098.Fa var .
1099However, unlike their unsafe counterparts,
1100.Nm TAILQ_FOREACH
1101and
1102.Nm TAILQ_FOREACH_REVERSE
1103permit to both remove
1104.Fa var
1105as well as free it from within the loop safely without interfering with the
1106traversal.
1107.Pp
1108The macro
1109.Nm TAILQ_FOREACH_FROM_SAFE
1110behaves identically to
1111.Nm TAILQ_FOREACH_SAFE
1112when
1113.Fa var
1114is NULL, else it treats
1115.Fa var
1116as a previously found TAILQ element and begins the loop at
1117.Fa var
1118instead of the first element in the TAILQ referenced by
1119.Fa head .
1120.Pp
1121The macro
1122.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1123behaves identically to
1124.Nm TAILQ_FOREACH_REVERSE_SAFE
1125when
1126.Fa var
1127is NULL, else it treats
1128.Fa var
1129as a previously found TAILQ element and begins the reverse loop at
1130.Fa var
1131instead of the last element in the TAILQ referenced by
1132.Fa head .
1133.Pp
1134The macro
1135.Nm TAILQ_INIT
1136initializes the tail queue referenced by
1137.Fa head .
1138.Pp
1139The macro
1140.Nm TAILQ_INSERT_HEAD
1141inserts the new element
1142.Fa elm
1143at the head of the tail queue.
1144.Pp
1145The macro
1146.Nm TAILQ_INSERT_TAIL
1147inserts the new element
1148.Fa elm
1149at the end of the tail queue.
1150.Pp
1151The macro
1152.Nm TAILQ_INSERT_AFTER
1153inserts the new element
1154.Fa elm
1155after the element
1156.Fa listelm .
1157.Pp
1158The macro
1159.Nm TAILQ_INSERT_BEFORE
1160inserts the new element
1161.Fa elm
1162before the element
1163.Fa listelm .
1164.Pp
1165The macro
1166.Nm TAILQ_LAST
1167returns the last item on the tail queue.
1168If the tail queue is empty the return value is
1169.Dv NULL .
1170.Pp
1171The macro
1172.Nm TAILQ_NEXT
1173returns the next item on the tail queue, or NULL if this item is the last.
1174.Pp
1175The macro
1176.Nm TAILQ_PREV
1177returns the previous item on the tail queue, or NULL if this item
1178is the first.
1179.Pp
1180The macro
1181.Nm TAILQ_REMOVE
1182removes the element
1183.Fa elm
1184from the tail queue.
1185.Pp
1186The macro
1187.Nm TAILQ_SWAP
1188swaps the contents of
1189.Fa head1
1190and
1191.Fa head2 .
1192.Sh TAIL QUEUE EXAMPLE
1193.Bd -literal
1194TAILQ_HEAD(tailhead, entry) head =
1195    TAILQ_HEAD_INITIALIZER(head);
1196struct tailhead *headp;			/* Tail queue head. */
1197struct entry {
1198	...
1199	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
1200	...
1201} *n1, *n2, *n3, *np;
1202
1203TAILQ_INIT(&head);			/* Initialize the queue. */
1204
1205n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
1206TAILQ_INSERT_HEAD(&head, n1, entries);
1207
1208n1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
1209TAILQ_INSERT_TAIL(&head, n1, entries);
1210
1211n2 = malloc(sizeof(struct entry));	/* Insert after. */
1212TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1213
1214n3 = malloc(sizeof(struct entry));	/* Insert before. */
1215TAILQ_INSERT_BEFORE(n2, n3, entries);
1216
1217TAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
1218free(n2);
1219					/* Forward traversal. */
1220TAILQ_FOREACH(np, &head, entries)
1221	np-> ...
1222					/* Safe forward traversal. */
1223TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1224	np->do_stuff();
1225	...
1226	TAILQ_REMOVE(&head, np, entries);
1227	free(np);
1228}
1229					/* Reverse traversal. */
1230TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1231	np-> ...
1232					/* TailQ Deletion. */
1233while (!TAILQ_EMPTY(&head)) {
1234	n1 = TAILQ_FIRST(&head);
1235	TAILQ_REMOVE(&head, n1, entries);
1236	free(n1);
1237}
1238					/* Faster TailQ Deletion. */
1239n1 = TAILQ_FIRST(&head);
1240while (n1 != NULL) {
1241	n2 = TAILQ_NEXT(n1, entries);
1242	free(n1);
1243	n1 = n2;
1244}
1245TAILQ_INIT(&head);
1246.Ed
1247.Sh SEE ALSO
1248.Xr tree 3
1249.Sh HISTORY
1250The
1251.Nm queue
1252functions first appeared in
1253.Bx 4.4 .
1254