xref: /freebsd/share/man/man3/queue.3 (revision d57e28adb2783f7143e36852865240d6b6485383)
1afe61c15SRodney W. Grimes.\" Copyright (c) 1993
2afe61c15SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
3afe61c15SRodney W. Grimes.\"
4afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
5afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions
6afe61c15SRodney W. Grimes.\" are met:
7afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
8afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
9afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
10afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
11afe61c15SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
12afe61c15SRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
13afe61c15SRodney W. Grimes.\"    must display the following acknowledgement:
14afe61c15SRodney W. Grimes.\"	This product includes software developed by the University of
15afe61c15SRodney W. Grimes.\"	California, Berkeley and its contributors.
16afe61c15SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
17afe61c15SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
18afe61c15SRodney W. Grimes.\"    without specific prior written permission.
19afe61c15SRodney W. Grimes.\"
20afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23afe61c15SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30afe61c15SRodney W. Grimes.\" SUCH DAMAGE.
31afe61c15SRodney W. Grimes.\"
32afe61c15SRodney W. Grimes.\"	@(#)queue.3	8.2 (Berkeley) 1/24/94
337f3dea24SPeter Wemm.\" $FreeBSD$
34afe61c15SRodney W. Grimes.\"
35d3df5ce1SMike Pritchard.Dd January 24, 1994
36afe61c15SRodney W. Grimes.Dt QUEUE 3
373d45e180SRuslan Ermilov.Os
38afe61c15SRodney W. Grimes.Sh NAME
39aea88892SPoul-Henning Kamp.Nm SLIST_EMPTY ,
404a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY ,
41aea88892SPoul-Henning Kamp.Nm SLIST_FIRST ,
4279ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH ,
434a775e8fSJustin T. Gibbs.Nm SLIST_HEAD ,
4403763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER ,
454a775e8fSJustin T. Gibbs.Nm SLIST_INIT ,
464a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER ,
474a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD ,
48aea88892SPoul-Henning Kamp.Nm SLIST_NEXT ,
494a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD ,
504a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE ,
51d57e28adSThomas Moestl.Nm STAILQ_CONCAT ,
5279ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY ,
534a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY ,
5479ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST ,
5579ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH ,
564a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD ,
5703763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER ,
584a775e8fSJustin T. Gibbs.Nm STAILQ_INIT ,
594a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER ,
604a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD ,
614a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL ,
6279ea9bc4SAlexey Zelkin.Nm STAILQ_LAST ,
6379ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT ,
644a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD ,
654a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE ,
6679ea9bc4SAlexey Zelkin.Nm LIST_EMPTY ,
67afe61c15SRodney W. Grimes.Nm LIST_ENTRY ,
6879ea9bc4SAlexey Zelkin.Nm LIST_FIRST ,
6979ea9bc4SAlexey Zelkin.Nm LIST_FOREACH ,
70afe61c15SRodney W. Grimes.Nm LIST_HEAD ,
7103763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER ,
72afe61c15SRodney W. Grimes.Nm LIST_INIT ,
73afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER ,
747658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE ,
75afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD ,
7679ea9bc4SAlexey Zelkin.Nm LIST_NEXT ,
77afe61c15SRodney W. Grimes.Nm LIST_REMOVE ,
78d57e28adSThomas Moestl.Nm TAILQ_CONCAT ,
79c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY ,
80afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY ,
81c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST ,
8279ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH ,
836c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE ,
84afe61c15SRodney W. Grimes.Nm TAILQ_HEAD ,
8503763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER ,
86afe61c15SRodney W. Grimes.Nm TAILQ_INIT ,
87afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER ,
887658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE ,
89afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD ,
90afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL ,
91c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST ,
92c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT ,
9379ea9bc4SAlexey Zelkin.Nm TAILQ_PREV ,
94b4660c37SBen Smithurst.Nm TAILQ_REMOVE
954a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues,
9624b85d18SPoul-Henning Kamplists and tail queues
97afe61c15SRodney W. Grimes.Sh SYNOPSIS
9832eef9aeSRuslan Ermilov.In sys/queue.h
998f20a914SMike Pritchard.\"
100aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head"
1014a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE"
102aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head"
10379ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1044a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE"
10503763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
1064a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head"
1074a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
1084a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
109aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
1104a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1114a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
1128f20a914SMike Pritchard.\"
113d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
11479ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head"
1154a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE"
11679ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head"
11779ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1184a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE"
11903763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
1204a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head"
1214a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
1224a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
1234a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
124f36b6e4bSMaxim Sobolev.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
12579ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
12602a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1274a775e8fSJustin T. Gibbs.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
1288f20a914SMike Pritchard.\"
12979ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head"
130afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE"
13179ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head"
13279ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
133afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE"
13403763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
135afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head"
1364a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
1374a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
138afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
13979ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
140afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
1418f20a914SMike Pritchard.\"
142d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
143c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head"
144afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE"
145c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head"
14679ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
1476c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
148afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE"
14903763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
150afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head"
151afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
1523652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
153afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
154afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
15579ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
156c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
15779ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
158afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
1598f20a914SMike Pritchard.\"
160afe61c15SRodney W. Grimes.Sh DESCRIPTION
161b86388c1SDima DorfmanThese macros define and operate on four types of data structures:
162b86388c1SDima Dorfmansingly-linked lists, singly-linked tail queues, lists, and tail queues.
163b86388c1SDima DorfmanAll four structures support the following functionality:
164afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
165afe61c15SRodney W. Grimes.It
166afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list.
167afe61c15SRodney W. Grimes.It
168afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list.
169afe61c15SRodney W. Grimes.It
1704a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list.
1717658b0a2SJustin T. Gibbs.It
1724a775e8fSJustin T. GibbsO(n) removal of any entry in the list.
173afe61c15SRodney W. Grimes.It
174afe61c15SRodney W. GrimesForward traversal through the list.
175afe61c15SRodney W. Grimes.El
176afe61c15SRodney W. Grimes.Pp
1774a775e8fSJustin T. GibbsSingly-linked lists are the simplest of the five data structures
1784a775e8fSJustin T. Gibbsand support only the above functionality.
1794a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets
1804a775e8fSJustin T. Gibbsand few or no removals,
1814a775e8fSJustin T. Gibbsor for implementing a LIFO queue.
1824a775e8fSJustin T. Gibbs.Pp
1834a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality:
1844a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
1854a775e8fSJustin T. Gibbs.It
1864a775e8fSJustin T. GibbsEntries can be added at the end of a list.
187d57e28adSThomas Moestl.It
188d57e28adSThomas MoestlThey may be concatenated.
1894a775e8fSJustin T. Gibbs.El
1904a775e8fSJustin T. GibbsHowever:
1914a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
1924a775e8fSJustin T. Gibbs.It
1934a775e8fSJustin T. GibbsAll list insertions must specify the head of the list.
1944a775e8fSJustin T. Gibbs.It
1954a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one.
1964a775e8fSJustin T. Gibbs.It
1974a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower
1984a775e8fSJustin T. Gibbsthan singly-linked lists.
1994a775e8fSJustin T. Gibbs.El
2004a775e8fSJustin T. Gibbs.Pp
2014a775e8fSJustin T. GibbsSingly-linked tailqs are ideal for applications with large datasets and
2024a775e8fSJustin T. Gibbsfew or no removals,
2034a775e8fSJustin T. Gibbsor for implementing a FIFO queue.
2044a775e8fSJustin T. Gibbs.Pp
205b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues)
206b86388c1SDima Dorfmanadditionally allow:
2074a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2084a775e8fSJustin T. Gibbs.It
2094a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list.
2104a775e8fSJustin T. Gibbs.It
2114a775e8fSJustin T. GibbsO(1) removal of any entry in the list.
2124a775e8fSJustin T. Gibbs.El
2134a775e8fSJustin T. GibbsHowever:
2144a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2154a775e8fSJustin T. Gibbs.It
2164a775e8fSJustin T. GibbsEach elements requires two pointers rather than one.
2174a775e8fSJustin T. Gibbs.It
2184a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about
2194a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures.
2204a775e8fSJustin T. Gibbs.El
2214a775e8fSJustin T. Gibbs.Pp
2224a775e8fSJustin T. GibbsLinked lists are the simplest of the doubly linked data structures and support
2234a775e8fSJustin T. Gibbsonly the above functionality over singly-linked lists.
224afe61c15SRodney W. Grimes.Pp
225afe61c15SRodney W. GrimesTail queues add the following functionality:
226afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
227afe61c15SRodney W. Grimes.It
228afe61c15SRodney W. GrimesEntries can be added at the end of a list.
2296c1d0fbfSArchie Cobbs.It
2306c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head.
231d57e28adSThomas Moestl.It
232d57e28adSThomas MoestlThey may be concatenated.
233afe61c15SRodney W. Grimes.El
234afe61c15SRodney W. GrimesHowever:
235afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
236afe61c15SRodney W. Grimes.It
237afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list.
238afe61c15SRodney W. Grimes.It
239afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one.
240afe61c15SRodney W. Grimes.It
241afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower
2424a775e8fSJustin T. Gibbsthan singly-linked lists.
243afe61c15SRodney W. Grimes.El
244afe61c15SRodney W. Grimes.Pp
245afe61c15SRodney W. GrimesIn the macro definitions,
246afe61c15SRodney W. Grimes.Fa TYPE
247afe61c15SRodney W. Grimesis the name of a user defined structure,
248afe61c15SRodney W. Grimesthat must contain a field of type
2494a775e8fSJustin T. Gibbs.Li SLIST_ENTRY ,
2504a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY ,
251afe61c15SRodney W. Grimes.Li LIST_ENTRY ,
252afe61c15SRodney W. Grimesor
25324b85d18SPoul-Henning Kamp.Li TAILQ_ENTRY ,
254afe61c15SRodney W. Grimesnamed
255afe61c15SRodney W. Grimes.Fa NAME .
256afe61c15SRodney W. GrimesThe argument
257afe61c15SRodney W. Grimes.Fa HEADNAME
258afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared
259afe61c15SRodney W. Grimesusing the macros
2604a775e8fSJustin T. Gibbs.Li SLIST_HEAD ,
2614a775e8fSJustin T. Gibbs.Li STAILQ_HEAD ,
262afe61c15SRodney W. Grimes.Li LIST_HEAD ,
263afe61c15SRodney W. Grimesor
26424b85d18SPoul-Henning Kamp.Li TAILQ_HEAD .
265afe61c15SRodney W. GrimesSee the examples below for further explanation of how these
266afe61c15SRodney W. Grimesmacros are used.
2674a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS
2684a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the
2694a775e8fSJustin T. Gibbs.Nm SLIST_HEAD
2704a775e8fSJustin T. Gibbsmacro.
2714a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element
2724a775e8fSJustin T. Gibbson the list.
2734a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation
2744a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements.
2754a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or
2764a775e8fSJustin T. Gibbsat the head of the list.
2774a775e8fSJustin T. GibbsAn
2784a775e8fSJustin T. Gibbs.Fa SLIST_HEAD
2794a775e8fSJustin T. Gibbsstructure is declared as follows:
2804a775e8fSJustin T. Gibbs.Bd -literal -offset indent
2814a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head;
2824a775e8fSJustin T. Gibbs.Ed
2838f20a914SMike Pritchard.Pp
2844a775e8fSJustin T. Gibbswhere
2854a775e8fSJustin T. Gibbs.Fa HEADNAME
2864a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
2874a775e8fSJustin T. Gibbs.Fa TYPE
2884a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list.
2894a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as:
2904a775e8fSJustin T. Gibbs.Bd -literal -offset indent
2914a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
2924a775e8fSJustin T. Gibbs.Ed
2938f20a914SMike Pritchard.Pp
2944a775e8fSJustin T. Gibbs(The names
2954a775e8fSJustin T. Gibbs.Li head
2964a775e8fSJustin T. Gibbsand
2974a775e8fSJustin T. Gibbs.Li headp
2984a775e8fSJustin T. Gibbsare user selectable.)
2994a775e8fSJustin T. Gibbs.Pp
3004a775e8fSJustin T. GibbsThe macro
30103763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER
30203763fe0SJake Burkholderevaluates to an initializer for the list
30303763fe0SJake Burkholder.Fa head .
30403763fe0SJake Burkholder.Pp
30503763fe0SJake BurkholderThe macro
30679ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY
30779ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list.
30879ea9bc4SAlexey Zelkin.Pp
30979ea9bc4SAlexey ZelkinThe macro
3104a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY
3114a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
3124a775e8fSJustin T. Gibbsthe list.
3134a775e8fSJustin T. Gibbs.Pp
3144a775e8fSJustin T. GibbsThe macro
31579ea9bc4SAlexey Zelkin.Nm SLIST_FIRST
31679ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty.
31779ea9bc4SAlexey Zelkin.Pp
31879ea9bc4SAlexey ZelkinThe macro
31979ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH
32079ea9bc4SAlexey Zelkintraverses the list referenced by
32179ea9bc4SAlexey Zelkin.Fa head
32279ea9bc4SAlexey Zelkinin the forward direction, assigning each element in
32379ea9bc4SAlexey Zelkinturn to
32479ea9bc4SAlexey Zelkin.Fa var .
32579ea9bc4SAlexey Zelkin.Pp
32679ea9bc4SAlexey ZelkinThe macro
3274a775e8fSJustin T. Gibbs.Nm SLIST_INIT
3284a775e8fSJustin T. Gibbsinitializes the list referenced by
3294a775e8fSJustin T. Gibbs.Fa head .
3304a775e8fSJustin T. Gibbs.Pp
3314a775e8fSJustin T. GibbsThe macro
3324a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD
3334a775e8fSJustin T. Gibbsinserts the new element
3344a775e8fSJustin T. Gibbs.Fa elm
3354a775e8fSJustin T. Gibbsat the head of the list.
3364a775e8fSJustin T. Gibbs.Pp
3374a775e8fSJustin T. GibbsThe macro
3384a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER
3394a775e8fSJustin T. Gibbsinserts the new element
3404a775e8fSJustin T. Gibbs.Fa elm
3414a775e8fSJustin T. Gibbsafter the element
3424a775e8fSJustin T. Gibbs.Fa listelm .
3434a775e8fSJustin T. Gibbs.Pp
3444a775e8fSJustin T. GibbsThe macro
34579ea9bc4SAlexey Zelkin.Nm SLIST_NEXT
34679ea9bc4SAlexey Zelkinreturns the next element in the list.
34779ea9bc4SAlexey Zelkin.Pp
34879ea9bc4SAlexey ZelkinThe macro
3494a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD
3504a775e8fSJustin T. Gibbsremoves the element
3514a775e8fSJustin T. Gibbs.Fa elm
3524a775e8fSJustin T. Gibbsfrom the head of the list.
3534a775e8fSJustin T. GibbsFor optimum efficiency,
3544a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use
3554a775e8fSJustin T. Gibbsthis macro instead of the generic
3564a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE
3574a775e8fSJustin T. Gibbsmacro.
3584a775e8fSJustin T. Gibbs.Pp
3594a775e8fSJustin T. GibbsThe macro
3604a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE
3614a775e8fSJustin T. Gibbsremoves the element
3624a775e8fSJustin T. Gibbs.Fa elm
3634a775e8fSJustin T. Gibbsfrom the list.
3644a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE
3654a775e8fSJustin T. Gibbs.Bd -literal
36603763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head =
36703763fe0SJake Burkholder    SLIST_HEAD_INITIALIZER(head);
3684a775e8fSJustin T. Gibbsstruct slisthead *headp;		/* Singly-linked List head. */
3694a775e8fSJustin T. Gibbsstruct entry {
3704a775e8fSJustin T. Gibbs	...
3714a775e8fSJustin T. Gibbs	SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
3724a775e8fSJustin T. Gibbs	...
3734a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
3744a775e8fSJustin T. Gibbs
3754a775e8fSJustin T. GibbsSLIST_INIT(&head);			/* Initialize the list. */
3764a775e8fSJustin T. Gibbs
3774a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
3784a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries);
3794a775e8fSJustin T. Gibbs
3804a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
3814a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries);
3824a775e8fSJustin T. Gibbs
3834a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
3844a775e8fSJustin T. Gibbsfree(n2);
3854a775e8fSJustin T. Gibbs
38679ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head);
38703763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries);	/* Deletion from the head. */
3884a775e8fSJustin T. Gibbsfree(n3);
3894a775e8fSJustin T. Gibbs					/* Forward traversal. */
39079ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries)
3914a775e8fSJustin T. Gibbs	np-> ...
3924a775e8fSJustin T. Gibbs
39379ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) {		/* List Deletion. */
39479ea9bc4SAlexey Zelkin	n1 = SLIST_FIRST(&head);
3954a775e8fSJustin T. Gibbs	SLIST_REMOVE_HEAD(&head, entries);
3964a775e8fSJustin T. Gibbs	free(n1);
3974a775e8fSJustin T. Gibbs}
3984a775e8fSJustin T. Gibbs.Ed
3994a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES
4004a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the
4014a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD
4024a775e8fSJustin T. Gibbsmacro.
4034a775e8fSJustin T. GibbsThis structure contains a pair of pointers,
4044a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to
4054a775e8fSJustin T. Gibbsthe last element in the tail queue.
4064a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer
4074a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary
4084a775e8fSJustin T. Gibbselements.
4094a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element,
4104a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue.
4114a775e8fSJustin T. GibbsA
4124a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD
4134a775e8fSJustin T. Gibbsstructure is declared as follows:
4144a775e8fSJustin T. Gibbs.Bd -literal -offset indent
4154a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head;
4164a775e8fSJustin T. Gibbs.Ed
4178f20a914SMike Pritchard.Pp
4184a775e8fSJustin T. Gibbswhere
4194a775e8fSJustin T. Gibbs.Li HEADNAME
4204a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
4214a775e8fSJustin T. Gibbs.Li TYPE
4224a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue.
4234a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as:
4244a775e8fSJustin T. Gibbs.Bd -literal -offset indent
4254a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
4264a775e8fSJustin T. Gibbs.Ed
4278f20a914SMike Pritchard.Pp
4284a775e8fSJustin T. Gibbs(The names
4294a775e8fSJustin T. Gibbs.Li head
4304a775e8fSJustin T. Gibbsand
4314a775e8fSJustin T. Gibbs.Li headp
4324a775e8fSJustin T. Gibbsare user selectable.)
4334a775e8fSJustin T. Gibbs.Pp
4344a775e8fSJustin T. GibbsThe macro
43503763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER
43603763fe0SJake Burkholderevaluates to an initializer for the tail queue
43703763fe0SJake Burkholder.Fa head .
43803763fe0SJake Burkholder.Pp
43903763fe0SJake BurkholderThe macro
440d57e28adSThomas Moestl.Nm STAILQ_CONCAT
441d57e28adSThomas Moestlconcatenates the tail queue headed by
442d57e28adSThomas Moestl.Fa head2
443d57e28adSThomas Moestlonto the end of the one headed by
444d57e28adSThomas Moestl.Fa head1
445d57e28adSThomas Moestlremoving all entries from the former.
446d57e28adSThomas Moestl.Pp
447d57e28adSThomas MoestlThe macro
44879ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY
44979ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue.
45079ea9bc4SAlexey Zelkin.Pp
45179ea9bc4SAlexey ZelkinThe macro
4524a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY
4534a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
4544a775e8fSJustin T. Gibbsthe tail queue.
4554a775e8fSJustin T. Gibbs.Pp
4564a775e8fSJustin T. GibbsThe macro
45779ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST
45879ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue
45979ea9bc4SAlexey Zelkinis empty.
46079ea9bc4SAlexey Zelkin.Pp
46179ea9bc4SAlexey ZelkinThe macro
46279ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH
46379ea9bc4SAlexey Zelkintraverses the tail queue referenced by
46479ea9bc4SAlexey Zelkin.Fa head
46579ea9bc4SAlexey Zelkinin the forward direction, assigning each element
46679ea9bc4SAlexey Zelkinin turn to
46779ea9bc4SAlexey Zelkin.Fa var .
46879ea9bc4SAlexey Zelkin.Pp
46979ea9bc4SAlexey ZelkinThe macro
4704a775e8fSJustin T. Gibbs.Nm STAILQ_INIT
4714a775e8fSJustin T. Gibbsinitializes the tail queue referenced by
4724a775e8fSJustin T. Gibbs.Fa head .
4734a775e8fSJustin T. Gibbs.Pp
4744a775e8fSJustin T. GibbsThe macro
4754a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD
4764a775e8fSJustin T. Gibbsinserts the new element
4774a775e8fSJustin T. Gibbs.Fa elm
4784a775e8fSJustin T. Gibbsat the head of the tail queue.
4794a775e8fSJustin T. Gibbs.Pp
4804a775e8fSJustin T. GibbsThe macro
4814a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL
4824a775e8fSJustin T. Gibbsinserts the new element
4834a775e8fSJustin T. Gibbs.Fa elm
4844a775e8fSJustin T. Gibbsat the end of the tail queue.
4854a775e8fSJustin T. Gibbs.Pp
4864a775e8fSJustin T. GibbsThe macro
4874a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER
4884a775e8fSJustin T. Gibbsinserts the new element
4894a775e8fSJustin T. Gibbs.Fa elm
4904a775e8fSJustin T. Gibbsafter the element
4914a775e8fSJustin T. Gibbs.Fa listelm .
4924a775e8fSJustin T. Gibbs.Pp
4934a775e8fSJustin T. GibbsThe macro
49479ea9bc4SAlexey Zelkin.Nm STAILQ_LAST
49579ea9bc4SAlexey Zelkinreturns the last item on the tail queue.
49679ea9bc4SAlexey ZelkinIf the tail queue is empty the return value is undefined.
49779ea9bc4SAlexey Zelkin.Pp
49879ea9bc4SAlexey ZelkinThe macro
49979ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT
50079ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last.
50179ea9bc4SAlexey Zelkin.Pp
50279ea9bc4SAlexey ZelkinThe macro
5034a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD
504ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue.
5054a775e8fSJustin T. GibbsFor optimum efficiency,
5064a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should
5074a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic
5084a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE
5094a775e8fSJustin T. Gibbsmacro.
5104a775e8fSJustin T. Gibbs.Pp
5114a775e8fSJustin T. GibbsThe macro
5124a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE
5134a775e8fSJustin T. Gibbsremoves the element
5144a775e8fSJustin T. Gibbs.Fa elm
5154a775e8fSJustin T. Gibbsfrom the tail queue.
5164a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
5174a775e8fSJustin T. Gibbs.Bd -literal
51803763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head =
51903763fe0SJake Burkholder    STAILQ_HEAD_INITIALIZER(head);
5204a775e8fSJustin T. Gibbsstruct stailhead *headp;		/* Singly-linked tail queue head. */
5214a775e8fSJustin T. Gibbsstruct entry {
5224a775e8fSJustin T. Gibbs	...
5234a775e8fSJustin T. Gibbs	STAILQ_ENTRY(entry) entries;	/* Tail queue. */
5244a775e8fSJustin T. Gibbs	...
5254a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
5264a775e8fSJustin T. Gibbs
5274a775e8fSJustin T. GibbsSTAILQ_INIT(&head);			/* Initialize the queue. */
5284a775e8fSJustin T. Gibbs
5294a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
5304a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries);
5314a775e8fSJustin T. Gibbs
5324a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
5334a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries);
5344a775e8fSJustin T. Gibbs
5354a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
5364a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries);
5374a775e8fSJustin T. Gibbs					/* Deletion. */
5384a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries);
5394a775e8fSJustin T. Gibbsfree(n2);
54003763fe0SJake Burkholder					/* Deletion from the head. */
54179ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head);
5424a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries);
5434a775e8fSJustin T. Gibbsfree(n3);
5444a775e8fSJustin T. Gibbs					/* Forward traversal. */
54579ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries)
5464a775e8fSJustin T. Gibbs	np-> ...
5474a775e8fSJustin T. Gibbs					/* TailQ Deletion. */
54879ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) {
54903763fe0SJake Burkholder	n1 = STAILQ_FIRST(&head);
550266e33fdSJohn Baldwin	STAILQ_REMOVE_HEAD(&head, entries);
5514a775e8fSJustin T. Gibbs	free(n1);
5524a775e8fSJustin T. Gibbs}
5534a775e8fSJustin T. Gibbs					/* Faster TailQ Deletion. */
55479ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head);
5554a775e8fSJustin T. Gibbswhile (n1 != NULL) {
55679ea9bc4SAlexey Zelkin	n2 = STAILQ_NEXT(n1, entries);
5574a775e8fSJustin T. Gibbs	free(n1);
5584a775e8fSJustin T. Gibbs	n1 = n2;
5594a775e8fSJustin T. Gibbs}
5604a775e8fSJustin T. GibbsSTAILQ_INIT(&head);
5614a775e8fSJustin T. Gibbs.Ed
562afe61c15SRodney W. Grimes.Sh LISTS
563afe61c15SRodney W. GrimesA list is headed by a structure defined by the
564afe61c15SRodney W. Grimes.Nm LIST_HEAD
565afe61c15SRodney W. Grimesmacro.
566afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element
567afe61c15SRodney W. Grimeson the list.
568afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
569afe61c15SRodney W. Grimesremoved without traversing the list.
5704a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element,
5714a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list.
572afe61c15SRodney W. GrimesA
573afe61c15SRodney W. Grimes.Fa LIST_HEAD
574afe61c15SRodney W. Grimesstructure is declared as follows:
575afe61c15SRodney W. Grimes.Bd -literal -offset indent
576afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head;
577afe61c15SRodney W. Grimes.Ed
5788f20a914SMike Pritchard.Pp
579afe61c15SRodney W. Grimeswhere
580afe61c15SRodney W. Grimes.Fa HEADNAME
581afe61c15SRodney W. Grimesis the name of the structure to be defined, and
582afe61c15SRodney W. Grimes.Fa TYPE
583afe61c15SRodney W. Grimesis the type of the elements to be linked into the list.
584afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as:
585afe61c15SRodney W. Grimes.Bd -literal -offset indent
586afe61c15SRodney W. Grimesstruct HEADNAME *headp;
587afe61c15SRodney W. Grimes.Ed
5888f20a914SMike Pritchard.Pp
589afe61c15SRodney W. Grimes(The names
590afe61c15SRodney W. Grimes.Li head
591afe61c15SRodney W. Grimesand
592afe61c15SRodney W. Grimes.Li headp
593afe61c15SRodney W. Grimesare user selectable.)
594afe61c15SRodney W. Grimes.Pp
595afe61c15SRodney W. GrimesThe macro
59603763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER
59703763fe0SJake Burkholderevaluates to an initializer for the list
59803763fe0SJake Burkholder.Fa head .
59903763fe0SJake Burkholder.Pp
60003763fe0SJake BurkholderThe macro
60179ea9bc4SAlexey Zelkin.Nm LIST_EMPTY
60279ea9bc4SAlexey Zelkinevaluates to true if their are no elements in the list.
60379ea9bc4SAlexey Zelkin.Pp
60479ea9bc4SAlexey ZelkinThe macro
605afe61c15SRodney W. Grimes.Nm LIST_ENTRY
606afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
607afe61c15SRodney W. Grimesthe list.
608afe61c15SRodney W. Grimes.Pp
609afe61c15SRodney W. GrimesThe macro
61079ea9bc4SAlexey Zelkin.Nm LIST_FIRST
61179ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list
61279ea9bc4SAlexey Zelkinis empty.
61379ea9bc4SAlexey Zelkin.Pp
61479ea9bc4SAlexey ZelkinThe macro
61579ea9bc4SAlexey Zelkin.Nm LIST_FOREACH
61679ea9bc4SAlexey Zelkintraverses the list referenced by
61779ea9bc4SAlexey Zelkin.Fa head
61879ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
61979ea9bc4SAlexey Zelkin.Fa var .
62079ea9bc4SAlexey Zelkin.Pp
62179ea9bc4SAlexey ZelkinThe macro
622afe61c15SRodney W. Grimes.Nm LIST_INIT
623afe61c15SRodney W. Grimesinitializes the list referenced by
624afe61c15SRodney W. Grimes.Fa head .
625afe61c15SRodney W. Grimes.Pp
626afe61c15SRodney W. GrimesThe macro
627afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD
628afe61c15SRodney W. Grimesinserts the new element
629afe61c15SRodney W. Grimes.Fa elm
630afe61c15SRodney W. Grimesat the head of the list.
631afe61c15SRodney W. Grimes.Pp
632afe61c15SRodney W. GrimesThe macro
633afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER
634afe61c15SRodney W. Grimesinserts the new element
635afe61c15SRodney W. Grimes.Fa elm
636afe61c15SRodney W. Grimesafter the element
637afe61c15SRodney W. Grimes.Fa listelm .
638afe61c15SRodney W. Grimes.Pp
639afe61c15SRodney W. GrimesThe macro
6407658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE
6417658b0a2SJustin T. Gibbsinserts the new element
6427658b0a2SJustin T. Gibbs.Fa elm
6437658b0a2SJustin T. Gibbsbefore the element
6447658b0a2SJustin T. Gibbs.Fa listelm .
6457658b0a2SJustin T. Gibbs.Pp
6467658b0a2SJustin T. GibbsThe macro
64779ea9bc4SAlexey Zelkin.Nm LIST_NEXT
64879ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last.
64979ea9bc4SAlexey Zelkin.Pp
65079ea9bc4SAlexey ZelkinThe macro
651afe61c15SRodney W. Grimes.Nm LIST_REMOVE
652afe61c15SRodney W. Grimesremoves the element
653afe61c15SRodney W. Grimes.Fa elm
654afe61c15SRodney W. Grimesfrom the list.
655afe61c15SRodney W. Grimes.Sh LIST EXAMPLE
656afe61c15SRodney W. Grimes.Bd -literal
65703763fe0SJake BurkholderLIST_HEAD(listhead, entry) head =
65803763fe0SJake Burkholder    LIST_HEAD_INITIALIZER(head);
659afe61c15SRodney W. Grimesstruct listhead *headp;			/* List head. */
660afe61c15SRodney W. Grimesstruct entry {
661afe61c15SRodney W. Grimes	...
662afe61c15SRodney W. Grimes	LIST_ENTRY(entry) entries;	/* List. */
663afe61c15SRodney W. Grimes	...
6647658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np;
665afe61c15SRodney W. Grimes
666afe61c15SRodney W. GrimesLIST_INIT(&head);			/* Initialize the list. */
667afe61c15SRodney W. Grimes
668afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
669afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries);
670afe61c15SRodney W. Grimes
671afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
672afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries);
6737658b0a2SJustin T. Gibbs
6747658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
6757658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries);
6767658b0a2SJustin T. Gibbs
6777658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries);		/* Deletion. */
6787658b0a2SJustin T. Gibbsfree(n2);
679afe61c15SRodney W. Grimes					/* Forward traversal. */
68079ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries)
681afe61c15SRodney W. Grimes	np-> ...
682afe61c15SRodney W. Grimes
68379ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) {		/* List Deletion. */
68479ea9bc4SAlexey Zelkin	n1 = LIST_FIRST(&head);
6857658b0a2SJustin T. Gibbs	LIST_REMOVE(n1, entries);
6867658b0a2SJustin T. Gibbs	free(n1);
6877658b0a2SJustin T. Gibbs}
6887658b0a2SJustin T. Gibbs
68903763fe0SJake Burkholdern1 = LIST_FIRST(&head);			/* Faster List Deletion. */
6907658b0a2SJustin T. Gibbswhile (n1 != NULL) {
69179ea9bc4SAlexey Zelkin	n2 = LIST_NEXT(n1, entries);
6927658b0a2SJustin T. Gibbs	free(n1);
6937658b0a2SJustin T. Gibbs	n1 = n2;
6947658b0a2SJustin T. Gibbs}
6957658b0a2SJustin T. GibbsLIST_INIT(&head);
696afe61c15SRodney W. Grimes.Ed
697afe61c15SRodney W. Grimes.Sh TAIL QUEUES
698afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the
699afe61c15SRodney W. Grimes.Nm TAILQ_HEAD
700afe61c15SRodney W. Grimesmacro.
701afe61c15SRodney W. GrimesThis structure contains a pair of pointers,
702afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to
703afe61c15SRodney W. Grimesthe last element in the tail queue.
704afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
705afe61c15SRodney W. Grimesremoved without traversing the tail queue.
706afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element,
7074a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue,
7084a775e8fSJustin T. Gibbsor at the end of the tail queue.
709afe61c15SRodney W. GrimesA
710afe61c15SRodney W. Grimes.Fa TAILQ_HEAD
711afe61c15SRodney W. Grimesstructure is declared as follows:
712afe61c15SRodney W. Grimes.Bd -literal -offset indent
713afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head;
714afe61c15SRodney W. Grimes.Ed
7158f20a914SMike Pritchard.Pp
716afe61c15SRodney W. Grimeswhere
717afe61c15SRodney W. Grimes.Li HEADNAME
718afe61c15SRodney W. Grimesis the name of the structure to be defined, and
719afe61c15SRodney W. Grimes.Li TYPE
720afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue.
721afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as:
722afe61c15SRodney W. Grimes.Bd -literal -offset indent
723afe61c15SRodney W. Grimesstruct HEADNAME *headp;
724afe61c15SRodney W. Grimes.Ed
7258f20a914SMike Pritchard.Pp
726afe61c15SRodney W. Grimes(The names
727afe61c15SRodney W. Grimes.Li head
728afe61c15SRodney W. Grimesand
729afe61c15SRodney W. Grimes.Li headp
730afe61c15SRodney W. Grimesare user selectable.)
731afe61c15SRodney W. Grimes.Pp
732afe61c15SRodney W. GrimesThe macro
73303763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER
73403763fe0SJake Burkholderevaluates to an initializer for the tail queue
73503763fe0SJake Burkholder.Fa head .
73603763fe0SJake Burkholder.Pp
73703763fe0SJake BurkholderThe macro
738d57e28adSThomas Moestl.Nm TAILQ_CONCAT
739d57e28adSThomas Moestlconcatenates the tail queue headed by
740d57e28adSThomas Moestl.Fa head2
741d57e28adSThomas Moestlonto the end of the one headed by
742d57e28adSThomas Moestl.Fa head1
743d57e28adSThomas Moestlremoving all entries from the former.
744d57e28adSThomas Moestl.Pp
745d57e28adSThomas MoestlThe macro
746c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY
747c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue.
748c5c15c16SPoul-Henning Kamp.Pp
749c5c15c16SPoul-Henning KampThe macro
750afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY
751afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
752afe61c15SRodney W. Grimesthe tail queue.
753afe61c15SRodney W. Grimes.Pp
754afe61c15SRodney W. GrimesThe macro
755c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST
756c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue
757c5c15c16SPoul-Henning Kampis empty.
758c5c15c16SPoul-Henning Kamp.Pp
759c5c15c16SPoul-Henning KampThe macro
76079ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH
76179ea9bc4SAlexey Zelkintraverses the tail queue referenced by
76279ea9bc4SAlexey Zelkin.Fa head
76379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
76479ea9bc4SAlexey Zelkin.Fa var .
765fb5293cfSRuslan Ermilov.Fa var
766fb5293cfSRuslan Ermilovis set to
767fb5293cfSRuslan Ermilov.Dv NULL
768fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements.
76979ea9bc4SAlexey Zelkin.Pp
77079ea9bc4SAlexey ZelkinThe macro
7716c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE
7726c1d0fbfSArchie Cobbstraverses the tail queue referenced by
7736c1d0fbfSArchie Cobbs.Fa head
7746c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to
7756c1d0fbfSArchie Cobbs.Fa var .
7766c1d0fbfSArchie Cobbs.Pp
7776c1d0fbfSArchie CobbsThe macro
778afe61c15SRodney W. Grimes.Nm TAILQ_INIT
779afe61c15SRodney W. Grimesinitializes the tail queue referenced by
780afe61c15SRodney W. Grimes.Fa head .
781afe61c15SRodney W. Grimes.Pp
782afe61c15SRodney W. GrimesThe macro
783afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD
784afe61c15SRodney W. Grimesinserts the new element
785afe61c15SRodney W. Grimes.Fa elm
786afe61c15SRodney W. Grimesat the head of the tail queue.
787afe61c15SRodney W. Grimes.Pp
788afe61c15SRodney W. GrimesThe macro
789afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL
790afe61c15SRodney W. Grimesinserts the new element
791afe61c15SRodney W. Grimes.Fa elm
792afe61c15SRodney W. Grimesat the end of the tail queue.
793afe61c15SRodney W. Grimes.Pp
794afe61c15SRodney W. GrimesThe macro
795afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER
796afe61c15SRodney W. Grimesinserts the new element
797afe61c15SRodney W. Grimes.Fa elm
798afe61c15SRodney W. Grimesafter the element
799afe61c15SRodney W. Grimes.Fa listelm .
800afe61c15SRodney W. Grimes.Pp
801afe61c15SRodney W. GrimesThe macro
8027658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE
8037658b0a2SJustin T. Gibbsinserts the new element
8047658b0a2SJustin T. Gibbs.Fa elm
8057658b0a2SJustin T. Gibbsbefore the element
8067658b0a2SJustin T. Gibbs.Fa listelm .
8077658b0a2SJustin T. Gibbs.Pp
8087658b0a2SJustin T. GibbsThe macro
809c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST
810c5c15c16SPoul-Henning Kampreturns the last item on the tail queue.
811c5c15c16SPoul-Henning KampIf the tail queue is empty the return value is undefined.
812c5c15c16SPoul-Henning Kamp.Pp
813c5c15c16SPoul-Henning KampThe macro
814c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT
81579ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last.
81679ea9bc4SAlexey Zelkin.Pp
81779ea9bc4SAlexey ZelkinThe macro
81879ea9bc4SAlexey Zelkin.Nm TAILQ_PREV
81979ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item
82079ea9bc4SAlexey Zelkinis the first.
821c5c15c16SPoul-Henning Kamp.Pp
822c5c15c16SPoul-Henning KampThe macro
823afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE
824afe61c15SRodney W. Grimesremoves the element
825afe61c15SRodney W. Grimes.Fa elm
826afe61c15SRodney W. Grimesfrom the tail queue.
827afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE
828afe61c15SRodney W. Grimes.Bd -literal
82903763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head =
83003763fe0SJake Burkholder    TAILQ_HEAD_INITIALIZER(head);
831afe61c15SRodney W. Grimesstruct tailhead *headp;			/* Tail queue head. */
832afe61c15SRodney W. Grimesstruct entry {
833afe61c15SRodney W. Grimes	...
834afe61c15SRodney W. Grimes	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
835afe61c15SRodney W. Grimes	...
8367658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np;
837afe61c15SRodney W. Grimes
838afe61c15SRodney W. GrimesTAILQ_INIT(&head);			/* Initialize the queue. */
839afe61c15SRodney W. Grimes
840afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
841afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries);
842afe61c15SRodney W. Grimes
843afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
844afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries);
845afe61c15SRodney W. Grimes
846afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
847afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries);
8487658b0a2SJustin T. Gibbs
8497658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
8503652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries);
8517658b0a2SJustin T. Gibbs
8527658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
8537658b0a2SJustin T. Gibbsfree(n2);
854afe61c15SRodney W. Grimes					/* Forward traversal. */
85579ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries)
856afe61c15SRodney W. Grimes	np-> ...
8576c1d0fbfSArchie Cobbs					/* Reverse traversal. */
8586c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
8596c1d0fbfSArchie Cobbs	np-> ...
8607658b0a2SJustin T. Gibbs					/* TailQ Deletion. */
861d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) {
862c5c15c16SPoul-Henning Kamp	n1 = TAILQ_FIRST(&head);
86379ea9bc4SAlexey Zelkin	TAILQ_REMOVE(&head, n1, entries);
8647658b0a2SJustin T. Gibbs	free(n1);
8657658b0a2SJustin T. Gibbs}
8667658b0a2SJustin T. Gibbs					/* Faster TailQ Deletion. */
867c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head);
8687658b0a2SJustin T. Gibbswhile (n1 != NULL) {
869c5c15c16SPoul-Henning Kamp	n2 = TAILQ_NEXT(n1, entries);
8707658b0a2SJustin T. Gibbs	free(n1);
8717658b0a2SJustin T. Gibbs	n1 = n2;
8727658b0a2SJustin T. Gibbs}
8737658b0a2SJustin T. GibbsTAILQ_INIT(&head);
874afe61c15SRodney W. Grimes.Ed
875afe61c15SRodney W. Grimes.Sh HISTORY
876afe61c15SRodney W. GrimesThe
877afe61c15SRodney W. Grimes.Nm queue
87821421932SMike Pritchardfunctions first appeared in
87921421932SMike Pritchard.Bx 4.4 .
880