xref: /freebsd/share/man/man3/queue.3 (revision 7ecb40192ed3f0022427ee056f25340c5c420337)
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.\"
35*7ecb4019SLawrence Stewart.Dd June 17, 2013
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 ,
43*7ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM ,
442724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE ,
45*7ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE ,
464a775e8fSJustin T. Gibbs.Nm SLIST_HEAD ,
4703763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER ,
484a775e8fSJustin T. Gibbs.Nm SLIST_INIT ,
494a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER ,
504a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD ,
51aea88892SPoul-Henning Kamp.Nm SLIST_NEXT ,
523d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER ,
534a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD ,
544a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE ,
55359295cdSMatthew D Fleming.Nm SLIST_SWAP ,
56d57e28adSThomas Moestl.Nm STAILQ_CONCAT ,
5779ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY ,
584a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY ,
5979ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST ,
6079ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH ,
61*7ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM ,
622724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE ,
63*7ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE ,
644a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD ,
6503763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER ,
664a775e8fSJustin T. Gibbs.Nm STAILQ_INIT ,
674a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER ,
684a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD ,
694a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL ,
7079ea9bc4SAlexey Zelkin.Nm STAILQ_LAST ,
7179ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT ,
723d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER ,
734a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD ,
744a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE ,
75359295cdSMatthew D Fleming.Nm STAILQ_SWAP ,
7679ea9bc4SAlexey Zelkin.Nm LIST_EMPTY ,
77afe61c15SRodney W. Grimes.Nm LIST_ENTRY ,
7879ea9bc4SAlexey Zelkin.Nm LIST_FIRST ,
7979ea9bc4SAlexey Zelkin.Nm LIST_FOREACH ,
80*7ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM ,
814250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE ,
82*7ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM_SAFE ,
83afe61c15SRodney W. Grimes.Nm LIST_HEAD ,
8403763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER ,
85afe61c15SRodney W. Grimes.Nm LIST_INIT ,
86afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER ,
877658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE ,
88afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD ,
8979ea9bc4SAlexey Zelkin.Nm LIST_NEXT ,
904170b083SEd Schouten.Nm LIST_PREV ,
91afe61c15SRodney W. Grimes.Nm LIST_REMOVE ,
92359295cdSMatthew D Fleming.Nm LIST_SWAP ,
93d57e28adSThomas Moestl.Nm TAILQ_CONCAT ,
94c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY ,
95afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY ,
96c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST ,
9779ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH ,
98*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM ,
992724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE ,
100*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE ,
1016c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE ,
102*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM ,
1032724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE ,
104*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
105afe61c15SRodney W. Grimes.Nm TAILQ_HEAD ,
10603763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER ,
107afe61c15SRodney W. Grimes.Nm TAILQ_INIT ,
108afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER ,
1097658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE ,
110afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD ,
111afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL ,
112c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST ,
113c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT ,
11479ea9bc4SAlexey Zelkin.Nm TAILQ_PREV ,
115359295cdSMatthew D Fleming.Nm TAILQ_REMOVE ,
116359295cdSMatthew D Fleming.Nm TAILQ_SWAP
1174a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues,
11824b85d18SPoul-Henning Kamplists and tail queues
119afe61c15SRodney W. Grimes.Sh SYNOPSIS
12032eef9aeSRuslan Ermilov.In sys/queue.h
1218f20a914SMike Pritchard.\"
122aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head"
1234a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE"
124aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head"
12579ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
126*7ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1272724bce2SAlexander Kabaev.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
128*7ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
1294a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE"
13003763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
1314a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head"
1324a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
1334a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
134aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
1353d98b75bSEd Schouten.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
1364a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1374a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
138359295cdSMatthew D Fleming.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
1398f20a914SMike Pritchard.\"
140d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
14179ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head"
1424a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE"
14379ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head"
14479ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
145*7ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1462724bce2SAlexander Kabaev.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
147*7ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
1484a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE"
14903763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
1504a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head"
1514a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
1524a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
1534a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
154f36b6e4bSMaxim Sobolev.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
15579ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
1563d98b75bSEd Schouten.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
15702a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1584a775e8fSJustin T. Gibbs.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
159359295cdSMatthew D Fleming.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
1608f20a914SMike Pritchard.\"
16179ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head"
162afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE"
16379ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head"
16479ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
165*7ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
1664250a68eSBosko Milekic.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
167*7ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
168afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE"
16903763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
170afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head"
1714a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
1724a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
173afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
17479ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
1754170b083SEd Schouten.Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
176afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
177359295cdSMatthew D Fleming.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
1788f20a914SMike Pritchard.\"
179d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
180c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head"
181afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE"
182c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head"
18379ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
184*7ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
1852724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
186*7ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
1876c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
188*7ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
1892724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
190*7ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
191afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE"
19203763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
193afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head"
194afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
1953652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
196afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
197afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
19879ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
199c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
20079ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
201afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
202359295cdSMatthew D Fleming.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
2038f20a914SMike Pritchard.\"
204afe61c15SRodney W. Grimes.Sh DESCRIPTION
205b86388c1SDima DorfmanThese macros define and operate on four types of data structures:
206b86388c1SDima Dorfmansingly-linked lists, singly-linked tail queues, lists, and tail queues.
207b86388c1SDima DorfmanAll four structures support the following functionality:
208afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
209afe61c15SRodney W. Grimes.It
210afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list.
211afe61c15SRodney W. Grimes.It
212afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list.
213afe61c15SRodney W. Grimes.It
2144a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list.
2157658b0a2SJustin T. Gibbs.It
216afe61c15SRodney W. GrimesForward traversal through the list.
217359295cdSMatthew D Fleming.It
218f9257802SRalf S. EngelschallSwapping the contents of two lists.
219afe61c15SRodney W. Grimes.El
220afe61c15SRodney W. Grimes.Pp
221d5fd66e1SEd MasteSingly-linked lists are the simplest of the four data structures
2224a775e8fSJustin T. Gibbsand support only the above functionality.
2234a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets
2244a775e8fSJustin T. Gibbsand few or no removals,
2254a775e8fSJustin T. Gibbsor for implementing a LIFO queue.
226ed741d4eSDavid E. O'BrienSingly-linked lists add the following functionality:
227ed741d4eSDavid E. O'Brien.Bl -enum -compact -offset indent
228ed741d4eSDavid E. O'Brien.It
229ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list.
230ed741d4eSDavid E. O'Brien.El
2314a775e8fSJustin T. Gibbs.Pp
2324a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality:
2334a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2344a775e8fSJustin T. Gibbs.It
2354a775e8fSJustin T. GibbsEntries can be added at the end of a list.
236d57e28adSThomas Moestl.It
237ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list.
238ed741d4eSDavid E. O'Brien.It
239d57e28adSThomas MoestlThey may be concatenated.
2404a775e8fSJustin T. Gibbs.El
2414a775e8fSJustin T. GibbsHowever:
2424a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2434a775e8fSJustin T. Gibbs.It
2444a775e8fSJustin T. GibbsAll list insertions must specify the head of the list.
2454a775e8fSJustin T. Gibbs.It
2464a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one.
2474a775e8fSJustin T. Gibbs.It
2484a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower
2494a775e8fSJustin T. Gibbsthan singly-linked lists.
2504a775e8fSJustin T. Gibbs.El
2514a775e8fSJustin T. Gibbs.Pp
252f9257802SRalf S. EngelschallSingly-linked tail queues are ideal for applications with large datasets and
2534a775e8fSJustin T. Gibbsfew or no removals,
2544a775e8fSJustin T. Gibbsor for implementing a FIFO queue.
2554a775e8fSJustin T. Gibbs.Pp
256b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues)
257b86388c1SDima Dorfmanadditionally allow:
2584a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2594a775e8fSJustin T. Gibbs.It
2604a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list.
2614a775e8fSJustin T. Gibbs.It
2624a775e8fSJustin T. GibbsO(1) removal of any entry in the list.
2634a775e8fSJustin T. Gibbs.El
2644a775e8fSJustin T. GibbsHowever:
2654a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2664a775e8fSJustin T. Gibbs.It
267ad035dafSChristian BruefferEach element requires two pointers rather than one.
2684a775e8fSJustin T. Gibbs.It
2694a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about
2704a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures.
2714a775e8fSJustin T. Gibbs.El
2724a775e8fSJustin T. Gibbs.Pp
2734170b083SEd SchoutenLinked lists are the simplest of the doubly linked data structures.
2744170b083SEd SchoutenThey add the following functionality over the above:
2754170b083SEd Schouten.Bl -enum -compact -offset indent
2764170b083SEd Schouten.It
2774170b083SEd SchoutenThey may be traversed backwards.
2784170b083SEd Schouten.El
2794170b083SEd SchoutenHowever:
2804170b083SEd Schouten.Bl -enum -compact -offset indent
2814170b083SEd Schouten.It
2824170b083SEd SchoutenTo traverse backwards, an entry to begin the traversal and the list in
2834170b083SEd Schoutenwhich it is contained must be specified.
2844170b083SEd Schouten.El
285afe61c15SRodney W. Grimes.Pp
286afe61c15SRodney W. GrimesTail queues add the following functionality:
287afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
288afe61c15SRodney W. Grimes.It
289afe61c15SRodney W. GrimesEntries can be added at the end of a list.
2906c1d0fbfSArchie Cobbs.It
2916c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head.
292d57e28adSThomas Moestl.It
293d57e28adSThomas MoestlThey may be concatenated.
294afe61c15SRodney W. Grimes.El
295afe61c15SRodney W. GrimesHowever:
296afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
297afe61c15SRodney W. Grimes.It
298afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list.
299afe61c15SRodney W. Grimes.It
300afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one.
301afe61c15SRodney W. Grimes.It
302afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower
3034a775e8fSJustin T. Gibbsthan singly-linked lists.
304afe61c15SRodney W. Grimes.El
305afe61c15SRodney W. Grimes.Pp
306afe61c15SRodney W. GrimesIn the macro definitions,
307afe61c15SRodney W. Grimes.Fa TYPE
308afe61c15SRodney W. Grimesis the name of a user defined structure,
309afe61c15SRodney W. Grimesthat must contain a field of type
3104a775e8fSJustin T. Gibbs.Li SLIST_ENTRY ,
3114a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY ,
312afe61c15SRodney W. Grimes.Li LIST_ENTRY ,
313afe61c15SRodney W. Grimesor
31424b85d18SPoul-Henning Kamp.Li TAILQ_ENTRY ,
315afe61c15SRodney W. Grimesnamed
316afe61c15SRodney W. Grimes.Fa NAME .
317afe61c15SRodney W. GrimesThe argument
318afe61c15SRodney W. Grimes.Fa HEADNAME
319afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared
320afe61c15SRodney W. Grimesusing the macros
3214a775e8fSJustin T. Gibbs.Li SLIST_HEAD ,
3224a775e8fSJustin T. Gibbs.Li STAILQ_HEAD ,
323afe61c15SRodney W. Grimes.Li LIST_HEAD ,
324afe61c15SRodney W. Grimesor
32524b85d18SPoul-Henning Kamp.Li TAILQ_HEAD .
326afe61c15SRodney W. GrimesSee the examples below for further explanation of how these
327afe61c15SRodney W. Grimesmacros are used.
3284a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS
3294a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the
3304a775e8fSJustin T. Gibbs.Nm SLIST_HEAD
3314a775e8fSJustin T. Gibbsmacro.
3324a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element
3334a775e8fSJustin T. Gibbson the list.
3344a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation
3354a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements.
3364a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or
3374a775e8fSJustin T. Gibbsat the head of the list.
3384a775e8fSJustin T. GibbsAn
3394a775e8fSJustin T. Gibbs.Fa SLIST_HEAD
3404a775e8fSJustin T. Gibbsstructure is declared as follows:
3414a775e8fSJustin T. Gibbs.Bd -literal -offset indent
3424a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head;
3434a775e8fSJustin T. Gibbs.Ed
3448f20a914SMike Pritchard.Pp
3454a775e8fSJustin T. Gibbswhere
3464a775e8fSJustin T. Gibbs.Fa HEADNAME
3474a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
3484a775e8fSJustin T. Gibbs.Fa TYPE
3494a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list.
3504a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as:
3514a775e8fSJustin T. Gibbs.Bd -literal -offset indent
3524a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
3534a775e8fSJustin T. Gibbs.Ed
3548f20a914SMike Pritchard.Pp
3554a775e8fSJustin T. Gibbs(The names
3564a775e8fSJustin T. Gibbs.Li head
3574a775e8fSJustin T. Gibbsand
3584a775e8fSJustin T. Gibbs.Li headp
3594a775e8fSJustin T. Gibbsare user selectable.)
3604a775e8fSJustin T. Gibbs.Pp
3614a775e8fSJustin T. GibbsThe macro
36203763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER
36303763fe0SJake Burkholderevaluates to an initializer for the list
36403763fe0SJake Burkholder.Fa head .
36503763fe0SJake Burkholder.Pp
36603763fe0SJake BurkholderThe macro
36779ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY
36879ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list.
36979ea9bc4SAlexey Zelkin.Pp
37079ea9bc4SAlexey ZelkinThe macro
3714a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY
3724a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
3734a775e8fSJustin T. Gibbsthe list.
3744a775e8fSJustin T. Gibbs.Pp
3754a775e8fSJustin T. GibbsThe macro
37679ea9bc4SAlexey Zelkin.Nm SLIST_FIRST
37779ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty.
37879ea9bc4SAlexey Zelkin.Pp
37979ea9bc4SAlexey ZelkinThe macro
38079ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH
38179ea9bc4SAlexey Zelkintraverses the list referenced by
38279ea9bc4SAlexey Zelkin.Fa head
38379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in
38479ea9bc4SAlexey Zelkinturn to
38579ea9bc4SAlexey Zelkin.Fa var .
38679ea9bc4SAlexey Zelkin.Pp
38779ea9bc4SAlexey ZelkinThe macro
388*7ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM
389*7ecb4019SLawrence Stewartbehaves identically to
390*7ecb4019SLawrence Stewart.Nm SLIST_FOREACH
391*7ecb4019SLawrence Stewartwhen
392*7ecb4019SLawrence Stewart.Fa var
393*7ecb4019SLawrence Stewartis NULL, else it treats
394*7ecb4019SLawrence Stewart.Fa var
395*7ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at
396*7ecb4019SLawrence Stewart.Fa var
397*7ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by
398*7ecb4019SLawrence Stewart.Fa head .
399*7ecb4019SLawrence Stewart.Pp
400*7ecb4019SLawrence StewartThe macro
4012724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE
4022724bce2SAlexander Kabaevtraverses the list referenced by
4032724bce2SAlexander Kabaev.Fa head
4042724bce2SAlexander Kabaevin the forward direction, assigning each element in
4052724bce2SAlexander Kabaevturn to
4062724bce2SAlexander Kabaev.Fa var .
4072724bce2SAlexander KabaevHowever, unlike
4082724bce2SAlexander Kabaev.Fn SLIST_FOREACH
4092724bce2SAlexander Kabaevhere it is permitted to both remove
4102724bce2SAlexander Kabaev.Fa var
4112724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the
4122724bce2SAlexander Kabaevtraversal.
4132724bce2SAlexander Kabaev.Pp
4142724bce2SAlexander KabaevThe macro
415*7ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE
416*7ecb4019SLawrence Stewartbehaves identically to
417*7ecb4019SLawrence Stewart.Nm SLIST_FOREACH_SAFE
418*7ecb4019SLawrence Stewartwhen
419*7ecb4019SLawrence Stewart.Fa var
420*7ecb4019SLawrence Stewartis NULL, else it treats
421*7ecb4019SLawrence Stewart.Fa var
422*7ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at
423*7ecb4019SLawrence Stewart.Fa var
424*7ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by
425*7ecb4019SLawrence Stewart.Fa head .
426*7ecb4019SLawrence Stewart.Pp
427*7ecb4019SLawrence StewartThe macro
4284a775e8fSJustin T. Gibbs.Nm SLIST_INIT
4294a775e8fSJustin T. Gibbsinitializes the list referenced by
4304a775e8fSJustin T. Gibbs.Fa head .
4314a775e8fSJustin T. Gibbs.Pp
4324a775e8fSJustin T. GibbsThe macro
4334a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD
4344a775e8fSJustin T. Gibbsinserts the new element
4354a775e8fSJustin T. Gibbs.Fa elm
4364a775e8fSJustin T. Gibbsat the head of the list.
4374a775e8fSJustin T. Gibbs.Pp
4384a775e8fSJustin T. GibbsThe macro
4394a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER
4404a775e8fSJustin T. Gibbsinserts the new element
4414a775e8fSJustin T. Gibbs.Fa elm
4424a775e8fSJustin T. Gibbsafter the element
4434a775e8fSJustin T. Gibbs.Fa listelm .
4444a775e8fSJustin T. Gibbs.Pp
4454a775e8fSJustin T. GibbsThe macro
44679ea9bc4SAlexey Zelkin.Nm SLIST_NEXT
44779ea9bc4SAlexey Zelkinreturns the next element in the list.
44879ea9bc4SAlexey Zelkin.Pp
44979ea9bc4SAlexey ZelkinThe macro
4503d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER
4513d98b75bSEd Schoutenremoves the element after
4523d98b75bSEd Schouten.Fa elm
453bc106255SEitan Adlerfrom the list.
454bc106255SEitan AdlerUnlike
4553d98b75bSEd Schouten.Fa SLIST_REMOVE ,
4563d98b75bSEd Schoutenthis macro does not traverse the entire list.
4573d98b75bSEd Schouten.Pp
4583d98b75bSEd SchoutenThe macro
4594a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD
4604a775e8fSJustin T. Gibbsremoves the element
4614a775e8fSJustin T. Gibbs.Fa elm
4624a775e8fSJustin T. Gibbsfrom the head of the list.
4634a775e8fSJustin T. GibbsFor optimum efficiency,
4644a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use
4654a775e8fSJustin T. Gibbsthis macro instead of the generic
4664a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE
4674a775e8fSJustin T. Gibbsmacro.
4684a775e8fSJustin T. Gibbs.Pp
4694a775e8fSJustin T. GibbsThe macro
4704a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE
4714a775e8fSJustin T. Gibbsremoves the element
4724a775e8fSJustin T. Gibbs.Fa elm
4734a775e8fSJustin T. Gibbsfrom the list.
474359295cdSMatthew D Fleming.Pp
475359295cdSMatthew D FlemingThe macro
476359295cdSMatthew D Fleming.Nm SLIST_SWAP
477359295cdSMatthew D Flemingswaps the contents of
478359295cdSMatthew D Fleming.Fa head1
479359295cdSMatthew D Flemingand
480359295cdSMatthew D Fleming.Fa head2 .
4814a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE
4824a775e8fSJustin T. Gibbs.Bd -literal
48303763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head =
48403763fe0SJake Burkholder    SLIST_HEAD_INITIALIZER(head);
4854a775e8fSJustin T. Gibbsstruct slisthead *headp;		/* Singly-linked List head. */
4864a775e8fSJustin T. Gibbsstruct entry {
4874a775e8fSJustin T. Gibbs	...
4884a775e8fSJustin T. Gibbs	SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
4894a775e8fSJustin T. Gibbs	...
4904a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
4914a775e8fSJustin T. Gibbs
4924a775e8fSJustin T. GibbsSLIST_INIT(&head);			/* Initialize the list. */
4934a775e8fSJustin T. Gibbs
4944a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
4954a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries);
4964a775e8fSJustin T. Gibbs
4974a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
4984a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries);
4994a775e8fSJustin T. Gibbs
5004a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
5014a775e8fSJustin T. Gibbsfree(n2);
5024a775e8fSJustin T. Gibbs
50379ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head);
50403763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries);	/* Deletion from the head. */
5054a775e8fSJustin T. Gibbsfree(n3);
5064a775e8fSJustin T. Gibbs					/* Forward traversal. */
50779ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries)
5084a775e8fSJustin T. Gibbs	np-> ...
5092724bce2SAlexander Kabaev					/* Safe forward traversal. */
5102724bce2SAlexander KabaevSLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
5112724bce2SAlexander Kabaev	np->do_stuff();
5122724bce2SAlexander Kabaev	...
5132724bce2SAlexander Kabaev	SLIST_REMOVE(&head, np, entry, entries);
5142724bce2SAlexander Kabaev	free(np);
5152724bce2SAlexander Kabaev}
5164a775e8fSJustin T. Gibbs
51779ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) {		/* List Deletion. */
51879ea9bc4SAlexey Zelkin	n1 = SLIST_FIRST(&head);
5194a775e8fSJustin T. Gibbs	SLIST_REMOVE_HEAD(&head, entries);
5204a775e8fSJustin T. Gibbs	free(n1);
5214a775e8fSJustin T. Gibbs}
5224a775e8fSJustin T. Gibbs.Ed
5234a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES
5244a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the
5254a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD
5264a775e8fSJustin T. Gibbsmacro.
5274a775e8fSJustin T. GibbsThis structure contains a pair of pointers,
5284a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to
5294a775e8fSJustin T. Gibbsthe last element in the tail queue.
5304a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer
5314a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary
5324a775e8fSJustin T. Gibbselements.
5334a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element,
5344a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue.
5354a775e8fSJustin T. GibbsA
5364a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD
5374a775e8fSJustin T. Gibbsstructure is declared as follows:
5384a775e8fSJustin T. Gibbs.Bd -literal -offset indent
5394a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head;
5404a775e8fSJustin T. Gibbs.Ed
5418f20a914SMike Pritchard.Pp
5424a775e8fSJustin T. Gibbswhere
5434a775e8fSJustin T. Gibbs.Li HEADNAME
5444a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
5454a775e8fSJustin T. Gibbs.Li TYPE
5464a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue.
5474a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as:
5484a775e8fSJustin T. Gibbs.Bd -literal -offset indent
5494a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
5504a775e8fSJustin T. Gibbs.Ed
5518f20a914SMike Pritchard.Pp
5524a775e8fSJustin T. Gibbs(The names
5534a775e8fSJustin T. Gibbs.Li head
5544a775e8fSJustin T. Gibbsand
5554a775e8fSJustin T. Gibbs.Li headp
5564a775e8fSJustin T. Gibbsare user selectable.)
5574a775e8fSJustin T. Gibbs.Pp
5584a775e8fSJustin T. GibbsThe macro
55903763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER
56003763fe0SJake Burkholderevaluates to an initializer for the tail queue
56103763fe0SJake Burkholder.Fa head .
56203763fe0SJake Burkholder.Pp
56303763fe0SJake BurkholderThe macro
564d57e28adSThomas Moestl.Nm STAILQ_CONCAT
565d57e28adSThomas Moestlconcatenates the tail queue headed by
566d57e28adSThomas Moestl.Fa head2
567d57e28adSThomas Moestlonto the end of the one headed by
568d57e28adSThomas Moestl.Fa head1
569d57e28adSThomas Moestlremoving all entries from the former.
570d57e28adSThomas Moestl.Pp
571d57e28adSThomas MoestlThe macro
57279ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY
57379ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue.
57479ea9bc4SAlexey Zelkin.Pp
57579ea9bc4SAlexey ZelkinThe macro
5764a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY
5774a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
5784a775e8fSJustin T. Gibbsthe tail queue.
5794a775e8fSJustin T. Gibbs.Pp
5804a775e8fSJustin T. GibbsThe macro
58179ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST
58279ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue
58379ea9bc4SAlexey Zelkinis empty.
58479ea9bc4SAlexey Zelkin.Pp
58579ea9bc4SAlexey ZelkinThe macro
58679ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH
58779ea9bc4SAlexey Zelkintraverses the tail queue referenced by
58879ea9bc4SAlexey Zelkin.Fa head
58979ea9bc4SAlexey Zelkinin the forward direction, assigning each element
59079ea9bc4SAlexey Zelkinin turn to
59179ea9bc4SAlexey Zelkin.Fa var .
59279ea9bc4SAlexey Zelkin.Pp
59379ea9bc4SAlexey ZelkinThe macro
594*7ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM
595*7ecb4019SLawrence Stewartbehaves identically to
596*7ecb4019SLawrence Stewart.Nm STAILQ_FOREACH
597*7ecb4019SLawrence Stewartwhen
598*7ecb4019SLawrence Stewart.Fa var
599*7ecb4019SLawrence Stewartis NULL, else it treats
600*7ecb4019SLawrence Stewart.Fa var
601*7ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at
602*7ecb4019SLawrence Stewart.Fa var
603*7ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by
604*7ecb4019SLawrence Stewart.Fa head .
605*7ecb4019SLawrence Stewart.Pp
606*7ecb4019SLawrence StewartThe macro
6072724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE
6082724bce2SAlexander Kabaevtraverses the tail queue referenced by
6092724bce2SAlexander Kabaev.Fa head
6102724bce2SAlexander Kabaevin the forward direction, assigning each element
6112724bce2SAlexander Kabaevin turn to
6122724bce2SAlexander Kabaev.Fa var .
6132724bce2SAlexander KabaevHowever, unlike
6142724bce2SAlexander Kabaev.Fn STAILQ_FOREACH
6152724bce2SAlexander Kabaevhere it is permitted to both remove
6162724bce2SAlexander Kabaev.Fa var
6172724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the
6182724bce2SAlexander Kabaevtraversal.
6192724bce2SAlexander Kabaev.Pp
6202724bce2SAlexander KabaevThe macro
621*7ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE
622*7ecb4019SLawrence Stewartbehaves identically to
623*7ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_SAFE
624*7ecb4019SLawrence Stewartwhen
625*7ecb4019SLawrence Stewart.Fa var
626*7ecb4019SLawrence Stewartis NULL, else it treats
627*7ecb4019SLawrence Stewart.Fa var
628*7ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at
629*7ecb4019SLawrence Stewart.Fa var
630*7ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by
631*7ecb4019SLawrence Stewart.Fa head .
632*7ecb4019SLawrence Stewart.Pp
633*7ecb4019SLawrence StewartThe macro
6344a775e8fSJustin T. Gibbs.Nm STAILQ_INIT
6354a775e8fSJustin T. Gibbsinitializes the tail queue referenced by
6364a775e8fSJustin T. Gibbs.Fa head .
6374a775e8fSJustin T. Gibbs.Pp
6384a775e8fSJustin T. GibbsThe macro
6394a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD
6404a775e8fSJustin T. Gibbsinserts the new element
6414a775e8fSJustin T. Gibbs.Fa elm
6424a775e8fSJustin T. Gibbsat the head of the tail queue.
6434a775e8fSJustin T. Gibbs.Pp
6444a775e8fSJustin T. GibbsThe macro
6454a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL
6464a775e8fSJustin T. Gibbsinserts the new element
6474a775e8fSJustin T. Gibbs.Fa elm
6484a775e8fSJustin T. Gibbsat the end of the tail queue.
6494a775e8fSJustin T. Gibbs.Pp
6504a775e8fSJustin T. GibbsThe macro
6514a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER
6524a775e8fSJustin T. Gibbsinserts the new element
6534a775e8fSJustin T. Gibbs.Fa elm
6544a775e8fSJustin T. Gibbsafter the element
6554a775e8fSJustin T. Gibbs.Fa listelm .
6564a775e8fSJustin T. Gibbs.Pp
6574a775e8fSJustin T. GibbsThe macro
65879ea9bc4SAlexey Zelkin.Nm STAILQ_LAST
65979ea9bc4SAlexey Zelkinreturns the last item on the tail queue.
660982ba1cbSKirk McKusickIf the tail queue is empty the return value is
661982ba1cbSKirk McKusick.Dv NULL .
66279ea9bc4SAlexey Zelkin.Pp
66379ea9bc4SAlexey ZelkinThe macro
66479ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT
66579ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last.
66679ea9bc4SAlexey Zelkin.Pp
66779ea9bc4SAlexey ZelkinThe macro
6683d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER
6693d98b75bSEd Schoutenremoves the element after
6703d98b75bSEd Schouten.Fa elm
671bc106255SEitan Adlerfrom the tail queue.
672bc106255SEitan AdlerUnlike
6733d98b75bSEd Schouten.Fa STAILQ_REMOVE ,
6743d98b75bSEd Schoutenthis macro does not traverse the entire tail queue.
6753d98b75bSEd Schouten.Pp
6763d98b75bSEd SchoutenThe macro
6774a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD
678ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue.
6794a775e8fSJustin T. GibbsFor optimum efficiency,
6804a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should
6814a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic
6824a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE
6834a775e8fSJustin T. Gibbsmacro.
6844a775e8fSJustin T. Gibbs.Pp
6854a775e8fSJustin T. GibbsThe macro
6864a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE
6874a775e8fSJustin T. Gibbsremoves the element
6884a775e8fSJustin T. Gibbs.Fa elm
6894a775e8fSJustin T. Gibbsfrom the tail queue.
690359295cdSMatthew D Fleming.Pp
691359295cdSMatthew D FlemingThe macro
692359295cdSMatthew D Fleming.Nm STAILQ_SWAP
693359295cdSMatthew D Flemingswaps the contents of
694359295cdSMatthew D Fleming.Fa head1
695359295cdSMatthew D Flemingand
696359295cdSMatthew D Fleming.Fa head2 .
6974a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
6984a775e8fSJustin T. Gibbs.Bd -literal
69903763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head =
70003763fe0SJake Burkholder    STAILQ_HEAD_INITIALIZER(head);
7014a775e8fSJustin T. Gibbsstruct stailhead *headp;		/* Singly-linked tail queue head. */
7024a775e8fSJustin T. Gibbsstruct entry {
7034a775e8fSJustin T. Gibbs	...
7044a775e8fSJustin T. Gibbs	STAILQ_ENTRY(entry) entries;	/* Tail queue. */
7054a775e8fSJustin T. Gibbs	...
7064a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
7074a775e8fSJustin T. Gibbs
7084a775e8fSJustin T. GibbsSTAILQ_INIT(&head);			/* Initialize the queue. */
7094a775e8fSJustin T. Gibbs
7104a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
7114a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries);
7124a775e8fSJustin T. Gibbs
7134a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
7144a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries);
7154a775e8fSJustin T. Gibbs
7164a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
7174a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries);
7184a775e8fSJustin T. Gibbs					/* Deletion. */
7194a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries);
7204a775e8fSJustin T. Gibbsfree(n2);
72103763fe0SJake Burkholder					/* Deletion from the head. */
72279ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head);
7234a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries);
7244a775e8fSJustin T. Gibbsfree(n3);
7254a775e8fSJustin T. Gibbs					/* Forward traversal. */
72679ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries)
7274a775e8fSJustin T. Gibbs	np-> ...
7282724bce2SAlexander Kabaev					/* Safe forward traversal. */
7292724bce2SAlexander KabaevSTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
7302724bce2SAlexander Kabaev	np->do_stuff();
7312724bce2SAlexander Kabaev	...
7322724bce2SAlexander Kabaev	STAILQ_REMOVE(&head, np, entry, entries);
7332724bce2SAlexander Kabaev	free(np);
7342724bce2SAlexander Kabaev}
7354a775e8fSJustin T. Gibbs					/* TailQ Deletion. */
73679ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) {
73703763fe0SJake Burkholder	n1 = STAILQ_FIRST(&head);
738266e33fdSJohn Baldwin	STAILQ_REMOVE_HEAD(&head, entries);
7394a775e8fSJustin T. Gibbs	free(n1);
7404a775e8fSJustin T. Gibbs}
7414a775e8fSJustin T. Gibbs					/* Faster TailQ Deletion. */
74279ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head);
7434a775e8fSJustin T. Gibbswhile (n1 != NULL) {
74479ea9bc4SAlexey Zelkin	n2 = STAILQ_NEXT(n1, entries);
7454a775e8fSJustin T. Gibbs	free(n1);
7464a775e8fSJustin T. Gibbs	n1 = n2;
7474a775e8fSJustin T. Gibbs}
7484a775e8fSJustin T. GibbsSTAILQ_INIT(&head);
7494a775e8fSJustin T. Gibbs.Ed
750afe61c15SRodney W. Grimes.Sh LISTS
751afe61c15SRodney W. GrimesA list is headed by a structure defined by the
752afe61c15SRodney W. Grimes.Nm LIST_HEAD
753afe61c15SRodney W. Grimesmacro.
754afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element
755afe61c15SRodney W. Grimeson the list.
756afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
757afe61c15SRodney W. Grimesremoved without traversing the list.
7584a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element,
7594a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list.
760afe61c15SRodney W. GrimesA
761afe61c15SRodney W. Grimes.Fa LIST_HEAD
762afe61c15SRodney W. Grimesstructure is declared as follows:
763afe61c15SRodney W. Grimes.Bd -literal -offset indent
764afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head;
765afe61c15SRodney W. Grimes.Ed
7668f20a914SMike Pritchard.Pp
767afe61c15SRodney W. Grimeswhere
768afe61c15SRodney W. Grimes.Fa HEADNAME
769afe61c15SRodney W. Grimesis the name of the structure to be defined, and
770afe61c15SRodney W. Grimes.Fa TYPE
771afe61c15SRodney W. Grimesis the type of the elements to be linked into the list.
772afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as:
773afe61c15SRodney W. Grimes.Bd -literal -offset indent
774afe61c15SRodney W. Grimesstruct HEADNAME *headp;
775afe61c15SRodney W. Grimes.Ed
7768f20a914SMike Pritchard.Pp
777afe61c15SRodney W. Grimes(The names
778afe61c15SRodney W. Grimes.Li head
779afe61c15SRodney W. Grimesand
780afe61c15SRodney W. Grimes.Li headp
781afe61c15SRodney W. Grimesare user selectable.)
782afe61c15SRodney W. Grimes.Pp
783afe61c15SRodney W. GrimesThe macro
78403763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER
78503763fe0SJake Burkholderevaluates to an initializer for the list
78603763fe0SJake Burkholder.Fa head .
78703763fe0SJake Burkholder.Pp
78803763fe0SJake BurkholderThe macro
78979ea9bc4SAlexey Zelkin.Nm LIST_EMPTY
790ddbb94adSTom Rhodesevaluates to true if there are no elements in the list.
79179ea9bc4SAlexey Zelkin.Pp
79279ea9bc4SAlexey ZelkinThe macro
793afe61c15SRodney W. Grimes.Nm LIST_ENTRY
794afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
795afe61c15SRodney W. Grimesthe list.
796afe61c15SRodney W. Grimes.Pp
797afe61c15SRodney W. GrimesThe macro
79879ea9bc4SAlexey Zelkin.Nm LIST_FIRST
79979ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list
80079ea9bc4SAlexey Zelkinis empty.
80179ea9bc4SAlexey Zelkin.Pp
80279ea9bc4SAlexey ZelkinThe macro
80379ea9bc4SAlexey Zelkin.Nm LIST_FOREACH
80479ea9bc4SAlexey Zelkintraverses the list referenced by
80579ea9bc4SAlexey Zelkin.Fa head
80679ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
80779ea9bc4SAlexey Zelkin.Fa var .
80879ea9bc4SAlexey Zelkin.Pp
80979ea9bc4SAlexey ZelkinThe macro
810*7ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM
811*7ecb4019SLawrence Stewartbehaves identically to
812*7ecb4019SLawrence Stewart.Nm LIST_FOREACH
813*7ecb4019SLawrence Stewartwhen
814*7ecb4019SLawrence Stewart.Fa var
815*7ecb4019SLawrence Stewartis NULL, else it treats
816*7ecb4019SLawrence Stewart.Fa var
817*7ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at
818*7ecb4019SLawrence Stewart.Fa var
819*7ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by
820*7ecb4019SLawrence Stewart.Fa head .
821*7ecb4019SLawrence Stewart.Pp
822*7ecb4019SLawrence StewartThe macro
8234250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE
8244250a68eSBosko Milekictraverses the list referenced by
8254250a68eSBosko Milekic.Fa head
8264250a68eSBosko Milekicin the forward direction, assigning each element in turn to
8274250a68eSBosko Milekic.Fa var .
8284250a68eSBosko MilekicHowever, unlike
8294250a68eSBosko Milekic.Fn LIST_FOREACH
8304250a68eSBosko Milekichere it is permitted to both remove
8314250a68eSBosko Milekic.Fa var
8324250a68eSBosko Milekicas well as free it from within the loop safely without interfering with the
8334250a68eSBosko Milekictraversal.
8344250a68eSBosko Milekic.Pp
8354250a68eSBosko MilekicThe macro
836*7ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM_SAFE
837*7ecb4019SLawrence Stewartbehaves identically to
838*7ecb4019SLawrence Stewart.Nm LIST_FOREACH_SAFE
839*7ecb4019SLawrence Stewartwhen
840*7ecb4019SLawrence Stewart.Fa var
841*7ecb4019SLawrence Stewartis NULL, else it treats
842*7ecb4019SLawrence Stewart.Fa var
843*7ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at
844*7ecb4019SLawrence Stewart.Fa var
845*7ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by
846*7ecb4019SLawrence Stewart.Fa head .
847*7ecb4019SLawrence Stewart.Pp
848*7ecb4019SLawrence StewartThe macro
849afe61c15SRodney W. Grimes.Nm LIST_INIT
850afe61c15SRodney W. Grimesinitializes the list referenced by
851afe61c15SRodney W. Grimes.Fa head .
852afe61c15SRodney W. Grimes.Pp
853afe61c15SRodney W. GrimesThe macro
854afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD
855afe61c15SRodney W. Grimesinserts the new element
856afe61c15SRodney W. Grimes.Fa elm
857afe61c15SRodney W. Grimesat the head of the list.
858afe61c15SRodney W. Grimes.Pp
859afe61c15SRodney W. GrimesThe macro
860afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER
861afe61c15SRodney W. Grimesinserts the new element
862afe61c15SRodney W. Grimes.Fa elm
863afe61c15SRodney W. Grimesafter the element
864afe61c15SRodney W. Grimes.Fa listelm .
865afe61c15SRodney W. Grimes.Pp
866afe61c15SRodney W. GrimesThe macro
8677658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE
8687658b0a2SJustin T. Gibbsinserts the new element
8697658b0a2SJustin T. Gibbs.Fa elm
8707658b0a2SJustin T. Gibbsbefore the element
8717658b0a2SJustin T. Gibbs.Fa listelm .
8727658b0a2SJustin T. Gibbs.Pp
8737658b0a2SJustin T. GibbsThe macro
87479ea9bc4SAlexey Zelkin.Nm LIST_NEXT
87579ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last.
87679ea9bc4SAlexey Zelkin.Pp
87779ea9bc4SAlexey ZelkinThe macro
8784170b083SEd Schouten.Nm LIST_PREV
8794170b083SEd Schoutenreturns the previous element in the list, or NULL if this is the first.
8804170b083SEd SchoutenList
8814170b083SEd Schouten.Fa head
8824170b083SEd Schoutenmust contain element
8834170b083SEd Schouten.Fa elm .
8844170b083SEd Schouten.Pp
8854170b083SEd SchoutenThe macro
886afe61c15SRodney W. Grimes.Nm LIST_REMOVE
887afe61c15SRodney W. Grimesremoves the element
888afe61c15SRodney W. Grimes.Fa elm
889afe61c15SRodney W. Grimesfrom the list.
890359295cdSMatthew D Fleming.Pp
891359295cdSMatthew D FlemingThe macro
892359295cdSMatthew D Fleming.Nm LIST_SWAP
893359295cdSMatthew D Flemingswaps the contents of
894359295cdSMatthew D Fleming.Fa head1
895359295cdSMatthew D Flemingand
896359295cdSMatthew D Fleming.Fa head2 .
897afe61c15SRodney W. Grimes.Sh LIST EXAMPLE
898afe61c15SRodney W. Grimes.Bd -literal
89903763fe0SJake BurkholderLIST_HEAD(listhead, entry) head =
90003763fe0SJake Burkholder    LIST_HEAD_INITIALIZER(head);
901afe61c15SRodney W. Grimesstruct listhead *headp;			/* List head. */
902afe61c15SRodney W. Grimesstruct entry {
903afe61c15SRodney W. Grimes	...
904afe61c15SRodney W. Grimes	LIST_ENTRY(entry) entries;	/* List. */
905afe61c15SRodney W. Grimes	...
9064250a68eSBosko Milekic} *n1, *n2, *n3, *np, *np_temp;
907afe61c15SRodney W. Grimes
908afe61c15SRodney W. GrimesLIST_INIT(&head);			/* Initialize the list. */
909afe61c15SRodney W. Grimes
910afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
911afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries);
912afe61c15SRodney W. Grimes
913afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
914afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries);
9157658b0a2SJustin T. Gibbs
9167658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
9177658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries);
9187658b0a2SJustin T. Gibbs
9197658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries);		/* Deletion. */
9207658b0a2SJustin T. Gibbsfree(n2);
921afe61c15SRodney W. Grimes					/* Forward traversal. */
92279ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries)
923afe61c15SRodney W. Grimes	np-> ...
924afe61c15SRodney W. Grimes
9254250a68eSBosko Milekic					/* Safe forward traversal. */
9264250a68eSBosko MilekicLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
9274250a68eSBosko Milekic	np->do_stuff();
9284250a68eSBosko Milekic	...
9294250a68eSBosko Milekic	LIST_REMOVE(np, entries);
9304250a68eSBosko Milekic	free(np);
9314250a68eSBosko Milekic}
9324250a68eSBosko Milekic
93379ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) {		/* List Deletion. */
93479ea9bc4SAlexey Zelkin	n1 = LIST_FIRST(&head);
9357658b0a2SJustin T. Gibbs	LIST_REMOVE(n1, entries);
9367658b0a2SJustin T. Gibbs	free(n1);
9377658b0a2SJustin T. Gibbs}
9387658b0a2SJustin T. Gibbs
93903763fe0SJake Burkholdern1 = LIST_FIRST(&head);			/* Faster List Deletion. */
9407658b0a2SJustin T. Gibbswhile (n1 != NULL) {
94179ea9bc4SAlexey Zelkin	n2 = LIST_NEXT(n1, entries);
9427658b0a2SJustin T. Gibbs	free(n1);
9437658b0a2SJustin T. Gibbs	n1 = n2;
9447658b0a2SJustin T. Gibbs}
9457658b0a2SJustin T. GibbsLIST_INIT(&head);
946afe61c15SRodney W. Grimes.Ed
947afe61c15SRodney W. Grimes.Sh TAIL QUEUES
948afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the
949afe61c15SRodney W. Grimes.Nm TAILQ_HEAD
950afe61c15SRodney W. Grimesmacro.
951afe61c15SRodney W. GrimesThis structure contains a pair of pointers,
952afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to
953afe61c15SRodney W. Grimesthe last element in the tail queue.
954afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
955afe61c15SRodney W. Grimesremoved without traversing the tail queue.
956afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element,
9574a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue,
9584a775e8fSJustin T. Gibbsor at the end of the tail queue.
959afe61c15SRodney W. GrimesA
960afe61c15SRodney W. Grimes.Fa TAILQ_HEAD
961afe61c15SRodney W. Grimesstructure is declared as follows:
962afe61c15SRodney W. Grimes.Bd -literal -offset indent
963afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head;
964afe61c15SRodney W. Grimes.Ed
9658f20a914SMike Pritchard.Pp
966afe61c15SRodney W. Grimeswhere
967afe61c15SRodney W. Grimes.Li HEADNAME
968afe61c15SRodney W. Grimesis the name of the structure to be defined, and
969afe61c15SRodney W. Grimes.Li TYPE
970afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue.
971afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as:
972afe61c15SRodney W. Grimes.Bd -literal -offset indent
973afe61c15SRodney W. Grimesstruct HEADNAME *headp;
974afe61c15SRodney W. Grimes.Ed
9758f20a914SMike Pritchard.Pp
976afe61c15SRodney W. Grimes(The names
977afe61c15SRodney W. Grimes.Li head
978afe61c15SRodney W. Grimesand
979afe61c15SRodney W. Grimes.Li headp
980afe61c15SRodney W. Grimesare user selectable.)
981afe61c15SRodney W. Grimes.Pp
982afe61c15SRodney W. GrimesThe macro
98303763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER
98403763fe0SJake Burkholderevaluates to an initializer for the tail queue
98503763fe0SJake Burkholder.Fa head .
98603763fe0SJake Burkholder.Pp
98703763fe0SJake BurkholderThe macro
988d57e28adSThomas Moestl.Nm TAILQ_CONCAT
989d57e28adSThomas Moestlconcatenates the tail queue headed by
990d57e28adSThomas Moestl.Fa head2
991d57e28adSThomas Moestlonto the end of the one headed by
992d57e28adSThomas Moestl.Fa head1
993d57e28adSThomas Moestlremoving all entries from the former.
994d57e28adSThomas Moestl.Pp
995d57e28adSThomas MoestlThe macro
996c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY
997c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue.
998c5c15c16SPoul-Henning Kamp.Pp
999c5c15c16SPoul-Henning KampThe macro
1000afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY
1001afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
1002afe61c15SRodney W. Grimesthe tail queue.
1003afe61c15SRodney W. Grimes.Pp
1004afe61c15SRodney W. GrimesThe macro
1005c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST
1006c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue
1007c5c15c16SPoul-Henning Kampis empty.
1008c5c15c16SPoul-Henning Kamp.Pp
1009c5c15c16SPoul-Henning KampThe macro
101079ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH
101179ea9bc4SAlexey Zelkintraverses the tail queue referenced by
101279ea9bc4SAlexey Zelkin.Fa head
101379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
101479ea9bc4SAlexey Zelkin.Fa var .
1015fb5293cfSRuslan Ermilov.Fa var
1016fb5293cfSRuslan Ermilovis set to
1017fb5293cfSRuslan Ermilov.Dv NULL
1018fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements.
101979ea9bc4SAlexey Zelkin.Pp
102079ea9bc4SAlexey ZelkinThe macro
1021*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM
1022*7ecb4019SLawrence Stewartbehaves identically to
1023*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH
1024*7ecb4019SLawrence Stewartwhen
1025*7ecb4019SLawrence Stewart.Fa var
1026*7ecb4019SLawrence Stewartis NULL, else it treats
1027*7ecb4019SLawrence Stewart.Fa var
1028*7ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at
1029*7ecb4019SLawrence Stewart.Fa var
1030*7ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by
1031*7ecb4019SLawrence Stewart.Fa head .
1032*7ecb4019SLawrence Stewart.Pp
1033*7ecb4019SLawrence StewartThe macro
10346c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE
10356c1d0fbfSArchie Cobbstraverses the tail queue referenced by
10366c1d0fbfSArchie Cobbs.Fa head
10376c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to
10386c1d0fbfSArchie Cobbs.Fa var .
10396c1d0fbfSArchie Cobbs.Pp
1040*7ecb4019SLawrence StewartThe macro
1041*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM
1042*7ecb4019SLawrence Stewartbehaves identically to
1043*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE
1044*7ecb4019SLawrence Stewartwhen
1045*7ecb4019SLawrence Stewart.Fa var
1046*7ecb4019SLawrence Stewartis NULL, else it treats
1047*7ecb4019SLawrence Stewart.Fa var
1048*7ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at
1049*7ecb4019SLawrence Stewart.Fa var
1050*7ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by
1051*7ecb4019SLawrence Stewart.Fa head .
1052*7ecb4019SLawrence Stewart.Pp
10532724bce2SAlexander KabaevThe macros
10542724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE
10552724bce2SAlexander Kabaevand
10562724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE
10572724bce2SAlexander Kabaevtraverse the list referenced by
10582724bce2SAlexander Kabaev.Fa head
10592724bce2SAlexander Kabaevin the forward or reverse direction respectively,
10602724bce2SAlexander Kabaevassigning each element in turn to
10612724bce2SAlexander Kabaev.Fa var .
10623b96c71fSMike PritchardHowever, unlike their unsafe counterparts,
10632724bce2SAlexander Kabaev.Nm TAILQ_FOREACH
10642724bce2SAlexander Kabaevand
10652724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE
10662724bce2SAlexander Kabaevpermit to both remove
10672724bce2SAlexander Kabaev.Fa var
10682724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the
10692724bce2SAlexander Kabaevtraversal.
10702724bce2SAlexander Kabaev.Pp
10716c1d0fbfSArchie CobbsThe macro
1072*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE
1073*7ecb4019SLawrence Stewartbehaves identically to
1074*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_SAFE
1075*7ecb4019SLawrence Stewartwhen
1076*7ecb4019SLawrence Stewart.Fa var
1077*7ecb4019SLawrence Stewartis NULL, else it treats
1078*7ecb4019SLawrence Stewart.Fa var
1079*7ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at
1080*7ecb4019SLawrence Stewart.Fa var
1081*7ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by
1082*7ecb4019SLawrence Stewart.Fa head .
1083*7ecb4019SLawrence Stewart.Pp
1084*7ecb4019SLawrence StewartThe macro
1085*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
1086*7ecb4019SLawrence Stewartbehaves identically to
1087*7ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_SAFE
1088*7ecb4019SLawrence Stewartwhen
1089*7ecb4019SLawrence Stewart.Fa var
1090*7ecb4019SLawrence Stewartis NULL, else it treats
1091*7ecb4019SLawrence Stewart.Fa var
1092*7ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at
1093*7ecb4019SLawrence Stewart.Fa var
1094*7ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by
1095*7ecb4019SLawrence Stewart.Fa head .
1096*7ecb4019SLawrence Stewart.Pp
1097*7ecb4019SLawrence StewartThe macro
1098afe61c15SRodney W. Grimes.Nm TAILQ_INIT
1099afe61c15SRodney W. Grimesinitializes the tail queue referenced by
1100afe61c15SRodney W. Grimes.Fa head .
1101afe61c15SRodney W. Grimes.Pp
1102afe61c15SRodney W. GrimesThe macro
1103afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD
1104afe61c15SRodney W. Grimesinserts the new element
1105afe61c15SRodney W. Grimes.Fa elm
1106afe61c15SRodney W. Grimesat the head of the tail queue.
1107afe61c15SRodney W. Grimes.Pp
1108afe61c15SRodney W. GrimesThe macro
1109afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL
1110afe61c15SRodney W. Grimesinserts the new element
1111afe61c15SRodney W. Grimes.Fa elm
1112afe61c15SRodney W. Grimesat the end of the tail queue.
1113afe61c15SRodney W. Grimes.Pp
1114afe61c15SRodney W. GrimesThe macro
1115afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER
1116afe61c15SRodney W. Grimesinserts the new element
1117afe61c15SRodney W. Grimes.Fa elm
1118afe61c15SRodney W. Grimesafter the element
1119afe61c15SRodney W. Grimes.Fa listelm .
1120afe61c15SRodney W. Grimes.Pp
1121afe61c15SRodney W. GrimesThe macro
11227658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE
11237658b0a2SJustin T. Gibbsinserts the new element
11247658b0a2SJustin T. Gibbs.Fa elm
11257658b0a2SJustin T. Gibbsbefore the element
11267658b0a2SJustin T. Gibbs.Fa listelm .
11277658b0a2SJustin T. Gibbs.Pp
11287658b0a2SJustin T. GibbsThe macro
1129c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST
1130c5c15c16SPoul-Henning Kampreturns the last item on the tail queue.
1131982ba1cbSKirk McKusickIf the tail queue is empty the return value is
1132982ba1cbSKirk McKusick.Dv NULL .
1133c5c15c16SPoul-Henning Kamp.Pp
1134c5c15c16SPoul-Henning KampThe macro
1135c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT
113679ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last.
113779ea9bc4SAlexey Zelkin.Pp
113879ea9bc4SAlexey ZelkinThe macro
113979ea9bc4SAlexey Zelkin.Nm TAILQ_PREV
114079ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item
114179ea9bc4SAlexey Zelkinis the first.
1142c5c15c16SPoul-Henning Kamp.Pp
1143c5c15c16SPoul-Henning KampThe macro
1144afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE
1145afe61c15SRodney W. Grimesremoves the element
1146afe61c15SRodney W. Grimes.Fa elm
1147afe61c15SRodney W. Grimesfrom the tail queue.
1148359295cdSMatthew D Fleming.Pp
1149359295cdSMatthew D FlemingThe macro
1150359295cdSMatthew D Fleming.Nm TAILQ_SWAP
1151359295cdSMatthew D Flemingswaps the contents of
1152359295cdSMatthew D Fleming.Fa head1
1153359295cdSMatthew D Flemingand
1154359295cdSMatthew D Fleming.Fa head2 .
1155afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE
1156afe61c15SRodney W. Grimes.Bd -literal
115703763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head =
115803763fe0SJake Burkholder    TAILQ_HEAD_INITIALIZER(head);
1159afe61c15SRodney W. Grimesstruct tailhead *headp;			/* Tail queue head. */
1160afe61c15SRodney W. Grimesstruct entry {
1161afe61c15SRodney W. Grimes	...
1162afe61c15SRodney W. Grimes	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
1163afe61c15SRodney W. Grimes	...
11647658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np;
1165afe61c15SRodney W. Grimes
1166afe61c15SRodney W. GrimesTAILQ_INIT(&head);			/* Initialize the queue. */
1167afe61c15SRodney W. Grimes
1168afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
1169afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries);
1170afe61c15SRodney W. Grimes
1171afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
1172afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries);
1173afe61c15SRodney W. Grimes
1174afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
1175afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries);
11767658b0a2SJustin T. Gibbs
11777658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
11783652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries);
11797658b0a2SJustin T. Gibbs
11807658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
11817658b0a2SJustin T. Gibbsfree(n2);
1182afe61c15SRodney W. Grimes					/* Forward traversal. */
118379ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries)
1184afe61c15SRodney W. Grimes	np-> ...
11852724bce2SAlexander Kabaev					/* Safe forward traversal. */
11862724bce2SAlexander KabaevTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
11872724bce2SAlexander Kabaev	np->do_stuff();
11882724bce2SAlexander Kabaev	...
11892724bce2SAlexander Kabaev	TAILQ_REMOVE(&head, np, entries);
11902724bce2SAlexander Kabaev	free(np);
11912724bce2SAlexander Kabaev}
11926c1d0fbfSArchie Cobbs					/* Reverse traversal. */
11936c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
11946c1d0fbfSArchie Cobbs	np-> ...
11957658b0a2SJustin T. Gibbs					/* TailQ Deletion. */
1196d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) {
1197c5c15c16SPoul-Henning Kamp	n1 = TAILQ_FIRST(&head);
119879ea9bc4SAlexey Zelkin	TAILQ_REMOVE(&head, n1, entries);
11997658b0a2SJustin T. Gibbs	free(n1);
12007658b0a2SJustin T. Gibbs}
12017658b0a2SJustin T. Gibbs					/* Faster TailQ Deletion. */
1202c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head);
12037658b0a2SJustin T. Gibbswhile (n1 != NULL) {
1204c5c15c16SPoul-Henning Kamp	n2 = TAILQ_NEXT(n1, entries);
12057658b0a2SJustin T. Gibbs	free(n1);
12067658b0a2SJustin T. Gibbs	n1 = n2;
12077658b0a2SJustin T. Gibbs}
12087658b0a2SJustin T. GibbsTAILQ_INIT(&head);
1209afe61c15SRodney W. Grimes.Ed
1210b9ec8f3bSSimon L. B. Nielsen.Sh SEE ALSO
1211b9ec8f3bSSimon L. B. Nielsen.Xr tree 3
1212afe61c15SRodney W. Grimes.Sh HISTORY
1213afe61c15SRodney W. GrimesThe
1214afe61c15SRodney W. Grimes.Nm queue
121521421932SMike Pritchardfunctions first appeared in
121621421932SMike Pritchard.Bx 4.4 .
1217