xref: /freebsd/share/man/man3/queue.3 (revision 60fa6e9f49f10fd292d7adc247336a7198490c0d)
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.
12dda5b397SEitan Adler.\" 3. Neither the name of the University nor the names of its contributors
13afe61c15SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
14afe61c15SRodney W. Grimes.\"    without specific prior written permission.
15afe61c15SRodney W. Grimes.\"
16afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19afe61c15SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26afe61c15SRodney W. Grimes.\" SUCH DAMAGE.
27afe61c15SRodney W. Grimes.\"
28afe61c15SRodney W. Grimes.\"	@(#)queue.3	8.2 (Berkeley) 1/24/94
297f3dea24SPeter Wemm.\" $FreeBSD$
30afe61c15SRodney W. Grimes.\"
3138b622e1SHans Petter Selasky.Dd June 24, 2015
32afe61c15SRodney W. Grimes.Dt QUEUE 3
333d45e180SRuslan Ermilov.Os
34afe61c15SRodney W. Grimes.Sh NAME
3538b622e1SHans Petter Selasky.Nm SLIST_CLASS_ENTRY ,
3638b622e1SHans Petter Selasky.Nm SLIST_CLASS_HEAD ,
37aea88892SPoul-Henning Kamp.Nm SLIST_EMPTY ,
384a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY ,
39aea88892SPoul-Henning Kamp.Nm SLIST_FIRST ,
4079ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH ,
417ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM ,
427ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE ,
4338b622e1SHans Petter Selasky.Nm SLIST_FOREACH_SAFE ,
444a775e8fSJustin T. Gibbs.Nm SLIST_HEAD ,
4503763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER ,
464a775e8fSJustin T. Gibbs.Nm SLIST_INIT ,
474a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER ,
484a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD ,
49aea88892SPoul-Henning Kamp.Nm SLIST_NEXT ,
5038b622e1SHans Petter Selasky.Nm SLIST_REMOVE ,
513d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER ,
524a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD ,
53359295cdSMatthew D Fleming.Nm SLIST_SWAP ,
5438b622e1SHans Petter Selasky.Nm STAILQ_CLASS_ENTRY ,
5538b622e1SHans Petter Selasky.Nm STAILQ_CLASS_HEAD ,
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 ,
617ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM ,
627ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE ,
6338b622e1SHans Petter Selasky.Nm STAILQ_FOREACH_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 ,
7238b622e1SHans Petter Selasky.Nm STAILQ_REMOVE ,
733d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER ,
744a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD ,
75359295cdSMatthew D Fleming.Nm STAILQ_SWAP ,
7638b622e1SHans Petter Selasky.Nm LIST_CLASS_ENTRY ,
7738b622e1SHans Petter Selasky.Nm LIST_CLASS_HEAD ,
7879ea9bc4SAlexey Zelkin.Nm LIST_EMPTY ,
79afe61c15SRodney W. Grimes.Nm LIST_ENTRY ,
8079ea9bc4SAlexey Zelkin.Nm LIST_FIRST ,
8179ea9bc4SAlexey Zelkin.Nm LIST_FOREACH ,
827ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM ,
837ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM_SAFE ,
8438b622e1SHans Petter Selasky.Nm LIST_FOREACH_SAFE ,
85afe61c15SRodney W. Grimes.Nm LIST_HEAD ,
8603763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER ,
87afe61c15SRodney W. Grimes.Nm LIST_INIT ,
88afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER ,
897658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE ,
90afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD ,
9179ea9bc4SAlexey Zelkin.Nm LIST_NEXT ,
924170b083SEd Schouten.Nm LIST_PREV ,
93afe61c15SRodney W. Grimes.Nm LIST_REMOVE ,
94359295cdSMatthew D Fleming.Nm LIST_SWAP ,
9538b622e1SHans Petter Selasky.Nm TAILQ_CLASS_ENTRY ,
9638b622e1SHans Petter Selasky.Nm TAILQ_CLASS_HEAD ,
97d57e28adSThomas Moestl.Nm TAILQ_CONCAT ,
98c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY ,
99afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY ,
100c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST ,
10179ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH ,
1027ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM ,
1037ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE ,
1046c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE ,
1057ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM ,
1067ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
10738b622e1SHans Petter Selasky.Nm TAILQ_FOREACH_REVERSE_SAFE ,
10838b622e1SHans Petter Selasky.Nm TAILQ_FOREACH_SAFE ,
109afe61c15SRodney W. Grimes.Nm TAILQ_HEAD ,
11003763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER ,
111afe61c15SRodney W. Grimes.Nm TAILQ_INIT ,
112afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER ,
1137658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE ,
114afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD ,
115afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL ,
116c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST ,
117c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT ,
11879ea9bc4SAlexey Zelkin.Nm TAILQ_PREV ,
119359295cdSMatthew D Fleming.Nm TAILQ_REMOVE ,
120359295cdSMatthew D Fleming.Nm TAILQ_SWAP
1214a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues,
12224b85d18SPoul-Henning Kamplists and tail queues
123afe61c15SRodney W. Grimes.Sh SYNOPSIS
12432eef9aeSRuslan Ermilov.In sys/queue.h
1258f20a914SMike Pritchard.\"
12638b622e1SHans Petter Selasky.Fn SLIST_CLASS_ENTRY "CLASSTYPE"
12738b622e1SHans Petter Selasky.Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
128aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head"
1294a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE"
130aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head"
13179ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1327ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
1337ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
13438b622e1SHans Petter Selasky.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
1354a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE"
13603763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
1374a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head"
1384a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
1394a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
140aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
14138b622e1SHans Petter Selasky.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
1423d98b75bSEd Schouten.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
1434a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
144*60fa6e9fSBenjamin Kaduk.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE"
1458f20a914SMike Pritchard.\"
14638b622e1SHans Petter Selasky.Fn STAILQ_CLASS_ENTRY "CLASSTYPE"
14738b622e1SHans Petter Selasky.Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
148d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
14979ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head"
1504a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE"
15179ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head"
15279ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1537ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
1547ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
15538b622e1SHans Petter Selasky.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
1564a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE"
15703763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
1584a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head"
1594a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
1604a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
1614a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
162d3f649deSBrooks Davis.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
16379ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
16438b622e1SHans Petter Selasky.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
1653d98b75bSEd Schouten.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
16602a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
167*60fa6e9fSBenjamin Kaduk.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE"
1688f20a914SMike Pritchard.\"
16938b622e1SHans Petter Selasky.Fn LIST_CLASS_ENTRY "CLASSTYPE"
17038b622e1SHans Petter Selasky.Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
17179ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head"
172afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE"
17379ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head"
17479ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
1757ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
1767ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
17738b622e1SHans Petter Selasky.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
178afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE"
17903763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
180afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head"
1814a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
1824a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
183afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
18479ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
1854170b083SEd Schouten.Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME"
186afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
187*60fa6e9fSBenjamin Kaduk.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE"
1888f20a914SMike Pritchard.\"
18938b622e1SHans Petter Selasky.Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
19038b622e1SHans Petter Selasky.Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
191d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
192c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head"
193afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE"
194c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head"
19579ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
1967ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
1977ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
1986c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
1997ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
2007ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
20138b622e1SHans Petter Selasky.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
20238b622e1SHans Petter Selasky.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
203afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE"
20403763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
205afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head"
206afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
2073652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
208afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
209afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
21079ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
211c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
21279ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
213afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
214*60fa6e9fSBenjamin Kaduk.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE"
2158f20a914SMike Pritchard.\"
216afe61c15SRodney W. Grimes.Sh DESCRIPTION
21738b622e1SHans Petter SelaskyThese macros define and operate on four types of data structures which
21838b622e1SHans Petter Selaskycan be used in both C and C++ source code:
21938b622e1SHans Petter Selasky.Bl -enum -compact -offset indent
22038b622e1SHans Petter Selasky.It
22138b622e1SHans Petter SelaskyLists
22238b622e1SHans Petter Selasky.It
22338b622e1SHans Petter SelaskySingly-linked lists
22438b622e1SHans Petter Selasky.It
22538b622e1SHans Petter SelaskySingly-linked tail queues
22638b622e1SHans Petter Selasky.It
22738b622e1SHans Petter SelaskyTail queues
22838b622e1SHans Petter Selasky.El
229b86388c1SDima DorfmanAll four structures support the following functionality:
230afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
231afe61c15SRodney W. Grimes.It
232afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list.
233afe61c15SRodney W. Grimes.It
234afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list.
235afe61c15SRodney W. Grimes.It
2364a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list.
2377658b0a2SJustin T. Gibbs.It
238afe61c15SRodney W. GrimesForward traversal through the list.
239359295cdSMatthew D Fleming.It
240f9257802SRalf S. EngelschallSwapping the contents of two lists.
241afe61c15SRodney W. Grimes.El
242afe61c15SRodney W. Grimes.Pp
243d5fd66e1SEd MasteSingly-linked lists are the simplest of the four data structures
2444a775e8fSJustin T. Gibbsand support only the above functionality.
2454a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets
2464a775e8fSJustin T. Gibbsand few or no removals,
2474a775e8fSJustin T. Gibbsor for implementing a LIFO queue.
248ed741d4eSDavid E. O'BrienSingly-linked lists add the following functionality:
249ed741d4eSDavid E. O'Brien.Bl -enum -compact -offset indent
250ed741d4eSDavid E. O'Brien.It
251ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list.
252ed741d4eSDavid E. O'Brien.El
2534a775e8fSJustin T. Gibbs.Pp
2544a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality:
2554a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2564a775e8fSJustin T. Gibbs.It
2574a775e8fSJustin T. GibbsEntries can be added at the end of a list.
258d57e28adSThomas Moestl.It
259ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list.
260ed741d4eSDavid E. O'Brien.It
261d57e28adSThomas MoestlThey may be concatenated.
2624a775e8fSJustin T. Gibbs.El
2634a775e8fSJustin T. GibbsHowever:
2644a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2654a775e8fSJustin T. Gibbs.It
2664a775e8fSJustin T. GibbsAll list insertions must specify the head of the list.
2674a775e8fSJustin T. Gibbs.It
2684a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one.
2694a775e8fSJustin T. Gibbs.It
2704a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower
2714a775e8fSJustin T. Gibbsthan singly-linked lists.
2724a775e8fSJustin T. Gibbs.El
2734a775e8fSJustin T. Gibbs.Pp
274f9257802SRalf S. EngelschallSingly-linked tail queues are ideal for applications with large datasets and
2754a775e8fSJustin T. Gibbsfew or no removals,
2764a775e8fSJustin T. Gibbsor for implementing a FIFO queue.
2774a775e8fSJustin T. Gibbs.Pp
278b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues)
279b86388c1SDima Dorfmanadditionally allow:
2804a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2814a775e8fSJustin T. Gibbs.It
2824a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list.
2834a775e8fSJustin T. Gibbs.It
2844a775e8fSJustin T. GibbsO(1) removal of any entry in the list.
2854a775e8fSJustin T. Gibbs.El
2864a775e8fSJustin T. GibbsHowever:
2874a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent
2884a775e8fSJustin T. Gibbs.It
289ad035dafSChristian BruefferEach element requires two pointers rather than one.
2904a775e8fSJustin T. Gibbs.It
2914a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about
2924a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures.
2934a775e8fSJustin T. Gibbs.El
2944a775e8fSJustin T. Gibbs.Pp
2954170b083SEd SchoutenLinked lists are the simplest of the doubly linked data structures.
2964170b083SEd SchoutenThey add the following functionality over the above:
2974170b083SEd Schouten.Bl -enum -compact -offset indent
2984170b083SEd Schouten.It
2994170b083SEd SchoutenThey may be traversed backwards.
3004170b083SEd Schouten.El
3014170b083SEd SchoutenHowever:
3024170b083SEd Schouten.Bl -enum -compact -offset indent
3034170b083SEd Schouten.It
3044170b083SEd SchoutenTo traverse backwards, an entry to begin the traversal and the list in
3054170b083SEd Schoutenwhich it is contained must be specified.
3064170b083SEd Schouten.El
307afe61c15SRodney W. Grimes.Pp
308afe61c15SRodney W. GrimesTail queues add the following functionality:
309afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
310afe61c15SRodney W. Grimes.It
311afe61c15SRodney W. GrimesEntries can be added at the end of a list.
3126c1d0fbfSArchie Cobbs.It
3136c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head.
314d57e28adSThomas Moestl.It
315d57e28adSThomas MoestlThey may be concatenated.
316afe61c15SRodney W. Grimes.El
317afe61c15SRodney W. GrimesHowever:
318afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent
319afe61c15SRodney W. Grimes.It
320afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list.
321afe61c15SRodney W. Grimes.It
322afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one.
323afe61c15SRodney W. Grimes.It
324afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower
3254a775e8fSJustin T. Gibbsthan singly-linked lists.
326afe61c15SRodney W. Grimes.El
327afe61c15SRodney W. Grimes.Pp
328afe61c15SRodney W. GrimesIn the macro definitions,
329afe61c15SRodney W. Grimes.Fa TYPE
33038b622e1SHans Petter Selaskyis the name of a user defined structure.
33138b622e1SHans Petter SelaskyThe structure must contain a field called
33238b622e1SHans Petter Selasky.Fa NAME
33338b622e1SHans Petter Selaskywhich is of type
3344a775e8fSJustin T. Gibbs.Li SLIST_ENTRY ,
3354a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY ,
336afe61c15SRodney W. Grimes.Li LIST_ENTRY ,
337afe61c15SRodney W. Grimesor
33838b622e1SHans Petter Selasky.Li TAILQ_ENTRY .
33938b622e1SHans Petter SelaskyIn the macro definitions,
34038b622e1SHans Petter Selasky.Fa CLASSTYPE
34138b622e1SHans Petter Selaskyis the name of a user defined class.
34238b622e1SHans Petter SelaskyThe class must contain a field called
34338b622e1SHans Petter Selasky.Fa NAME
34438b622e1SHans Petter Selaskywhich is of type
34538b622e1SHans Petter Selasky.Li SLIST_CLASS_ENTRY ,
34638b622e1SHans Petter Selasky.Li STAILQ_CLASS_ENTRY ,
34738b622e1SHans Petter Selasky.Li LIST_CLASS_ENTRY ,
34838b622e1SHans Petter Selaskyor
34938b622e1SHans Petter Selasky.Li TAILQ_CLASS_ENTRY .
350afe61c15SRodney W. GrimesThe argument
351afe61c15SRodney W. Grimes.Fa HEADNAME
352afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared
353afe61c15SRodney W. Grimesusing the macros
3544a775e8fSJustin T. Gibbs.Li SLIST_HEAD ,
35538b622e1SHans Petter Selasky.Li SLIST_CLASS_HEAD ,
3564a775e8fSJustin T. Gibbs.Li STAILQ_HEAD ,
35738b622e1SHans Petter Selasky.Li STAILQ_CLASS_HEAD ,
358afe61c15SRodney W. Grimes.Li LIST_HEAD ,
35938b622e1SHans Petter Selasky.Li LIST_CLASS_HEAD ,
36038b622e1SHans Petter Selasky.Li TAILQ_HEAD ,
361afe61c15SRodney W. Grimesor
36238b622e1SHans Petter Selasky.Li TAILQ_CLASS_HEAD .
363afe61c15SRodney W. GrimesSee the examples below for further explanation of how these
364afe61c15SRodney W. Grimesmacros are used.
3654a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS
3664a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the
3674a775e8fSJustin T. Gibbs.Nm SLIST_HEAD
3684a775e8fSJustin T. Gibbsmacro.
3694a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element
3704a775e8fSJustin T. Gibbson the list.
3714a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation
3724a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements.
3734a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or
3744a775e8fSJustin T. Gibbsat the head of the list.
3754a775e8fSJustin T. GibbsAn
3764a775e8fSJustin T. Gibbs.Fa SLIST_HEAD
3774a775e8fSJustin T. Gibbsstructure is declared as follows:
3784a775e8fSJustin T. Gibbs.Bd -literal -offset indent
3794a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head;
3804a775e8fSJustin T. Gibbs.Ed
3818f20a914SMike Pritchard.Pp
3824a775e8fSJustin T. Gibbswhere
3834a775e8fSJustin T. Gibbs.Fa HEADNAME
3844a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
3854a775e8fSJustin T. Gibbs.Fa TYPE
3864a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list.
3874a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as:
3884a775e8fSJustin T. Gibbs.Bd -literal -offset indent
3894a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
3904a775e8fSJustin T. Gibbs.Ed
3918f20a914SMike Pritchard.Pp
3924a775e8fSJustin T. Gibbs(The names
3934a775e8fSJustin T. Gibbs.Li head
3944a775e8fSJustin T. Gibbsand
3954a775e8fSJustin T. Gibbs.Li headp
3964a775e8fSJustin T. Gibbsare user selectable.)
3974a775e8fSJustin T. Gibbs.Pp
3984a775e8fSJustin T. GibbsThe macro
39903763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER
40003763fe0SJake Burkholderevaluates to an initializer for the list
40103763fe0SJake Burkholder.Fa head .
40203763fe0SJake Burkholder.Pp
40303763fe0SJake BurkholderThe macro
40479ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY
40579ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list.
40679ea9bc4SAlexey Zelkin.Pp
40779ea9bc4SAlexey ZelkinThe macro
4084a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY
4094a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
4104a775e8fSJustin T. Gibbsthe list.
4114a775e8fSJustin T. Gibbs.Pp
4124a775e8fSJustin T. GibbsThe macro
41379ea9bc4SAlexey Zelkin.Nm SLIST_FIRST
41479ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty.
41579ea9bc4SAlexey Zelkin.Pp
41679ea9bc4SAlexey ZelkinThe macro
41779ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH
41879ea9bc4SAlexey Zelkintraverses the list referenced by
41979ea9bc4SAlexey Zelkin.Fa head
42079ea9bc4SAlexey Zelkinin the forward direction, assigning each element in
42179ea9bc4SAlexey Zelkinturn to
42279ea9bc4SAlexey Zelkin.Fa var .
42379ea9bc4SAlexey Zelkin.Pp
42479ea9bc4SAlexey ZelkinThe macro
4257ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM
4267ecb4019SLawrence Stewartbehaves identically to
4277ecb4019SLawrence Stewart.Nm SLIST_FOREACH
4287ecb4019SLawrence Stewartwhen
4297ecb4019SLawrence Stewart.Fa var
4307ecb4019SLawrence Stewartis NULL, else it treats
4317ecb4019SLawrence Stewart.Fa var
4327ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at
4337ecb4019SLawrence Stewart.Fa var
4347ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by
4357ecb4019SLawrence Stewart.Fa head .
4367ecb4019SLawrence Stewart.Pp
4377ecb4019SLawrence StewartThe macro
4382724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE
4392724bce2SAlexander Kabaevtraverses the list referenced by
4402724bce2SAlexander Kabaev.Fa head
4412724bce2SAlexander Kabaevin the forward direction, assigning each element in
4422724bce2SAlexander Kabaevturn to
4432724bce2SAlexander Kabaev.Fa var .
4442724bce2SAlexander KabaevHowever, unlike
4452724bce2SAlexander Kabaev.Fn SLIST_FOREACH
4462724bce2SAlexander Kabaevhere it is permitted to both remove
4472724bce2SAlexander Kabaev.Fa var
4482724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the
4492724bce2SAlexander Kabaevtraversal.
4502724bce2SAlexander Kabaev.Pp
4512724bce2SAlexander KabaevThe macro
4527ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE
4537ecb4019SLawrence Stewartbehaves identically to
4547ecb4019SLawrence Stewart.Nm SLIST_FOREACH_SAFE
4557ecb4019SLawrence Stewartwhen
4567ecb4019SLawrence Stewart.Fa var
4577ecb4019SLawrence Stewartis NULL, else it treats
4587ecb4019SLawrence Stewart.Fa var
4597ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at
4607ecb4019SLawrence Stewart.Fa var
4617ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by
4627ecb4019SLawrence Stewart.Fa head .
4637ecb4019SLawrence Stewart.Pp
4647ecb4019SLawrence StewartThe macro
4654a775e8fSJustin T. Gibbs.Nm SLIST_INIT
4664a775e8fSJustin T. Gibbsinitializes the list referenced by
4674a775e8fSJustin T. Gibbs.Fa head .
4684a775e8fSJustin T. Gibbs.Pp
4694a775e8fSJustin T. GibbsThe macro
4704a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD
4714a775e8fSJustin T. Gibbsinserts the new element
4724a775e8fSJustin T. Gibbs.Fa elm
4734a775e8fSJustin T. Gibbsat the head of the list.
4744a775e8fSJustin T. Gibbs.Pp
4754a775e8fSJustin T. GibbsThe macro
4764a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER
4774a775e8fSJustin T. Gibbsinserts the new element
4784a775e8fSJustin T. Gibbs.Fa elm
4794a775e8fSJustin T. Gibbsafter the element
4804a775e8fSJustin T. Gibbs.Fa listelm .
4814a775e8fSJustin T. Gibbs.Pp
4824a775e8fSJustin T. GibbsThe macro
48379ea9bc4SAlexey Zelkin.Nm SLIST_NEXT
48479ea9bc4SAlexey Zelkinreturns the next element in the list.
48579ea9bc4SAlexey Zelkin.Pp
48679ea9bc4SAlexey ZelkinThe macro
4873d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER
4883d98b75bSEd Schoutenremoves the element after
4893d98b75bSEd Schouten.Fa elm
490bc106255SEitan Adlerfrom the list.
491bc106255SEitan AdlerUnlike
4923d98b75bSEd Schouten.Fa SLIST_REMOVE ,
4933d98b75bSEd Schoutenthis macro does not traverse the entire list.
4943d98b75bSEd Schouten.Pp
4953d98b75bSEd SchoutenThe macro
4964a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD
4974a775e8fSJustin T. Gibbsremoves the element
4984a775e8fSJustin T. Gibbs.Fa elm
4994a775e8fSJustin T. Gibbsfrom the head of the list.
5004a775e8fSJustin T. GibbsFor optimum efficiency,
5014a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use
5024a775e8fSJustin T. Gibbsthis macro instead of the generic
5034a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE
5044a775e8fSJustin T. Gibbsmacro.
5054a775e8fSJustin T. Gibbs.Pp
5064a775e8fSJustin T. GibbsThe macro
5074a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE
5084a775e8fSJustin T. Gibbsremoves the element
5094a775e8fSJustin T. Gibbs.Fa elm
5104a775e8fSJustin T. Gibbsfrom the list.
511359295cdSMatthew D Fleming.Pp
512359295cdSMatthew D FlemingThe macro
513359295cdSMatthew D Fleming.Nm SLIST_SWAP
514359295cdSMatthew D Flemingswaps the contents of
515359295cdSMatthew D Fleming.Fa head1
516359295cdSMatthew D Flemingand
517359295cdSMatthew D Fleming.Fa head2 .
5184a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE
5194a775e8fSJustin T. Gibbs.Bd -literal
52003763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head =
52103763fe0SJake Burkholder    SLIST_HEAD_INITIALIZER(head);
5224a775e8fSJustin T. Gibbsstruct slisthead *headp;		/* Singly-linked List head. */
5234a775e8fSJustin T. Gibbsstruct entry {
5244a775e8fSJustin T. Gibbs	...
5254a775e8fSJustin T. Gibbs	SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
5264a775e8fSJustin T. Gibbs	...
5274a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
5284a775e8fSJustin T. Gibbs
5294a775e8fSJustin T. GibbsSLIST_INIT(&head);			/* Initialize the list. */
5304a775e8fSJustin T. Gibbs
5314a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
5324a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries);
5334a775e8fSJustin T. Gibbs
5344a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
5354a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries);
5364a775e8fSJustin T. Gibbs
5374a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
5384a775e8fSJustin T. Gibbsfree(n2);
5394a775e8fSJustin T. Gibbs
54079ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head);
54103763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries);	/* Deletion from the head. */
5424a775e8fSJustin T. Gibbsfree(n3);
5434a775e8fSJustin T. Gibbs					/* Forward traversal. */
54479ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries)
5454a775e8fSJustin T. Gibbs	np-> ...
5462724bce2SAlexander Kabaev					/* Safe forward traversal. */
5472724bce2SAlexander KabaevSLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
5482724bce2SAlexander Kabaev	np->do_stuff();
5492724bce2SAlexander Kabaev	...
5502724bce2SAlexander Kabaev	SLIST_REMOVE(&head, np, entry, entries);
5512724bce2SAlexander Kabaev	free(np);
5522724bce2SAlexander Kabaev}
5534a775e8fSJustin T. Gibbs
55479ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) {		/* List Deletion. */
55579ea9bc4SAlexey Zelkin	n1 = SLIST_FIRST(&head);
5564a775e8fSJustin T. Gibbs	SLIST_REMOVE_HEAD(&head, entries);
5574a775e8fSJustin T. Gibbs	free(n1);
5584a775e8fSJustin T. Gibbs}
5594a775e8fSJustin T. Gibbs.Ed
5604a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES
5614a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the
5624a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD
5634a775e8fSJustin T. Gibbsmacro.
5644a775e8fSJustin T. GibbsThis structure contains a pair of pointers,
5654a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to
5664a775e8fSJustin T. Gibbsthe last element in the tail queue.
5674a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer
5684a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary
5694a775e8fSJustin T. Gibbselements.
5704a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element,
5714a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue.
5724a775e8fSJustin T. GibbsA
5734a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD
5744a775e8fSJustin T. Gibbsstructure is declared as follows:
5754a775e8fSJustin T. Gibbs.Bd -literal -offset indent
5764a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head;
5774a775e8fSJustin T. Gibbs.Ed
5788f20a914SMike Pritchard.Pp
5794a775e8fSJustin T. Gibbswhere
5804a775e8fSJustin T. Gibbs.Li HEADNAME
5814a775e8fSJustin T. Gibbsis the name of the structure to be defined, and
5824a775e8fSJustin T. Gibbs.Li TYPE
5834a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue.
5844a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as:
5854a775e8fSJustin T. Gibbs.Bd -literal -offset indent
5864a775e8fSJustin T. Gibbsstruct HEADNAME *headp;
5874a775e8fSJustin T. Gibbs.Ed
5888f20a914SMike Pritchard.Pp
5894a775e8fSJustin T. Gibbs(The names
5904a775e8fSJustin T. Gibbs.Li head
5914a775e8fSJustin T. Gibbsand
5924a775e8fSJustin T. Gibbs.Li headp
5934a775e8fSJustin T. Gibbsare user selectable.)
5944a775e8fSJustin T. Gibbs.Pp
5954a775e8fSJustin T. GibbsThe macro
59603763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER
59703763fe0SJake Burkholderevaluates to an initializer for the tail queue
59803763fe0SJake Burkholder.Fa head .
59903763fe0SJake Burkholder.Pp
60003763fe0SJake BurkholderThe macro
601d57e28adSThomas Moestl.Nm STAILQ_CONCAT
602d57e28adSThomas Moestlconcatenates the tail queue headed by
603d57e28adSThomas Moestl.Fa head2
604d57e28adSThomas Moestlonto the end of the one headed by
605d57e28adSThomas Moestl.Fa head1
606d57e28adSThomas Moestlremoving all entries from the former.
607d57e28adSThomas Moestl.Pp
608d57e28adSThomas MoestlThe macro
60979ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY
61079ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue.
61179ea9bc4SAlexey Zelkin.Pp
61279ea9bc4SAlexey ZelkinThe macro
6134a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY
6144a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in
6154a775e8fSJustin T. Gibbsthe tail queue.
6164a775e8fSJustin T. Gibbs.Pp
6174a775e8fSJustin T. GibbsThe macro
61879ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST
61979ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue
62079ea9bc4SAlexey Zelkinis empty.
62179ea9bc4SAlexey Zelkin.Pp
62279ea9bc4SAlexey ZelkinThe macro
62379ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH
62479ea9bc4SAlexey Zelkintraverses the tail queue referenced by
62579ea9bc4SAlexey Zelkin.Fa head
62679ea9bc4SAlexey Zelkinin the forward direction, assigning each element
62779ea9bc4SAlexey Zelkinin turn to
62879ea9bc4SAlexey Zelkin.Fa var .
62979ea9bc4SAlexey Zelkin.Pp
63079ea9bc4SAlexey ZelkinThe macro
6317ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM
6327ecb4019SLawrence Stewartbehaves identically to
6337ecb4019SLawrence Stewart.Nm STAILQ_FOREACH
6347ecb4019SLawrence Stewartwhen
6357ecb4019SLawrence Stewart.Fa var
6367ecb4019SLawrence Stewartis NULL, else it treats
6377ecb4019SLawrence Stewart.Fa var
6387ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at
6397ecb4019SLawrence Stewart.Fa var
6407ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by
6417ecb4019SLawrence Stewart.Fa head .
6427ecb4019SLawrence Stewart.Pp
6437ecb4019SLawrence StewartThe macro
6442724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE
6452724bce2SAlexander Kabaevtraverses the tail queue referenced by
6462724bce2SAlexander Kabaev.Fa head
6472724bce2SAlexander Kabaevin the forward direction, assigning each element
6482724bce2SAlexander Kabaevin turn to
6492724bce2SAlexander Kabaev.Fa var .
6502724bce2SAlexander KabaevHowever, unlike
6512724bce2SAlexander Kabaev.Fn STAILQ_FOREACH
6522724bce2SAlexander Kabaevhere it is permitted to both remove
6532724bce2SAlexander Kabaev.Fa var
6542724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the
6552724bce2SAlexander Kabaevtraversal.
6562724bce2SAlexander Kabaev.Pp
6572724bce2SAlexander KabaevThe macro
6587ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE
6597ecb4019SLawrence Stewartbehaves identically to
6607ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_SAFE
6617ecb4019SLawrence Stewartwhen
6627ecb4019SLawrence Stewart.Fa var
6637ecb4019SLawrence Stewartis NULL, else it treats
6647ecb4019SLawrence Stewart.Fa var
6657ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at
6667ecb4019SLawrence Stewart.Fa var
6677ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by
6687ecb4019SLawrence Stewart.Fa head .
6697ecb4019SLawrence Stewart.Pp
6707ecb4019SLawrence StewartThe macro
6714a775e8fSJustin T. Gibbs.Nm STAILQ_INIT
6724a775e8fSJustin T. Gibbsinitializes the tail queue referenced by
6734a775e8fSJustin T. Gibbs.Fa head .
6744a775e8fSJustin T. Gibbs.Pp
6754a775e8fSJustin T. GibbsThe macro
6764a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD
6774a775e8fSJustin T. Gibbsinserts the new element
6784a775e8fSJustin T. Gibbs.Fa elm
6794a775e8fSJustin T. Gibbsat the head of the tail queue.
6804a775e8fSJustin T. Gibbs.Pp
6814a775e8fSJustin T. GibbsThe macro
6824a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL
6834a775e8fSJustin T. Gibbsinserts the new element
6844a775e8fSJustin T. Gibbs.Fa elm
6854a775e8fSJustin T. Gibbsat the end of the tail queue.
6864a775e8fSJustin T. Gibbs.Pp
6874a775e8fSJustin T. GibbsThe macro
6884a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER
6894a775e8fSJustin T. Gibbsinserts the new element
6904a775e8fSJustin T. Gibbs.Fa elm
6914a775e8fSJustin T. Gibbsafter the element
6924a775e8fSJustin T. Gibbs.Fa listelm .
6934a775e8fSJustin T. Gibbs.Pp
6944a775e8fSJustin T. GibbsThe macro
69579ea9bc4SAlexey Zelkin.Nm STAILQ_LAST
69679ea9bc4SAlexey Zelkinreturns the last item on the tail queue.
697982ba1cbSKirk McKusickIf the tail queue is empty the return value is
698982ba1cbSKirk McKusick.Dv NULL .
69979ea9bc4SAlexey Zelkin.Pp
70079ea9bc4SAlexey ZelkinThe macro
70179ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT
70279ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last.
70379ea9bc4SAlexey Zelkin.Pp
70479ea9bc4SAlexey ZelkinThe macro
7053d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER
7063d98b75bSEd Schoutenremoves the element after
7073d98b75bSEd Schouten.Fa elm
708bc106255SEitan Adlerfrom the tail queue.
709bc106255SEitan AdlerUnlike
7103d98b75bSEd Schouten.Fa STAILQ_REMOVE ,
7113d98b75bSEd Schoutenthis macro does not traverse the entire tail queue.
7123d98b75bSEd Schouten.Pp
7133d98b75bSEd SchoutenThe macro
7144a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD
715ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue.
7164a775e8fSJustin T. GibbsFor optimum efficiency,
7174a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should
7184a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic
7194a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE
7204a775e8fSJustin T. Gibbsmacro.
7214a775e8fSJustin T. Gibbs.Pp
7224a775e8fSJustin T. GibbsThe macro
7234a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE
7244a775e8fSJustin T. Gibbsremoves the element
7254a775e8fSJustin T. Gibbs.Fa elm
7264a775e8fSJustin T. Gibbsfrom the tail queue.
727359295cdSMatthew D Fleming.Pp
728359295cdSMatthew D FlemingThe macro
729359295cdSMatthew D Fleming.Nm STAILQ_SWAP
730359295cdSMatthew D Flemingswaps the contents of
731359295cdSMatthew D Fleming.Fa head1
732359295cdSMatthew D Flemingand
733359295cdSMatthew D Fleming.Fa head2 .
7344a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
7354a775e8fSJustin T. Gibbs.Bd -literal
73603763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head =
73703763fe0SJake Burkholder    STAILQ_HEAD_INITIALIZER(head);
7384a775e8fSJustin T. Gibbsstruct stailhead *headp;		/* Singly-linked tail queue head. */
7394a775e8fSJustin T. Gibbsstruct entry {
7404a775e8fSJustin T. Gibbs	...
7414a775e8fSJustin T. Gibbs	STAILQ_ENTRY(entry) entries;	/* Tail queue. */
7424a775e8fSJustin T. Gibbs	...
7434a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np;
7444a775e8fSJustin T. Gibbs
7454a775e8fSJustin T. GibbsSTAILQ_INIT(&head);			/* Initialize the queue. */
7464a775e8fSJustin T. Gibbs
7474a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
7484a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries);
7494a775e8fSJustin T. Gibbs
7504a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
7514a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries);
7524a775e8fSJustin T. Gibbs
7534a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry));	/* Insert after. */
7544a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries);
7554a775e8fSJustin T. Gibbs					/* Deletion. */
7564a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries);
7574a775e8fSJustin T. Gibbsfree(n2);
75803763fe0SJake Burkholder					/* Deletion from the head. */
75979ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head);
7604a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries);
7614a775e8fSJustin T. Gibbsfree(n3);
7624a775e8fSJustin T. Gibbs					/* Forward traversal. */
76379ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries)
7644a775e8fSJustin T. Gibbs	np-> ...
7652724bce2SAlexander Kabaev					/* Safe forward traversal. */
7662724bce2SAlexander KabaevSTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
7672724bce2SAlexander Kabaev	np->do_stuff();
7682724bce2SAlexander Kabaev	...
7692724bce2SAlexander Kabaev	STAILQ_REMOVE(&head, np, entry, entries);
7702724bce2SAlexander Kabaev	free(np);
7712724bce2SAlexander Kabaev}
7724a775e8fSJustin T. Gibbs					/* TailQ Deletion. */
77379ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) {
77403763fe0SJake Burkholder	n1 = STAILQ_FIRST(&head);
775266e33fdSJohn Baldwin	STAILQ_REMOVE_HEAD(&head, entries);
7764a775e8fSJustin T. Gibbs	free(n1);
7774a775e8fSJustin T. Gibbs}
7784a775e8fSJustin T. Gibbs					/* Faster TailQ Deletion. */
77979ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head);
7804a775e8fSJustin T. Gibbswhile (n1 != NULL) {
78179ea9bc4SAlexey Zelkin	n2 = STAILQ_NEXT(n1, entries);
7824a775e8fSJustin T. Gibbs	free(n1);
7834a775e8fSJustin T. Gibbs	n1 = n2;
7844a775e8fSJustin T. Gibbs}
7854a775e8fSJustin T. GibbsSTAILQ_INIT(&head);
7864a775e8fSJustin T. Gibbs.Ed
787afe61c15SRodney W. Grimes.Sh LISTS
788afe61c15SRodney W. GrimesA list is headed by a structure defined by the
789afe61c15SRodney W. Grimes.Nm LIST_HEAD
790afe61c15SRodney W. Grimesmacro.
791afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element
792afe61c15SRodney W. Grimeson the list.
793afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
794afe61c15SRodney W. Grimesremoved without traversing the list.
7954a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element,
7964a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list.
797afe61c15SRodney W. GrimesA
798afe61c15SRodney W. Grimes.Fa LIST_HEAD
799afe61c15SRodney W. Grimesstructure is declared as follows:
800afe61c15SRodney W. Grimes.Bd -literal -offset indent
801afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head;
802afe61c15SRodney W. Grimes.Ed
8038f20a914SMike Pritchard.Pp
804afe61c15SRodney W. Grimeswhere
805afe61c15SRodney W. Grimes.Fa HEADNAME
806afe61c15SRodney W. Grimesis the name of the structure to be defined, and
807afe61c15SRodney W. Grimes.Fa TYPE
808afe61c15SRodney W. Grimesis the type of the elements to be linked into the list.
809afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as:
810afe61c15SRodney W. Grimes.Bd -literal -offset indent
811afe61c15SRodney W. Grimesstruct HEADNAME *headp;
812afe61c15SRodney W. Grimes.Ed
8138f20a914SMike Pritchard.Pp
814afe61c15SRodney W. Grimes(The names
815afe61c15SRodney W. Grimes.Li head
816afe61c15SRodney W. Grimesand
817afe61c15SRodney W. Grimes.Li headp
818afe61c15SRodney W. Grimesare user selectable.)
819afe61c15SRodney W. Grimes.Pp
820afe61c15SRodney W. GrimesThe macro
82103763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER
82203763fe0SJake Burkholderevaluates to an initializer for the list
82303763fe0SJake Burkholder.Fa head .
82403763fe0SJake Burkholder.Pp
82503763fe0SJake BurkholderThe macro
82679ea9bc4SAlexey Zelkin.Nm LIST_EMPTY
827ddbb94adSTom Rhodesevaluates to true if there are no elements in the list.
82879ea9bc4SAlexey Zelkin.Pp
82979ea9bc4SAlexey ZelkinThe macro
830afe61c15SRodney W. Grimes.Nm LIST_ENTRY
831afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
832afe61c15SRodney W. Grimesthe list.
833afe61c15SRodney W. Grimes.Pp
834afe61c15SRodney W. GrimesThe macro
83579ea9bc4SAlexey Zelkin.Nm LIST_FIRST
83679ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list
83779ea9bc4SAlexey Zelkinis empty.
83879ea9bc4SAlexey Zelkin.Pp
83979ea9bc4SAlexey ZelkinThe macro
84079ea9bc4SAlexey Zelkin.Nm LIST_FOREACH
84179ea9bc4SAlexey Zelkintraverses the list referenced by
84279ea9bc4SAlexey Zelkin.Fa head
84379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
84479ea9bc4SAlexey Zelkin.Fa var .
84579ea9bc4SAlexey Zelkin.Pp
84679ea9bc4SAlexey ZelkinThe macro
8477ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM
8487ecb4019SLawrence Stewartbehaves identically to
8497ecb4019SLawrence Stewart.Nm LIST_FOREACH
8507ecb4019SLawrence Stewartwhen
8517ecb4019SLawrence Stewart.Fa var
8527ecb4019SLawrence Stewartis NULL, else it treats
8537ecb4019SLawrence Stewart.Fa var
8547ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at
8557ecb4019SLawrence Stewart.Fa var
8567ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by
8577ecb4019SLawrence Stewart.Fa head .
8587ecb4019SLawrence Stewart.Pp
8597ecb4019SLawrence StewartThe macro
8604250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE
8614250a68eSBosko Milekictraverses the list referenced by
8624250a68eSBosko Milekic.Fa head
8634250a68eSBosko Milekicin the forward direction, assigning each element in turn to
8644250a68eSBosko Milekic.Fa var .
8654250a68eSBosko MilekicHowever, unlike
8664250a68eSBosko Milekic.Fn LIST_FOREACH
8674250a68eSBosko Milekichere it is permitted to both remove
8684250a68eSBosko Milekic.Fa var
8694250a68eSBosko Milekicas well as free it from within the loop safely without interfering with the
8704250a68eSBosko Milekictraversal.
8714250a68eSBosko Milekic.Pp
8724250a68eSBosko MilekicThe macro
8737ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM_SAFE
8747ecb4019SLawrence Stewartbehaves identically to
8757ecb4019SLawrence Stewart.Nm LIST_FOREACH_SAFE
8767ecb4019SLawrence Stewartwhen
8777ecb4019SLawrence Stewart.Fa var
8787ecb4019SLawrence Stewartis NULL, else it treats
8797ecb4019SLawrence Stewart.Fa var
8807ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at
8817ecb4019SLawrence Stewart.Fa var
8827ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by
8837ecb4019SLawrence Stewart.Fa head .
8847ecb4019SLawrence Stewart.Pp
8857ecb4019SLawrence StewartThe macro
886afe61c15SRodney W. Grimes.Nm LIST_INIT
887afe61c15SRodney W. Grimesinitializes the list referenced by
888afe61c15SRodney W. Grimes.Fa head .
889afe61c15SRodney W. Grimes.Pp
890afe61c15SRodney W. GrimesThe macro
891afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD
892afe61c15SRodney W. Grimesinserts the new element
893afe61c15SRodney W. Grimes.Fa elm
894afe61c15SRodney W. Grimesat the head of the list.
895afe61c15SRodney W. Grimes.Pp
896afe61c15SRodney W. GrimesThe macro
897afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER
898afe61c15SRodney W. Grimesinserts the new element
899afe61c15SRodney W. Grimes.Fa elm
900afe61c15SRodney W. Grimesafter the element
901afe61c15SRodney W. Grimes.Fa listelm .
902afe61c15SRodney W. Grimes.Pp
903afe61c15SRodney W. GrimesThe macro
9047658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE
9057658b0a2SJustin T. Gibbsinserts the new element
9067658b0a2SJustin T. Gibbs.Fa elm
9077658b0a2SJustin T. Gibbsbefore the element
9087658b0a2SJustin T. Gibbs.Fa listelm .
9097658b0a2SJustin T. Gibbs.Pp
9107658b0a2SJustin T. GibbsThe macro
91179ea9bc4SAlexey Zelkin.Nm LIST_NEXT
91279ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last.
91379ea9bc4SAlexey Zelkin.Pp
91479ea9bc4SAlexey ZelkinThe macro
9154170b083SEd Schouten.Nm LIST_PREV
9164170b083SEd Schoutenreturns the previous element in the list, or NULL if this is the first.
9174170b083SEd SchoutenList
9184170b083SEd Schouten.Fa head
9194170b083SEd Schoutenmust contain element
9204170b083SEd Schouten.Fa elm .
9214170b083SEd Schouten.Pp
9224170b083SEd SchoutenThe macro
923afe61c15SRodney W. Grimes.Nm LIST_REMOVE
924afe61c15SRodney W. Grimesremoves the element
925afe61c15SRodney W. Grimes.Fa elm
926afe61c15SRodney W. Grimesfrom the list.
927359295cdSMatthew D Fleming.Pp
928359295cdSMatthew D FlemingThe macro
929359295cdSMatthew D Fleming.Nm LIST_SWAP
930359295cdSMatthew D Flemingswaps the contents of
931359295cdSMatthew D Fleming.Fa head1
932359295cdSMatthew D Flemingand
933359295cdSMatthew D Fleming.Fa head2 .
934afe61c15SRodney W. Grimes.Sh LIST EXAMPLE
935afe61c15SRodney W. Grimes.Bd -literal
93603763fe0SJake BurkholderLIST_HEAD(listhead, entry) head =
93703763fe0SJake Burkholder    LIST_HEAD_INITIALIZER(head);
938afe61c15SRodney W. Grimesstruct listhead *headp;			/* List head. */
939afe61c15SRodney W. Grimesstruct entry {
940afe61c15SRodney W. Grimes	...
941afe61c15SRodney W. Grimes	LIST_ENTRY(entry) entries;	/* List. */
942afe61c15SRodney W. Grimes	...
9434250a68eSBosko Milekic} *n1, *n2, *n3, *np, *np_temp;
944afe61c15SRodney W. Grimes
945afe61c15SRodney W. GrimesLIST_INIT(&head);			/* Initialize the list. */
946afe61c15SRodney W. Grimes
947afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
948afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries);
949afe61c15SRodney W. Grimes
950afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
951afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries);
9527658b0a2SJustin T. Gibbs
9537658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
9547658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries);
9557658b0a2SJustin T. Gibbs
9567658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries);		/* Deletion. */
9577658b0a2SJustin T. Gibbsfree(n2);
958afe61c15SRodney W. Grimes					/* Forward traversal. */
95979ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries)
960afe61c15SRodney W. Grimes	np-> ...
961afe61c15SRodney W. Grimes
9624250a68eSBosko Milekic					/* Safe forward traversal. */
9634250a68eSBosko MilekicLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
9644250a68eSBosko Milekic	np->do_stuff();
9654250a68eSBosko Milekic	...
9664250a68eSBosko Milekic	LIST_REMOVE(np, entries);
9674250a68eSBosko Milekic	free(np);
9684250a68eSBosko Milekic}
9694250a68eSBosko Milekic
97079ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) {		/* List Deletion. */
97179ea9bc4SAlexey Zelkin	n1 = LIST_FIRST(&head);
9727658b0a2SJustin T. Gibbs	LIST_REMOVE(n1, entries);
9737658b0a2SJustin T. Gibbs	free(n1);
9747658b0a2SJustin T. Gibbs}
9757658b0a2SJustin T. Gibbs
97603763fe0SJake Burkholdern1 = LIST_FIRST(&head);			/* Faster List Deletion. */
9777658b0a2SJustin T. Gibbswhile (n1 != NULL) {
97879ea9bc4SAlexey Zelkin	n2 = LIST_NEXT(n1, entries);
9797658b0a2SJustin T. Gibbs	free(n1);
9807658b0a2SJustin T. Gibbs	n1 = n2;
9817658b0a2SJustin T. Gibbs}
9827658b0a2SJustin T. GibbsLIST_INIT(&head);
983afe61c15SRodney W. Grimes.Ed
984afe61c15SRodney W. Grimes.Sh TAIL QUEUES
985afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the
986afe61c15SRodney W. Grimes.Nm TAILQ_HEAD
987afe61c15SRodney W. Grimesmacro.
988afe61c15SRodney W. GrimesThis structure contains a pair of pointers,
989afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to
990afe61c15SRodney W. Grimesthe last element in the tail queue.
991afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be
992afe61c15SRodney W. Grimesremoved without traversing the tail queue.
993afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element,
9944a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue,
9954a775e8fSJustin T. Gibbsor at the end of the tail queue.
996afe61c15SRodney W. GrimesA
997afe61c15SRodney W. Grimes.Fa TAILQ_HEAD
998afe61c15SRodney W. Grimesstructure is declared as follows:
999afe61c15SRodney W. Grimes.Bd -literal -offset indent
1000afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head;
1001afe61c15SRodney W. Grimes.Ed
10028f20a914SMike Pritchard.Pp
1003afe61c15SRodney W. Grimeswhere
1004afe61c15SRodney W. Grimes.Li HEADNAME
1005afe61c15SRodney W. Grimesis the name of the structure to be defined, and
1006afe61c15SRodney W. Grimes.Li TYPE
1007afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue.
1008afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as:
1009afe61c15SRodney W. Grimes.Bd -literal -offset indent
1010afe61c15SRodney W. Grimesstruct HEADNAME *headp;
1011afe61c15SRodney W. Grimes.Ed
10128f20a914SMike Pritchard.Pp
1013afe61c15SRodney W. Grimes(The names
1014afe61c15SRodney W. Grimes.Li head
1015afe61c15SRodney W. Grimesand
1016afe61c15SRodney W. Grimes.Li headp
1017afe61c15SRodney W. Grimesare user selectable.)
1018afe61c15SRodney W. Grimes.Pp
1019afe61c15SRodney W. GrimesThe macro
102003763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER
102103763fe0SJake Burkholderevaluates to an initializer for the tail queue
102203763fe0SJake Burkholder.Fa head .
102303763fe0SJake Burkholder.Pp
102403763fe0SJake BurkholderThe macro
1025d57e28adSThomas Moestl.Nm TAILQ_CONCAT
1026d57e28adSThomas Moestlconcatenates the tail queue headed by
1027d57e28adSThomas Moestl.Fa head2
1028d57e28adSThomas Moestlonto the end of the one headed by
1029d57e28adSThomas Moestl.Fa head1
1030d57e28adSThomas Moestlremoving all entries from the former.
1031d57e28adSThomas Moestl.Pp
1032d57e28adSThomas MoestlThe macro
1033c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY
1034c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue.
1035c5c15c16SPoul-Henning Kamp.Pp
1036c5c15c16SPoul-Henning KampThe macro
1037afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY
1038afe61c15SRodney W. Grimesdeclares a structure that connects the elements in
1039afe61c15SRodney W. Grimesthe tail queue.
1040afe61c15SRodney W. Grimes.Pp
1041afe61c15SRodney W. GrimesThe macro
1042c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST
1043c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue
1044c5c15c16SPoul-Henning Kampis empty.
1045c5c15c16SPoul-Henning Kamp.Pp
1046c5c15c16SPoul-Henning KampThe macro
104779ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH
104879ea9bc4SAlexey Zelkintraverses the tail queue referenced by
104979ea9bc4SAlexey Zelkin.Fa head
105079ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to
105179ea9bc4SAlexey Zelkin.Fa var .
1052fb5293cfSRuslan Ermilov.Fa var
1053fb5293cfSRuslan Ermilovis set to
1054fb5293cfSRuslan Ermilov.Dv NULL
1055fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements.
105679ea9bc4SAlexey Zelkin.Pp
105779ea9bc4SAlexey ZelkinThe macro
10587ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM
10597ecb4019SLawrence Stewartbehaves identically to
10607ecb4019SLawrence Stewart.Nm TAILQ_FOREACH
10617ecb4019SLawrence Stewartwhen
10627ecb4019SLawrence Stewart.Fa var
10637ecb4019SLawrence Stewartis NULL, else it treats
10647ecb4019SLawrence Stewart.Fa var
10657ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at
10667ecb4019SLawrence Stewart.Fa var
10677ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by
10687ecb4019SLawrence Stewart.Fa head .
10697ecb4019SLawrence Stewart.Pp
10707ecb4019SLawrence StewartThe macro
10716c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE
10726c1d0fbfSArchie Cobbstraverses the tail queue referenced by
10736c1d0fbfSArchie Cobbs.Fa head
10746c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to
10756c1d0fbfSArchie Cobbs.Fa var .
10766c1d0fbfSArchie Cobbs.Pp
10777ecb4019SLawrence StewartThe macro
10787ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM
10797ecb4019SLawrence Stewartbehaves identically to
10807ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE
10817ecb4019SLawrence Stewartwhen
10827ecb4019SLawrence Stewart.Fa var
10837ecb4019SLawrence Stewartis NULL, else it treats
10847ecb4019SLawrence Stewart.Fa var
10857ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at
10867ecb4019SLawrence Stewart.Fa var
10877ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by
10887ecb4019SLawrence Stewart.Fa head .
10897ecb4019SLawrence Stewart.Pp
10902724bce2SAlexander KabaevThe macros
10912724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE
10922724bce2SAlexander Kabaevand
10932724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE
10942724bce2SAlexander Kabaevtraverse the list referenced by
10952724bce2SAlexander Kabaev.Fa head
10962724bce2SAlexander Kabaevin the forward or reverse direction respectively,
10972724bce2SAlexander Kabaevassigning each element in turn to
10982724bce2SAlexander Kabaev.Fa var .
10993b96c71fSMike PritchardHowever, unlike their unsafe counterparts,
11002724bce2SAlexander Kabaev.Nm TAILQ_FOREACH
11012724bce2SAlexander Kabaevand
11022724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE
11032724bce2SAlexander Kabaevpermit to both remove
11042724bce2SAlexander Kabaev.Fa var
11052724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the
11062724bce2SAlexander Kabaevtraversal.
11072724bce2SAlexander Kabaev.Pp
11086c1d0fbfSArchie CobbsThe macro
11097ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE
11107ecb4019SLawrence Stewartbehaves identically to
11117ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_SAFE
11127ecb4019SLawrence Stewartwhen
11137ecb4019SLawrence Stewart.Fa var
11147ecb4019SLawrence Stewartis NULL, else it treats
11157ecb4019SLawrence Stewart.Fa var
11167ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at
11177ecb4019SLawrence Stewart.Fa var
11187ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by
11197ecb4019SLawrence Stewart.Fa head .
11207ecb4019SLawrence Stewart.Pp
11217ecb4019SLawrence StewartThe macro
11227ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE
11237ecb4019SLawrence Stewartbehaves identically to
11247ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_SAFE
11257ecb4019SLawrence Stewartwhen
11267ecb4019SLawrence Stewart.Fa var
11277ecb4019SLawrence Stewartis NULL, else it treats
11287ecb4019SLawrence Stewart.Fa var
11297ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at
11307ecb4019SLawrence Stewart.Fa var
11317ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by
11327ecb4019SLawrence Stewart.Fa head .
11337ecb4019SLawrence Stewart.Pp
11347ecb4019SLawrence StewartThe macro
1135afe61c15SRodney W. Grimes.Nm TAILQ_INIT
1136afe61c15SRodney W. Grimesinitializes the tail queue referenced by
1137afe61c15SRodney W. Grimes.Fa head .
1138afe61c15SRodney W. Grimes.Pp
1139afe61c15SRodney W. GrimesThe macro
1140afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD
1141afe61c15SRodney W. Grimesinserts the new element
1142afe61c15SRodney W. Grimes.Fa elm
1143afe61c15SRodney W. Grimesat the head of the tail queue.
1144afe61c15SRodney W. Grimes.Pp
1145afe61c15SRodney W. GrimesThe macro
1146afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL
1147afe61c15SRodney W. Grimesinserts the new element
1148afe61c15SRodney W. Grimes.Fa elm
1149afe61c15SRodney W. Grimesat the end of the tail queue.
1150afe61c15SRodney W. Grimes.Pp
1151afe61c15SRodney W. GrimesThe macro
1152afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER
1153afe61c15SRodney W. Grimesinserts the new element
1154afe61c15SRodney W. Grimes.Fa elm
1155afe61c15SRodney W. Grimesafter the element
1156afe61c15SRodney W. Grimes.Fa listelm .
1157afe61c15SRodney W. Grimes.Pp
1158afe61c15SRodney W. GrimesThe macro
11597658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE
11607658b0a2SJustin T. Gibbsinserts the new element
11617658b0a2SJustin T. Gibbs.Fa elm
11627658b0a2SJustin T. Gibbsbefore the element
11637658b0a2SJustin T. Gibbs.Fa listelm .
11647658b0a2SJustin T. Gibbs.Pp
11657658b0a2SJustin T. GibbsThe macro
1166c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST
1167c5c15c16SPoul-Henning Kampreturns the last item on the tail queue.
1168982ba1cbSKirk McKusickIf the tail queue is empty the return value is
1169982ba1cbSKirk McKusick.Dv NULL .
1170c5c15c16SPoul-Henning Kamp.Pp
1171c5c15c16SPoul-Henning KampThe macro
1172c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT
117379ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last.
117479ea9bc4SAlexey Zelkin.Pp
117579ea9bc4SAlexey ZelkinThe macro
117679ea9bc4SAlexey Zelkin.Nm TAILQ_PREV
117779ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item
117879ea9bc4SAlexey Zelkinis the first.
1179c5c15c16SPoul-Henning Kamp.Pp
1180c5c15c16SPoul-Henning KampThe macro
1181afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE
1182afe61c15SRodney W. Grimesremoves the element
1183afe61c15SRodney W. Grimes.Fa elm
1184afe61c15SRodney W. Grimesfrom the tail queue.
1185359295cdSMatthew D Fleming.Pp
1186359295cdSMatthew D FlemingThe macro
1187359295cdSMatthew D Fleming.Nm TAILQ_SWAP
1188359295cdSMatthew D Flemingswaps the contents of
1189359295cdSMatthew D Fleming.Fa head1
1190359295cdSMatthew D Flemingand
1191359295cdSMatthew D Fleming.Fa head2 .
1192afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE
1193afe61c15SRodney W. Grimes.Bd -literal
119403763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head =
119503763fe0SJake Burkholder    TAILQ_HEAD_INITIALIZER(head);
1196afe61c15SRodney W. Grimesstruct tailhead *headp;			/* Tail queue head. */
1197afe61c15SRodney W. Grimesstruct entry {
1198afe61c15SRodney W. Grimes	...
1199afe61c15SRodney W. Grimes	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
1200afe61c15SRodney W. Grimes	...
12017658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np;
1202afe61c15SRodney W. Grimes
1203afe61c15SRodney W. GrimesTAILQ_INIT(&head);			/* Initialize the queue. */
1204afe61c15SRodney W. Grimes
1205afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
1206afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries);
1207afe61c15SRodney W. Grimes
1208afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
1209afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries);
1210afe61c15SRodney W. Grimes
1211afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry));	/* Insert after. */
1212afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries);
12137658b0a2SJustin T. Gibbs
12147658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry));	/* Insert before. */
12153652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries);
12167658b0a2SJustin T. Gibbs
12177658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
12187658b0a2SJustin T. Gibbsfree(n2);
1219afe61c15SRodney W. Grimes					/* Forward traversal. */
122079ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries)
1221afe61c15SRodney W. Grimes	np-> ...
12222724bce2SAlexander Kabaev					/* Safe forward traversal. */
12232724bce2SAlexander KabaevTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
12242724bce2SAlexander Kabaev	np->do_stuff();
12252724bce2SAlexander Kabaev	...
12262724bce2SAlexander Kabaev	TAILQ_REMOVE(&head, np, entries);
12272724bce2SAlexander Kabaev	free(np);
12282724bce2SAlexander Kabaev}
12296c1d0fbfSArchie Cobbs					/* Reverse traversal. */
12306c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
12316c1d0fbfSArchie Cobbs	np-> ...
12327658b0a2SJustin T. Gibbs					/* TailQ Deletion. */
1233d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) {
1234c5c15c16SPoul-Henning Kamp	n1 = TAILQ_FIRST(&head);
123579ea9bc4SAlexey Zelkin	TAILQ_REMOVE(&head, n1, entries);
12367658b0a2SJustin T. Gibbs	free(n1);
12377658b0a2SJustin T. Gibbs}
12387658b0a2SJustin T. Gibbs					/* Faster TailQ Deletion. */
1239c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head);
12407658b0a2SJustin T. Gibbswhile (n1 != NULL) {
1241c5c15c16SPoul-Henning Kamp	n2 = TAILQ_NEXT(n1, entries);
12427658b0a2SJustin T. Gibbs	free(n1);
12437658b0a2SJustin T. Gibbs	n1 = n2;
12447658b0a2SJustin T. Gibbs}
12457658b0a2SJustin T. GibbsTAILQ_INIT(&head);
1246afe61c15SRodney W. Grimes.Ed
1247b9ec8f3bSSimon L. B. Nielsen.Sh SEE ALSO
1248b9ec8f3bSSimon L. B. Nielsen.Xr tree 3
1249afe61c15SRodney W. Grimes.Sh HISTORY
1250afe61c15SRodney W. GrimesThe
1251afe61c15SRodney W. Grimes.Nm queue
125221421932SMike Pritchardfunctions first appeared in
125321421932SMike Pritchard.Bx 4.4 .
1254