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*359295cdSMatthew D Fleming.Dd May 13, 2011 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 , 432724bce2SAlexander Kabaev.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 , 503d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER , 514a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD , 524a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE , 53*359295cdSMatthew D Fleming.Nm SLIST_SWAP , 54d57e28adSThomas Moestl.Nm STAILQ_CONCAT , 5579ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY , 564a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY , 5779ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST , 5879ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH , 592724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE , 604a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD , 6103763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER , 624a775e8fSJustin T. Gibbs.Nm STAILQ_INIT , 634a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER , 644a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD , 654a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL , 6679ea9bc4SAlexey Zelkin.Nm STAILQ_LAST , 6779ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT , 683d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER , 694a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD , 704a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE , 71*359295cdSMatthew D Fleming.Nm STAILQ_SWAP , 7279ea9bc4SAlexey Zelkin.Nm LIST_EMPTY , 73afe61c15SRodney W. Grimes.Nm LIST_ENTRY , 7479ea9bc4SAlexey Zelkin.Nm LIST_FIRST , 7579ea9bc4SAlexey Zelkin.Nm LIST_FOREACH , 764250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE , 77afe61c15SRodney W. Grimes.Nm LIST_HEAD , 7803763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER , 79afe61c15SRodney W. Grimes.Nm LIST_INIT , 80afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER , 817658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE , 82afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD , 8379ea9bc4SAlexey Zelkin.Nm LIST_NEXT , 84afe61c15SRodney W. Grimes.Nm LIST_REMOVE , 85*359295cdSMatthew D Fleming.Nm LIST_SWAP , 86d57e28adSThomas Moestl.Nm TAILQ_CONCAT , 87c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY , 88afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY , 89c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST , 9079ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH , 912724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE , 926c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE , 932724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE , 94afe61c15SRodney W. Grimes.Nm TAILQ_HEAD , 9503763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER , 96afe61c15SRodney W. Grimes.Nm TAILQ_INIT , 97afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER , 987658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE , 99afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD , 100afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL , 101c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST , 102c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT , 10379ea9bc4SAlexey Zelkin.Nm TAILQ_PREV , 104*359295cdSMatthew D Fleming.Nm TAILQ_REMOVE , 105*359295cdSMatthew D Fleming.Nm TAILQ_SWAP 1064a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues, 10724b85d18SPoul-Henning Kamplists and tail queues 108afe61c15SRodney W. Grimes.Sh SYNOPSIS 10932eef9aeSRuslan Ermilov.In sys/queue.h 1108f20a914SMike Pritchard.\" 111aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head" 1124a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE" 113aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head" 11479ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1152724bce2SAlexander Kabaev.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 1164a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE" 11703763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head" 1184a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head" 1194a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 1204a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 121aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 1223d98b75bSEd Schouten.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME" 1234a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1244a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 125*359295cdSMatthew D Fleming.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME" 1268f20a914SMike Pritchard.\" 127d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" 12879ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 1294a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE" 13079ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head" 13179ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1322724bce2SAlexander Kabaev.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 1334a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE" 13403763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" 1354a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head" 1364a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 1374a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 1384a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 139f36b6e4bSMaxim Sobolev.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME" 14079ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 1413d98b75bSEd Schouten.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 14202a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1434a775e8fSJustin T. Gibbs.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 144*359295cdSMatthew D Fleming.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME" 1458f20a914SMike Pritchard.\" 14679ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head" 147afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE" 14879ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head" 14979ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 1504250a68eSBosko Milekic.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 151afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE" 15203763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head" 153afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head" 1544a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 1554a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 156afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 15779ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 158afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 159*359295cdSMatthew D Fleming.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 1608f20a914SMike Pritchard.\" 161d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME" 162c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 163afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE" 164c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head" 16579ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 1662724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 1676c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 1682724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 169afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE" 17003763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head" 171afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head" 172afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 1733652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 174afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 175afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 17679ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 177c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 17879ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 179afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 180*359295cdSMatthew D Fleming.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME" 1818f20a914SMike Pritchard.\" 182afe61c15SRodney W. Grimes.Sh DESCRIPTION 183b86388c1SDima DorfmanThese macros define and operate on four types of data structures: 184b86388c1SDima Dorfmansingly-linked lists, singly-linked tail queues, lists, and tail queues. 185b86388c1SDima DorfmanAll four structures support the following functionality: 186afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 187afe61c15SRodney W. Grimes.It 188afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list. 189afe61c15SRodney W. Grimes.It 190afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list. 191afe61c15SRodney W. Grimes.It 1924a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list. 1937658b0a2SJustin T. Gibbs.It 194afe61c15SRodney W. GrimesForward traversal through the list. 195*359295cdSMatthew D Fleming.It 196*359295cdSMatthew D FlemingSwawpping the contents of two lists. 197afe61c15SRodney W. Grimes.El 198afe61c15SRodney W. Grimes.Pp 199d5fd66e1SEd MasteSingly-linked lists are the simplest of the four data structures 2004a775e8fSJustin T. Gibbsand support only the above functionality. 2014a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets 2024a775e8fSJustin T. Gibbsand few or no removals, 2034a775e8fSJustin T. Gibbsor for implementing a LIFO queue. 204ed741d4eSDavid E. O'BrienSingly-linked lists add the following functionality: 205ed741d4eSDavid E. O'Brien.Bl -enum -compact -offset indent 206ed741d4eSDavid E. O'Brien.It 207ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 208ed741d4eSDavid E. O'Brien.El 2094a775e8fSJustin T. Gibbs.Pp 2104a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality: 2114a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2124a775e8fSJustin T. Gibbs.It 2134a775e8fSJustin T. GibbsEntries can be added at the end of a list. 214d57e28adSThomas Moestl.It 215ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 216ed741d4eSDavid E. O'Brien.It 217d57e28adSThomas MoestlThey may be concatenated. 2184a775e8fSJustin T. Gibbs.El 2194a775e8fSJustin T. GibbsHowever: 2204a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2214a775e8fSJustin T. Gibbs.It 2224a775e8fSJustin T. GibbsAll list insertions must specify the head of the list. 2234a775e8fSJustin T. Gibbs.It 2244a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one. 2254a775e8fSJustin T. Gibbs.It 2264a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower 2274a775e8fSJustin T. Gibbsthan singly-linked lists. 2284a775e8fSJustin T. Gibbs.El 2294a775e8fSJustin T. Gibbs.Pp 2304a775e8fSJustin T. GibbsSingly-linked tailqs are ideal for applications with large datasets and 2314a775e8fSJustin T. Gibbsfew or no removals, 2324a775e8fSJustin T. Gibbsor for implementing a FIFO queue. 2334a775e8fSJustin T. Gibbs.Pp 234b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues) 235b86388c1SDima Dorfmanadditionally allow: 2364a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2374a775e8fSJustin T. Gibbs.It 2384a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list. 2394a775e8fSJustin T. Gibbs.It 2404a775e8fSJustin T. GibbsO(1) removal of any entry in the list. 2414a775e8fSJustin T. Gibbs.El 2424a775e8fSJustin T. GibbsHowever: 2434a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2444a775e8fSJustin T. Gibbs.It 245ad035dafSChristian BruefferEach element requires two pointers rather than one. 2464a775e8fSJustin T. Gibbs.It 2474a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about 2484a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures. 2494a775e8fSJustin T. Gibbs.El 2504a775e8fSJustin T. Gibbs.Pp 2514a775e8fSJustin T. GibbsLinked lists are the simplest of the doubly linked data structures and support 2524a775e8fSJustin T. Gibbsonly the above functionality over singly-linked lists. 253afe61c15SRodney W. Grimes.Pp 254afe61c15SRodney W. GrimesTail queues add the following functionality: 255afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 256afe61c15SRodney W. Grimes.It 257afe61c15SRodney W. GrimesEntries can be added at the end of a list. 2586c1d0fbfSArchie Cobbs.It 2596c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head. 260d57e28adSThomas Moestl.It 261d57e28adSThomas MoestlThey may be concatenated. 262afe61c15SRodney W. Grimes.El 263afe61c15SRodney W. GrimesHowever: 264afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 265afe61c15SRodney W. Grimes.It 266afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list. 267afe61c15SRodney W. Grimes.It 268afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one. 269afe61c15SRodney W. Grimes.It 270afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower 2714a775e8fSJustin T. Gibbsthan singly-linked lists. 272afe61c15SRodney W. Grimes.El 273afe61c15SRodney W. Grimes.Pp 274afe61c15SRodney W. GrimesIn the macro definitions, 275afe61c15SRodney W. Grimes.Fa TYPE 276afe61c15SRodney W. Grimesis the name of a user defined structure, 277afe61c15SRodney W. Grimesthat must contain a field of type 2784a775e8fSJustin T. Gibbs.Li SLIST_ENTRY , 2794a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY , 280afe61c15SRodney W. Grimes.Li LIST_ENTRY , 281afe61c15SRodney W. Grimesor 28224b85d18SPoul-Henning Kamp.Li TAILQ_ENTRY , 283afe61c15SRodney W. Grimesnamed 284afe61c15SRodney W. Grimes.Fa NAME . 285afe61c15SRodney W. GrimesThe argument 286afe61c15SRodney W. Grimes.Fa HEADNAME 287afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared 288afe61c15SRodney W. Grimesusing the macros 2894a775e8fSJustin T. Gibbs.Li SLIST_HEAD , 2904a775e8fSJustin T. Gibbs.Li STAILQ_HEAD , 291afe61c15SRodney W. Grimes.Li LIST_HEAD , 292afe61c15SRodney W. Grimesor 29324b85d18SPoul-Henning Kamp.Li TAILQ_HEAD . 294afe61c15SRodney W. GrimesSee the examples below for further explanation of how these 295afe61c15SRodney W. Grimesmacros are used. 2964a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS 2974a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the 2984a775e8fSJustin T. Gibbs.Nm SLIST_HEAD 2994a775e8fSJustin T. Gibbsmacro. 3004a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element 3014a775e8fSJustin T. Gibbson the list. 3024a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation 3034a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements. 3044a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or 3054a775e8fSJustin T. Gibbsat the head of the list. 3064a775e8fSJustin T. GibbsAn 3074a775e8fSJustin T. Gibbs.Fa SLIST_HEAD 3084a775e8fSJustin T. Gibbsstructure is declared as follows: 3094a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3104a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head; 3114a775e8fSJustin T. Gibbs.Ed 3128f20a914SMike Pritchard.Pp 3134a775e8fSJustin T. Gibbswhere 3144a775e8fSJustin T. Gibbs.Fa HEADNAME 3154a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 3164a775e8fSJustin T. Gibbs.Fa TYPE 3174a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list. 3184a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as: 3194a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3204a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 3214a775e8fSJustin T. Gibbs.Ed 3228f20a914SMike Pritchard.Pp 3234a775e8fSJustin T. Gibbs(The names 3244a775e8fSJustin T. Gibbs.Li head 3254a775e8fSJustin T. Gibbsand 3264a775e8fSJustin T. Gibbs.Li headp 3274a775e8fSJustin T. Gibbsare user selectable.) 3284a775e8fSJustin T. Gibbs.Pp 3294a775e8fSJustin T. GibbsThe macro 33003763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER 33103763fe0SJake Burkholderevaluates to an initializer for the list 33203763fe0SJake Burkholder.Fa head . 33303763fe0SJake Burkholder.Pp 33403763fe0SJake BurkholderThe macro 33579ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY 33679ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list. 33779ea9bc4SAlexey Zelkin.Pp 33879ea9bc4SAlexey ZelkinThe macro 3394a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY 3404a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 3414a775e8fSJustin T. Gibbsthe list. 3424a775e8fSJustin T. Gibbs.Pp 3434a775e8fSJustin T. GibbsThe macro 34479ea9bc4SAlexey Zelkin.Nm SLIST_FIRST 34579ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty. 34679ea9bc4SAlexey Zelkin.Pp 34779ea9bc4SAlexey ZelkinThe macro 34879ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH 34979ea9bc4SAlexey Zelkintraverses the list referenced by 35079ea9bc4SAlexey Zelkin.Fa head 35179ea9bc4SAlexey Zelkinin the forward direction, assigning each element in 35279ea9bc4SAlexey Zelkinturn to 35379ea9bc4SAlexey Zelkin.Fa var . 35479ea9bc4SAlexey Zelkin.Pp 35579ea9bc4SAlexey ZelkinThe macro 3562724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE 3572724bce2SAlexander Kabaevtraverses the list referenced by 3582724bce2SAlexander Kabaev.Fa head 3592724bce2SAlexander Kabaevin the forward direction, assigning each element in 3602724bce2SAlexander Kabaevturn to 3612724bce2SAlexander Kabaev.Fa var . 3622724bce2SAlexander KabaevHowever, unlike 3632724bce2SAlexander Kabaev.Fn SLIST_FOREACH 3642724bce2SAlexander Kabaevhere it is permitted to both remove 3652724bce2SAlexander Kabaev.Fa var 3662724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 3672724bce2SAlexander Kabaevtraversal. 3682724bce2SAlexander Kabaev.Pp 3692724bce2SAlexander KabaevThe macro 3704a775e8fSJustin T. Gibbs.Nm SLIST_INIT 3714a775e8fSJustin T. Gibbsinitializes the list referenced by 3724a775e8fSJustin T. Gibbs.Fa head . 3734a775e8fSJustin T. Gibbs.Pp 3744a775e8fSJustin T. GibbsThe macro 3754a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD 3764a775e8fSJustin T. Gibbsinserts the new element 3774a775e8fSJustin T. Gibbs.Fa elm 3784a775e8fSJustin T. Gibbsat the head of the list. 3794a775e8fSJustin T. Gibbs.Pp 3804a775e8fSJustin T. GibbsThe macro 3814a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER 3824a775e8fSJustin T. Gibbsinserts the new element 3834a775e8fSJustin T. Gibbs.Fa elm 3844a775e8fSJustin T. Gibbsafter the element 3854a775e8fSJustin T. Gibbs.Fa listelm . 3864a775e8fSJustin T. Gibbs.Pp 3874a775e8fSJustin T. GibbsThe macro 38879ea9bc4SAlexey Zelkin.Nm SLIST_NEXT 38979ea9bc4SAlexey Zelkinreturns the next element in the list. 39079ea9bc4SAlexey Zelkin.Pp 39179ea9bc4SAlexey ZelkinThe macro 3923d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER 3933d98b75bSEd Schoutenremoves the element after 3943d98b75bSEd Schouten.Fa elm 3953d98b75bSEd Schoutenfrom the list. Unlike 3963d98b75bSEd Schouten.Fa SLIST_REMOVE , 3973d98b75bSEd Schoutenthis macro does not traverse the entire list. 3983d98b75bSEd Schouten.Pp 3993d98b75bSEd SchoutenThe macro 4004a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD 4014a775e8fSJustin T. Gibbsremoves the element 4024a775e8fSJustin T. Gibbs.Fa elm 4034a775e8fSJustin T. Gibbsfrom the head of the list. 4044a775e8fSJustin T. GibbsFor optimum efficiency, 4054a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use 4064a775e8fSJustin T. Gibbsthis macro instead of the generic 4074a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE 4084a775e8fSJustin T. Gibbsmacro. 4094a775e8fSJustin T. Gibbs.Pp 4104a775e8fSJustin T. GibbsThe macro 4114a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE 4124a775e8fSJustin T. Gibbsremoves the element 4134a775e8fSJustin T. Gibbs.Fa elm 4144a775e8fSJustin T. Gibbsfrom the list. 415*359295cdSMatthew D Fleming.Pp 416*359295cdSMatthew D FlemingThe macro 417*359295cdSMatthew D Fleming.Nm SLIST_SWAP 418*359295cdSMatthew D Flemingswaps the contents of 419*359295cdSMatthew D Fleming.Fa head1 420*359295cdSMatthew D Flemingand 421*359295cdSMatthew D Fleming.Fa head2 . 4224a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE 4234a775e8fSJustin T. Gibbs.Bd -literal 42403763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head = 42503763fe0SJake Burkholder SLIST_HEAD_INITIALIZER(head); 4264a775e8fSJustin T. Gibbsstruct slisthead *headp; /* Singly-linked List head. */ 4274a775e8fSJustin T. Gibbsstruct entry { 4284a775e8fSJustin T. Gibbs ... 4294a775e8fSJustin T. Gibbs SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 4304a775e8fSJustin T. Gibbs ... 4314a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 4324a775e8fSJustin T. Gibbs 4334a775e8fSJustin T. GibbsSLIST_INIT(&head); /* Initialize the list. */ 4344a775e8fSJustin T. Gibbs 4354a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 4364a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries); 4374a775e8fSJustin T. Gibbs 4384a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 4394a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries); 4404a775e8fSJustin T. Gibbs 4414a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 4424a775e8fSJustin T. Gibbsfree(n2); 4434a775e8fSJustin T. Gibbs 44479ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head); 44503763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 4464a775e8fSJustin T. Gibbsfree(n3); 4474a775e8fSJustin T. Gibbs /* Forward traversal. */ 44879ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries) 4494a775e8fSJustin T. Gibbs np-> ... 4502724bce2SAlexander Kabaev /* Safe forward traversal. */ 4512724bce2SAlexander KabaevSLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 4522724bce2SAlexander Kabaev np->do_stuff(); 4532724bce2SAlexander Kabaev ... 4542724bce2SAlexander Kabaev SLIST_REMOVE(&head, np, entry, entries); 4552724bce2SAlexander Kabaev free(np); 4562724bce2SAlexander Kabaev} 4574a775e8fSJustin T. Gibbs 45879ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) { /* List Deletion. */ 45979ea9bc4SAlexey Zelkin n1 = SLIST_FIRST(&head); 4604a775e8fSJustin T. Gibbs SLIST_REMOVE_HEAD(&head, entries); 4614a775e8fSJustin T. Gibbs free(n1); 4624a775e8fSJustin T. Gibbs} 4634a775e8fSJustin T. Gibbs.Ed 4644a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES 4654a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the 4664a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD 4674a775e8fSJustin T. Gibbsmacro. 4684a775e8fSJustin T. GibbsThis structure contains a pair of pointers, 4694a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to 4704a775e8fSJustin T. Gibbsthe last element in the tail queue. 4714a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer 4724a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary 4734a775e8fSJustin T. Gibbselements. 4744a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element, 4754a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue. 4764a775e8fSJustin T. GibbsA 4774a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD 4784a775e8fSJustin T. Gibbsstructure is declared as follows: 4794a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4804a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head; 4814a775e8fSJustin T. Gibbs.Ed 4828f20a914SMike Pritchard.Pp 4834a775e8fSJustin T. Gibbswhere 4844a775e8fSJustin T. Gibbs.Li HEADNAME 4854a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 4864a775e8fSJustin T. Gibbs.Li TYPE 4874a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue. 4884a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as: 4894a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4904a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 4914a775e8fSJustin T. Gibbs.Ed 4928f20a914SMike Pritchard.Pp 4934a775e8fSJustin T. Gibbs(The names 4944a775e8fSJustin T. Gibbs.Li head 4954a775e8fSJustin T. Gibbsand 4964a775e8fSJustin T. Gibbs.Li headp 4974a775e8fSJustin T. Gibbsare user selectable.) 4984a775e8fSJustin T. Gibbs.Pp 4994a775e8fSJustin T. GibbsThe macro 50003763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER 50103763fe0SJake Burkholderevaluates to an initializer for the tail queue 50203763fe0SJake Burkholder.Fa head . 50303763fe0SJake Burkholder.Pp 50403763fe0SJake BurkholderThe macro 505d57e28adSThomas Moestl.Nm STAILQ_CONCAT 506d57e28adSThomas Moestlconcatenates the tail queue headed by 507d57e28adSThomas Moestl.Fa head2 508d57e28adSThomas Moestlonto the end of the one headed by 509d57e28adSThomas Moestl.Fa head1 510d57e28adSThomas Moestlremoving all entries from the former. 511d57e28adSThomas Moestl.Pp 512d57e28adSThomas MoestlThe macro 51379ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY 51479ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue. 51579ea9bc4SAlexey Zelkin.Pp 51679ea9bc4SAlexey ZelkinThe macro 5174a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY 5184a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 5194a775e8fSJustin T. Gibbsthe tail queue. 5204a775e8fSJustin T. Gibbs.Pp 5214a775e8fSJustin T. GibbsThe macro 52279ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST 52379ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue 52479ea9bc4SAlexey Zelkinis empty. 52579ea9bc4SAlexey Zelkin.Pp 52679ea9bc4SAlexey ZelkinThe macro 52779ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH 52879ea9bc4SAlexey Zelkintraverses the tail queue referenced by 52979ea9bc4SAlexey Zelkin.Fa head 53079ea9bc4SAlexey Zelkinin the forward direction, assigning each element 53179ea9bc4SAlexey Zelkinin turn to 53279ea9bc4SAlexey Zelkin.Fa var . 53379ea9bc4SAlexey Zelkin.Pp 53479ea9bc4SAlexey ZelkinThe macro 5352724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE 5362724bce2SAlexander Kabaevtraverses the tail queue referenced by 5372724bce2SAlexander Kabaev.Fa head 5382724bce2SAlexander Kabaevin the forward direction, assigning each element 5392724bce2SAlexander Kabaevin turn to 5402724bce2SAlexander Kabaev.Fa var . 5412724bce2SAlexander KabaevHowever, unlike 5422724bce2SAlexander Kabaev.Fn STAILQ_FOREACH 5432724bce2SAlexander Kabaevhere it is permitted to both remove 5442724bce2SAlexander Kabaev.Fa var 5452724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 5462724bce2SAlexander Kabaevtraversal. 5472724bce2SAlexander Kabaev.Pp 5482724bce2SAlexander KabaevThe macro 5494a775e8fSJustin T. Gibbs.Nm STAILQ_INIT 5504a775e8fSJustin T. Gibbsinitializes the tail queue referenced by 5514a775e8fSJustin T. Gibbs.Fa head . 5524a775e8fSJustin T. Gibbs.Pp 5534a775e8fSJustin T. GibbsThe macro 5544a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD 5554a775e8fSJustin T. Gibbsinserts the new element 5564a775e8fSJustin T. Gibbs.Fa elm 5574a775e8fSJustin T. Gibbsat the head of the tail queue. 5584a775e8fSJustin T. Gibbs.Pp 5594a775e8fSJustin T. GibbsThe macro 5604a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL 5614a775e8fSJustin T. Gibbsinserts the new element 5624a775e8fSJustin T. Gibbs.Fa elm 5634a775e8fSJustin T. Gibbsat the end of the tail queue. 5644a775e8fSJustin T. Gibbs.Pp 5654a775e8fSJustin T. GibbsThe macro 5664a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER 5674a775e8fSJustin T. Gibbsinserts the new element 5684a775e8fSJustin T. Gibbs.Fa elm 5694a775e8fSJustin T. Gibbsafter the element 5704a775e8fSJustin T. Gibbs.Fa listelm . 5714a775e8fSJustin T. Gibbs.Pp 5724a775e8fSJustin T. GibbsThe macro 57379ea9bc4SAlexey Zelkin.Nm STAILQ_LAST 57479ea9bc4SAlexey Zelkinreturns the last item on the tail queue. 575982ba1cbSKirk McKusickIf the tail queue is empty the return value is 576982ba1cbSKirk McKusick.Dv NULL . 57779ea9bc4SAlexey Zelkin.Pp 57879ea9bc4SAlexey ZelkinThe macro 57979ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT 58079ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last. 58179ea9bc4SAlexey Zelkin.Pp 58279ea9bc4SAlexey ZelkinThe macro 5833d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER 5843d98b75bSEd Schoutenremoves the element after 5853d98b75bSEd Schouten.Fa elm 5863d98b75bSEd Schoutenfrom the tail queue. Unlike 5873d98b75bSEd Schouten.Fa STAILQ_REMOVE , 5883d98b75bSEd Schoutenthis macro does not traverse the entire tail queue. 5893d98b75bSEd Schouten.Pp 5903d98b75bSEd SchoutenThe macro 5914a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD 592ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue. 5934a775e8fSJustin T. GibbsFor optimum efficiency, 5944a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should 5954a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic 5964a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE 5974a775e8fSJustin T. Gibbsmacro. 5984a775e8fSJustin T. Gibbs.Pp 5994a775e8fSJustin T. GibbsThe macro 6004a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE 6014a775e8fSJustin T. Gibbsremoves the element 6024a775e8fSJustin T. Gibbs.Fa elm 6034a775e8fSJustin T. Gibbsfrom the tail queue. 604*359295cdSMatthew D Fleming.Pp 605*359295cdSMatthew D FlemingThe macro 606*359295cdSMatthew D Fleming.Nm STAILQ_SWAP 607*359295cdSMatthew D Flemingswaps the contents of 608*359295cdSMatthew D Fleming.Fa head1 609*359295cdSMatthew D Flemingand 610*359295cdSMatthew D Fleming.Fa head2 . 6114a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE 6124a775e8fSJustin T. Gibbs.Bd -literal 61303763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head = 61403763fe0SJake Burkholder STAILQ_HEAD_INITIALIZER(head); 6154a775e8fSJustin T. Gibbsstruct stailhead *headp; /* Singly-linked tail queue head. */ 6164a775e8fSJustin T. Gibbsstruct entry { 6174a775e8fSJustin T. Gibbs ... 6184a775e8fSJustin T. Gibbs STAILQ_ENTRY(entry) entries; /* Tail queue. */ 6194a775e8fSJustin T. Gibbs ... 6204a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 6214a775e8fSJustin T. Gibbs 6224a775e8fSJustin T. GibbsSTAILQ_INIT(&head); /* Initialize the queue. */ 6234a775e8fSJustin T. Gibbs 6244a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 6254a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries); 6264a775e8fSJustin T. Gibbs 6274a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 6284a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries); 6294a775e8fSJustin T. Gibbs 6304a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 6314a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries); 6324a775e8fSJustin T. Gibbs /* Deletion. */ 6334a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries); 6344a775e8fSJustin T. Gibbsfree(n2); 63503763fe0SJake Burkholder /* Deletion from the head. */ 63679ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head); 6374a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries); 6384a775e8fSJustin T. Gibbsfree(n3); 6394a775e8fSJustin T. Gibbs /* Forward traversal. */ 64079ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries) 6414a775e8fSJustin T. Gibbs np-> ... 6422724bce2SAlexander Kabaev /* Safe forward traversal. */ 6432724bce2SAlexander KabaevSTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 6442724bce2SAlexander Kabaev np->do_stuff(); 6452724bce2SAlexander Kabaev ... 6462724bce2SAlexander Kabaev STAILQ_REMOVE(&head, np, entry, entries); 6472724bce2SAlexander Kabaev free(np); 6482724bce2SAlexander Kabaev} 6494a775e8fSJustin T. Gibbs /* TailQ Deletion. */ 65079ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) { 65103763fe0SJake Burkholder n1 = STAILQ_FIRST(&head); 652266e33fdSJohn Baldwin STAILQ_REMOVE_HEAD(&head, entries); 6534a775e8fSJustin T. Gibbs free(n1); 6544a775e8fSJustin T. Gibbs} 6554a775e8fSJustin T. Gibbs /* Faster TailQ Deletion. */ 65679ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head); 6574a775e8fSJustin T. Gibbswhile (n1 != NULL) { 65879ea9bc4SAlexey Zelkin n2 = STAILQ_NEXT(n1, entries); 6594a775e8fSJustin T. Gibbs free(n1); 6604a775e8fSJustin T. Gibbs n1 = n2; 6614a775e8fSJustin T. Gibbs} 6624a775e8fSJustin T. GibbsSTAILQ_INIT(&head); 6634a775e8fSJustin T. Gibbs.Ed 664afe61c15SRodney W. Grimes.Sh LISTS 665afe61c15SRodney W. GrimesA list is headed by a structure defined by the 666afe61c15SRodney W. Grimes.Nm LIST_HEAD 667afe61c15SRodney W. Grimesmacro. 668afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element 669afe61c15SRodney W. Grimeson the list. 670afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 671afe61c15SRodney W. Grimesremoved without traversing the list. 6724a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element, 6734a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list. 674afe61c15SRodney W. GrimesA 675afe61c15SRodney W. Grimes.Fa LIST_HEAD 676afe61c15SRodney W. Grimesstructure is declared as follows: 677afe61c15SRodney W. Grimes.Bd -literal -offset indent 678afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head; 679afe61c15SRodney W. Grimes.Ed 6808f20a914SMike Pritchard.Pp 681afe61c15SRodney W. Grimeswhere 682afe61c15SRodney W. Grimes.Fa HEADNAME 683afe61c15SRodney W. Grimesis the name of the structure to be defined, and 684afe61c15SRodney W. Grimes.Fa TYPE 685afe61c15SRodney W. Grimesis the type of the elements to be linked into the list. 686afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as: 687afe61c15SRodney W. Grimes.Bd -literal -offset indent 688afe61c15SRodney W. Grimesstruct HEADNAME *headp; 689afe61c15SRodney W. Grimes.Ed 6908f20a914SMike Pritchard.Pp 691afe61c15SRodney W. Grimes(The names 692afe61c15SRodney W. Grimes.Li head 693afe61c15SRodney W. Grimesand 694afe61c15SRodney W. Grimes.Li headp 695afe61c15SRodney W. Grimesare user selectable.) 696afe61c15SRodney W. Grimes.Pp 697afe61c15SRodney W. GrimesThe macro 69803763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER 69903763fe0SJake Burkholderevaluates to an initializer for the list 70003763fe0SJake Burkholder.Fa head . 70103763fe0SJake Burkholder.Pp 70203763fe0SJake BurkholderThe macro 70379ea9bc4SAlexey Zelkin.Nm LIST_EMPTY 704ddbb94adSTom Rhodesevaluates to true if there are no elements in the list. 70579ea9bc4SAlexey Zelkin.Pp 70679ea9bc4SAlexey ZelkinThe macro 707afe61c15SRodney W. Grimes.Nm LIST_ENTRY 708afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 709afe61c15SRodney W. Grimesthe list. 710afe61c15SRodney W. Grimes.Pp 711afe61c15SRodney W. GrimesThe macro 71279ea9bc4SAlexey Zelkin.Nm LIST_FIRST 71379ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list 71479ea9bc4SAlexey Zelkinis empty. 71579ea9bc4SAlexey Zelkin.Pp 71679ea9bc4SAlexey ZelkinThe macro 71779ea9bc4SAlexey Zelkin.Nm LIST_FOREACH 71879ea9bc4SAlexey Zelkintraverses the list referenced by 71979ea9bc4SAlexey Zelkin.Fa head 72079ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 72179ea9bc4SAlexey Zelkin.Fa var . 72279ea9bc4SAlexey Zelkin.Pp 72379ea9bc4SAlexey ZelkinThe macro 7244250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE 7254250a68eSBosko Milekictraverses the list referenced by 7264250a68eSBosko Milekic.Fa head 7274250a68eSBosko Milekicin the forward direction, assigning each element in turn to 7284250a68eSBosko Milekic.Fa var . 7294250a68eSBosko MilekicHowever, unlike 7304250a68eSBosko Milekic.Fn LIST_FOREACH 7314250a68eSBosko Milekichere it is permitted to both remove 7324250a68eSBosko Milekic.Fa var 7334250a68eSBosko Milekicas well as free it from within the loop safely without interfering with the 7344250a68eSBosko Milekictraversal. 7354250a68eSBosko Milekic.Pp 7364250a68eSBosko MilekicThe macro 737afe61c15SRodney W. Grimes.Nm LIST_INIT 738afe61c15SRodney W. Grimesinitializes the list referenced by 739afe61c15SRodney W. Grimes.Fa head . 740afe61c15SRodney W. Grimes.Pp 741afe61c15SRodney W. GrimesThe macro 742afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD 743afe61c15SRodney W. Grimesinserts the new element 744afe61c15SRodney W. Grimes.Fa elm 745afe61c15SRodney W. Grimesat the head of the list. 746afe61c15SRodney W. Grimes.Pp 747afe61c15SRodney W. GrimesThe macro 748afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER 749afe61c15SRodney W. Grimesinserts the new element 750afe61c15SRodney W. Grimes.Fa elm 751afe61c15SRodney W. Grimesafter the element 752afe61c15SRodney W. Grimes.Fa listelm . 753afe61c15SRodney W. Grimes.Pp 754afe61c15SRodney W. GrimesThe macro 7557658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE 7567658b0a2SJustin T. Gibbsinserts the new element 7577658b0a2SJustin T. Gibbs.Fa elm 7587658b0a2SJustin T. Gibbsbefore the element 7597658b0a2SJustin T. Gibbs.Fa listelm . 7607658b0a2SJustin T. Gibbs.Pp 7617658b0a2SJustin T. GibbsThe macro 76279ea9bc4SAlexey Zelkin.Nm LIST_NEXT 76379ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last. 76479ea9bc4SAlexey Zelkin.Pp 76579ea9bc4SAlexey ZelkinThe macro 766afe61c15SRodney W. Grimes.Nm LIST_REMOVE 767afe61c15SRodney W. Grimesremoves the element 768afe61c15SRodney W. Grimes.Fa elm 769afe61c15SRodney W. Grimesfrom the list. 770*359295cdSMatthew D Fleming.Pp 771*359295cdSMatthew D FlemingThe macro 772*359295cdSMatthew D Fleming.Nm LIST_SWAP 773*359295cdSMatthew D Flemingswaps the contents of 774*359295cdSMatthew D Fleming.Fa head1 775*359295cdSMatthew D Flemingand 776*359295cdSMatthew D Fleming.Fa head2 . 777afe61c15SRodney W. Grimes.Sh LIST EXAMPLE 778afe61c15SRodney W. Grimes.Bd -literal 77903763fe0SJake BurkholderLIST_HEAD(listhead, entry) head = 78003763fe0SJake Burkholder LIST_HEAD_INITIALIZER(head); 781afe61c15SRodney W. Grimesstruct listhead *headp; /* List head. */ 782afe61c15SRodney W. Grimesstruct entry { 783afe61c15SRodney W. Grimes ... 784afe61c15SRodney W. Grimes LIST_ENTRY(entry) entries; /* List. */ 785afe61c15SRodney W. Grimes ... 7864250a68eSBosko Milekic} *n1, *n2, *n3, *np, *np_temp; 787afe61c15SRodney W. Grimes 788afe61c15SRodney W. GrimesLIST_INIT(&head); /* Initialize the list. */ 789afe61c15SRodney W. Grimes 790afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 791afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries); 792afe61c15SRodney W. Grimes 793afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 794afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries); 7957658b0a2SJustin T. Gibbs 7967658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 7977658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries); 7987658b0a2SJustin T. Gibbs 7997658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries); /* Deletion. */ 8007658b0a2SJustin T. Gibbsfree(n2); 801afe61c15SRodney W. Grimes /* Forward traversal. */ 80279ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries) 803afe61c15SRodney W. Grimes np-> ... 804afe61c15SRodney W. Grimes 8054250a68eSBosko Milekic /* Safe forward traversal. */ 8064250a68eSBosko MilekicLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 8074250a68eSBosko Milekic np->do_stuff(); 8084250a68eSBosko Milekic ... 8094250a68eSBosko Milekic LIST_REMOVE(np, entries); 8104250a68eSBosko Milekic free(np); 8114250a68eSBosko Milekic} 8124250a68eSBosko Milekic 81379ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) { /* List Deletion. */ 81479ea9bc4SAlexey Zelkin n1 = LIST_FIRST(&head); 8157658b0a2SJustin T. Gibbs LIST_REMOVE(n1, entries); 8167658b0a2SJustin T. Gibbs free(n1); 8177658b0a2SJustin T. Gibbs} 8187658b0a2SJustin T. Gibbs 81903763fe0SJake Burkholdern1 = LIST_FIRST(&head); /* Faster List Deletion. */ 8207658b0a2SJustin T. Gibbswhile (n1 != NULL) { 82179ea9bc4SAlexey Zelkin n2 = LIST_NEXT(n1, entries); 8227658b0a2SJustin T. Gibbs free(n1); 8237658b0a2SJustin T. Gibbs n1 = n2; 8247658b0a2SJustin T. Gibbs} 8257658b0a2SJustin T. GibbsLIST_INIT(&head); 826afe61c15SRodney W. Grimes.Ed 827afe61c15SRodney W. Grimes.Sh TAIL QUEUES 828afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the 829afe61c15SRodney W. Grimes.Nm TAILQ_HEAD 830afe61c15SRodney W. Grimesmacro. 831afe61c15SRodney W. GrimesThis structure contains a pair of pointers, 832afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to 833afe61c15SRodney W. Grimesthe last element in the tail queue. 834afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 835afe61c15SRodney W. Grimesremoved without traversing the tail queue. 836afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element, 8374a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue, 8384a775e8fSJustin T. Gibbsor at the end of the tail queue. 839afe61c15SRodney W. GrimesA 840afe61c15SRodney W. Grimes.Fa TAILQ_HEAD 841afe61c15SRodney W. Grimesstructure is declared as follows: 842afe61c15SRodney W. Grimes.Bd -literal -offset indent 843afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head; 844afe61c15SRodney W. Grimes.Ed 8458f20a914SMike Pritchard.Pp 846afe61c15SRodney W. Grimeswhere 847afe61c15SRodney W. Grimes.Li HEADNAME 848afe61c15SRodney W. Grimesis the name of the structure to be defined, and 849afe61c15SRodney W. Grimes.Li TYPE 850afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue. 851afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as: 852afe61c15SRodney W. Grimes.Bd -literal -offset indent 853afe61c15SRodney W. Grimesstruct HEADNAME *headp; 854afe61c15SRodney W. Grimes.Ed 8558f20a914SMike Pritchard.Pp 856afe61c15SRodney W. Grimes(The names 857afe61c15SRodney W. Grimes.Li head 858afe61c15SRodney W. Grimesand 859afe61c15SRodney W. Grimes.Li headp 860afe61c15SRodney W. Grimesare user selectable.) 861afe61c15SRodney W. Grimes.Pp 862afe61c15SRodney W. GrimesThe macro 86303763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER 86403763fe0SJake Burkholderevaluates to an initializer for the tail queue 86503763fe0SJake Burkholder.Fa head . 86603763fe0SJake Burkholder.Pp 86703763fe0SJake BurkholderThe macro 868d57e28adSThomas Moestl.Nm TAILQ_CONCAT 869d57e28adSThomas Moestlconcatenates the tail queue headed by 870d57e28adSThomas Moestl.Fa head2 871d57e28adSThomas Moestlonto the end of the one headed by 872d57e28adSThomas Moestl.Fa head1 873d57e28adSThomas Moestlremoving all entries from the former. 874d57e28adSThomas Moestl.Pp 875d57e28adSThomas MoestlThe macro 876c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY 877c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue. 878c5c15c16SPoul-Henning Kamp.Pp 879c5c15c16SPoul-Henning KampThe macro 880afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY 881afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 882afe61c15SRodney W. Grimesthe tail queue. 883afe61c15SRodney W. Grimes.Pp 884afe61c15SRodney W. GrimesThe macro 885c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST 886c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue 887c5c15c16SPoul-Henning Kampis empty. 888c5c15c16SPoul-Henning Kamp.Pp 889c5c15c16SPoul-Henning KampThe macro 89079ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH 89179ea9bc4SAlexey Zelkintraverses the tail queue referenced by 89279ea9bc4SAlexey Zelkin.Fa head 89379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 89479ea9bc4SAlexey Zelkin.Fa var . 895fb5293cfSRuslan Ermilov.Fa var 896fb5293cfSRuslan Ermilovis set to 897fb5293cfSRuslan Ermilov.Dv NULL 898fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements. 89979ea9bc4SAlexey Zelkin.Pp 90079ea9bc4SAlexey ZelkinThe macro 9016c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE 9026c1d0fbfSArchie Cobbstraverses the tail queue referenced by 9036c1d0fbfSArchie Cobbs.Fa head 9046c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to 9056c1d0fbfSArchie Cobbs.Fa var . 9066c1d0fbfSArchie Cobbs.Pp 9072724bce2SAlexander KabaevThe macros 9082724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE 9092724bce2SAlexander Kabaevand 9102724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE 9112724bce2SAlexander Kabaevtraverse the list referenced by 9122724bce2SAlexander Kabaev.Fa head 9132724bce2SAlexander Kabaevin the forward or reverse direction respectively, 9142724bce2SAlexander Kabaevassigning each element in turn to 9152724bce2SAlexander Kabaev.Fa var . 9163b96c71fSMike PritchardHowever, unlike their unsafe counterparts, 9172724bce2SAlexander Kabaev.Nm TAILQ_FOREACH 9182724bce2SAlexander Kabaevand 9192724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE 9202724bce2SAlexander Kabaevpermit to both remove 9212724bce2SAlexander Kabaev.Fa var 9222724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 9232724bce2SAlexander Kabaevtraversal. 9242724bce2SAlexander Kabaev.Pp 9256c1d0fbfSArchie CobbsThe macro 926afe61c15SRodney W. Grimes.Nm TAILQ_INIT 927afe61c15SRodney W. Grimesinitializes the tail queue referenced by 928afe61c15SRodney W. Grimes.Fa head . 929afe61c15SRodney W. Grimes.Pp 930afe61c15SRodney W. GrimesThe macro 931afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD 932afe61c15SRodney W. Grimesinserts the new element 933afe61c15SRodney W. Grimes.Fa elm 934afe61c15SRodney W. Grimesat the head of the tail queue. 935afe61c15SRodney W. Grimes.Pp 936afe61c15SRodney W. GrimesThe macro 937afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL 938afe61c15SRodney W. Grimesinserts the new element 939afe61c15SRodney W. Grimes.Fa elm 940afe61c15SRodney W. Grimesat the end of the tail queue. 941afe61c15SRodney W. Grimes.Pp 942afe61c15SRodney W. GrimesThe macro 943afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER 944afe61c15SRodney W. Grimesinserts the new element 945afe61c15SRodney W. Grimes.Fa elm 946afe61c15SRodney W. Grimesafter the element 947afe61c15SRodney W. Grimes.Fa listelm . 948afe61c15SRodney W. Grimes.Pp 949afe61c15SRodney W. GrimesThe macro 9507658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE 9517658b0a2SJustin T. Gibbsinserts the new element 9527658b0a2SJustin T. Gibbs.Fa elm 9537658b0a2SJustin T. Gibbsbefore the element 9547658b0a2SJustin T. Gibbs.Fa listelm . 9557658b0a2SJustin T. Gibbs.Pp 9567658b0a2SJustin T. GibbsThe macro 957c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST 958c5c15c16SPoul-Henning Kampreturns the last item on the tail queue. 959982ba1cbSKirk McKusickIf the tail queue is empty the return value is 960982ba1cbSKirk McKusick.Dv NULL . 961c5c15c16SPoul-Henning Kamp.Pp 962c5c15c16SPoul-Henning KampThe macro 963c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT 96479ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last. 96579ea9bc4SAlexey Zelkin.Pp 96679ea9bc4SAlexey ZelkinThe macro 96779ea9bc4SAlexey Zelkin.Nm TAILQ_PREV 96879ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item 96979ea9bc4SAlexey Zelkinis the first. 970c5c15c16SPoul-Henning Kamp.Pp 971c5c15c16SPoul-Henning KampThe macro 972afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE 973afe61c15SRodney W. Grimesremoves the element 974afe61c15SRodney W. Grimes.Fa elm 975afe61c15SRodney W. Grimesfrom the tail queue. 976*359295cdSMatthew D Fleming.Pp 977*359295cdSMatthew D FlemingThe macro 978*359295cdSMatthew D Fleming.Nm TAILQ_SWAP 979*359295cdSMatthew D Flemingswaps the contents of 980*359295cdSMatthew D Fleming.Fa head1 981*359295cdSMatthew D Flemingand 982*359295cdSMatthew D Fleming.Fa head2 . 983afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE 984afe61c15SRodney W. Grimes.Bd -literal 98503763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head = 98603763fe0SJake Burkholder TAILQ_HEAD_INITIALIZER(head); 987afe61c15SRodney W. Grimesstruct tailhead *headp; /* Tail queue head. */ 988afe61c15SRodney W. Grimesstruct entry { 989afe61c15SRodney W. Grimes ... 990afe61c15SRodney W. Grimes TAILQ_ENTRY(entry) entries; /* Tail queue. */ 991afe61c15SRodney W. Grimes ... 9927658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np; 993afe61c15SRodney W. Grimes 994afe61c15SRodney W. GrimesTAILQ_INIT(&head); /* Initialize the queue. */ 995afe61c15SRodney W. Grimes 996afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 997afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries); 998afe61c15SRodney W. Grimes 999afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 1000afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries); 1001afe61c15SRodney W. Grimes 1002afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 1003afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries); 10047658b0a2SJustin T. Gibbs 10057658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 10063652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries); 10077658b0a2SJustin T. Gibbs 10087658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries); /* Deletion. */ 10097658b0a2SJustin T. Gibbsfree(n2); 1010afe61c15SRodney W. Grimes /* Forward traversal. */ 101179ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries) 1012afe61c15SRodney W. Grimes np-> ... 10132724bce2SAlexander Kabaev /* Safe forward traversal. */ 10142724bce2SAlexander KabaevTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 10152724bce2SAlexander Kabaev np->do_stuff(); 10162724bce2SAlexander Kabaev ... 10172724bce2SAlexander Kabaev TAILQ_REMOVE(&head, np, entries); 10182724bce2SAlexander Kabaev free(np); 10192724bce2SAlexander Kabaev} 10206c1d0fbfSArchie Cobbs /* Reverse traversal. */ 10216c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 10226c1d0fbfSArchie Cobbs np-> ... 10237658b0a2SJustin T. Gibbs /* TailQ Deletion. */ 1024d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) { 1025c5c15c16SPoul-Henning Kamp n1 = TAILQ_FIRST(&head); 102679ea9bc4SAlexey Zelkin TAILQ_REMOVE(&head, n1, entries); 10277658b0a2SJustin T. Gibbs free(n1); 10287658b0a2SJustin T. Gibbs} 10297658b0a2SJustin T. Gibbs /* Faster TailQ Deletion. */ 1030c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head); 10317658b0a2SJustin T. Gibbswhile (n1 != NULL) { 1032c5c15c16SPoul-Henning Kamp n2 = TAILQ_NEXT(n1, entries); 10337658b0a2SJustin T. Gibbs free(n1); 10347658b0a2SJustin T. Gibbs n1 = n2; 10357658b0a2SJustin T. Gibbs} 10367658b0a2SJustin T. GibbsTAILQ_INIT(&head); 1037afe61c15SRodney W. Grimes.Ed 1038b9ec8f3bSSimon L. B. Nielsen.Sh SEE ALSO 1039b9ec8f3bSSimon L. B. Nielsen.Xr tree 3 1040afe61c15SRodney W. Grimes.Sh HISTORY 1041afe61c15SRodney W. GrimesThe 1042afe61c15SRodney W. Grimes.Nm queue 104321421932SMike Pritchardfunctions first appeared in 104421421932SMike Pritchard.Bx 4.4 . 1045