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