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.\" 354170b083SEd Schouten.Dd Sep 12, 2012 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 , 53359295cdSMatthew 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 , 71359295cdSMatthew 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 , 844170b083SEd Schouten.Nm LIST_PREV , 85afe61c15SRodney W. Grimes.Nm LIST_REMOVE , 86359295cdSMatthew D Fleming.Nm LIST_SWAP , 87d57e28adSThomas Moestl.Nm TAILQ_CONCAT , 88c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY , 89afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY , 90c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST , 9179ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH , 922724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE , 936c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE , 942724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE , 95afe61c15SRodney W. Grimes.Nm TAILQ_HEAD , 9603763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER , 97afe61c15SRodney W. Grimes.Nm TAILQ_INIT , 98afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER , 997658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE , 100afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD , 101afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL , 102c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST , 103c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT , 10479ea9bc4SAlexey Zelkin.Nm TAILQ_PREV , 105359295cdSMatthew D Fleming.Nm TAILQ_REMOVE , 106359295cdSMatthew D Fleming.Nm TAILQ_SWAP 1074a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues, 10824b85d18SPoul-Henning Kamplists and tail queues 109afe61c15SRodney W. Grimes.Sh SYNOPSIS 11032eef9aeSRuslan Ermilov.In sys/queue.h 1118f20a914SMike Pritchard.\" 112aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head" 1134a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE" 114aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head" 11579ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1162724bce2SAlexander Kabaev.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 1174a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE" 11803763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head" 1194a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head" 1204a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 1214a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 122aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 1233d98b75bSEd Schouten.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME" 1244a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1254a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 126359295cdSMatthew D Fleming.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME" 1278f20a914SMike Pritchard.\" 128d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" 12979ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 1304a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE" 13179ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head" 13279ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1332724bce2SAlexander Kabaev.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 1344a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE" 13503763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" 1364a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head" 1374a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 1384a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 1394a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 140f36b6e4bSMaxim Sobolev.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME" 14179ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 1423d98b75bSEd Schouten.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 14302a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1444a775e8fSJustin T. Gibbs.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 145359295cdSMatthew D Fleming.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME" 1468f20a914SMike Pritchard.\" 14779ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head" 148afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE" 14979ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head" 15079ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 1514250a68eSBosko Milekic.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 152afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE" 15303763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head" 154afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head" 1554a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 1564a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 157afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 15879ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 1594170b083SEd Schouten.Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME" 160afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 161359295cdSMatthew D Fleming.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 1628f20a914SMike Pritchard.\" 163d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME" 164c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 165afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE" 166c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head" 16779ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 1682724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 1696c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 1702724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 171afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE" 17203763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head" 173afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head" 174afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 1753652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 176afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 177afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 17879ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 179c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 18079ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 181afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 182359295cdSMatthew D Fleming.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME" 1838f20a914SMike Pritchard.\" 184afe61c15SRodney W. Grimes.Sh DESCRIPTION 185b86388c1SDima DorfmanThese macros define and operate on four types of data structures: 186b86388c1SDima Dorfmansingly-linked lists, singly-linked tail queues, lists, and tail queues. 187b86388c1SDima DorfmanAll four structures support the following functionality: 188afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 189afe61c15SRodney W. Grimes.It 190afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list. 191afe61c15SRodney W. Grimes.It 192afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list. 193afe61c15SRodney W. Grimes.It 1944a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list. 1957658b0a2SJustin T. Gibbs.It 196afe61c15SRodney W. GrimesForward traversal through the list. 197359295cdSMatthew D Fleming.It 198f9257802SRalf S. EngelschallSwapping the contents of two lists. 199afe61c15SRodney W. Grimes.El 200afe61c15SRodney W. Grimes.Pp 201d5fd66e1SEd MasteSingly-linked lists are the simplest of the four data structures 2024a775e8fSJustin T. Gibbsand support only the above functionality. 2034a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets 2044a775e8fSJustin T. Gibbsand few or no removals, 2054a775e8fSJustin T. Gibbsor for implementing a LIFO queue. 206ed741d4eSDavid E. O'BrienSingly-linked lists add the following functionality: 207ed741d4eSDavid E. O'Brien.Bl -enum -compact -offset indent 208ed741d4eSDavid E. O'Brien.It 209ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 210ed741d4eSDavid E. O'Brien.El 2114a775e8fSJustin T. Gibbs.Pp 2124a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality: 2134a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2144a775e8fSJustin T. Gibbs.It 2154a775e8fSJustin T. GibbsEntries can be added at the end of a list. 216d57e28adSThomas Moestl.It 217ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 218ed741d4eSDavid E. O'Brien.It 219d57e28adSThomas MoestlThey may be concatenated. 2204a775e8fSJustin T. Gibbs.El 2214a775e8fSJustin T. GibbsHowever: 2224a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2234a775e8fSJustin T. Gibbs.It 2244a775e8fSJustin T. GibbsAll list insertions must specify the head of the list. 2254a775e8fSJustin T. Gibbs.It 2264a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one. 2274a775e8fSJustin T. Gibbs.It 2284a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower 2294a775e8fSJustin T. Gibbsthan singly-linked lists. 2304a775e8fSJustin T. Gibbs.El 2314a775e8fSJustin T. Gibbs.Pp 232f9257802SRalf S. EngelschallSingly-linked tail queues are ideal for applications with large datasets and 2334a775e8fSJustin T. Gibbsfew or no removals, 2344a775e8fSJustin T. Gibbsor for implementing a FIFO queue. 2354a775e8fSJustin T. Gibbs.Pp 236b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues) 237b86388c1SDima Dorfmanadditionally allow: 2384a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2394a775e8fSJustin T. Gibbs.It 2404a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list. 2414a775e8fSJustin T. Gibbs.It 2424a775e8fSJustin T. GibbsO(1) removal of any entry in the list. 2434a775e8fSJustin T. Gibbs.El 2444a775e8fSJustin T. GibbsHowever: 2454a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2464a775e8fSJustin T. Gibbs.It 247ad035dafSChristian BruefferEach element requires two pointers rather than one. 2484a775e8fSJustin T. Gibbs.It 2494a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about 2504a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures. 2514a775e8fSJustin T. Gibbs.El 2524a775e8fSJustin T. Gibbs.Pp 2534170b083SEd SchoutenLinked lists are the simplest of the doubly linked data structures. 2544170b083SEd SchoutenThey add the following functionality over the above: 2554170b083SEd Schouten.Bl -enum -compact -offset indent 2564170b083SEd Schouten.It 2574170b083SEd SchoutenThey may be traversed backwards. 2584170b083SEd Schouten.El 2594170b083SEd SchoutenHowever: 2604170b083SEd Schouten.Bl -enum -compact -offset indent 2614170b083SEd Schouten.It 2624170b083SEd SchoutenTo traverse backwards, an entry to begin the traversal and the list in 2634170b083SEd Schoutenwhich it is contained must be specified. 2644170b083SEd Schouten.El 265afe61c15SRodney W. Grimes.Pp 266afe61c15SRodney W. GrimesTail queues add the following functionality: 267afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 268afe61c15SRodney W. Grimes.It 269afe61c15SRodney W. GrimesEntries can be added at the end of a list. 2706c1d0fbfSArchie Cobbs.It 2716c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head. 272d57e28adSThomas Moestl.It 273d57e28adSThomas MoestlThey may be concatenated. 274afe61c15SRodney W. Grimes.El 275afe61c15SRodney W. GrimesHowever: 276afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 277afe61c15SRodney W. Grimes.It 278afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list. 279afe61c15SRodney W. Grimes.It 280afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one. 281afe61c15SRodney W. Grimes.It 282afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower 2834a775e8fSJustin T. Gibbsthan singly-linked lists. 284afe61c15SRodney W. Grimes.El 285afe61c15SRodney W. Grimes.Pp 286afe61c15SRodney W. GrimesIn the macro definitions, 287afe61c15SRodney W. Grimes.Fa TYPE 288afe61c15SRodney W. Grimesis the name of a user defined structure, 289afe61c15SRodney W. Grimesthat must contain a field of type 2904a775e8fSJustin T. Gibbs.Li SLIST_ENTRY , 2914a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY , 292afe61c15SRodney W. Grimes.Li LIST_ENTRY , 293afe61c15SRodney W. Grimesor 29424b85d18SPoul-Henning Kamp.Li TAILQ_ENTRY , 295afe61c15SRodney W. Grimesnamed 296afe61c15SRodney W. Grimes.Fa NAME . 297afe61c15SRodney W. GrimesThe argument 298afe61c15SRodney W. Grimes.Fa HEADNAME 299afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared 300afe61c15SRodney W. Grimesusing the macros 3014a775e8fSJustin T. Gibbs.Li SLIST_HEAD , 3024a775e8fSJustin T. Gibbs.Li STAILQ_HEAD , 303afe61c15SRodney W. Grimes.Li LIST_HEAD , 304afe61c15SRodney W. Grimesor 30524b85d18SPoul-Henning Kamp.Li TAILQ_HEAD . 306afe61c15SRodney W. GrimesSee the examples below for further explanation of how these 307afe61c15SRodney W. Grimesmacros are used. 3084a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS 3094a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the 3104a775e8fSJustin T. Gibbs.Nm SLIST_HEAD 3114a775e8fSJustin T. Gibbsmacro. 3124a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element 3134a775e8fSJustin T. Gibbson the list. 3144a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation 3154a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements. 3164a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or 3174a775e8fSJustin T. Gibbsat the head of the list. 3184a775e8fSJustin T. GibbsAn 3194a775e8fSJustin T. Gibbs.Fa SLIST_HEAD 3204a775e8fSJustin T. Gibbsstructure is declared as follows: 3214a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3224a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head; 3234a775e8fSJustin T. Gibbs.Ed 3248f20a914SMike Pritchard.Pp 3254a775e8fSJustin T. Gibbswhere 3264a775e8fSJustin T. Gibbs.Fa HEADNAME 3274a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 3284a775e8fSJustin T. Gibbs.Fa TYPE 3294a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list. 3304a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as: 3314a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3324a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 3334a775e8fSJustin T. Gibbs.Ed 3348f20a914SMike Pritchard.Pp 3354a775e8fSJustin T. Gibbs(The names 3364a775e8fSJustin T. Gibbs.Li head 3374a775e8fSJustin T. Gibbsand 3384a775e8fSJustin T. Gibbs.Li headp 3394a775e8fSJustin T. Gibbsare user selectable.) 3404a775e8fSJustin T. Gibbs.Pp 3414a775e8fSJustin T. GibbsThe macro 34203763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER 34303763fe0SJake Burkholderevaluates to an initializer for the list 34403763fe0SJake Burkholder.Fa head . 34503763fe0SJake Burkholder.Pp 34603763fe0SJake BurkholderThe macro 34779ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY 34879ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list. 34979ea9bc4SAlexey Zelkin.Pp 35079ea9bc4SAlexey ZelkinThe macro 3514a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY 3524a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 3534a775e8fSJustin T. Gibbsthe list. 3544a775e8fSJustin T. Gibbs.Pp 3554a775e8fSJustin T. GibbsThe macro 35679ea9bc4SAlexey Zelkin.Nm SLIST_FIRST 35779ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty. 35879ea9bc4SAlexey Zelkin.Pp 35979ea9bc4SAlexey ZelkinThe macro 36079ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH 36179ea9bc4SAlexey Zelkintraverses the list referenced by 36279ea9bc4SAlexey Zelkin.Fa head 36379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in 36479ea9bc4SAlexey Zelkinturn to 36579ea9bc4SAlexey Zelkin.Fa var . 36679ea9bc4SAlexey Zelkin.Pp 36779ea9bc4SAlexey ZelkinThe macro 3682724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE 3692724bce2SAlexander Kabaevtraverses the list referenced by 3702724bce2SAlexander Kabaev.Fa head 3712724bce2SAlexander Kabaevin the forward direction, assigning each element in 3722724bce2SAlexander Kabaevturn to 3732724bce2SAlexander Kabaev.Fa var . 3742724bce2SAlexander KabaevHowever, unlike 3752724bce2SAlexander Kabaev.Fn SLIST_FOREACH 3762724bce2SAlexander Kabaevhere it is permitted to both remove 3772724bce2SAlexander Kabaev.Fa var 3782724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 3792724bce2SAlexander Kabaevtraversal. 3802724bce2SAlexander Kabaev.Pp 3812724bce2SAlexander KabaevThe macro 3824a775e8fSJustin T. Gibbs.Nm SLIST_INIT 3834a775e8fSJustin T. Gibbsinitializes the list referenced by 3844a775e8fSJustin T. Gibbs.Fa head . 3854a775e8fSJustin T. Gibbs.Pp 3864a775e8fSJustin T. GibbsThe macro 3874a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD 3884a775e8fSJustin T. Gibbsinserts the new element 3894a775e8fSJustin T. Gibbs.Fa elm 3904a775e8fSJustin T. Gibbsat the head of the list. 3914a775e8fSJustin T. Gibbs.Pp 3924a775e8fSJustin T. GibbsThe macro 3934a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER 3944a775e8fSJustin T. Gibbsinserts the new element 3954a775e8fSJustin T. Gibbs.Fa elm 3964a775e8fSJustin T. Gibbsafter the element 3974a775e8fSJustin T. Gibbs.Fa listelm . 3984a775e8fSJustin T. Gibbs.Pp 3994a775e8fSJustin T. GibbsThe macro 40079ea9bc4SAlexey Zelkin.Nm SLIST_NEXT 40179ea9bc4SAlexey Zelkinreturns the next element in the list. 40279ea9bc4SAlexey Zelkin.Pp 40379ea9bc4SAlexey ZelkinThe macro 4043d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER 4053d98b75bSEd Schoutenremoves the element after 4063d98b75bSEd Schouten.Fa elm 407*bc106255SEitan Adlerfrom the list. 408*bc106255SEitan AdlerUnlike 4093d98b75bSEd Schouten.Fa SLIST_REMOVE , 4103d98b75bSEd Schoutenthis macro does not traverse the entire list. 4113d98b75bSEd Schouten.Pp 4123d98b75bSEd SchoutenThe macro 4134a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD 4144a775e8fSJustin T. Gibbsremoves the element 4154a775e8fSJustin T. Gibbs.Fa elm 4164a775e8fSJustin T. Gibbsfrom the head of the list. 4174a775e8fSJustin T. GibbsFor optimum efficiency, 4184a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use 4194a775e8fSJustin T. Gibbsthis macro instead of the generic 4204a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE 4214a775e8fSJustin T. Gibbsmacro. 4224a775e8fSJustin T. Gibbs.Pp 4234a775e8fSJustin T. GibbsThe macro 4244a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE 4254a775e8fSJustin T. Gibbsremoves the element 4264a775e8fSJustin T. Gibbs.Fa elm 4274a775e8fSJustin T. Gibbsfrom the list. 428359295cdSMatthew D Fleming.Pp 429359295cdSMatthew D FlemingThe macro 430359295cdSMatthew D Fleming.Nm SLIST_SWAP 431359295cdSMatthew D Flemingswaps the contents of 432359295cdSMatthew D Fleming.Fa head1 433359295cdSMatthew D Flemingand 434359295cdSMatthew D Fleming.Fa head2 . 4354a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE 4364a775e8fSJustin T. Gibbs.Bd -literal 43703763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head = 43803763fe0SJake Burkholder SLIST_HEAD_INITIALIZER(head); 4394a775e8fSJustin T. Gibbsstruct slisthead *headp; /* Singly-linked List head. */ 4404a775e8fSJustin T. Gibbsstruct entry { 4414a775e8fSJustin T. Gibbs ... 4424a775e8fSJustin T. Gibbs SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 4434a775e8fSJustin T. Gibbs ... 4444a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 4454a775e8fSJustin T. Gibbs 4464a775e8fSJustin T. GibbsSLIST_INIT(&head); /* Initialize the list. */ 4474a775e8fSJustin T. Gibbs 4484a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 4494a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries); 4504a775e8fSJustin T. Gibbs 4514a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 4524a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries); 4534a775e8fSJustin T. Gibbs 4544a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 4554a775e8fSJustin T. Gibbsfree(n2); 4564a775e8fSJustin T. Gibbs 45779ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head); 45803763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 4594a775e8fSJustin T. Gibbsfree(n3); 4604a775e8fSJustin T. Gibbs /* Forward traversal. */ 46179ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries) 4624a775e8fSJustin T. Gibbs np-> ... 4632724bce2SAlexander Kabaev /* Safe forward traversal. */ 4642724bce2SAlexander KabaevSLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 4652724bce2SAlexander Kabaev np->do_stuff(); 4662724bce2SAlexander Kabaev ... 4672724bce2SAlexander Kabaev SLIST_REMOVE(&head, np, entry, entries); 4682724bce2SAlexander Kabaev free(np); 4692724bce2SAlexander Kabaev} 4704a775e8fSJustin T. Gibbs 47179ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) { /* List Deletion. */ 47279ea9bc4SAlexey Zelkin n1 = SLIST_FIRST(&head); 4734a775e8fSJustin T. Gibbs SLIST_REMOVE_HEAD(&head, entries); 4744a775e8fSJustin T. Gibbs free(n1); 4754a775e8fSJustin T. Gibbs} 4764a775e8fSJustin T. Gibbs.Ed 4774a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES 4784a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the 4794a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD 4804a775e8fSJustin T. Gibbsmacro. 4814a775e8fSJustin T. GibbsThis structure contains a pair of pointers, 4824a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to 4834a775e8fSJustin T. Gibbsthe last element in the tail queue. 4844a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer 4854a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary 4864a775e8fSJustin T. Gibbselements. 4874a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element, 4884a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue. 4894a775e8fSJustin T. GibbsA 4904a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD 4914a775e8fSJustin T. Gibbsstructure is declared as follows: 4924a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4934a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head; 4944a775e8fSJustin T. Gibbs.Ed 4958f20a914SMike Pritchard.Pp 4964a775e8fSJustin T. Gibbswhere 4974a775e8fSJustin T. Gibbs.Li HEADNAME 4984a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 4994a775e8fSJustin T. Gibbs.Li TYPE 5004a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue. 5014a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as: 5024a775e8fSJustin T. Gibbs.Bd -literal -offset indent 5034a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 5044a775e8fSJustin T. Gibbs.Ed 5058f20a914SMike Pritchard.Pp 5064a775e8fSJustin T. Gibbs(The names 5074a775e8fSJustin T. Gibbs.Li head 5084a775e8fSJustin T. Gibbsand 5094a775e8fSJustin T. Gibbs.Li headp 5104a775e8fSJustin T. Gibbsare user selectable.) 5114a775e8fSJustin T. Gibbs.Pp 5124a775e8fSJustin T. GibbsThe macro 51303763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER 51403763fe0SJake Burkholderevaluates to an initializer for the tail queue 51503763fe0SJake Burkholder.Fa head . 51603763fe0SJake Burkholder.Pp 51703763fe0SJake BurkholderThe macro 518d57e28adSThomas Moestl.Nm STAILQ_CONCAT 519d57e28adSThomas Moestlconcatenates the tail queue headed by 520d57e28adSThomas Moestl.Fa head2 521d57e28adSThomas Moestlonto the end of the one headed by 522d57e28adSThomas Moestl.Fa head1 523d57e28adSThomas Moestlremoving all entries from the former. 524d57e28adSThomas Moestl.Pp 525d57e28adSThomas MoestlThe macro 52679ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY 52779ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue. 52879ea9bc4SAlexey Zelkin.Pp 52979ea9bc4SAlexey ZelkinThe macro 5304a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY 5314a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 5324a775e8fSJustin T. Gibbsthe tail queue. 5334a775e8fSJustin T. Gibbs.Pp 5344a775e8fSJustin T. GibbsThe macro 53579ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST 53679ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue 53779ea9bc4SAlexey Zelkinis empty. 53879ea9bc4SAlexey Zelkin.Pp 53979ea9bc4SAlexey ZelkinThe macro 54079ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH 54179ea9bc4SAlexey Zelkintraverses the tail queue referenced by 54279ea9bc4SAlexey Zelkin.Fa head 54379ea9bc4SAlexey Zelkinin the forward direction, assigning each element 54479ea9bc4SAlexey Zelkinin turn to 54579ea9bc4SAlexey Zelkin.Fa var . 54679ea9bc4SAlexey Zelkin.Pp 54779ea9bc4SAlexey ZelkinThe macro 5482724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE 5492724bce2SAlexander Kabaevtraverses the tail queue referenced by 5502724bce2SAlexander Kabaev.Fa head 5512724bce2SAlexander Kabaevin the forward direction, assigning each element 5522724bce2SAlexander Kabaevin turn to 5532724bce2SAlexander Kabaev.Fa var . 5542724bce2SAlexander KabaevHowever, unlike 5552724bce2SAlexander Kabaev.Fn STAILQ_FOREACH 5562724bce2SAlexander Kabaevhere it is permitted to both remove 5572724bce2SAlexander Kabaev.Fa var 5582724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 5592724bce2SAlexander Kabaevtraversal. 5602724bce2SAlexander Kabaev.Pp 5612724bce2SAlexander KabaevThe macro 5624a775e8fSJustin T. Gibbs.Nm STAILQ_INIT 5634a775e8fSJustin T. Gibbsinitializes the tail queue referenced by 5644a775e8fSJustin T. Gibbs.Fa head . 5654a775e8fSJustin T. Gibbs.Pp 5664a775e8fSJustin T. GibbsThe macro 5674a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD 5684a775e8fSJustin T. Gibbsinserts the new element 5694a775e8fSJustin T. Gibbs.Fa elm 5704a775e8fSJustin T. Gibbsat the head of the tail queue. 5714a775e8fSJustin T. Gibbs.Pp 5724a775e8fSJustin T. GibbsThe macro 5734a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL 5744a775e8fSJustin T. Gibbsinserts the new element 5754a775e8fSJustin T. Gibbs.Fa elm 5764a775e8fSJustin T. Gibbsat the end of the tail queue. 5774a775e8fSJustin T. Gibbs.Pp 5784a775e8fSJustin T. GibbsThe macro 5794a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER 5804a775e8fSJustin T. Gibbsinserts the new element 5814a775e8fSJustin T. Gibbs.Fa elm 5824a775e8fSJustin T. Gibbsafter the element 5834a775e8fSJustin T. Gibbs.Fa listelm . 5844a775e8fSJustin T. Gibbs.Pp 5854a775e8fSJustin T. GibbsThe macro 58679ea9bc4SAlexey Zelkin.Nm STAILQ_LAST 58779ea9bc4SAlexey Zelkinreturns the last item on the tail queue. 588982ba1cbSKirk McKusickIf the tail queue is empty the return value is 589982ba1cbSKirk McKusick.Dv NULL . 59079ea9bc4SAlexey Zelkin.Pp 59179ea9bc4SAlexey ZelkinThe macro 59279ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT 59379ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last. 59479ea9bc4SAlexey Zelkin.Pp 59579ea9bc4SAlexey ZelkinThe macro 5963d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER 5973d98b75bSEd Schoutenremoves the element after 5983d98b75bSEd Schouten.Fa elm 599*bc106255SEitan Adlerfrom the tail queue. 600*bc106255SEitan AdlerUnlike 6013d98b75bSEd Schouten.Fa STAILQ_REMOVE , 6023d98b75bSEd Schoutenthis macro does not traverse the entire tail queue. 6033d98b75bSEd Schouten.Pp 6043d98b75bSEd SchoutenThe macro 6054a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD 606ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue. 6074a775e8fSJustin T. GibbsFor optimum efficiency, 6084a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should 6094a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic 6104a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE 6114a775e8fSJustin T. Gibbsmacro. 6124a775e8fSJustin T. Gibbs.Pp 6134a775e8fSJustin T. GibbsThe macro 6144a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE 6154a775e8fSJustin T. Gibbsremoves the element 6164a775e8fSJustin T. Gibbs.Fa elm 6174a775e8fSJustin T. Gibbsfrom the tail queue. 618359295cdSMatthew D Fleming.Pp 619359295cdSMatthew D FlemingThe macro 620359295cdSMatthew D Fleming.Nm STAILQ_SWAP 621359295cdSMatthew D Flemingswaps the contents of 622359295cdSMatthew D Fleming.Fa head1 623359295cdSMatthew D Flemingand 624359295cdSMatthew D Fleming.Fa head2 . 6254a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE 6264a775e8fSJustin T. Gibbs.Bd -literal 62703763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head = 62803763fe0SJake Burkholder STAILQ_HEAD_INITIALIZER(head); 6294a775e8fSJustin T. Gibbsstruct stailhead *headp; /* Singly-linked tail queue head. */ 6304a775e8fSJustin T. Gibbsstruct entry { 6314a775e8fSJustin T. Gibbs ... 6324a775e8fSJustin T. Gibbs STAILQ_ENTRY(entry) entries; /* Tail queue. */ 6334a775e8fSJustin T. Gibbs ... 6344a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 6354a775e8fSJustin T. Gibbs 6364a775e8fSJustin T. GibbsSTAILQ_INIT(&head); /* Initialize the queue. */ 6374a775e8fSJustin T. Gibbs 6384a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 6394a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries); 6404a775e8fSJustin T. Gibbs 6414a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 6424a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries); 6434a775e8fSJustin T. Gibbs 6444a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 6454a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries); 6464a775e8fSJustin T. Gibbs /* Deletion. */ 6474a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries); 6484a775e8fSJustin T. Gibbsfree(n2); 64903763fe0SJake Burkholder /* Deletion from the head. */ 65079ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head); 6514a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries); 6524a775e8fSJustin T. Gibbsfree(n3); 6534a775e8fSJustin T. Gibbs /* Forward traversal. */ 65479ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries) 6554a775e8fSJustin T. Gibbs np-> ... 6562724bce2SAlexander Kabaev /* Safe forward traversal. */ 6572724bce2SAlexander KabaevSTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 6582724bce2SAlexander Kabaev np->do_stuff(); 6592724bce2SAlexander Kabaev ... 6602724bce2SAlexander Kabaev STAILQ_REMOVE(&head, np, entry, entries); 6612724bce2SAlexander Kabaev free(np); 6622724bce2SAlexander Kabaev} 6634a775e8fSJustin T. Gibbs /* TailQ Deletion. */ 66479ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) { 66503763fe0SJake Burkholder n1 = STAILQ_FIRST(&head); 666266e33fdSJohn Baldwin STAILQ_REMOVE_HEAD(&head, entries); 6674a775e8fSJustin T. Gibbs free(n1); 6684a775e8fSJustin T. Gibbs} 6694a775e8fSJustin T. Gibbs /* Faster TailQ Deletion. */ 67079ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head); 6714a775e8fSJustin T. Gibbswhile (n1 != NULL) { 67279ea9bc4SAlexey Zelkin n2 = STAILQ_NEXT(n1, entries); 6734a775e8fSJustin T. Gibbs free(n1); 6744a775e8fSJustin T. Gibbs n1 = n2; 6754a775e8fSJustin T. Gibbs} 6764a775e8fSJustin T. GibbsSTAILQ_INIT(&head); 6774a775e8fSJustin T. Gibbs.Ed 678afe61c15SRodney W. Grimes.Sh LISTS 679afe61c15SRodney W. GrimesA list is headed by a structure defined by the 680afe61c15SRodney W. Grimes.Nm LIST_HEAD 681afe61c15SRodney W. Grimesmacro. 682afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element 683afe61c15SRodney W. Grimeson the list. 684afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 685afe61c15SRodney W. Grimesremoved without traversing the list. 6864a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element, 6874a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list. 688afe61c15SRodney W. GrimesA 689afe61c15SRodney W. Grimes.Fa LIST_HEAD 690afe61c15SRodney W. Grimesstructure is declared as follows: 691afe61c15SRodney W. Grimes.Bd -literal -offset indent 692afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head; 693afe61c15SRodney W. Grimes.Ed 6948f20a914SMike Pritchard.Pp 695afe61c15SRodney W. Grimeswhere 696afe61c15SRodney W. Grimes.Fa HEADNAME 697afe61c15SRodney W. Grimesis the name of the structure to be defined, and 698afe61c15SRodney W. Grimes.Fa TYPE 699afe61c15SRodney W. Grimesis the type of the elements to be linked into the list. 700afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as: 701afe61c15SRodney W. Grimes.Bd -literal -offset indent 702afe61c15SRodney W. Grimesstruct HEADNAME *headp; 703afe61c15SRodney W. Grimes.Ed 7048f20a914SMike Pritchard.Pp 705afe61c15SRodney W. Grimes(The names 706afe61c15SRodney W. Grimes.Li head 707afe61c15SRodney W. Grimesand 708afe61c15SRodney W. Grimes.Li headp 709afe61c15SRodney W. Grimesare user selectable.) 710afe61c15SRodney W. Grimes.Pp 711afe61c15SRodney W. GrimesThe macro 71203763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER 71303763fe0SJake Burkholderevaluates to an initializer for the list 71403763fe0SJake Burkholder.Fa head . 71503763fe0SJake Burkholder.Pp 71603763fe0SJake BurkholderThe macro 71779ea9bc4SAlexey Zelkin.Nm LIST_EMPTY 718ddbb94adSTom Rhodesevaluates to true if there are no elements in the list. 71979ea9bc4SAlexey Zelkin.Pp 72079ea9bc4SAlexey ZelkinThe macro 721afe61c15SRodney W. Grimes.Nm LIST_ENTRY 722afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 723afe61c15SRodney W. Grimesthe list. 724afe61c15SRodney W. Grimes.Pp 725afe61c15SRodney W. GrimesThe macro 72679ea9bc4SAlexey Zelkin.Nm LIST_FIRST 72779ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list 72879ea9bc4SAlexey Zelkinis empty. 72979ea9bc4SAlexey Zelkin.Pp 73079ea9bc4SAlexey ZelkinThe macro 73179ea9bc4SAlexey Zelkin.Nm LIST_FOREACH 73279ea9bc4SAlexey Zelkintraverses the list referenced by 73379ea9bc4SAlexey Zelkin.Fa head 73479ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 73579ea9bc4SAlexey Zelkin.Fa var . 73679ea9bc4SAlexey Zelkin.Pp 73779ea9bc4SAlexey ZelkinThe macro 7384250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE 7394250a68eSBosko Milekictraverses the list referenced by 7404250a68eSBosko Milekic.Fa head 7414250a68eSBosko Milekicin the forward direction, assigning each element in turn to 7424250a68eSBosko Milekic.Fa var . 7434250a68eSBosko MilekicHowever, unlike 7444250a68eSBosko Milekic.Fn LIST_FOREACH 7454250a68eSBosko Milekichere it is permitted to both remove 7464250a68eSBosko Milekic.Fa var 7474250a68eSBosko Milekicas well as free it from within the loop safely without interfering with the 7484250a68eSBosko Milekictraversal. 7494250a68eSBosko Milekic.Pp 7504250a68eSBosko MilekicThe macro 751afe61c15SRodney W. Grimes.Nm LIST_INIT 752afe61c15SRodney W. Grimesinitializes the list referenced by 753afe61c15SRodney W. Grimes.Fa head . 754afe61c15SRodney W. Grimes.Pp 755afe61c15SRodney W. GrimesThe macro 756afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD 757afe61c15SRodney W. Grimesinserts the new element 758afe61c15SRodney W. Grimes.Fa elm 759afe61c15SRodney W. Grimesat the head of the list. 760afe61c15SRodney W. Grimes.Pp 761afe61c15SRodney W. GrimesThe macro 762afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER 763afe61c15SRodney W. Grimesinserts the new element 764afe61c15SRodney W. Grimes.Fa elm 765afe61c15SRodney W. Grimesafter the element 766afe61c15SRodney W. Grimes.Fa listelm . 767afe61c15SRodney W. Grimes.Pp 768afe61c15SRodney W. GrimesThe macro 7697658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE 7707658b0a2SJustin T. Gibbsinserts the new element 7717658b0a2SJustin T. Gibbs.Fa elm 7727658b0a2SJustin T. Gibbsbefore the element 7737658b0a2SJustin T. Gibbs.Fa listelm . 7747658b0a2SJustin T. Gibbs.Pp 7757658b0a2SJustin T. GibbsThe macro 77679ea9bc4SAlexey Zelkin.Nm LIST_NEXT 77779ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last. 77879ea9bc4SAlexey Zelkin.Pp 77979ea9bc4SAlexey ZelkinThe macro 7804170b083SEd Schouten.Nm LIST_PREV 7814170b083SEd Schoutenreturns the previous element in the list, or NULL if this is the first. 7824170b083SEd SchoutenList 7834170b083SEd Schouten.Fa head 7844170b083SEd Schoutenmust contain element 7854170b083SEd Schouten.Fa elm . 7864170b083SEd Schouten.Pp 7874170b083SEd SchoutenThe macro 788afe61c15SRodney W. Grimes.Nm LIST_REMOVE 789afe61c15SRodney W. Grimesremoves the element 790afe61c15SRodney W. Grimes.Fa elm 791afe61c15SRodney W. Grimesfrom the list. 792359295cdSMatthew D Fleming.Pp 793359295cdSMatthew D FlemingThe macro 794359295cdSMatthew D Fleming.Nm LIST_SWAP 795359295cdSMatthew D Flemingswaps the contents of 796359295cdSMatthew D Fleming.Fa head1 797359295cdSMatthew D Flemingand 798359295cdSMatthew D Fleming.Fa head2 . 799afe61c15SRodney W. Grimes.Sh LIST EXAMPLE 800afe61c15SRodney W. Grimes.Bd -literal 80103763fe0SJake BurkholderLIST_HEAD(listhead, entry) head = 80203763fe0SJake Burkholder LIST_HEAD_INITIALIZER(head); 803afe61c15SRodney W. Grimesstruct listhead *headp; /* List head. */ 804afe61c15SRodney W. Grimesstruct entry { 805afe61c15SRodney W. Grimes ... 806afe61c15SRodney W. Grimes LIST_ENTRY(entry) entries; /* List. */ 807afe61c15SRodney W. Grimes ... 8084250a68eSBosko Milekic} *n1, *n2, *n3, *np, *np_temp; 809afe61c15SRodney W. Grimes 810afe61c15SRodney W. GrimesLIST_INIT(&head); /* Initialize the list. */ 811afe61c15SRodney W. Grimes 812afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 813afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries); 814afe61c15SRodney W. Grimes 815afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 816afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries); 8177658b0a2SJustin T. Gibbs 8187658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 8197658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries); 8207658b0a2SJustin T. Gibbs 8217658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries); /* Deletion. */ 8227658b0a2SJustin T. Gibbsfree(n2); 823afe61c15SRodney W. Grimes /* Forward traversal. */ 82479ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries) 825afe61c15SRodney W. Grimes np-> ... 826afe61c15SRodney W. Grimes 8274250a68eSBosko Milekic /* Safe forward traversal. */ 8284250a68eSBosko MilekicLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 8294250a68eSBosko Milekic np->do_stuff(); 8304250a68eSBosko Milekic ... 8314250a68eSBosko Milekic LIST_REMOVE(np, entries); 8324250a68eSBosko Milekic free(np); 8334250a68eSBosko Milekic} 8344250a68eSBosko Milekic 83579ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) { /* List Deletion. */ 83679ea9bc4SAlexey Zelkin n1 = LIST_FIRST(&head); 8377658b0a2SJustin T. Gibbs LIST_REMOVE(n1, entries); 8387658b0a2SJustin T. Gibbs free(n1); 8397658b0a2SJustin T. Gibbs} 8407658b0a2SJustin T. Gibbs 84103763fe0SJake Burkholdern1 = LIST_FIRST(&head); /* Faster List Deletion. */ 8427658b0a2SJustin T. Gibbswhile (n1 != NULL) { 84379ea9bc4SAlexey Zelkin n2 = LIST_NEXT(n1, entries); 8447658b0a2SJustin T. Gibbs free(n1); 8457658b0a2SJustin T. Gibbs n1 = n2; 8467658b0a2SJustin T. Gibbs} 8477658b0a2SJustin T. GibbsLIST_INIT(&head); 848afe61c15SRodney W. Grimes.Ed 849afe61c15SRodney W. Grimes.Sh TAIL QUEUES 850afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the 851afe61c15SRodney W. Grimes.Nm TAILQ_HEAD 852afe61c15SRodney W. Grimesmacro. 853afe61c15SRodney W. GrimesThis structure contains a pair of pointers, 854afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to 855afe61c15SRodney W. Grimesthe last element in the tail queue. 856afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 857afe61c15SRodney W. Grimesremoved without traversing the tail queue. 858afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element, 8594a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue, 8604a775e8fSJustin T. Gibbsor at the end of the tail queue. 861afe61c15SRodney W. GrimesA 862afe61c15SRodney W. Grimes.Fa TAILQ_HEAD 863afe61c15SRodney W. Grimesstructure is declared as follows: 864afe61c15SRodney W. Grimes.Bd -literal -offset indent 865afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head; 866afe61c15SRodney W. Grimes.Ed 8678f20a914SMike Pritchard.Pp 868afe61c15SRodney W. Grimeswhere 869afe61c15SRodney W. Grimes.Li HEADNAME 870afe61c15SRodney W. Grimesis the name of the structure to be defined, and 871afe61c15SRodney W. Grimes.Li TYPE 872afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue. 873afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as: 874afe61c15SRodney W. Grimes.Bd -literal -offset indent 875afe61c15SRodney W. Grimesstruct HEADNAME *headp; 876afe61c15SRodney W. Grimes.Ed 8778f20a914SMike Pritchard.Pp 878afe61c15SRodney W. Grimes(The names 879afe61c15SRodney W. Grimes.Li head 880afe61c15SRodney W. Grimesand 881afe61c15SRodney W. Grimes.Li headp 882afe61c15SRodney W. Grimesare user selectable.) 883afe61c15SRodney W. Grimes.Pp 884afe61c15SRodney W. GrimesThe macro 88503763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER 88603763fe0SJake Burkholderevaluates to an initializer for the tail queue 88703763fe0SJake Burkholder.Fa head . 88803763fe0SJake Burkholder.Pp 88903763fe0SJake BurkholderThe macro 890d57e28adSThomas Moestl.Nm TAILQ_CONCAT 891d57e28adSThomas Moestlconcatenates the tail queue headed by 892d57e28adSThomas Moestl.Fa head2 893d57e28adSThomas Moestlonto the end of the one headed by 894d57e28adSThomas Moestl.Fa head1 895d57e28adSThomas Moestlremoving all entries from the former. 896d57e28adSThomas Moestl.Pp 897d57e28adSThomas MoestlThe macro 898c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY 899c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue. 900c5c15c16SPoul-Henning Kamp.Pp 901c5c15c16SPoul-Henning KampThe macro 902afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY 903afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 904afe61c15SRodney W. Grimesthe tail queue. 905afe61c15SRodney W. Grimes.Pp 906afe61c15SRodney W. GrimesThe macro 907c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST 908c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue 909c5c15c16SPoul-Henning Kampis empty. 910c5c15c16SPoul-Henning Kamp.Pp 911c5c15c16SPoul-Henning KampThe macro 91279ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH 91379ea9bc4SAlexey Zelkintraverses the tail queue referenced by 91479ea9bc4SAlexey Zelkin.Fa head 91579ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 91679ea9bc4SAlexey Zelkin.Fa var . 917fb5293cfSRuslan Ermilov.Fa var 918fb5293cfSRuslan Ermilovis set to 919fb5293cfSRuslan Ermilov.Dv NULL 920fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements. 92179ea9bc4SAlexey Zelkin.Pp 92279ea9bc4SAlexey ZelkinThe macro 9236c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE 9246c1d0fbfSArchie Cobbstraverses the tail queue referenced by 9256c1d0fbfSArchie Cobbs.Fa head 9266c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to 9276c1d0fbfSArchie Cobbs.Fa var . 9286c1d0fbfSArchie Cobbs.Pp 9292724bce2SAlexander KabaevThe macros 9302724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE 9312724bce2SAlexander Kabaevand 9322724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE 9332724bce2SAlexander Kabaevtraverse the list referenced by 9342724bce2SAlexander Kabaev.Fa head 9352724bce2SAlexander Kabaevin the forward or reverse direction respectively, 9362724bce2SAlexander Kabaevassigning each element in turn to 9372724bce2SAlexander Kabaev.Fa var . 9383b96c71fSMike PritchardHowever, unlike their unsafe counterparts, 9392724bce2SAlexander Kabaev.Nm TAILQ_FOREACH 9402724bce2SAlexander Kabaevand 9412724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE 9422724bce2SAlexander Kabaevpermit to both remove 9432724bce2SAlexander Kabaev.Fa var 9442724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 9452724bce2SAlexander Kabaevtraversal. 9462724bce2SAlexander Kabaev.Pp 9476c1d0fbfSArchie CobbsThe macro 948afe61c15SRodney W. Grimes.Nm TAILQ_INIT 949afe61c15SRodney W. Grimesinitializes the tail queue referenced by 950afe61c15SRodney W. Grimes.Fa head . 951afe61c15SRodney W. Grimes.Pp 952afe61c15SRodney W. GrimesThe macro 953afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD 954afe61c15SRodney W. Grimesinserts the new element 955afe61c15SRodney W. Grimes.Fa elm 956afe61c15SRodney W. Grimesat the head of the tail queue. 957afe61c15SRodney W. Grimes.Pp 958afe61c15SRodney W. GrimesThe macro 959afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL 960afe61c15SRodney W. Grimesinserts the new element 961afe61c15SRodney W. Grimes.Fa elm 962afe61c15SRodney W. Grimesat the end of the tail queue. 963afe61c15SRodney W. Grimes.Pp 964afe61c15SRodney W. GrimesThe macro 965afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER 966afe61c15SRodney W. Grimesinserts the new element 967afe61c15SRodney W. Grimes.Fa elm 968afe61c15SRodney W. Grimesafter the element 969afe61c15SRodney W. Grimes.Fa listelm . 970afe61c15SRodney W. Grimes.Pp 971afe61c15SRodney W. GrimesThe macro 9727658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE 9737658b0a2SJustin T. Gibbsinserts the new element 9747658b0a2SJustin T. Gibbs.Fa elm 9757658b0a2SJustin T. Gibbsbefore the element 9767658b0a2SJustin T. Gibbs.Fa listelm . 9777658b0a2SJustin T. Gibbs.Pp 9787658b0a2SJustin T. GibbsThe macro 979c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST 980c5c15c16SPoul-Henning Kampreturns the last item on the tail queue. 981982ba1cbSKirk McKusickIf the tail queue is empty the return value is 982982ba1cbSKirk McKusick.Dv NULL . 983c5c15c16SPoul-Henning Kamp.Pp 984c5c15c16SPoul-Henning KampThe macro 985c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT 98679ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last. 98779ea9bc4SAlexey Zelkin.Pp 98879ea9bc4SAlexey ZelkinThe macro 98979ea9bc4SAlexey Zelkin.Nm TAILQ_PREV 99079ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item 99179ea9bc4SAlexey Zelkinis the first. 992c5c15c16SPoul-Henning Kamp.Pp 993c5c15c16SPoul-Henning KampThe macro 994afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE 995afe61c15SRodney W. Grimesremoves the element 996afe61c15SRodney W. Grimes.Fa elm 997afe61c15SRodney W. Grimesfrom the tail queue. 998359295cdSMatthew D Fleming.Pp 999359295cdSMatthew D FlemingThe macro 1000359295cdSMatthew D Fleming.Nm TAILQ_SWAP 1001359295cdSMatthew D Flemingswaps the contents of 1002359295cdSMatthew D Fleming.Fa head1 1003359295cdSMatthew D Flemingand 1004359295cdSMatthew D Fleming.Fa head2 . 1005afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE 1006afe61c15SRodney W. Grimes.Bd -literal 100703763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head = 100803763fe0SJake Burkholder TAILQ_HEAD_INITIALIZER(head); 1009afe61c15SRodney W. Grimesstruct tailhead *headp; /* Tail queue head. */ 1010afe61c15SRodney W. Grimesstruct entry { 1011afe61c15SRodney W. Grimes ... 1012afe61c15SRodney W. Grimes TAILQ_ENTRY(entry) entries; /* Tail queue. */ 1013afe61c15SRodney W. Grimes ... 10147658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np; 1015afe61c15SRodney W. Grimes 1016afe61c15SRodney W. GrimesTAILQ_INIT(&head); /* Initialize the queue. */ 1017afe61c15SRodney W. Grimes 1018afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1019afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries); 1020afe61c15SRodney W. Grimes 1021afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 1022afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries); 1023afe61c15SRodney W. Grimes 1024afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 1025afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries); 10267658b0a2SJustin T. Gibbs 10277658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 10283652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries); 10297658b0a2SJustin T. Gibbs 10307658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries); /* Deletion. */ 10317658b0a2SJustin T. Gibbsfree(n2); 1032afe61c15SRodney W. Grimes /* Forward traversal. */ 103379ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries) 1034afe61c15SRodney W. Grimes np-> ... 10352724bce2SAlexander Kabaev /* Safe forward traversal. */ 10362724bce2SAlexander KabaevTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 10372724bce2SAlexander Kabaev np->do_stuff(); 10382724bce2SAlexander Kabaev ... 10392724bce2SAlexander Kabaev TAILQ_REMOVE(&head, np, entries); 10402724bce2SAlexander Kabaev free(np); 10412724bce2SAlexander Kabaev} 10426c1d0fbfSArchie Cobbs /* Reverse traversal. */ 10436c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 10446c1d0fbfSArchie Cobbs np-> ... 10457658b0a2SJustin T. Gibbs /* TailQ Deletion. */ 1046d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) { 1047c5c15c16SPoul-Henning Kamp n1 = TAILQ_FIRST(&head); 104879ea9bc4SAlexey Zelkin TAILQ_REMOVE(&head, n1, entries); 10497658b0a2SJustin T. Gibbs free(n1); 10507658b0a2SJustin T. Gibbs} 10517658b0a2SJustin T. Gibbs /* Faster TailQ Deletion. */ 1052c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head); 10537658b0a2SJustin T. Gibbswhile (n1 != NULL) { 1054c5c15c16SPoul-Henning Kamp n2 = TAILQ_NEXT(n1, entries); 10557658b0a2SJustin T. Gibbs free(n1); 10567658b0a2SJustin T. Gibbs n1 = n2; 10577658b0a2SJustin T. Gibbs} 10587658b0a2SJustin T. GibbsTAILQ_INIT(&head); 1059afe61c15SRodney W. Grimes.Ed 1060b9ec8f3bSSimon L. B. Nielsen.Sh SEE ALSO 1061b9ec8f3bSSimon L. B. Nielsen.Xr tree 3 1062afe61c15SRodney W. Grimes.Sh HISTORY 1063afe61c15SRodney W. GrimesThe 1064afe61c15SRodney W. Grimes.Nm queue 106521421932SMike Pritchardfunctions first appeared in 106621421932SMike Pritchard.Bx 4.4 . 1067