xref: /freebsd/share/man/man3/queue.3 (revision fb5293cf533b28bd724159bbf36836123613ec64)
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 ,
5179ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY ,
524a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY ,
5379ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST ,
5479ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH ,
554a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD ,
5603763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER ,
574a775e8fSJustin T. Gibbs.Nm STAILQ_INIT ,
584a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER ,
594a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD ,
604a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL ,
6179ea9bc4SAlexey Zelkin.Nm STAILQ_LAST ,
6279ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT ,
634a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD ,
644a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE ,
6579ea9bc4SAlexey Zelkin.Nm LIST_EMPTY ,
66afe61c15SRodney W. Grimes.Nm LIST_ENTRY ,
6779ea9bc4SAlexey Zelkin.Nm LIST_FIRST ,
6879ea9bc4SAlexey Zelkin.Nm LIST_FOREACH ,
69afe61c15SRodney W. Grimes.Nm LIST_HEAD ,
7003763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER ,
71afe61c15SRodney W. Grimes.Nm LIST_INIT ,
72afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER ,
737658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE ,
74afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD ,
7579ea9bc4SAlexey Zelkin.Nm LIST_NEXT ,
76afe61c15SRodney W. Grimes.Nm LIST_REMOVE ,
77c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY ,
78afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY ,
79c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST ,
8079ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH ,
816c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE ,
82afe61c15SRodney W. Grimes.Nm TAILQ_HEAD ,
8303763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER ,
84afe61c15SRodney W. Grimes.Nm TAILQ_INIT ,
85afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER ,
867658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE ,
87afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD ,
88afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL ,
89c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST ,
90c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT ,
9179ea9bc4SAlexey Zelkin.Nm TAILQ_PREV ,
92b4660c37SBen Smithurst.Nm TAILQ_REMOVE
934a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues,
9424b85d18SPoul-Henning Kamplists and tail queues
95afe61c15SRodney W. Grimes.Sh SYNOPSIS
9632eef9aeSRuslan Ermilov.In sys/queue.h
978f20a914SMike Pritchard.\"
98aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head"
994a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE"
100aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head"
10179ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1024a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE"
10303763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
1044a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head"
1054a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
1064a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
107aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
1084a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1094a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
1108f20a914SMike Pritchard.\"
11179ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head"
1124a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE"
11379ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head"
11479ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1154a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE"
11603763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
1174a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head"
1184a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
1194a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
1204a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
121f36b6e4bSMaxim Sobolev.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
12279ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
12302a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1244a775e8fSJustin T. Gibbs.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
1258f20a914SMike Pritchard.\"
12679ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head"
127afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE"
12879ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head"
12979ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
130afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE"
13103763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
132afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head"
1334a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
1344a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
135afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
13679ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
137afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
1388f20a914SMike Pritchard.\"
139c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head"
140afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE"
141c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head"
14279ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
1436c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
144afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE"
14503763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
146afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head"
147afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
1483652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
149afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
150afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
15179ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
152c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
15379ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
154afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
1558f20a914SMike Pritchard.\"
156afe61c15SRodney W. Grimes.Sh DESCRIPTION
157b86388c1SDima DorfmanThese macros define and operate on four types of data structures:
158b86388c1SDima Dorfmansingly-linked lists, singly-linked tail queues, lists, and tail queues.
159b86388c1SDima DorfmanAll four structures support the following functionality:
160afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
161afe61c15SRodney W. Grimes.It
162afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list.
163afe61c15SRodney W. Grimes.It
164afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list.
165afe61c15SRodney W. Grimes.It
1664a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list.
1677658b0a2SJustin T. Gibbs.It
1684a775e8fSJustin T. GibbsO(n) removal of any entry in the list.
169afe61c15SRodney W. Grimes.It
170afe61c15SRodney W. GrimesForward traversal through the list.
171afe61c15SRodney W. Grimes.El
172afe61c15SRodney W. Grimes.Pp
1734a775e8fSJustin T. GibbsSingly-linked lists are the simplest of the five data structures
1744a775e8fSJustin T. Gibbsand support only the above functionality.
1754a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets
1764a775e8fSJustin T. Gibbsand few or no removals,
1774a775e8fSJustin T. Gibbsor for implementing a LIFO queue.
1784a775e8fSJustin T. Gibbs.Pp
1794a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality:
1804a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
1814a775e8fSJustin T. Gibbs.It
1824a775e8fSJustin T. GibbsEntries can be added at the end of a list.
1834a775e8fSJustin T. Gibbs.El
1844a775e8fSJustin T. GibbsHowever:
1854a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
1864a775e8fSJustin T. Gibbs.It
1874a775e8fSJustin T. GibbsAll list insertions must specify the head of the list.
1884a775e8fSJustin T. Gibbs.It
1894a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one.
1904a775e8fSJustin T. Gibbs.It
1914a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower
1924a775e8fSJustin T. Gibbsthan singly-linked lists.
1934a775e8fSJustin T. Gibbs.El
1944a775e8fSJustin T. Gibbs.Pp
1954a775e8fSJustin T. GibbsSingly-linked tailqs are ideal for applications with large datasets and
1964a775e8fSJustin T. Gibbsfew or no removals,
1974a775e8fSJustin T. Gibbsor for implementing a FIFO queue.
1984a775e8fSJustin T. Gibbs.Pp
199b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues)
200b86388c1SDima Dorfmanadditionally allow:
2014a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2024a775e8fSJustin T. Gibbs.It
2034a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list.
2044a775e8fSJustin T. Gibbs.It
2054a775e8fSJustin T. GibbsO(1) removal of any entry in the list.
2064a775e8fSJustin T. Gibbs.El
2074a775e8fSJustin T. GibbsHowever:
2084a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2094a775e8fSJustin T. Gibbs.It
2104a775e8fSJustin T. GibbsEach elements requires two pointers rather than one.
2114a775e8fSJustin T. Gibbs.It
2124a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about
2134a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures.
2144a775e8fSJustin T. Gibbs.El
2154a775e8fSJustin T. Gibbs.Pp
2164a775e8fSJustin T. GibbsLinked lists are the simplest of the doubly linked data structures and support
2174a775e8fSJustin T. Gibbsonly the above functionality over singly-linked lists.
218afe61c15SRodney W. Grimes.Pp
219afe61c15SRodney W. GrimesTail queues add the following functionality:
220afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
221afe61c15SRodney W. Grimes.It
222afe61c15SRodney W. GrimesEntries can be added at the end of a list.
2236c1d0fbfSArchie Cobbs.It
2246c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head.
225afe61c15SRodney W. Grimes.El
226afe61c15SRodney W. GrimesHowever:
227afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
228afe61c15SRodney W. Grimes.It
229afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list.
230afe61c15SRodney W. Grimes.It
231afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one.
232afe61c15SRodney W. Grimes.It
233afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower
2344a775e8fSJustin T. Gibbsthan singly-linked lists.
235afe61c15SRodney W. Grimes.El
236afe61c15SRodney W. Grimes.Pp
237afe61c15SRodney W. GrimesIn the macro definitions,
238afe61c15SRodney W. Grimes.Fa TYPE
239afe61c15SRodney W. Grimesis the name of a user defined structure,
240afe61c15SRodney W. Grimesthat must contain a field of type
2414a775e8fSJustin T. Gibbs.Li SLIST_ENTRY ,
2424a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY ,
243afe61c15SRodney W. Grimes.Li LIST_ENTRY ,
244afe61c15SRodney W. Grimesor
24524b85d18SPoul-Henning Kamp.Li TAILQ_ENTRY ,
246afe61c15SRodney W. Grimesnamed
247afe61c15SRodney W. Grimes.Fa NAME .
248afe61c15SRodney W. GrimesThe argument
249afe61c15SRodney W. Grimes.Fa HEADNAME
250afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared
251afe61c15SRodney W. Grimesusing the macros
2524a775e8fSJustin T. Gibbs.Li SLIST_HEAD ,
2534a775e8fSJustin T. Gibbs.Li STAILQ_HEAD ,
254afe61c15SRodney W. Grimes.Li LIST_HEAD ,
255afe61c15SRodney W. Grimesor
25624b85d18SPoul-Henning Kamp.Li TAILQ_HEAD .
257afe61c15SRodney W. GrimesSee the examples below for further explanation of how these
258afe61c15SRodney W. Grimesmacros are used.
2594a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS
2604a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the
2614a775e8fSJustin T. Gibbs.Nm SLIST_HEAD
2624a775e8fSJustin T. Gibbsmacro.
2634a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element
2644a775e8fSJustin T. Gibbson the list.
2654a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation
2664a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements.
2674a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or
2684a775e8fSJustin T. Gibbsat the head of the list.
2694a775e8fSJustin T. GibbsAn
2704a775e8fSJustin T. Gibbs.Fa SLIST_HEAD
2714a775e8fSJustin T. Gibbsstructure is declared as follows:
2724a775e8fSJustin T. Gibbs.Bd -literal -offset indent
2734a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head;
2744a775e8fSJustin T. Gibbs.Ed
2758f20a914SMike Pritchard.Pp
2764a775e8fSJustin T. Gibbswhere
2774a775e8fSJustin T. Gibbs.Fa HEADNAME
2784a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
2794a775e8fSJustin T. Gibbs.Fa TYPE
2804a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list.
2814a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as:
2824a775e8fSJustin T. Gibbs.Bd -literal -offset indent
2834a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
2844a775e8fSJustin T. Gibbs.Ed
2858f20a914SMike Pritchard.Pp
2864a775e8fSJustin T. Gibbs(The names
2874a775e8fSJustin T. Gibbs.Li head
2884a775e8fSJustin T. Gibbsand
2894a775e8fSJustin T. Gibbs.Li headp
2904a775e8fSJustin T. Gibbsare user selectable.)
2914a775e8fSJustin T. Gibbs.Pp
2924a775e8fSJustin T. GibbsThe macro
29303763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER
29403763fe0SJake Burkholderevaluates to an initializer for the list
29503763fe0SJake Burkholder.Fa head .
29603763fe0SJake Burkholder.Pp
29703763fe0SJake BurkholderThe macro
29879ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY
29979ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list.
30079ea9bc4SAlexey Zelkin.Pp
30179ea9bc4SAlexey ZelkinThe macro
3024a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY
3034a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
3044a775e8fSJustin T. Gibbsthe list.
3054a775e8fSJustin T. Gibbs.Pp
3064a775e8fSJustin T. GibbsThe macro
30779ea9bc4SAlexey Zelkin.Nm SLIST_FIRST
30879ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty.
30979ea9bc4SAlexey Zelkin.Pp
31079ea9bc4SAlexey ZelkinThe macro
31179ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH
31279ea9bc4SAlexey Zelkintraverses the list referenced by
31379ea9bc4SAlexey Zelkin.Fa head
31479ea9bc4SAlexey Zelkinin the forward direction, assigning each element in
31579ea9bc4SAlexey Zelkinturn to
31679ea9bc4SAlexey Zelkin.Fa var .
31779ea9bc4SAlexey Zelkin.Pp
31879ea9bc4SAlexey ZelkinThe macro
3194a775e8fSJustin T. Gibbs.Nm SLIST_INIT
3204a775e8fSJustin T. Gibbsinitializes the list referenced by
3214a775e8fSJustin T. Gibbs.Fa head .
3224a775e8fSJustin T. Gibbs.Pp
3234a775e8fSJustin T. GibbsThe macro
3244a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD
3254a775e8fSJustin T. Gibbsinserts the new element
3264a775e8fSJustin T. Gibbs.Fa elm
3274a775e8fSJustin T. Gibbsat the head of the list.
3284a775e8fSJustin T. Gibbs.Pp
3294a775e8fSJustin T. GibbsThe macro
3304a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER
3314a775e8fSJustin T. Gibbsinserts the new element
3324a775e8fSJustin T. Gibbs.Fa elm
3334a775e8fSJustin T. Gibbsafter the element
3344a775e8fSJustin T. Gibbs.Fa listelm .
3354a775e8fSJustin T. Gibbs.Pp
3364a775e8fSJustin T. GibbsThe macro
33779ea9bc4SAlexey Zelkin.Nm SLIST_NEXT
33879ea9bc4SAlexey Zelkinreturns the next element in the list.
33979ea9bc4SAlexey Zelkin.Pp
34079ea9bc4SAlexey ZelkinThe macro
3414a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD
3424a775e8fSJustin T. Gibbsremoves the element
3434a775e8fSJustin T. Gibbs.Fa elm
3444a775e8fSJustin T. Gibbsfrom the head of the list.
3454a775e8fSJustin T. GibbsFor optimum efficiency,
3464a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use
3474a775e8fSJustin T. Gibbsthis macro instead of the generic
3484a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE
3494a775e8fSJustin T. Gibbsmacro.
3504a775e8fSJustin T. Gibbs.Pp
3514a775e8fSJustin T. GibbsThe macro
3524a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE
3534a775e8fSJustin T. Gibbsremoves the element
3544a775e8fSJustin T. Gibbs.Fa elm
3554a775e8fSJustin T. Gibbsfrom the list.
3564a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE
3574a775e8fSJustin T. Gibbs.Bd -literal
35803763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head =
35903763fe0SJake Burkholder    SLIST_HEAD_INITIALIZER(head);
3604a775e8fSJustin T. Gibbsstruct slisthead *headp;		/* Singly-linked List head. */
3614a775e8fSJustin T. Gibbsstruct entry {
3624a775e8fSJustin T. Gibbs	...
3634a775e8fSJustin T. Gibbs	SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
3644a775e8fSJustin T. Gibbs	...
3654a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
3664a775e8fSJustin T. Gibbs
3674a775e8fSJustin T. GibbsSLIST_INIT(&head);			/* Initialize the list. */
3684a775e8fSJustin T. Gibbs
3694a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
3704a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries);
3714a775e8fSJustin T. Gibbs
3724a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
3734a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries);
3744a775e8fSJustin T. Gibbs
3754a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
3764a775e8fSJustin T. Gibbsfree(n2);
3774a775e8fSJustin T. Gibbs
37879ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head);
37903763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries);	/* Deletion from the head. */
3804a775e8fSJustin T. Gibbsfree(n3);
3814a775e8fSJustin T. Gibbs					/* Forward traversal. */
38279ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries)
3834a775e8fSJustin T. Gibbs	np-> ...
3844a775e8fSJustin T. Gibbs
38579ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) {		/* List Deletion. */
38679ea9bc4SAlexey Zelkin	n1 = SLIST_FIRST(&head);
3874a775e8fSJustin T. Gibbs	SLIST_REMOVE_HEAD(&head, entries);
3884a775e8fSJustin T. Gibbs	free(n1);
3894a775e8fSJustin T. Gibbs}
3904a775e8fSJustin T. Gibbs.Ed
3914a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES
3924a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the
3934a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD
3944a775e8fSJustin T. Gibbsmacro.
3954a775e8fSJustin T. GibbsThis structure contains a pair of pointers,
3964a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to
3974a775e8fSJustin T. Gibbsthe last element in the tail queue.
3984a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer
3994a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary
4004a775e8fSJustin T. Gibbselements.
4014a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element,
4024a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue.
4034a775e8fSJustin T. GibbsA
4044a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD
4054a775e8fSJustin T. Gibbsstructure is declared as follows:
4064a775e8fSJustin T. Gibbs.Bd -literal -offset indent
4074a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head;
4084a775e8fSJustin T. Gibbs.Ed
4098f20a914SMike Pritchard.Pp
4104a775e8fSJustin T. Gibbswhere
4114a775e8fSJustin T. Gibbs.Li HEADNAME
4124a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
4134a775e8fSJustin T. Gibbs.Li TYPE
4144a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue.
4154a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as:
4164a775e8fSJustin T. Gibbs.Bd -literal -offset indent
4174a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
4184a775e8fSJustin T. Gibbs.Ed
4198f20a914SMike Pritchard.Pp
4204a775e8fSJustin T. Gibbs(The names
4214a775e8fSJustin T. Gibbs.Li head
4224a775e8fSJustin T. Gibbsand
4234a775e8fSJustin T. Gibbs.Li headp
4244a775e8fSJustin T. Gibbsare user selectable.)
4254a775e8fSJustin T. Gibbs.Pp
4264a775e8fSJustin T. GibbsThe macro
42703763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER
42803763fe0SJake Burkholderevaluates to an initializer for the tail queue
42903763fe0SJake Burkholder.Fa head .
43003763fe0SJake Burkholder.Pp
43103763fe0SJake BurkholderThe macro
43279ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY
43379ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue.
43479ea9bc4SAlexey Zelkin.Pp
43579ea9bc4SAlexey ZelkinThe macro
4364a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY
4374a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
4384a775e8fSJustin T. Gibbsthe tail queue.
4394a775e8fSJustin T. Gibbs.Pp
4404a775e8fSJustin T. GibbsThe macro
44179ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST
44279ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue
44379ea9bc4SAlexey Zelkinis empty.
44479ea9bc4SAlexey Zelkin.Pp
44579ea9bc4SAlexey ZelkinThe macro
44679ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH
44779ea9bc4SAlexey Zelkintraverses the tail queue referenced by
44879ea9bc4SAlexey Zelkin.Fa head
44979ea9bc4SAlexey Zelkinin the forward direction, assigning each element
45079ea9bc4SAlexey Zelkinin turn to
45179ea9bc4SAlexey Zelkin.Fa var .
45279ea9bc4SAlexey Zelkin.Pp
45379ea9bc4SAlexey ZelkinThe macro
4544a775e8fSJustin T. Gibbs.Nm STAILQ_INIT
4554a775e8fSJustin T. Gibbsinitializes the tail queue referenced by
4564a775e8fSJustin T. Gibbs.Fa head .
4574a775e8fSJustin T. Gibbs.Pp
4584a775e8fSJustin T. GibbsThe macro
4594a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD
4604a775e8fSJustin T. Gibbsinserts the new element
4614a775e8fSJustin T. Gibbs.Fa elm
4624a775e8fSJustin T. Gibbsat the head of the tail queue.
4634a775e8fSJustin T. Gibbs.Pp
4644a775e8fSJustin T. GibbsThe macro
4654a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL
4664a775e8fSJustin T. Gibbsinserts the new element
4674a775e8fSJustin T. Gibbs.Fa elm
4684a775e8fSJustin T. Gibbsat the end of the tail queue.
4694a775e8fSJustin T. Gibbs.Pp
4704a775e8fSJustin T. GibbsThe macro
4714a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER
4724a775e8fSJustin T. Gibbsinserts the new element
4734a775e8fSJustin T. Gibbs.Fa elm
4744a775e8fSJustin T. Gibbsafter the element
4754a775e8fSJustin T. Gibbs.Fa listelm .
4764a775e8fSJustin T. Gibbs.Pp
4774a775e8fSJustin T. GibbsThe macro
47879ea9bc4SAlexey Zelkin.Nm STAILQ_LAST
47979ea9bc4SAlexey Zelkinreturns the last item on the tail queue.
48079ea9bc4SAlexey ZelkinIf the tail queue is empty the return value is undefined.
48179ea9bc4SAlexey Zelkin.Pp
48279ea9bc4SAlexey ZelkinThe macro
48379ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT
48479ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last.
48579ea9bc4SAlexey Zelkin.Pp
48679ea9bc4SAlexey ZelkinThe macro
4874a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD
488ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue.
4894a775e8fSJustin T. GibbsFor optimum efficiency,
4904a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should
4914a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic
4924a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE
4934a775e8fSJustin T. Gibbsmacro.
4944a775e8fSJustin T. Gibbs.Pp
4954a775e8fSJustin T. GibbsThe macro
4964a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE
4974a775e8fSJustin T. Gibbsremoves the element
4984a775e8fSJustin T. Gibbs.Fa elm
4994a775e8fSJustin T. Gibbsfrom the tail queue.
5004a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
5014a775e8fSJustin T. Gibbs.Bd -literal
50203763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head =
50303763fe0SJake Burkholder    STAILQ_HEAD_INITIALIZER(head);
5044a775e8fSJustin T. Gibbsstruct stailhead *headp;		/* Singly-linked tail queue head. */
5054a775e8fSJustin T. Gibbsstruct entry {
5064a775e8fSJustin T. Gibbs	...
5074a775e8fSJustin T. Gibbs	STAILQ_ENTRY(entry) entries;	/* Tail queue. */
5084a775e8fSJustin T. Gibbs	...
5094a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
5104a775e8fSJustin T. Gibbs
5114a775e8fSJustin T. GibbsSTAILQ_INIT(&head);			/* Initialize the queue. */
5124a775e8fSJustin T. Gibbs
5134a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
5144a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries);
5154a775e8fSJustin T. Gibbs
5164a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
5174a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries);
5184a775e8fSJustin T. Gibbs
5194a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
5204a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries);
5214a775e8fSJustin T. Gibbs					/* Deletion. */
5224a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries);
5234a775e8fSJustin T. Gibbsfree(n2);
52403763fe0SJake Burkholder					/* Deletion from the head. */
52579ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head);
5264a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries);
5274a775e8fSJustin T. Gibbsfree(n3);
5284a775e8fSJustin T. Gibbs					/* Forward traversal. */
52979ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries)
5304a775e8fSJustin T. Gibbs	np-> ...
5314a775e8fSJustin T. Gibbs					/* TailQ Deletion. */
53279ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) {
53303763fe0SJake Burkholder	n1 = STAILQ_FIRST(&head);
534266e33fdSJohn Baldwin	STAILQ_REMOVE_HEAD(&head, entries);
5354a775e8fSJustin T. Gibbs	free(n1);
5364a775e8fSJustin T. Gibbs}
5374a775e8fSJustin T. Gibbs					/* Faster TailQ Deletion. */
53879ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head);
5394a775e8fSJustin T. Gibbswhile (n1 != NULL) {
54079ea9bc4SAlexey Zelkin	n2 = STAILQ_NEXT(n1, entries);
5414a775e8fSJustin T. Gibbs	free(n1);
5424a775e8fSJustin T. Gibbs	n1 = n2;
5434a775e8fSJustin T. Gibbs}
5444a775e8fSJustin T. GibbsSTAILQ_INIT(&head);
5454a775e8fSJustin T. Gibbs.Ed
546afe61c15SRodney W. Grimes.Sh LISTS
547afe61c15SRodney W. GrimesA list is headed by a structure defined by the
548afe61c15SRodney W. Grimes.Nm LIST_HEAD
549afe61c15SRodney W. Grimesmacro.
550afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element
551afe61c15SRodney W. Grimeson the list.
552afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
553afe61c15SRodney W. Grimesremoved without traversing the list.
5544a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element,
5554a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list.
556afe61c15SRodney W. GrimesA
557afe61c15SRodney W. Grimes.Fa LIST_HEAD
558afe61c15SRodney W. Grimesstructure is declared as follows:
559afe61c15SRodney W. Grimes.Bd -literal -offset indent
560afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head;
561afe61c15SRodney W. Grimes.Ed
5628f20a914SMike Pritchard.Pp
563afe61c15SRodney W. Grimeswhere
564afe61c15SRodney W. Grimes.Fa HEADNAME
565afe61c15SRodney W. Grimesis the name of the structure to be defined, and
566afe61c15SRodney W. Grimes.Fa TYPE
567afe61c15SRodney W. Grimesis the type of the elements to be linked into the list.
568afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as:
569afe61c15SRodney W. Grimes.Bd -literal -offset indent
570afe61c15SRodney W. Grimesstruct HEADNAME *headp;
571afe61c15SRodney W. Grimes.Ed
5728f20a914SMike Pritchard.Pp
573afe61c15SRodney W. Grimes(The names
574afe61c15SRodney W. Grimes.Li head
575afe61c15SRodney W. Grimesand
576afe61c15SRodney W. Grimes.Li headp
577afe61c15SRodney W. Grimesare user selectable.)
578afe61c15SRodney W. Grimes.Pp
579afe61c15SRodney W. GrimesThe macro
58003763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER
58103763fe0SJake Burkholderevaluates to an initializer for the list
58203763fe0SJake Burkholder.Fa head .
58303763fe0SJake Burkholder.Pp
58403763fe0SJake BurkholderThe macro
58579ea9bc4SAlexey Zelkin.Nm LIST_EMPTY
58679ea9bc4SAlexey Zelkinevaluates to true if their are no elements in the list.
58779ea9bc4SAlexey Zelkin.Pp
58879ea9bc4SAlexey ZelkinThe macro
589afe61c15SRodney W. Grimes.Nm LIST_ENTRY
590afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
591afe61c15SRodney W. Grimesthe list.
592afe61c15SRodney W. Grimes.Pp
593afe61c15SRodney W. GrimesThe macro
59479ea9bc4SAlexey Zelkin.Nm LIST_FIRST
59579ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list
59679ea9bc4SAlexey Zelkinis empty.
59779ea9bc4SAlexey Zelkin.Pp
59879ea9bc4SAlexey ZelkinThe macro
59979ea9bc4SAlexey Zelkin.Nm LIST_FOREACH
60079ea9bc4SAlexey Zelkintraverses the list referenced by
60179ea9bc4SAlexey Zelkin.Fa head
60279ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
60379ea9bc4SAlexey Zelkin.Fa var .
60479ea9bc4SAlexey Zelkin.Pp
60579ea9bc4SAlexey ZelkinThe macro
606afe61c15SRodney W. Grimes.Nm LIST_INIT
607afe61c15SRodney W. Grimesinitializes the list referenced by
608afe61c15SRodney W. Grimes.Fa head .
609afe61c15SRodney W. Grimes.Pp
610afe61c15SRodney W. GrimesThe macro
611afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD
612afe61c15SRodney W. Grimesinserts the new element
613afe61c15SRodney W. Grimes.Fa elm
614afe61c15SRodney W. Grimesat the head of the list.
615afe61c15SRodney W. Grimes.Pp
616afe61c15SRodney W. GrimesThe macro
617afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER
618afe61c15SRodney W. Grimesinserts the new element
619afe61c15SRodney W. Grimes.Fa elm
620afe61c15SRodney W. Grimesafter the element
621afe61c15SRodney W. Grimes.Fa listelm .
622afe61c15SRodney W. Grimes.Pp
623afe61c15SRodney W. GrimesThe macro
6247658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE
6257658b0a2SJustin T. Gibbsinserts the new element
6267658b0a2SJustin T. Gibbs.Fa elm
6277658b0a2SJustin T. Gibbsbefore the element
6287658b0a2SJustin T. Gibbs.Fa listelm .
6297658b0a2SJustin T. Gibbs.Pp
6307658b0a2SJustin T. GibbsThe macro
63179ea9bc4SAlexey Zelkin.Nm LIST_NEXT
63279ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last.
63379ea9bc4SAlexey Zelkin.Pp
63479ea9bc4SAlexey ZelkinThe macro
635afe61c15SRodney W. Grimes.Nm LIST_REMOVE
636afe61c15SRodney W. Grimesremoves the element
637afe61c15SRodney W. Grimes.Fa elm
638afe61c15SRodney W. Grimesfrom the list.
639afe61c15SRodney W. Grimes.Sh LIST EXAMPLE
640afe61c15SRodney W. Grimes.Bd -literal
64103763fe0SJake BurkholderLIST_HEAD(listhead, entry) head =
64203763fe0SJake Burkholder    LIST_HEAD_INITIALIZER(head);
643afe61c15SRodney W. Grimesstruct listhead *headp;			/* List head. */
644afe61c15SRodney W. Grimesstruct entry {
645afe61c15SRodney W. Grimes	...
646afe61c15SRodney W. Grimes	LIST_ENTRY(entry) entries;	/* List. */
647afe61c15SRodney W. Grimes	...
6487658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np;
649afe61c15SRodney W. Grimes
650afe61c15SRodney W. GrimesLIST_INIT(&head);			/* Initialize the list. */
651afe61c15SRodney W. Grimes
652afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
653afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries);
654afe61c15SRodney W. Grimes
655afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
656afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries);
6577658b0a2SJustin T. Gibbs
6587658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
6597658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries);
6607658b0a2SJustin T. Gibbs
6617658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries);		/* Deletion. */
6627658b0a2SJustin T. Gibbsfree(n2);
663afe61c15SRodney W. Grimes					/* Forward traversal. */
66479ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries)
665afe61c15SRodney W. Grimes	np-> ...
666afe61c15SRodney W. Grimes
66779ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) {		/* List Deletion. */
66879ea9bc4SAlexey Zelkin	n1 = LIST_FIRST(&head);
6697658b0a2SJustin T. Gibbs	LIST_REMOVE(n1, entries);
6707658b0a2SJustin T. Gibbs	free(n1);
6717658b0a2SJustin T. Gibbs}
6727658b0a2SJustin T. Gibbs
67303763fe0SJake Burkholdern1 = LIST_FIRST(&head);			/* Faster List Deletion. */
6747658b0a2SJustin T. Gibbswhile (n1 != NULL) {
67579ea9bc4SAlexey Zelkin	n2 = LIST_NEXT(n1, entries);
6767658b0a2SJustin T. Gibbs	free(n1);
6777658b0a2SJustin T. Gibbs	n1 = n2;
6787658b0a2SJustin T. Gibbs}
6797658b0a2SJustin T. GibbsLIST_INIT(&head);
680afe61c15SRodney W. Grimes.Ed
681afe61c15SRodney W. Grimes.Sh TAIL QUEUES
682afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the
683afe61c15SRodney W. Grimes.Nm TAILQ_HEAD
684afe61c15SRodney W. Grimesmacro.
685afe61c15SRodney W. GrimesThis structure contains a pair of pointers,
686afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to
687afe61c15SRodney W. Grimesthe last element in the tail queue.
688afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
689afe61c15SRodney W. Grimesremoved without traversing the tail queue.
690afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element,
6914a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue,
6924a775e8fSJustin T. Gibbsor at the end of the tail queue.
693afe61c15SRodney W. GrimesA
694afe61c15SRodney W. Grimes.Fa TAILQ_HEAD
695afe61c15SRodney W. Grimesstructure is declared as follows:
696afe61c15SRodney W. Grimes.Bd -literal -offset indent
697afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head;
698afe61c15SRodney W. Grimes.Ed
6998f20a914SMike Pritchard.Pp
700afe61c15SRodney W. Grimeswhere
701afe61c15SRodney W. Grimes.Li HEADNAME
702afe61c15SRodney W. Grimesis the name of the structure to be defined, and
703afe61c15SRodney W. Grimes.Li TYPE
704afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue.
705afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as:
706afe61c15SRodney W. Grimes.Bd -literal -offset indent
707afe61c15SRodney W. Grimesstruct HEADNAME *headp;
708afe61c15SRodney W. Grimes.Ed
7098f20a914SMike Pritchard.Pp
710afe61c15SRodney W. Grimes(The names
711afe61c15SRodney W. Grimes.Li head
712afe61c15SRodney W. Grimesand
713afe61c15SRodney W. Grimes.Li headp
714afe61c15SRodney W. Grimesare user selectable.)
715afe61c15SRodney W. Grimes.Pp
716afe61c15SRodney W. GrimesThe macro
71703763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER
71803763fe0SJake Burkholderevaluates to an initializer for the tail queue
71903763fe0SJake Burkholder.Fa head .
72003763fe0SJake Burkholder.Pp
72103763fe0SJake BurkholderThe macro
722c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY
723c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue.
724c5c15c16SPoul-Henning Kamp.Pp
725c5c15c16SPoul-Henning KampThe macro
726afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY
727afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
728afe61c15SRodney W. Grimesthe tail queue.
729afe61c15SRodney W. Grimes.Pp
730afe61c15SRodney W. GrimesThe macro
731c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST
732c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue
733c5c15c16SPoul-Henning Kampis empty.
734c5c15c16SPoul-Henning Kamp.Pp
735c5c15c16SPoul-Henning KampThe macro
73679ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH
73779ea9bc4SAlexey Zelkintraverses the tail queue referenced by
73879ea9bc4SAlexey Zelkin.Fa head
73979ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
74079ea9bc4SAlexey Zelkin.Fa var .
741fb5293cfSRuslan Ermilov.Fa var
742fb5293cfSRuslan Ermilovis set to
743fb5293cfSRuslan Ermilov.Dv NULL
744fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements.
74579ea9bc4SAlexey Zelkin.Pp
74679ea9bc4SAlexey ZelkinThe macro
7476c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE
7486c1d0fbfSArchie Cobbstraverses the tail queue referenced by
7496c1d0fbfSArchie Cobbs.Fa head
7506c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to
7516c1d0fbfSArchie Cobbs.Fa var .
7526c1d0fbfSArchie Cobbs.Pp
7536c1d0fbfSArchie CobbsThe macro
754afe61c15SRodney W. Grimes.Nm TAILQ_INIT
755afe61c15SRodney W. Grimesinitializes the tail queue referenced by
756afe61c15SRodney W. Grimes.Fa head .
757afe61c15SRodney W. Grimes.Pp
758afe61c15SRodney W. GrimesThe macro
759afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD
760afe61c15SRodney W. Grimesinserts the new element
761afe61c15SRodney W. Grimes.Fa elm
762afe61c15SRodney W. Grimesat the head of the tail queue.
763afe61c15SRodney W. Grimes.Pp
764afe61c15SRodney W. GrimesThe macro
765afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL
766afe61c15SRodney W. Grimesinserts the new element
767afe61c15SRodney W. Grimes.Fa elm
768afe61c15SRodney W. Grimesat the end of the tail queue.
769afe61c15SRodney W. Grimes.Pp
770afe61c15SRodney W. GrimesThe macro
771afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER
772afe61c15SRodney W. Grimesinserts the new element
773afe61c15SRodney W. Grimes.Fa elm
774afe61c15SRodney W. Grimesafter the element
775afe61c15SRodney W. Grimes.Fa listelm .
776afe61c15SRodney W. Grimes.Pp
777afe61c15SRodney W. GrimesThe macro
7787658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE
7797658b0a2SJustin T. Gibbsinserts the new element
7807658b0a2SJustin T. Gibbs.Fa elm
7817658b0a2SJustin T. Gibbsbefore the element
7827658b0a2SJustin T. Gibbs.Fa listelm .
7837658b0a2SJustin T. Gibbs.Pp
7847658b0a2SJustin T. GibbsThe macro
785c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST
786c5c15c16SPoul-Henning Kampreturns the last item on the tail queue.
787c5c15c16SPoul-Henning KampIf the tail queue is empty the return value is undefined.
788c5c15c16SPoul-Henning Kamp.Pp
789c5c15c16SPoul-Henning KampThe macro
790c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT
79179ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last.
79279ea9bc4SAlexey Zelkin.Pp
79379ea9bc4SAlexey ZelkinThe macro
79479ea9bc4SAlexey Zelkin.Nm TAILQ_PREV
79579ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item
79679ea9bc4SAlexey Zelkinis the first.
797c5c15c16SPoul-Henning Kamp.Pp
798c5c15c16SPoul-Henning KampThe macro
799afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE
800afe61c15SRodney W. Grimesremoves the element
801afe61c15SRodney W. Grimes.Fa elm
802afe61c15SRodney W. Grimesfrom the tail queue.
803afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE
804afe61c15SRodney W. Grimes.Bd -literal
80503763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head =
80603763fe0SJake Burkholder    TAILQ_HEAD_INITIALIZER(head);
807afe61c15SRodney W. Grimesstruct tailhead *headp;			/* Tail queue head. */
808afe61c15SRodney W. Grimesstruct entry {
809afe61c15SRodney W. Grimes	...
810afe61c15SRodney W. Grimes	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
811afe61c15SRodney W. Grimes	...
8127658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np;
813afe61c15SRodney W. Grimes
814afe61c15SRodney W. GrimesTAILQ_INIT(&head);			/* Initialize the queue. */
815afe61c15SRodney W. Grimes
816afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
817afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries);
818afe61c15SRodney W. Grimes
819afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
820afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries);
821afe61c15SRodney W. Grimes
822afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
823afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries);
8247658b0a2SJustin T. Gibbs
8257658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
8263652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries);
8277658b0a2SJustin T. Gibbs
8287658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
8297658b0a2SJustin T. Gibbsfree(n2);
830afe61c15SRodney W. Grimes					/* Forward traversal. */
83179ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries)
832afe61c15SRodney W. Grimes	np-> ...
8336c1d0fbfSArchie Cobbs					/* Reverse traversal. */
8346c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
8356c1d0fbfSArchie Cobbs	np-> ...
8367658b0a2SJustin T. Gibbs					/* TailQ Deletion. */
837d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) {
838c5c15c16SPoul-Henning Kamp	n1 = TAILQ_FIRST(&head);
83979ea9bc4SAlexey Zelkin	TAILQ_REMOVE(&head, n1, entries);
8407658b0a2SJustin T. Gibbs	free(n1);
8417658b0a2SJustin T. Gibbs}
8427658b0a2SJustin T. Gibbs					/* Faster TailQ Deletion. */
843c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head);
8447658b0a2SJustin T. Gibbswhile (n1 != NULL) {
845c5c15c16SPoul-Henning Kamp	n2 = TAILQ_NEXT(n1, entries);
8467658b0a2SJustin T. Gibbs	free(n1);
8477658b0a2SJustin T. Gibbs	n1 = n2;
8487658b0a2SJustin T. Gibbs}
8497658b0a2SJustin T. GibbsTAILQ_INIT(&head);
850afe61c15SRodney W. Grimes.Ed
851afe61c15SRodney W. Grimes.Sh HISTORY
852afe61c15SRodney W. GrimesThe
853afe61c15SRodney W. Grimes.Nm queue
85421421932SMike Pritchardfunctions first appeared in
85521421932SMike Pritchard.Bx 4.4 .
856