xref: /freebsd/share/man/man3/queue.3 (revision a5a186baf2c89ebfd795d74d9223d57acd8c0383)
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.Dd April 28, 2025
29.Dt QUEUE 3
30.Os
31.Sh NAME
32.Nm SLIST_CLASS_ENTRY ,
33.Nm SLIST_CLASS_HEAD ,
34.Nm SLIST_CONCAT ,
35.Nm SLIST_EMPTY ,
36.Nm SLIST_EMPTY_ATOMIC ,
37.Nm SLIST_ENTRY ,
38.Nm SLIST_FIRST ,
39.Nm SLIST_FOREACH ,
40.Nm SLIST_FOREACH_FROM ,
41.Nm SLIST_FOREACH_FROM_SAFE ,
42.Nm SLIST_FOREACH_SAFE ,
43.Nm SLIST_HEAD ,
44.Nm SLIST_HEAD_INITIALIZER ,
45.Nm SLIST_INIT ,
46.Nm SLIST_INSERT_AFTER ,
47.Nm SLIST_INSERT_HEAD ,
48.Nm SLIST_NEXT ,
49.Nm SLIST_REMOVE ,
50.Nm SLIST_REMOVE_AFTER ,
51.Nm SLIST_REMOVE_HEAD ,
52.Nm SLIST_SPLIT_AFTER ,
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_EMPTY_ATOMIC ,
59.Nm STAILQ_ENTRY ,
60.Nm STAILQ_FIRST ,
61.Nm STAILQ_FOREACH ,
62.Nm STAILQ_FOREACH_FROM ,
63.Nm STAILQ_FOREACH_FROM_SAFE ,
64.Nm STAILQ_FOREACH_SAFE ,
65.Nm STAILQ_HEAD ,
66.Nm STAILQ_HEAD_INITIALIZER ,
67.Nm STAILQ_INIT ,
68.Nm STAILQ_INSERT_AFTER ,
69.Nm STAILQ_INSERT_HEAD ,
70.Nm STAILQ_INSERT_TAIL ,
71.Nm STAILQ_LAST ,
72.Nm STAILQ_NEXT ,
73.Nm STAILQ_REMOVE ,
74.Nm STAILQ_REMOVE_AFTER ,
75.Nm STAILQ_REMOVE_HEAD ,
76.Nm STAILQ_REVERSE ,
77.Nm STAILQ_SPLIT_AFTER ,
78.Nm STAILQ_SWAP ,
79.Nm LIST_CLASS_ENTRY ,
80.Nm LIST_CLASS_HEAD ,
81.Nm LIST_CONCAT ,
82.Nm LIST_EMPTY ,
83.Nm LIST_EMPTY_ATOMIC ,
84.Nm LIST_ENTRY ,
85.Nm LIST_FIRST ,
86.Nm LIST_FOREACH ,
87.Nm LIST_FOREACH_FROM ,
88.Nm LIST_FOREACH_FROM_SAFE ,
89.Nm LIST_FOREACH_SAFE ,
90.Nm LIST_HEAD ,
91.Nm LIST_HEAD_INITIALIZER ,
92.Nm LIST_INIT ,
93.Nm LIST_INSERT_AFTER ,
94.Nm LIST_INSERT_BEFORE ,
95.Nm LIST_INSERT_HEAD ,
96.Nm LIST_NEXT ,
97.Nm LIST_PREV ,
98.Nm LIST_REMOVE ,
99.Nm LIST_REPLACE ,
100.Nm LIST_SPLIT_AFTER ,
101.Nm LIST_SWAP ,
102.Nm TAILQ_CLASS_ENTRY ,
103.Nm TAILQ_CLASS_HEAD ,
104.Nm TAILQ_CONCAT ,
105.Nm TAILQ_EMPTY ,
106.Nm TAILQ_EMPTY_ATOMIC ,
107.Nm TAILQ_ENTRY ,
108.Nm TAILQ_FIRST ,
109.Nm TAILQ_FOREACH ,
110.Nm TAILQ_FOREACH_FROM ,
111.Nm TAILQ_FOREACH_FROM_SAFE ,
112.Nm TAILQ_FOREACH_REVERSE ,
113.Nm TAILQ_FOREACH_REVERSE_FROM ,
114.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
115.Nm TAILQ_FOREACH_REVERSE_SAFE ,
116.Nm TAILQ_FOREACH_SAFE ,
117.Nm TAILQ_HEAD ,
118.Nm TAILQ_HEAD_INITIALIZER ,
119.Nm TAILQ_INIT ,
120.Nm TAILQ_INSERT_AFTER ,
121.Nm TAILQ_INSERT_BEFORE ,
122.Nm TAILQ_INSERT_HEAD ,
123.Nm TAILQ_INSERT_TAIL ,
124.Nm TAILQ_LAST ,
125.Nm TAILQ_NEXT ,
126.Nm TAILQ_PREV ,
127.Nm TAILQ_REMOVE ,
128.Nm TAILQ_REPLACE ,
129.Nm TAILQ_SPLIT_AFTER ,
130.Nm TAILQ_SWAP
131.Nd implementations of singly-linked lists, singly-linked tail queues,
132lists and tail queues
133.Sh SYNOPSIS
134.In sys/queue.h
135.\"
136.Fn SLIST_CLASS_ENTRY "CLASSTYPE"
137.Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
138.Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME"
139.Fn SLIST_EMPTY "SLIST_HEAD *head"
140.Fn SLIST_EMPTY_ATOMIC "SLIST_HEAD *head"
141.Fn SLIST_ENTRY "TYPE"
142.Fn SLIST_FIRST "SLIST_HEAD *head"
143.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
144.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
145.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
146.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
147.Fn SLIST_HEAD "HEADNAME" "TYPE"
148.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
149.Fn SLIST_INIT "SLIST_HEAD *head"
150.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
151.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
152.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
153.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
154.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
155.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
156.Fn SLIST_SPLIT_AFTER "SLIST_HEAD *head" "TYPE *elm" "SLIST_HEAD *rest" "SLIST_ENTRY NAME"
157.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE"
158.\"
159.Fn STAILQ_CLASS_ENTRY "CLASSTYPE"
160.Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
161.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
162.Fn STAILQ_EMPTY "STAILQ_HEAD *head"
163.Fn STAILQ_EMPTY_ATOMIC "STAILQ_HEAD *head"
164.Fn STAILQ_ENTRY "TYPE"
165.Fn STAILQ_FIRST "STAILQ_HEAD *head"
166.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
167.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
168.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
169.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
170.Fn STAILQ_HEAD "HEADNAME" "TYPE"
171.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
172.Fn STAILQ_INIT "STAILQ_HEAD *head"
173.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
174.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
175.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
176.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
177.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
178.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
179.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
180.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
181.Fn STAILQ_REVERSE "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
182.Fn STAILQ_SPLIT_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_HEAD *rest" "STAILQ_ENTRY NAME"
183.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE"
184.\"
185.Fn LIST_CLASS_ENTRY "CLASSTYPE"
186.Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
187.Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
188.Fn LIST_EMPTY "LIST_HEAD *head"
189.Fn LIST_EMPTY_ATOMIC "LIST_HEAD *head"
190.Fn LIST_ENTRY "TYPE"
191.Fn LIST_FIRST "LIST_HEAD *head"
192.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
193.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
194.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
195.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
196.Fn LIST_HEAD "HEADNAME" "TYPE"
197.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
198.Fn LIST_INIT "LIST_HEAD *head"
199.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
200.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
201.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
202.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
203.Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
204.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
205.Fn LIST_REPLACE "TYPE *elm" "TYPE *new" "LIST_ENTRY NAME"
206.Fn LIST_SPLIT_AFTER "LIST_HEAD *head" "TYPE *elm" "LIST_HEAD *rest" "LIST_ENTRY NAME"
207.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
208.\"
209.Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
210.Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
211.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
212.Fn TAILQ_EMPTY "TAILQ_HEAD *head"
213.Fn TAILQ_EMPTY_ATOMIC "TAILQ_HEAD *head"
214.Fn TAILQ_ENTRY "TYPE"
215.Fn TAILQ_FIRST "TAILQ_HEAD *head"
216.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
217.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
218.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
219.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
220.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
221.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
222.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
223.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
224.Fn TAILQ_HEAD "HEADNAME" "TYPE"
225.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
226.Fn TAILQ_INIT "TAILQ_HEAD *head"
227.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
228.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
229.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
230.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
231.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
232.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
233.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
234.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
235.Fn TAILQ_REPLACE "TAILQ_HEAD *head" "TYPE *elm" "TYPE *new" "TAILQ_ENTRY NAME"
236.Fn TAILQ_SPLIT_AFTER "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_HEAD *rest" "TAILQ_ENTRY NAME"
237.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
238.\"
239.Sh DESCRIPTION
240These macros define and operate on four types of data structures which
241can be used in both C and C++ source code:
242.Bl -enum -compact -offset indent
243.It
244Lists
245.It
246Singly-linked lists
247.It
248Singly-linked tail queues
249.It
250Tail queues
251.El
252All four structures support the following functionality:
253.Bl -enum -compact -offset indent
254.It
255Insertion of a new entry at the head of the list.
256.It
257Insertion of a new entry after any element in the list.
258.It
259O(1) removal of an entry from the head of the list.
260.It
261Forward traversal through the list.
262.It
263Splitting a list in two after any element in the list.
264.It
265Swapping the contents of two lists.
266.El
267.Pp
268Singly-linked lists are the simplest of the four data structures
269and support only the above functionality.
270Singly-linked lists are ideal for applications with large datasets
271and few or no removals,
272or for implementing a LIFO queue.
273Singly-linked lists add the following functionality:
274.Bl -enum -compact -offset indent
275.It
276O(n) removal of any entry in the list.
277.It
278O(n) concatenation of two lists.
279.El
280.Pp
281Singly-linked tail queues add the following functionality:
282.Bl -enum -compact -offset indent
283.It
284Entries can be added at the end of a list.
285.It
286O(n) removal of any entry in the list.
287.It
288They may be concatenated.
289.El
290However:
291.Bl -enum -compact -offset indent
292.It
293All list insertions must specify the head of the list.
294.It
295Each head entry requires two pointers rather than one.
296.It
297Code size is about 15% greater and operations run about 20% slower
298than singly-linked lists.
299.El
300.Pp
301Singly-linked tail queues are ideal for applications with large datasets and
302few or no removals,
303or for implementing a FIFO queue.
304.Pp
305All doubly linked types of data structures (lists and tail queues)
306additionally allow:
307.Bl -enum -compact -offset indent
308.It
309Insertion of a new entry before any element in the list.
310.It
311O(1) removal of any entry in the list.
312.El
313However:
314.Bl -enum -compact -offset indent
315.It
316Each element requires two pointers rather than one.
317.It
318Code size and execution time of operations (except for removal) is about
319twice that of the singly-linked data-structures.
320.El
321.Pp
322Linked lists are the simplest of the doubly linked data structures.
323They add the following functionality over the above:
324.Bl -enum -compact -offset indent
325.It
326O(n) concatenation of two lists.
327.It
328They may be traversed backwards.
329.El
330However:
331.Bl -enum -compact -offset indent
332.It
333To traverse backwards, an entry to begin the traversal and the list in
334which it is contained must be specified.
335.El
336.Pp
337Tail queues add the following functionality:
338.Bl -enum -compact -offset indent
339.It
340Entries can be added at the end of a list.
341.It
342They may be traversed backwards, from tail to head.
343.It
344They may be concatenated.
345.El
346However:
347.Bl -enum -compact -offset indent
348.It
349All list insertions and removals must specify the head of the list.
350.It
351Each head entry requires two pointers rather than one.
352.It
353Code size is about 15% greater and operations run about 20% slower
354than singly-linked lists.
355.El
356.Pp
357In the macro definitions,
358.Fa TYPE
359is the name of a user defined structure.
360The structure must contain a field called
361.Fa NAME
362which is of type
363.Li SLIST_ENTRY ,
364.Li STAILQ_ENTRY ,
365.Li LIST_ENTRY ,
366or
367.Li TAILQ_ENTRY .
368In the macro definitions,
369.Fa CLASSTYPE
370is the name of a user defined class.
371The class must contain a field called
372.Fa NAME
373which is of type
374.Li SLIST_CLASS_ENTRY ,
375.Li STAILQ_CLASS_ENTRY ,
376.Li LIST_CLASS_ENTRY ,
377or
378.Li TAILQ_CLASS_ENTRY .
379The argument
380.Fa HEADNAME
381is the name of a user defined structure that must be declared
382using the macros
383.Li SLIST_HEAD ,
384.Li SLIST_CLASS_HEAD ,
385.Li STAILQ_HEAD ,
386.Li STAILQ_CLASS_HEAD ,
387.Li LIST_HEAD ,
388.Li LIST_CLASS_HEAD ,
389.Li TAILQ_HEAD ,
390or
391.Li TAILQ_CLASS_HEAD .
392See the examples below for further explanation of how these
393macros are used.
394.Sh SINGLY-LINKED LISTS
395A singly-linked list is headed by a structure defined by the
396.Nm SLIST_HEAD
397macro.
398This structure contains a single pointer to the first element
399on the list.
400The elements are singly linked for minimum space and pointer manipulation
401overhead at the expense of O(n) removal for arbitrary elements.
402New elements can be added to the list after an existing element or
403at the head of the list.
404An
405.Fa SLIST_HEAD
406structure is declared as follows:
407.Bd -literal -offset indent
408SLIST_HEAD(HEADNAME, TYPE) head;
409.Ed
410.Pp
411where
412.Fa HEADNAME
413is the name of the structure to be defined, and
414.Fa TYPE
415is the type of the elements to be linked into the list.
416A pointer to the head of the list can later be declared as:
417.Bd -literal -offset indent
418struct HEADNAME *headp;
419.Ed
420.Pp
421(The names
422.Li head
423and
424.Li headp
425are user selectable.)
426.Pp
427The macro
428.Nm SLIST_HEAD_INITIALIZER
429evaluates to an initializer for the list
430.Fa head .
431.Pp
432The macro
433.Nm SLIST_CONCAT
434concatenates the list headed by
435.Fa head2
436onto the end of the one headed by
437.Fa head1
438removing all entries from the former.
439Use of this macro should be avoided as it traverses the entirety of the
440.Fa head1
441list.
442A singly-linked tail queue should be used if this macro is needed in
443high-usage code paths or to operate on long lists.
444.Pp
445The macro
446.Nm SLIST_EMPTY
447evaluates to true if there are no elements in the list.
448The
449.Nm SLIST_EMPTY_ATOMIC
450variant has the same behavior, but can be safely used in contexts where it is
451possible that a different thread is concurrently updating the list.
452.Pp
453The macro
454.Nm SLIST_ENTRY
455declares a structure that connects the elements in
456the list.
457.Pp
458The macro
459.Nm SLIST_FIRST
460returns the first element in the list or NULL if the list is empty.
461.Pp
462The macro
463.Nm SLIST_FOREACH
464traverses the list referenced by
465.Fa head
466in the forward direction, assigning each element in
467turn to
468.Fa var .
469.Pp
470The macro
471.Nm SLIST_FOREACH_FROM
472behaves identically to
473.Nm SLIST_FOREACH
474when
475.Fa var
476is NULL, else it treats
477.Fa var
478as a previously found SLIST element and begins the loop at
479.Fa var
480instead of the first element in the SLIST referenced by
481.Fa head .
482.Pp
483The macro
484.Nm SLIST_FOREACH_SAFE
485traverses the list referenced by
486.Fa head
487in the forward direction, assigning each element in
488turn to
489.Fa var .
490However, unlike
491.Fn SLIST_FOREACH
492here it is permitted to both remove
493.Fa var
494as well as free it from within the loop safely without interfering with the
495traversal.
496.Pp
497The macro
498.Nm SLIST_FOREACH_FROM_SAFE
499behaves identically to
500.Nm SLIST_FOREACH_SAFE
501when
502.Fa var
503is NULL, else it treats
504.Fa var
505as a previously found SLIST element and begins the loop at
506.Fa var
507instead of the first element in the SLIST referenced by
508.Fa head .
509.Pp
510The macro
511.Nm SLIST_INIT
512initializes the list referenced by
513.Fa head .
514.Pp
515The macro
516.Nm SLIST_INSERT_HEAD
517inserts the new element
518.Fa elm
519at the head of the list.
520.Pp
521The macro
522.Nm SLIST_INSERT_AFTER
523inserts the new element
524.Fa elm
525after the element
526.Fa listelm .
527.Pp
528The macro
529.Nm SLIST_NEXT
530returns the next element in the list.
531.Pp
532The macro
533.Nm SLIST_REMOVE_AFTER
534removes the element after
535.Fa elm
536from the list.
537Unlike
538.Fa SLIST_REMOVE ,
539this macro does not traverse the entire list.
540.Pp
541The macro
542.Nm SLIST_REMOVE_HEAD
543removes the element
544.Fa elm
545from the head of the list.
546For optimum efficiency,
547elements being removed from the head of the list should explicitly use
548this macro instead of the generic
549.Fa SLIST_REMOVE
550macro.
551.Pp
552The macro
553.Nm SLIST_REMOVE
554removes the element
555.Fa elm
556from the list.
557Use of this macro should be avoided as it traverses the entire list.
558A doubly-linked list should be used if this macro is needed in
559high-usage code paths or to operate on long lists.
560.Pp
561The macro
562.Nm SLIST_SPLIT_AFTER
563splits the list referenced by
564.Fa head ,
565making
566.Fa rest
567reference the list formed by elements after
568.Fa elm
569in
570.Fa head .
571.Pp
572The macro
573.Nm SLIST_SWAP
574swaps the contents of
575.Fa head1
576and
577.Fa head2 .
578.Sh SINGLY-LINKED LIST EXAMPLE
579.Bd -literal
580SLIST_HEAD(slisthead, entry) head =
581    SLIST_HEAD_INITIALIZER(head);
582struct slisthead *headp;		/* Singly-linked List head. */
583struct entry {
584	...
585	SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
586	...
587} *n1, *n2, *n3, *np;
588
589SLIST_INIT(&head);			/* Initialize the list. */
590
591n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
592SLIST_INSERT_HEAD(&head, n1, entries);
593
594n2 = malloc(sizeof(struct entry));	/* Insert after. */
595SLIST_INSERT_AFTER(n1, n2, entries);
596
597SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
598free(n2);
599
600n3 = SLIST_FIRST(&head);
601SLIST_REMOVE_HEAD(&head, entries);	/* Deletion from the head. */
602free(n3);
603					/* Forward traversal. */
604SLIST_FOREACH(np, &head, entries)
605	np-> ...
606					/* Safe forward traversal. */
607SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
608	np->do_stuff();
609	...
610	SLIST_REMOVE(&head, np, entry, entries);
611	free(np);
612}
613
614while (!SLIST_EMPTY(&head)) {		/* List Deletion. */
615	n1 = SLIST_FIRST(&head);
616	SLIST_REMOVE_HEAD(&head, entries);
617	free(n1);
618}
619.Ed
620.Sh SINGLY-LINKED TAIL QUEUES
621A singly-linked tail queue is headed by a structure defined by the
622.Nm STAILQ_HEAD
623macro.
624This structure contains a pair of pointers,
625one to the first element in the tail queue and the other to
626the last element in the tail queue.
627The elements are singly linked for minimum space and pointer
628manipulation overhead at the expense of O(n) removal for arbitrary
629elements.
630New elements can be added to the tail queue after an existing element,
631at the head of the tail queue, or at the end of the tail queue.
632A
633.Fa STAILQ_HEAD
634structure is declared as follows:
635.Bd -literal -offset indent
636STAILQ_HEAD(HEADNAME, TYPE) head;
637.Ed
638.Pp
639where
640.Li HEADNAME
641is the name of the structure to be defined, and
642.Li TYPE
643is the type of the elements to be linked into the tail queue.
644A pointer to the head of the tail queue can later be declared as:
645.Bd -literal -offset indent
646struct HEADNAME *headp;
647.Ed
648.Pp
649(The names
650.Li head
651and
652.Li headp
653are user selectable.)
654.Pp
655The macro
656.Nm STAILQ_HEAD_INITIALIZER
657evaluates to an initializer for the tail queue
658.Fa head .
659.Pp
660The macro
661.Nm STAILQ_CONCAT
662concatenates the tail queue headed by
663.Fa head2
664onto the end of the one headed by
665.Fa head1
666removing all entries from the former.
667.Pp
668The macro
669.Nm STAILQ_EMPTY
670evaluates to true if there are no items on the tail queue.
671The
672.Nm STAILQ_EMPTY_ATOMIC
673variant has the same behavior, but can be safely used in contexts where it is
674possible that a different thread is concurrently updating the queue.
675.Pp
676The macro
677.Nm STAILQ_ENTRY
678declares a structure that connects the elements in
679the tail queue.
680.Pp
681The macro
682.Nm STAILQ_FIRST
683returns the first item on the tail queue or NULL if the tail queue
684is empty.
685.Pp
686The macro
687.Nm STAILQ_FOREACH
688traverses the tail queue referenced by
689.Fa head
690in the forward direction, assigning each element
691in turn to
692.Fa var .
693.Pp
694The macro
695.Nm STAILQ_FOREACH_FROM
696behaves identically to
697.Nm STAILQ_FOREACH
698when
699.Fa var
700is NULL, else it treats
701.Fa var
702as a previously found STAILQ element and begins the loop at
703.Fa var
704instead of the first element in the STAILQ referenced by
705.Fa head .
706.Pp
707The macro
708.Nm STAILQ_FOREACH_SAFE
709traverses the tail queue referenced by
710.Fa head
711in the forward direction, assigning each element
712in turn to
713.Fa var .
714However, unlike
715.Fn STAILQ_FOREACH
716here it is permitted to both remove
717.Fa var
718as well as free it from within the loop safely without interfering with the
719traversal.
720.Pp
721The macro
722.Nm STAILQ_FOREACH_FROM_SAFE
723behaves identically to
724.Nm STAILQ_FOREACH_SAFE
725when
726.Fa var
727is NULL, else it treats
728.Fa var
729as a previously found STAILQ element and begins the loop at
730.Fa var
731instead of the first element in the STAILQ referenced by
732.Fa head .
733.Pp
734The macro
735.Nm STAILQ_INIT
736initializes the tail queue referenced by
737.Fa head .
738.Pp
739The macro
740.Nm STAILQ_INSERT_HEAD
741inserts the new element
742.Fa elm
743at the head of the tail queue.
744.Pp
745The macro
746.Nm STAILQ_INSERT_TAIL
747inserts the new element
748.Fa elm
749at the end of the tail queue.
750.Pp
751The macro
752.Nm STAILQ_INSERT_AFTER
753inserts the new element
754.Fa elm
755after the element
756.Fa listelm .
757.Pp
758The macro
759.Nm STAILQ_LAST
760returns the last item on the tail queue.
761If the tail queue is empty the return value is
762.Dv NULL .
763.Pp
764The macro
765.Nm STAILQ_NEXT
766returns the next item on the tail queue, or NULL this item is the last.
767.Pp
768The macro
769.Nm STAILQ_REMOVE_AFTER
770removes the element after
771.Fa elm
772from the tail queue.
773Unlike
774.Fa STAILQ_REMOVE ,
775this macro does not traverse the entire tail queue.
776.Pp
777The macro
778.Nm STAILQ_REMOVE_HEAD
779removes the element at the head of the tail queue.
780For optimum efficiency,
781elements being removed from the head of the tail queue should
782use this macro explicitly rather than the generic
783.Fa STAILQ_REMOVE
784macro.
785.Pp
786The macro
787.Nm STAILQ_REMOVE
788removes the element
789.Fa elm
790from the tail queue.
791Use of this macro should be avoided as it traverses the entire list.
792A doubly-linked tail queue should be used if this macro is needed in
793high-usage code paths or to operate on long tail queues.
794.Pp
795The macro
796.Nm STAILQ_REVERSE
797reverses the queue in place.
798.Pp
799The macro
800.Nm STAILQ_SPLIT_AFTER
801splits the tail queue referenced by
802.Fa head ,
803making
804.Fa rest
805reference the tail queue formed by elements after
806.Fa elm
807in
808.Fa head .
809.Pp
810The macro
811.Nm STAILQ_SWAP
812swaps the contents of
813.Fa head1
814and
815.Fa head2 .
816.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
817.Bd -literal
818STAILQ_HEAD(stailhead, entry) head =
819    STAILQ_HEAD_INITIALIZER(head);
820struct stailhead *headp;		/* Singly-linked tail queue head. */
821struct entry {
822	...
823	STAILQ_ENTRY(entry) entries;	/* Tail queue. */
824	...
825} *n1, *n2, *n3, *np;
826
827STAILQ_INIT(&head);			/* Initialize the queue. */
828
829n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
830STAILQ_INSERT_HEAD(&head, n1, entries);
831
832n1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
833STAILQ_INSERT_TAIL(&head, n1, entries);
834
835n2 = malloc(sizeof(struct entry));	/* Insert after. */
836STAILQ_INSERT_AFTER(&head, n1, n2, entries);
837					/* Deletion. */
838STAILQ_REMOVE(&head, n2, entry, entries);
839free(n2);
840					/* Deletion from the head. */
841n3 = STAILQ_FIRST(&head);
842STAILQ_REMOVE_HEAD(&head, entries);
843free(n3);
844					/* Forward traversal. */
845STAILQ_FOREACH(np, &head, entries)
846	np-> ...
847					/* Safe forward traversal. */
848STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
849	np->do_stuff();
850	...
851	STAILQ_REMOVE(&head, np, entry, entries);
852	free(np);
853}
854					/* TailQ Deletion. */
855while (!STAILQ_EMPTY(&head)) {
856	n1 = STAILQ_FIRST(&head);
857	STAILQ_REMOVE_HEAD(&head, entries);
858	free(n1);
859}
860					/* Faster TailQ Deletion. */
861n1 = STAILQ_FIRST(&head);
862while (n1 != NULL) {
863	n2 = STAILQ_NEXT(n1, entries);
864	free(n1);
865	n1 = n2;
866}
867STAILQ_INIT(&head);
868.Ed
869.Sh LISTS
870A list is headed by a structure defined by the
871.Nm LIST_HEAD
872macro.
873This structure contains a single pointer to the first element
874on the list.
875The elements are doubly linked so that an arbitrary element can be
876removed without traversing the list.
877New elements can be added to the list after an existing element,
878before an existing element, or at the head of the list.
879A
880.Fa LIST_HEAD
881structure is declared as follows:
882.Bd -literal -offset indent
883LIST_HEAD(HEADNAME, TYPE) head;
884.Ed
885.Pp
886where
887.Fa HEADNAME
888is the name of the structure to be defined, and
889.Fa TYPE
890is the type of the elements to be linked into the list.
891A pointer to the head of the list can later be declared as:
892.Bd -literal -offset indent
893struct HEADNAME *headp;
894.Ed
895.Pp
896(The names
897.Li head
898and
899.Li headp
900are user selectable.)
901.Pp
902The macro
903.Nm LIST_HEAD_INITIALIZER
904evaluates to an initializer for the list
905.Fa head .
906.Pp
907The macro
908.Nm LIST_CONCAT
909concatenates the list headed by
910.Fa head2
911onto the end of the one headed by
912.Fa head1
913removing all entries from the former.
914Use of this macro should be avoided as it traverses the entirety of the
915.Fa head1
916list.
917A tail queue should be used if this macro is needed in
918high-usage code paths or to operate on long lists.
919.Pp
920The macro
921.Nm LIST_EMPTY
922evaluates to true if there are no elements in the list.
923The
924.Nm LIST_EMPTY_ATOMIC
925variant has the same behavior, but can be safely used in contexts where it is
926possible that a different thread is concurrently updating the list.
927.Pp
928The macro
929.Nm LIST_ENTRY
930declares a structure that connects the elements in
931the list.
932.Pp
933The macro
934.Nm LIST_FIRST
935returns the first element in the list or NULL if the list
936is empty.
937.Pp
938The macro
939.Nm LIST_FOREACH
940traverses the list referenced by
941.Fa head
942in the forward direction, assigning each element in turn to
943.Fa var .
944.Pp
945The macro
946.Nm LIST_FOREACH_FROM
947behaves identically to
948.Nm LIST_FOREACH
949when
950.Fa var
951is NULL, else it treats
952.Fa var
953as a previously found LIST element and begins the loop at
954.Fa var
955instead of the first element in the LIST referenced by
956.Fa head .
957.Pp
958The macro
959.Nm LIST_FOREACH_SAFE
960traverses the list referenced by
961.Fa head
962in the forward direction, assigning each element in turn to
963.Fa var .
964However, unlike
965.Fn LIST_FOREACH
966here it is permitted to both remove
967.Fa var
968as well as free it from within the loop safely without interfering with the
969traversal.
970.Pp
971The macro
972.Nm LIST_FOREACH_FROM_SAFE
973behaves identically to
974.Nm LIST_FOREACH_SAFE
975when
976.Fa var
977is NULL, else it treats
978.Fa var
979as a previously found LIST element and begins the loop at
980.Fa var
981instead of the first element in the LIST referenced by
982.Fa head .
983.Pp
984The macro
985.Nm LIST_INIT
986initializes the list referenced by
987.Fa head .
988.Pp
989The macro
990.Nm LIST_INSERT_HEAD
991inserts the new element
992.Fa elm
993at the head of the list.
994.Pp
995The macro
996.Nm LIST_INSERT_AFTER
997inserts the new element
998.Fa elm
999after the element
1000.Fa listelm .
1001.Pp
1002The macro
1003.Nm LIST_INSERT_BEFORE
1004inserts the new element
1005.Fa elm
1006before the element
1007.Fa listelm .
1008.Pp
1009The macro
1010.Nm LIST_NEXT
1011returns the next element in the list, or NULL if this is the last.
1012.Pp
1013The macro
1014.Nm LIST_PREV
1015returns the previous element in the list, or NULL if this is the first.
1016List
1017.Fa head
1018must contain element
1019.Fa elm .
1020.Pp
1021The macro
1022.Nm LIST_REMOVE
1023removes the element
1024.Fa elm
1025from the list.
1026.Pp
1027The macro
1028.Fn LIST_REPLACE
1029replaces the element
1030.Fa elm
1031with
1032.Fa new
1033in the list.
1034The element
1035.Fa new
1036must not already be on a list.
1037.Pp
1038The macro
1039.Nm LIST_SPLIT_AFTER
1040splits the list referenced by
1041.Fa head ,
1042making
1043.Fa rest
1044reference the list formed by elements after
1045.Fa elm
1046in
1047.Fa head .
1048.Pp
1049The macro
1050.Nm LIST_SWAP
1051swaps the contents of
1052.Fa head1
1053and
1054.Fa head2 .
1055.Sh LIST EXAMPLE
1056.Bd -literal
1057LIST_HEAD(listhead, entry) head =
1058    LIST_HEAD_INITIALIZER(head);
1059struct listhead *headp;			/* List head. */
1060struct entry {
1061	...
1062	LIST_ENTRY(entry) entries;	/* List. */
1063	...
1064} *n1, *n2, *n3, *np, *np_temp;
1065
1066LIST_INIT(&head);			/* Initialize the list. */
1067
1068n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
1069LIST_INSERT_HEAD(&head, n1, entries);
1070
1071n2 = malloc(sizeof(struct entry));	/* Insert after. */
1072LIST_INSERT_AFTER(n1, n2, entries);
1073
1074n3 = malloc(sizeof(struct entry));	/* Insert before. */
1075LIST_INSERT_BEFORE(n2, n3, entries);
1076
1077LIST_REMOVE(n2, entries);		/* Deletion. */
1078free(n2);
1079					/* Forward traversal. */
1080LIST_FOREACH(np, &head, entries)
1081	np-> ...
1082
1083					/* Safe forward traversal. */
1084LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
1085	np->do_stuff();
1086	...
1087	LIST_REMOVE(np, entries);
1088	free(np);
1089}
1090
1091while (!LIST_EMPTY(&head)) {		/* List Deletion. */
1092	n1 = LIST_FIRST(&head);
1093	LIST_REMOVE(n1, entries);
1094	free(n1);
1095}
1096
1097n1 = LIST_FIRST(&head);			/* Faster List Deletion. */
1098while (n1 != NULL) {
1099	n2 = LIST_NEXT(n1, entries);
1100	free(n1);
1101	n1 = n2;
1102}
1103LIST_INIT(&head);
1104.Ed
1105.Sh TAIL QUEUES
1106A tail queue is headed by a structure defined by the
1107.Nm TAILQ_HEAD
1108macro.
1109This structure contains a pair of pointers,
1110one to the first element in the tail queue and the other to
1111the last element in the tail queue.
1112The elements are doubly linked so that an arbitrary element can be
1113removed without traversing the tail queue.
1114New elements can be added to the tail queue after an existing element,
1115before an existing element, at the head of the tail queue,
1116or at the end of the tail queue.
1117A
1118.Fa TAILQ_HEAD
1119structure is declared as follows:
1120.Bd -literal -offset indent
1121TAILQ_HEAD(HEADNAME, TYPE) head;
1122.Ed
1123.Pp
1124where
1125.Li HEADNAME
1126is the name of the structure to be defined, and
1127.Li TYPE
1128is the type of the elements to be linked into the tail queue.
1129A pointer to the head of the tail queue can later be declared as:
1130.Bd -literal -offset indent
1131struct HEADNAME *headp;
1132.Ed
1133.Pp
1134(The names
1135.Li head
1136and
1137.Li headp
1138are user selectable.)
1139.Pp
1140The macro
1141.Nm TAILQ_HEAD_INITIALIZER
1142evaluates to an initializer for the tail queue
1143.Fa head .
1144.Pp
1145The macro
1146.Nm TAILQ_CONCAT
1147concatenates the tail queue headed by
1148.Fa head2
1149onto the end of the one headed by
1150.Fa head1
1151removing all entries from the former.
1152.Pp
1153The macro
1154.Nm TAILQ_EMPTY
1155evaluates to true if there are no items on the tail queue.
1156The
1157.Nm TAILQ_EMPTY_ATOMIC
1158variant has the same behavior, but can be safely used in contexts where it is
1159possible that a different thread is concurrently updating the queue.
1160.Pp
1161The macro
1162.Nm TAILQ_ENTRY
1163declares a structure that connects the elements in
1164the tail queue.
1165.Pp
1166The macro
1167.Nm TAILQ_FIRST
1168returns the first item on the tail queue or NULL if the tail queue
1169is empty.
1170.Pp
1171The macro
1172.Nm TAILQ_FOREACH
1173traverses the tail queue referenced by
1174.Fa head
1175in the forward direction, assigning each element in turn to
1176.Fa var .
1177.Fa var
1178is set to
1179.Dv NULL
1180if the loop completes normally, or if there were no elements.
1181.Pp
1182The macro
1183.Nm TAILQ_FOREACH_FROM
1184behaves identically to
1185.Nm TAILQ_FOREACH
1186when
1187.Fa var
1188is NULL, else it treats
1189.Fa var
1190as a previously found TAILQ element and begins the loop at
1191.Fa var
1192instead of the first element in the TAILQ referenced by
1193.Fa head .
1194.Pp
1195The macro
1196.Nm TAILQ_FOREACH_REVERSE
1197traverses the tail queue referenced by
1198.Fa head
1199in the reverse direction, assigning each element in turn to
1200.Fa var .
1201.Pp
1202The macro
1203.Nm TAILQ_FOREACH_REVERSE_FROM
1204behaves identically to
1205.Nm TAILQ_FOREACH_REVERSE
1206when
1207.Fa var
1208is NULL, else it treats
1209.Fa var
1210as a previously found TAILQ element and begins the reverse loop at
1211.Fa var
1212instead of the last element in the TAILQ referenced by
1213.Fa head .
1214.Pp
1215The macros
1216.Nm TAILQ_FOREACH_SAFE
1217and
1218.Nm TAILQ_FOREACH_REVERSE_SAFE
1219traverse the list referenced by
1220.Fa head
1221in the forward or reverse direction respectively,
1222assigning each element in turn to
1223.Fa var .
1224However, unlike their unsafe counterparts,
1225.Nm TAILQ_FOREACH
1226and
1227.Nm TAILQ_FOREACH_REVERSE
1228permit to both remove
1229.Fa var
1230as well as free it from within the loop safely without interfering with the
1231traversal.
1232.Pp
1233The macro
1234.Nm TAILQ_FOREACH_FROM_SAFE
1235behaves identically to
1236.Nm TAILQ_FOREACH_SAFE
1237when
1238.Fa var
1239is NULL, else it treats
1240.Fa var
1241as a previously found TAILQ element and begins the loop at
1242.Fa var
1243instead of the first element in the TAILQ referenced by
1244.Fa head .
1245.Pp
1246The macro
1247.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1248behaves identically to
1249.Nm TAILQ_FOREACH_REVERSE_SAFE
1250when
1251.Fa var
1252is NULL, else it treats
1253.Fa var
1254as a previously found TAILQ element and begins the reverse loop at
1255.Fa var
1256instead of the last element in the TAILQ referenced by
1257.Fa head .
1258.Pp
1259The macro
1260.Nm TAILQ_INIT
1261initializes the tail queue referenced by
1262.Fa head .
1263.Pp
1264The macro
1265.Nm TAILQ_INSERT_HEAD
1266inserts the new element
1267.Fa elm
1268at the head of the tail queue.
1269.Pp
1270The macro
1271.Nm TAILQ_INSERT_TAIL
1272inserts the new element
1273.Fa elm
1274at the end of the tail queue.
1275.Pp
1276The macro
1277.Nm TAILQ_INSERT_AFTER
1278inserts the new element
1279.Fa elm
1280after the element
1281.Fa listelm .
1282.Pp
1283The macro
1284.Nm TAILQ_INSERT_BEFORE
1285inserts the new element
1286.Fa elm
1287before the element
1288.Fa listelm .
1289.Pp
1290The macro
1291.Nm TAILQ_LAST
1292returns the last item on the tail queue.
1293If the tail queue is empty the return value is
1294.Dv NULL .
1295.Pp
1296The macro
1297.Nm TAILQ_NEXT
1298returns the next item on the tail queue, or NULL if this item is the last.
1299.Pp
1300The macro
1301.Nm TAILQ_PREV
1302returns the previous item on the tail queue, or NULL if this item
1303is the first.
1304.Pp
1305The macro
1306.Nm TAILQ_REMOVE
1307removes the element
1308.Fa elm
1309from the tail queue.
1310.Pp
1311The macro
1312.Fn TAILQ_REPLACE
1313replaces the element
1314.Fa elm
1315with
1316.Fa new
1317in the tail queue.
1318The element
1319.Fa new
1320must not already be on a list.
1321.Pp
1322The macro
1323.Nm TAILQ_SPLIT_AFTER
1324splits the tail queue referenced by
1325.Fa head ,
1326making
1327.Fa rest
1328reference the tail queue formed by elements after
1329.Fa elm
1330in
1331.Fa head .
1332.Pp
1333The macro
1334.Nm TAILQ_SWAP
1335swaps the contents of
1336.Fa head1
1337and
1338.Fa head2 .
1339.Sh TAIL QUEUE EXAMPLE
1340.Bd -literal
1341TAILQ_HEAD(tailhead, entry) head =
1342    TAILQ_HEAD_INITIALIZER(head);
1343struct tailhead *headp;			/* Tail queue head. */
1344struct entry {
1345	...
1346	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
1347	...
1348} *n1, *n2, *n3, *n4, *np;
1349
1350TAILQ_INIT(&head);			/* Initialize the queue. */
1351
1352n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
1353TAILQ_INSERT_HEAD(&head, n1, entries);
1354
1355n1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
1356TAILQ_INSERT_TAIL(&head, n1, entries);
1357
1358n2 = malloc(sizeof(struct entry));	/* Insert after. */
1359TAILQ_INSERT_AFTER(&head, n1, n2, entries);
1360
1361n3 = malloc(sizeof(struct entry));	/* Insert before. */
1362TAILQ_INSERT_BEFORE(n2, n3, entries);
1363
1364TAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
1365free(n2);
1366
1367n4 = malloc(sizeof(struct entry));	/* Replacement. */
1368TAILQ_REPLACE(&head, n3, n4, entries);
1369free(n3);
1370					/* Forward traversal. */
1371TAILQ_FOREACH(np, &head, entries)
1372	np-> ...
1373					/* Safe forward traversal. */
1374TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
1375	np->do_stuff();
1376	...
1377	TAILQ_REMOVE(&head, np, entries);
1378	free(np);
1379}
1380					/* Reverse traversal. */
1381TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
1382	np-> ...
1383					/* TailQ Deletion. */
1384while (!TAILQ_EMPTY(&head)) {
1385	n1 = TAILQ_FIRST(&head);
1386	TAILQ_REMOVE(&head, n1, entries);
1387	free(n1);
1388}
1389					/* Faster TailQ Deletion. */
1390n1 = TAILQ_FIRST(&head);
1391while (n1 != NULL) {
1392	n2 = TAILQ_NEXT(n1, entries);
1393	free(n1);
1394	n1 = n2;
1395}
1396TAILQ_INIT(&head);
1397.Ed
1398.Sh DIAGNOSTICS
1399.Nm queue(3)
1400provides several diagnostic and debugging facilities.
1401.Pp
1402Check code that performs basic integrity and API conformance checks is
1403automatically inserted when using queue macros in the kernel if compiling it
1404with
1405.Va INVARIANTS .
1406One can request insertion or elision of check code by respectively defining one
1407of the macros
1408.Va QUEUE_MACRO_DEBUG_ASSERTIONS
1409or
1410.Va QUEUE_MACRO_NO_DEBUG_ASSERTIONS
1411before first inclusion of
1412.In sys/queue.h .
1413When check code encounters an anomaly, it panics the kernel or aborts the
1414program.
1415To this end, in the kernel or in
1416.Va _STANDALONE
1417builds, it by default calls
1418.Fn panic ,
1419while in userland builds it prints the diagnostic message on
1420.Dv stderr
1421and then calls
1422.Fn abort .
1423These behaviors can be overriden by defining a custom
1424.Fn QMD_PANIC
1425macro before first inclusion of
1426.In sys/queue.h .
1427The diagnostic messages automatically include the source file, line and function
1428where the failing check occured.
1429This behavior can be overriden by defining a custom
1430.Fn QMD_ASSERT
1431macro before first inclusion of
1432.In sys/queue.h .
1433.Pp
1434The
1435.Fn SLIST_REMOVE_PREVPTR
1436macro is available to aid debugging:
1437.Bl -hang -offset indent
1438.It Fn SLIST_REMOVE_PREVPTR "TYPE **prev" "TYPE *elm" "SLIST_ENTRY NAME"
1439.Pp
1440Removes element
1441.Fa elm ,
1442which must directly follow the element whose
1443.Va &SLIST_NEXT()
1444is
1445.Fa prev ,
1446from the list.
1447This macro may insert, under conditions detailed above, check code that
1448validates that
1449.Fa elm
1450indeed follows
1451.Fa prev
1452in the list
1453.Po
1454through the
1455.Fn QMD_SLIST_CHECK_PREVPTR
1456macro
1457.Pc .
1458.El
1459.Pp
1460When debugging, it can be useful to trace queue changes.
1461To enable tracing, define the macro
1462.Va QUEUE_MACRO_DEBUG_TRACE .
1463Note that, at the moment, only macros for regular tail queues have been
1464instrumented.
1465.Pp
1466It can also be useful to trash pointers that have been unlinked from a queue,
1467to detect use after removal.
1468To enable pointer trashing, define the macro
1469.Va QUEUE_MACRO_DEBUG_TRASH
1470at compile time.
1471Note that, at the moment, only a limited number of macros have been
1472instrumented.
1473The macro
1474.Fn QMD_IS_TRASHED "void *ptr"
1475returns true if
1476.Fa ptr
1477has been trashed by the
1478.Va QUEUE_MACRO_DEBUG_TRASH
1479option.
1480.Sh SEE ALSO
1481.Xr arb 3 ,
1482.Xr tree 3
1483.Sh HISTORY
1484The
1485.Nm queue
1486functions first appeared in
1487.Bx 4.4 .
1488