1afe61c15SRodney W. Grimes.\" Copyright (c) 1993 2afe61c15SRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 3afe61c15SRodney W. Grimes.\" 4afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 5afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions 6afe61c15SRodney W. Grimes.\" are met: 7afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 8afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 9afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 10afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 11afe61c15SRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12afe61c15SRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software 13afe61c15SRodney W. Grimes.\" must display the following acknowledgement: 14afe61c15SRodney W. Grimes.\" This product includes software developed by the University of 15afe61c15SRodney W. Grimes.\" California, Berkeley and its contributors. 16afe61c15SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors 17afe61c15SRodney W. Grimes.\" may be used to endorse or promote products derived from this software 18afe61c15SRodney W. Grimes.\" without specific prior written permission. 19afe61c15SRodney W. Grimes.\" 20afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23afe61c15SRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30afe61c15SRodney W. Grimes.\" SUCH DAMAGE. 31afe61c15SRodney W. Grimes.\" 32afe61c15SRodney W. Grimes.\" @(#)queue.3 8.2 (Berkeley) 1/24/94 337f3dea24SPeter Wemm.\" $FreeBSD$ 34afe61c15SRodney W. Grimes.\" 35d3df5ce1SMike Pritchard.Dd January 24, 1994 36afe61c15SRodney W. Grimes.Dt QUEUE 3 373d45e180SRuslan Ermilov.Os 38afe61c15SRodney W. Grimes.Sh NAME 39aea88892SPoul-Henning Kamp.Nm SLIST_EMPTY , 404a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY , 41aea88892SPoul-Henning Kamp.Nm SLIST_FIRST , 4279ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH , 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 , 504a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD , 514a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE , 52d57e28adSThomas Moestl.Nm STAILQ_CONCAT , 5379ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY , 544a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY , 5579ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST , 5679ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH , 572724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE , 584a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD , 5903763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER , 604a775e8fSJustin T. Gibbs.Nm STAILQ_INIT , 614a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER , 624a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD , 634a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL , 6479ea9bc4SAlexey Zelkin.Nm STAILQ_LAST , 6579ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT , 664a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD , 674a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE , 6879ea9bc4SAlexey Zelkin.Nm LIST_EMPTY , 69afe61c15SRodney W. Grimes.Nm LIST_ENTRY , 7079ea9bc4SAlexey Zelkin.Nm LIST_FIRST , 7179ea9bc4SAlexey Zelkin.Nm LIST_FOREACH , 724250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE , 73afe61c15SRodney W. Grimes.Nm LIST_HEAD , 7403763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER , 75afe61c15SRodney W. Grimes.Nm LIST_INIT , 76afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER , 777658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE , 78afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD , 7979ea9bc4SAlexey Zelkin.Nm LIST_NEXT , 80afe61c15SRodney W. Grimes.Nm LIST_REMOVE , 81d57e28adSThomas Moestl.Nm TAILQ_CONCAT , 82c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY , 83afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY , 84c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST , 8579ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH , 862724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE , 876c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE , 882724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE , 89afe61c15SRodney W. Grimes.Nm TAILQ_HEAD , 9003763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER , 91afe61c15SRodney W. Grimes.Nm TAILQ_INIT , 92afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER , 937658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE , 94afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD , 95afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL , 96c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST , 97c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT , 9879ea9bc4SAlexey Zelkin.Nm TAILQ_PREV , 99b4660c37SBen Smithurst.Nm TAILQ_REMOVE 1004a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues, 10124b85d18SPoul-Henning Kamplists and tail queues 102afe61c15SRodney W. Grimes.Sh SYNOPSIS 10332eef9aeSRuslan Ermilov.In sys/queue.h 1048f20a914SMike Pritchard.\" 105aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head" 1064a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE" 107aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head" 10879ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1092724bce2SAlexander Kabaev.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 1104a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE" 11103763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head" 1124a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head" 1134a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 1144a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 115aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 1164a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1174a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 1188f20a914SMike Pritchard.\" 119d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" 12079ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 1214a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE" 12279ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head" 12379ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1242724bce2SAlexander Kabaev.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 1254a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE" 12603763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" 1274a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head" 1284a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 1294a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 1304a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 131f36b6e4bSMaxim Sobolev.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME" 13279ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 13302a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1344a775e8fSJustin T. Gibbs.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 1358f20a914SMike Pritchard.\" 13679ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head" 137afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE" 13879ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head" 13979ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 1404250a68eSBosko Milekic.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 141afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE" 14203763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head" 143afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head" 1444a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 1454a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 146afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 14779ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 148afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 1498f20a914SMike Pritchard.\" 150d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME" 151c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 152afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE" 153c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head" 15479ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 1552724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 1566c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 1572724bce2SAlexander Kabaev.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 158afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE" 15903763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head" 160afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head" 161afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 1623652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 163afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 164afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 16579ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 166c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 16779ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 168afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 1698f20a914SMike Pritchard.\" 170afe61c15SRodney W. Grimes.Sh DESCRIPTION 171b86388c1SDima DorfmanThese macros define and operate on four types of data structures: 172b86388c1SDima Dorfmansingly-linked lists, singly-linked tail queues, lists, and tail queues. 173b86388c1SDima DorfmanAll four structures support the following functionality: 174afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 175afe61c15SRodney W. Grimes.It 176afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list. 177afe61c15SRodney W. Grimes.It 178afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list. 179afe61c15SRodney W. Grimes.It 1804a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list. 1817658b0a2SJustin T. Gibbs.It 1824a775e8fSJustin T. GibbsO(n) removal of any entry in the list. 183afe61c15SRodney W. Grimes.It 184afe61c15SRodney W. GrimesForward traversal through the list. 185afe61c15SRodney W. Grimes.El 186afe61c15SRodney W. Grimes.Pp 1874a775e8fSJustin T. GibbsSingly-linked lists are the simplest of the five data structures 1884a775e8fSJustin T. Gibbsand support only the above functionality. 1894a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets 1904a775e8fSJustin T. Gibbsand few or no removals, 1914a775e8fSJustin T. Gibbsor for implementing a LIFO queue. 1924a775e8fSJustin T. Gibbs.Pp 1934a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality: 1944a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 1954a775e8fSJustin T. Gibbs.It 1964a775e8fSJustin T. GibbsEntries can be added at the end of a list. 197d57e28adSThomas Moestl.It 198d57e28adSThomas MoestlThey may be concatenated. 1994a775e8fSJustin T. Gibbs.El 2004a775e8fSJustin T. GibbsHowever: 2014a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2024a775e8fSJustin T. Gibbs.It 2034a775e8fSJustin T. GibbsAll list insertions must specify the head of the list. 2044a775e8fSJustin T. Gibbs.It 2054a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one. 2064a775e8fSJustin T. Gibbs.It 2074a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower 2084a775e8fSJustin T. Gibbsthan singly-linked lists. 2094a775e8fSJustin T. Gibbs.El 2104a775e8fSJustin T. Gibbs.Pp 2114a775e8fSJustin T. GibbsSingly-linked tailqs are ideal for applications with large datasets and 2124a775e8fSJustin T. Gibbsfew or no removals, 2134a775e8fSJustin T. Gibbsor for implementing a FIFO queue. 2144a775e8fSJustin T. Gibbs.Pp 215b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues) 216b86388c1SDima Dorfmanadditionally allow: 2174a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2184a775e8fSJustin T. Gibbs.It 2194a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list. 2204a775e8fSJustin T. Gibbs.It 2214a775e8fSJustin T. GibbsO(1) removal of any entry in the list. 2224a775e8fSJustin T. Gibbs.El 2234a775e8fSJustin T. GibbsHowever: 2244a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2254a775e8fSJustin T. Gibbs.It 2264a775e8fSJustin T. GibbsEach elements requires two pointers rather than one. 2274a775e8fSJustin T. Gibbs.It 2284a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about 2294a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures. 2304a775e8fSJustin T. Gibbs.El 2314a775e8fSJustin T. Gibbs.Pp 2324a775e8fSJustin T. GibbsLinked lists are the simplest of the doubly linked data structures and support 2334a775e8fSJustin T. Gibbsonly the above functionality over singly-linked lists. 234afe61c15SRodney W. Grimes.Pp 235afe61c15SRodney W. GrimesTail queues add the following functionality: 236afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 237afe61c15SRodney W. Grimes.It 238afe61c15SRodney W. GrimesEntries can be added at the end of a list. 2396c1d0fbfSArchie Cobbs.It 2406c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head. 241d57e28adSThomas Moestl.It 242d57e28adSThomas MoestlThey may be concatenated. 243afe61c15SRodney W. Grimes.El 244afe61c15SRodney W. GrimesHowever: 245afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 246afe61c15SRodney W. Grimes.It 247afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list. 248afe61c15SRodney W. Grimes.It 249afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one. 250afe61c15SRodney W. Grimes.It 251afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower 2524a775e8fSJustin T. Gibbsthan singly-linked lists. 253afe61c15SRodney W. Grimes.El 254afe61c15SRodney W. Grimes.Pp 255afe61c15SRodney W. GrimesIn the macro definitions, 256afe61c15SRodney W. Grimes.Fa TYPE 257afe61c15SRodney W. Grimesis the name of a user defined structure, 258afe61c15SRodney W. Grimesthat must contain a field of type 2594a775e8fSJustin T. Gibbs.Li SLIST_ENTRY , 2604a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY , 261afe61c15SRodney W. Grimes.Li LIST_ENTRY , 262afe61c15SRodney W. Grimesor 26324b85d18SPoul-Henning Kamp.Li TAILQ_ENTRY , 264afe61c15SRodney W. Grimesnamed 265afe61c15SRodney W. Grimes.Fa NAME . 266afe61c15SRodney W. GrimesThe argument 267afe61c15SRodney W. Grimes.Fa HEADNAME 268afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared 269afe61c15SRodney W. Grimesusing the macros 2704a775e8fSJustin T. Gibbs.Li SLIST_HEAD , 2714a775e8fSJustin T. Gibbs.Li STAILQ_HEAD , 272afe61c15SRodney W. Grimes.Li LIST_HEAD , 273afe61c15SRodney W. Grimesor 27424b85d18SPoul-Henning Kamp.Li TAILQ_HEAD . 275afe61c15SRodney W. GrimesSee the examples below for further explanation of how these 276afe61c15SRodney W. Grimesmacros are used. 2774a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS 2784a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the 2794a775e8fSJustin T. Gibbs.Nm SLIST_HEAD 2804a775e8fSJustin T. Gibbsmacro. 2814a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element 2824a775e8fSJustin T. Gibbson the list. 2834a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation 2844a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements. 2854a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or 2864a775e8fSJustin T. Gibbsat the head of the list. 2874a775e8fSJustin T. GibbsAn 2884a775e8fSJustin T. Gibbs.Fa SLIST_HEAD 2894a775e8fSJustin T. Gibbsstructure is declared as follows: 2904a775e8fSJustin T. Gibbs.Bd -literal -offset indent 2914a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head; 2924a775e8fSJustin T. Gibbs.Ed 2938f20a914SMike Pritchard.Pp 2944a775e8fSJustin T. Gibbswhere 2954a775e8fSJustin T. Gibbs.Fa HEADNAME 2964a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 2974a775e8fSJustin T. Gibbs.Fa TYPE 2984a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list. 2994a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as: 3004a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3014a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 3024a775e8fSJustin T. Gibbs.Ed 3038f20a914SMike Pritchard.Pp 3044a775e8fSJustin T. Gibbs(The names 3054a775e8fSJustin T. Gibbs.Li head 3064a775e8fSJustin T. Gibbsand 3074a775e8fSJustin T. Gibbs.Li headp 3084a775e8fSJustin T. Gibbsare user selectable.) 3094a775e8fSJustin T. Gibbs.Pp 3104a775e8fSJustin T. GibbsThe macro 31103763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER 31203763fe0SJake Burkholderevaluates to an initializer for the list 31303763fe0SJake Burkholder.Fa head . 31403763fe0SJake Burkholder.Pp 31503763fe0SJake BurkholderThe macro 31679ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY 31779ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list. 31879ea9bc4SAlexey Zelkin.Pp 31979ea9bc4SAlexey ZelkinThe macro 3204a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY 3214a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 3224a775e8fSJustin T. Gibbsthe list. 3234a775e8fSJustin T. Gibbs.Pp 3244a775e8fSJustin T. GibbsThe macro 32579ea9bc4SAlexey Zelkin.Nm SLIST_FIRST 32679ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty. 32779ea9bc4SAlexey Zelkin.Pp 32879ea9bc4SAlexey ZelkinThe macro 32979ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH 33079ea9bc4SAlexey Zelkintraverses the list referenced by 33179ea9bc4SAlexey Zelkin.Fa head 33279ea9bc4SAlexey Zelkinin the forward direction, assigning each element in 33379ea9bc4SAlexey Zelkinturn to 33479ea9bc4SAlexey Zelkin.Fa var . 33579ea9bc4SAlexey Zelkin.Pp 33679ea9bc4SAlexey ZelkinThe macro 3372724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE 3382724bce2SAlexander Kabaevtraverses the list referenced by 3392724bce2SAlexander Kabaev.Fa head 3402724bce2SAlexander Kabaevin the forward direction, assigning each element in 3412724bce2SAlexander Kabaevturn to 3422724bce2SAlexander Kabaev.Fa var . 3432724bce2SAlexander KabaevHowever, unlike 3442724bce2SAlexander Kabaev.Fn SLIST_FOREACH 3452724bce2SAlexander Kabaevhere it is permitted to both remove 3462724bce2SAlexander Kabaev.Fa var 3472724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 3482724bce2SAlexander Kabaevtraversal. 3492724bce2SAlexander Kabaev.Pp 3502724bce2SAlexander KabaevThe macro 3514a775e8fSJustin T. Gibbs.Nm SLIST_INIT 3524a775e8fSJustin T. Gibbsinitializes the list referenced by 3534a775e8fSJustin T. Gibbs.Fa head . 3544a775e8fSJustin T. Gibbs.Pp 3554a775e8fSJustin T. GibbsThe macro 3564a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD 3574a775e8fSJustin T. Gibbsinserts the new element 3584a775e8fSJustin T. Gibbs.Fa elm 3594a775e8fSJustin T. Gibbsat the head of the list. 3604a775e8fSJustin T. Gibbs.Pp 3614a775e8fSJustin T. GibbsThe macro 3624a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER 3634a775e8fSJustin T. Gibbsinserts the new element 3644a775e8fSJustin T. Gibbs.Fa elm 3654a775e8fSJustin T. Gibbsafter the element 3664a775e8fSJustin T. Gibbs.Fa listelm . 3674a775e8fSJustin T. Gibbs.Pp 3684a775e8fSJustin T. GibbsThe macro 36979ea9bc4SAlexey Zelkin.Nm SLIST_NEXT 37079ea9bc4SAlexey Zelkinreturns the next element in the list. 37179ea9bc4SAlexey Zelkin.Pp 37279ea9bc4SAlexey ZelkinThe macro 3734a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD 3744a775e8fSJustin T. Gibbsremoves the element 3754a775e8fSJustin T. Gibbs.Fa elm 3764a775e8fSJustin T. Gibbsfrom the head of the list. 3774a775e8fSJustin T. GibbsFor optimum efficiency, 3784a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use 3794a775e8fSJustin T. Gibbsthis macro instead of the generic 3804a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE 3814a775e8fSJustin T. Gibbsmacro. 3824a775e8fSJustin T. Gibbs.Pp 3834a775e8fSJustin T. GibbsThe macro 3844a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE 3854a775e8fSJustin T. Gibbsremoves the element 3864a775e8fSJustin T. Gibbs.Fa elm 3874a775e8fSJustin T. Gibbsfrom the list. 3884a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE 3894a775e8fSJustin T. Gibbs.Bd -literal 39003763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head = 39103763fe0SJake Burkholder SLIST_HEAD_INITIALIZER(head); 3924a775e8fSJustin T. Gibbsstruct slisthead *headp; /* Singly-linked List head. */ 3934a775e8fSJustin T. Gibbsstruct entry { 3944a775e8fSJustin T. Gibbs ... 3954a775e8fSJustin T. Gibbs SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 3964a775e8fSJustin T. Gibbs ... 3974a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 3984a775e8fSJustin T. Gibbs 3994a775e8fSJustin T. GibbsSLIST_INIT(&head); /* Initialize the list. */ 4004a775e8fSJustin T. Gibbs 4014a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 4024a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries); 4034a775e8fSJustin T. Gibbs 4044a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 4054a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries); 4064a775e8fSJustin T. Gibbs 4074a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 4084a775e8fSJustin T. Gibbsfree(n2); 4094a775e8fSJustin T. Gibbs 41079ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head); 41103763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 4124a775e8fSJustin T. Gibbsfree(n3); 4134a775e8fSJustin T. Gibbs /* Forward traversal. */ 41479ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries) 4154a775e8fSJustin T. Gibbs np-> ... 4162724bce2SAlexander Kabaev /* Safe forward traversal. */ 4172724bce2SAlexander KabaevSLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 4182724bce2SAlexander Kabaev np->do_stuff(); 4192724bce2SAlexander Kabaev ... 4202724bce2SAlexander Kabaev SLIST_REMOVE(&head, np, entry, entries); 4212724bce2SAlexander Kabaev free(np); 4222724bce2SAlexander Kabaev} 4234a775e8fSJustin T. Gibbs 42479ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) { /* List Deletion. */ 42579ea9bc4SAlexey Zelkin n1 = SLIST_FIRST(&head); 4264a775e8fSJustin T. Gibbs SLIST_REMOVE_HEAD(&head, entries); 4274a775e8fSJustin T. Gibbs free(n1); 4284a775e8fSJustin T. Gibbs} 4294a775e8fSJustin T. Gibbs.Ed 4304a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES 4314a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the 4324a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD 4334a775e8fSJustin T. Gibbsmacro. 4344a775e8fSJustin T. GibbsThis structure contains a pair of pointers, 4354a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to 4364a775e8fSJustin T. Gibbsthe last element in the tail queue. 4374a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer 4384a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary 4394a775e8fSJustin T. Gibbselements. 4404a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element, 4414a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue. 4424a775e8fSJustin T. GibbsA 4434a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD 4444a775e8fSJustin T. Gibbsstructure is declared as follows: 4454a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4464a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head; 4474a775e8fSJustin T. Gibbs.Ed 4488f20a914SMike Pritchard.Pp 4494a775e8fSJustin T. Gibbswhere 4504a775e8fSJustin T. Gibbs.Li HEADNAME 4514a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 4524a775e8fSJustin T. Gibbs.Li TYPE 4534a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue. 4544a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as: 4554a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4564a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 4574a775e8fSJustin T. Gibbs.Ed 4588f20a914SMike Pritchard.Pp 4594a775e8fSJustin T. Gibbs(The names 4604a775e8fSJustin T. Gibbs.Li head 4614a775e8fSJustin T. Gibbsand 4624a775e8fSJustin T. Gibbs.Li headp 4634a775e8fSJustin T. Gibbsare user selectable.) 4644a775e8fSJustin T. Gibbs.Pp 4654a775e8fSJustin T. GibbsThe macro 46603763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER 46703763fe0SJake Burkholderevaluates to an initializer for the tail queue 46803763fe0SJake Burkholder.Fa head . 46903763fe0SJake Burkholder.Pp 47003763fe0SJake BurkholderThe macro 471d57e28adSThomas Moestl.Nm STAILQ_CONCAT 472d57e28adSThomas Moestlconcatenates the tail queue headed by 473d57e28adSThomas Moestl.Fa head2 474d57e28adSThomas Moestlonto the end of the one headed by 475d57e28adSThomas Moestl.Fa head1 476d57e28adSThomas Moestlremoving all entries from the former. 477d57e28adSThomas Moestl.Pp 478d57e28adSThomas MoestlThe macro 47979ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY 48079ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue. 48179ea9bc4SAlexey Zelkin.Pp 48279ea9bc4SAlexey ZelkinThe macro 4834a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY 4844a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 4854a775e8fSJustin T. Gibbsthe tail queue. 4864a775e8fSJustin T. Gibbs.Pp 4874a775e8fSJustin T. GibbsThe macro 48879ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST 48979ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue 49079ea9bc4SAlexey Zelkinis empty. 49179ea9bc4SAlexey Zelkin.Pp 49279ea9bc4SAlexey ZelkinThe macro 49379ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH 49479ea9bc4SAlexey Zelkintraverses the tail queue referenced by 49579ea9bc4SAlexey Zelkin.Fa head 49679ea9bc4SAlexey Zelkinin the forward direction, assigning each element 49779ea9bc4SAlexey Zelkinin turn to 49879ea9bc4SAlexey Zelkin.Fa var . 49979ea9bc4SAlexey Zelkin.Pp 50079ea9bc4SAlexey ZelkinThe macro 5012724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE 5022724bce2SAlexander Kabaevtraverses the tail queue referenced by 5032724bce2SAlexander Kabaev.Fa head 5042724bce2SAlexander Kabaevin the forward direction, assigning each element 5052724bce2SAlexander Kabaevin turn to 5062724bce2SAlexander Kabaev.Fa var . 5072724bce2SAlexander KabaevHowever, unlike 5082724bce2SAlexander Kabaev.Fn STAILQ_FOREACH 5092724bce2SAlexander Kabaevhere it is permitted to both remove 5102724bce2SAlexander Kabaev.Fa var 5112724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 5122724bce2SAlexander Kabaevtraversal. 5132724bce2SAlexander Kabaev.Pp 5142724bce2SAlexander KabaevThe macro 5154a775e8fSJustin T. Gibbs.Nm STAILQ_INIT 5164a775e8fSJustin T. Gibbsinitializes the tail queue referenced by 5174a775e8fSJustin T. Gibbs.Fa head . 5184a775e8fSJustin T. Gibbs.Pp 5194a775e8fSJustin T. GibbsThe macro 5204a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD 5214a775e8fSJustin T. Gibbsinserts the new element 5224a775e8fSJustin T. Gibbs.Fa elm 5234a775e8fSJustin T. Gibbsat the head of the tail queue. 5244a775e8fSJustin T. Gibbs.Pp 5254a775e8fSJustin T. GibbsThe macro 5264a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL 5274a775e8fSJustin T. Gibbsinserts the new element 5284a775e8fSJustin T. Gibbs.Fa elm 5294a775e8fSJustin T. Gibbsat the end of the tail queue. 5304a775e8fSJustin T. Gibbs.Pp 5314a775e8fSJustin T. GibbsThe macro 5324a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER 5334a775e8fSJustin T. Gibbsinserts the new element 5344a775e8fSJustin T. Gibbs.Fa elm 5354a775e8fSJustin T. Gibbsafter the element 5364a775e8fSJustin T. Gibbs.Fa listelm . 5374a775e8fSJustin T. Gibbs.Pp 5384a775e8fSJustin T. GibbsThe macro 53979ea9bc4SAlexey Zelkin.Nm STAILQ_LAST 54079ea9bc4SAlexey Zelkinreturns the last item on the tail queue. 54179ea9bc4SAlexey ZelkinIf the tail queue is empty the return value is undefined. 54279ea9bc4SAlexey Zelkin.Pp 54379ea9bc4SAlexey ZelkinThe macro 54479ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT 54579ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last. 54679ea9bc4SAlexey Zelkin.Pp 54779ea9bc4SAlexey ZelkinThe macro 5484a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD 549ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue. 5504a775e8fSJustin T. GibbsFor optimum efficiency, 5514a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should 5524a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic 5534a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE 5544a775e8fSJustin T. Gibbsmacro. 5554a775e8fSJustin T. Gibbs.Pp 5564a775e8fSJustin T. GibbsThe macro 5574a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE 5584a775e8fSJustin T. Gibbsremoves the element 5594a775e8fSJustin T. Gibbs.Fa elm 5604a775e8fSJustin T. Gibbsfrom the tail queue. 5614a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE 5624a775e8fSJustin T. Gibbs.Bd -literal 56303763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head = 56403763fe0SJake Burkholder STAILQ_HEAD_INITIALIZER(head); 5654a775e8fSJustin T. Gibbsstruct stailhead *headp; /* Singly-linked tail queue head. */ 5664a775e8fSJustin T. Gibbsstruct entry { 5674a775e8fSJustin T. Gibbs ... 5684a775e8fSJustin T. Gibbs STAILQ_ENTRY(entry) entries; /* Tail queue. */ 5694a775e8fSJustin T. Gibbs ... 5704a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 5714a775e8fSJustin T. Gibbs 5724a775e8fSJustin T. GibbsSTAILQ_INIT(&head); /* Initialize the queue. */ 5734a775e8fSJustin T. Gibbs 5744a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 5754a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries); 5764a775e8fSJustin T. Gibbs 5774a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 5784a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries); 5794a775e8fSJustin T. Gibbs 5804a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 5814a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries); 5824a775e8fSJustin T. Gibbs /* Deletion. */ 5834a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries); 5844a775e8fSJustin T. Gibbsfree(n2); 58503763fe0SJake Burkholder /* Deletion from the head. */ 58679ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head); 5874a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries); 5884a775e8fSJustin T. Gibbsfree(n3); 5894a775e8fSJustin T. Gibbs /* Forward traversal. */ 59079ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries) 5914a775e8fSJustin T. Gibbs np-> ... 5922724bce2SAlexander Kabaev /* Safe forward traversal. */ 5932724bce2SAlexander KabaevSTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 5942724bce2SAlexander Kabaev np->do_stuff(); 5952724bce2SAlexander Kabaev ... 5962724bce2SAlexander Kabaev STAILQ_REMOVE(&head, np, entry, entries); 5972724bce2SAlexander Kabaev free(np); 5982724bce2SAlexander Kabaev} 5994a775e8fSJustin T. Gibbs /* TailQ Deletion. */ 60079ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) { 60103763fe0SJake Burkholder n1 = STAILQ_FIRST(&head); 602266e33fdSJohn Baldwin STAILQ_REMOVE_HEAD(&head, entries); 6034a775e8fSJustin T. Gibbs free(n1); 6044a775e8fSJustin T. Gibbs} 6054a775e8fSJustin T. Gibbs /* Faster TailQ Deletion. */ 60679ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head); 6074a775e8fSJustin T. Gibbswhile (n1 != NULL) { 60879ea9bc4SAlexey Zelkin n2 = STAILQ_NEXT(n1, entries); 6094a775e8fSJustin T. Gibbs free(n1); 6104a775e8fSJustin T. Gibbs n1 = n2; 6114a775e8fSJustin T. Gibbs} 6124a775e8fSJustin T. GibbsSTAILQ_INIT(&head); 6134a775e8fSJustin T. Gibbs.Ed 614afe61c15SRodney W. Grimes.Sh LISTS 615afe61c15SRodney W. GrimesA list is headed by a structure defined by the 616afe61c15SRodney W. Grimes.Nm LIST_HEAD 617afe61c15SRodney W. Grimesmacro. 618afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element 619afe61c15SRodney W. Grimeson the list. 620afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 621afe61c15SRodney W. Grimesremoved without traversing the list. 6224a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element, 6234a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list. 624afe61c15SRodney W. GrimesA 625afe61c15SRodney W. Grimes.Fa LIST_HEAD 626afe61c15SRodney W. Grimesstructure is declared as follows: 627afe61c15SRodney W. Grimes.Bd -literal -offset indent 628afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head; 629afe61c15SRodney W. Grimes.Ed 6308f20a914SMike Pritchard.Pp 631afe61c15SRodney W. Grimeswhere 632afe61c15SRodney W. Grimes.Fa HEADNAME 633afe61c15SRodney W. Grimesis the name of the structure to be defined, and 634afe61c15SRodney W. Grimes.Fa TYPE 635afe61c15SRodney W. Grimesis the type of the elements to be linked into the list. 636afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as: 637afe61c15SRodney W. Grimes.Bd -literal -offset indent 638afe61c15SRodney W. Grimesstruct HEADNAME *headp; 639afe61c15SRodney W. Grimes.Ed 6408f20a914SMike Pritchard.Pp 641afe61c15SRodney W. Grimes(The names 642afe61c15SRodney W. Grimes.Li head 643afe61c15SRodney W. Grimesand 644afe61c15SRodney W. Grimes.Li headp 645afe61c15SRodney W. Grimesare user selectable.) 646afe61c15SRodney W. Grimes.Pp 647afe61c15SRodney W. GrimesThe macro 64803763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER 64903763fe0SJake Burkholderevaluates to an initializer for the list 65003763fe0SJake Burkholder.Fa head . 65103763fe0SJake Burkholder.Pp 65203763fe0SJake BurkholderThe macro 65379ea9bc4SAlexey Zelkin.Nm LIST_EMPTY 65479ea9bc4SAlexey Zelkinevaluates to true if their are no elements in the list. 65579ea9bc4SAlexey Zelkin.Pp 65679ea9bc4SAlexey ZelkinThe macro 657afe61c15SRodney W. Grimes.Nm LIST_ENTRY 658afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 659afe61c15SRodney W. Grimesthe list. 660afe61c15SRodney W. Grimes.Pp 661afe61c15SRodney W. GrimesThe macro 66279ea9bc4SAlexey Zelkin.Nm LIST_FIRST 66379ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list 66479ea9bc4SAlexey Zelkinis empty. 66579ea9bc4SAlexey Zelkin.Pp 66679ea9bc4SAlexey ZelkinThe macro 66779ea9bc4SAlexey Zelkin.Nm LIST_FOREACH 66879ea9bc4SAlexey Zelkintraverses the list referenced by 66979ea9bc4SAlexey Zelkin.Fa head 67079ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 67179ea9bc4SAlexey Zelkin.Fa var . 67279ea9bc4SAlexey Zelkin.Pp 67379ea9bc4SAlexey ZelkinThe macro 6744250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE 6754250a68eSBosko Milekictraverses the list referenced by 6764250a68eSBosko Milekic.Fa head 6774250a68eSBosko Milekicin the forward direction, assigning each element in turn to 6784250a68eSBosko Milekic.Fa var . 6794250a68eSBosko MilekicHowever, unlike 6804250a68eSBosko Milekic.Fn LIST_FOREACH 6814250a68eSBosko Milekichere it is permitted to both remove 6824250a68eSBosko Milekic.Fa var 6834250a68eSBosko Milekicas well as free it from within the loop safely without interfering with the 6844250a68eSBosko Milekictraversal. 6854250a68eSBosko Milekic.Pp 6864250a68eSBosko MilekicThe macro 687afe61c15SRodney W. Grimes.Nm LIST_INIT 688afe61c15SRodney W. Grimesinitializes the list referenced by 689afe61c15SRodney W. Grimes.Fa head . 690afe61c15SRodney W. Grimes.Pp 691afe61c15SRodney W. GrimesThe macro 692afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD 693afe61c15SRodney W. Grimesinserts the new element 694afe61c15SRodney W. Grimes.Fa elm 695afe61c15SRodney W. Grimesat the head of the list. 696afe61c15SRodney W. Grimes.Pp 697afe61c15SRodney W. GrimesThe macro 698afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER 699afe61c15SRodney W. Grimesinserts the new element 700afe61c15SRodney W. Grimes.Fa elm 701afe61c15SRodney W. Grimesafter the element 702afe61c15SRodney W. Grimes.Fa listelm . 703afe61c15SRodney W. Grimes.Pp 704afe61c15SRodney W. GrimesThe macro 7057658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE 7067658b0a2SJustin T. Gibbsinserts the new element 7077658b0a2SJustin T. Gibbs.Fa elm 7087658b0a2SJustin T. Gibbsbefore the element 7097658b0a2SJustin T. Gibbs.Fa listelm . 7107658b0a2SJustin T. Gibbs.Pp 7117658b0a2SJustin T. GibbsThe macro 71279ea9bc4SAlexey Zelkin.Nm LIST_NEXT 71379ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last. 71479ea9bc4SAlexey Zelkin.Pp 71579ea9bc4SAlexey ZelkinThe macro 716afe61c15SRodney W. Grimes.Nm LIST_REMOVE 717afe61c15SRodney W. Grimesremoves the element 718afe61c15SRodney W. Grimes.Fa elm 719afe61c15SRodney W. Grimesfrom the list. 720afe61c15SRodney W. Grimes.Sh LIST EXAMPLE 721afe61c15SRodney W. Grimes.Bd -literal 72203763fe0SJake BurkholderLIST_HEAD(listhead, entry) head = 72303763fe0SJake Burkholder LIST_HEAD_INITIALIZER(head); 724afe61c15SRodney W. Grimesstruct listhead *headp; /* List head. */ 725afe61c15SRodney W. Grimesstruct entry { 726afe61c15SRodney W. Grimes ... 727afe61c15SRodney W. Grimes LIST_ENTRY(entry) entries; /* List. */ 728afe61c15SRodney W. Grimes ... 7294250a68eSBosko Milekic} *n1, *n2, *n3, *np, *np_temp; 730afe61c15SRodney W. Grimes 731afe61c15SRodney W. GrimesLIST_INIT(&head); /* Initialize the list. */ 732afe61c15SRodney W. Grimes 733afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 734afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries); 735afe61c15SRodney W. Grimes 736afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 737afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries); 7387658b0a2SJustin T. Gibbs 7397658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 7407658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries); 7417658b0a2SJustin T. Gibbs 7427658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries); /* Deletion. */ 7437658b0a2SJustin T. Gibbsfree(n2); 744afe61c15SRodney W. Grimes /* Forward traversal. */ 74579ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries) 746afe61c15SRodney W. Grimes np-> ... 747afe61c15SRodney W. Grimes 7484250a68eSBosko Milekic /* Safe forward traversal. */ 7494250a68eSBosko MilekicLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 7504250a68eSBosko Milekic np->do_stuff(); 7514250a68eSBosko Milekic ... 7524250a68eSBosko Milekic LIST_REMOVE(np, entries); 7534250a68eSBosko Milekic free(np); 7544250a68eSBosko Milekic} 7554250a68eSBosko Milekic 75679ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) { /* List Deletion. */ 75779ea9bc4SAlexey Zelkin n1 = LIST_FIRST(&head); 7587658b0a2SJustin T. Gibbs LIST_REMOVE(n1, entries); 7597658b0a2SJustin T. Gibbs free(n1); 7607658b0a2SJustin T. Gibbs} 7617658b0a2SJustin T. Gibbs 76203763fe0SJake Burkholdern1 = LIST_FIRST(&head); /* Faster List Deletion. */ 7637658b0a2SJustin T. Gibbswhile (n1 != NULL) { 76479ea9bc4SAlexey Zelkin n2 = LIST_NEXT(n1, entries); 7657658b0a2SJustin T. Gibbs free(n1); 7667658b0a2SJustin T. Gibbs n1 = n2; 7677658b0a2SJustin T. Gibbs} 7687658b0a2SJustin T. GibbsLIST_INIT(&head); 769afe61c15SRodney W. Grimes.Ed 770afe61c15SRodney W. Grimes.Sh TAIL QUEUES 771afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the 772afe61c15SRodney W. Grimes.Nm TAILQ_HEAD 773afe61c15SRodney W. Grimesmacro. 774afe61c15SRodney W. GrimesThis structure contains a pair of pointers, 775afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to 776afe61c15SRodney W. Grimesthe last element in the tail queue. 777afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 778afe61c15SRodney W. Grimesremoved without traversing the tail queue. 779afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element, 7804a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue, 7814a775e8fSJustin T. Gibbsor at the end of the tail queue. 782afe61c15SRodney W. GrimesA 783afe61c15SRodney W. Grimes.Fa TAILQ_HEAD 784afe61c15SRodney W. Grimesstructure is declared as follows: 785afe61c15SRodney W. Grimes.Bd -literal -offset indent 786afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head; 787afe61c15SRodney W. Grimes.Ed 7888f20a914SMike Pritchard.Pp 789afe61c15SRodney W. Grimeswhere 790afe61c15SRodney W. Grimes.Li HEADNAME 791afe61c15SRodney W. Grimesis the name of the structure to be defined, and 792afe61c15SRodney W. Grimes.Li TYPE 793afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue. 794afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as: 795afe61c15SRodney W. Grimes.Bd -literal -offset indent 796afe61c15SRodney W. Grimesstruct HEADNAME *headp; 797afe61c15SRodney W. Grimes.Ed 7988f20a914SMike Pritchard.Pp 799afe61c15SRodney W. Grimes(The names 800afe61c15SRodney W. Grimes.Li head 801afe61c15SRodney W. Grimesand 802afe61c15SRodney W. Grimes.Li headp 803afe61c15SRodney W. Grimesare user selectable.) 804afe61c15SRodney W. Grimes.Pp 805afe61c15SRodney W. GrimesThe macro 80603763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER 80703763fe0SJake Burkholderevaluates to an initializer for the tail queue 80803763fe0SJake Burkholder.Fa head . 80903763fe0SJake Burkholder.Pp 81003763fe0SJake BurkholderThe macro 811d57e28adSThomas Moestl.Nm TAILQ_CONCAT 812d57e28adSThomas Moestlconcatenates the tail queue headed by 813d57e28adSThomas Moestl.Fa head2 814d57e28adSThomas Moestlonto the end of the one headed by 815d57e28adSThomas Moestl.Fa head1 816d57e28adSThomas Moestlremoving all entries from the former. 817d57e28adSThomas Moestl.Pp 818d57e28adSThomas MoestlThe macro 819c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY 820c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue. 821c5c15c16SPoul-Henning Kamp.Pp 822c5c15c16SPoul-Henning KampThe macro 823afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY 824afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 825afe61c15SRodney W. Grimesthe tail queue. 826afe61c15SRodney W. Grimes.Pp 827afe61c15SRodney W. GrimesThe macro 828c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST 829c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue 830c5c15c16SPoul-Henning Kampis empty. 831c5c15c16SPoul-Henning Kamp.Pp 832c5c15c16SPoul-Henning KampThe macro 83379ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH 83479ea9bc4SAlexey Zelkintraverses the tail queue referenced by 83579ea9bc4SAlexey Zelkin.Fa head 83679ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 83779ea9bc4SAlexey Zelkin.Fa var . 838fb5293cfSRuslan Ermilov.Fa var 839fb5293cfSRuslan Ermilovis set to 840fb5293cfSRuslan Ermilov.Dv NULL 841fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements. 84279ea9bc4SAlexey Zelkin.Pp 84379ea9bc4SAlexey ZelkinThe macro 8446c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE 8456c1d0fbfSArchie Cobbstraverses the tail queue referenced by 8466c1d0fbfSArchie Cobbs.Fa head 8476c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to 8486c1d0fbfSArchie Cobbs.Fa var . 8496c1d0fbfSArchie Cobbs.Pp 8502724bce2SAlexander KabaevThe macros 8512724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE 8522724bce2SAlexander Kabaevand 8532724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE 8542724bce2SAlexander Kabaevtraverse the list referenced by 8552724bce2SAlexander Kabaev.Fa head 8562724bce2SAlexander Kabaevin the forward or reverse direction respectively, 8572724bce2SAlexander Kabaevassigning each element in turn to 8582724bce2SAlexander Kabaev.Fa var . 8593b96c71fSMike PritchardHowever, unlike their unsafe counterparts, 8602724bce2SAlexander Kabaev.Nm TAILQ_FOREACH 8612724bce2SAlexander Kabaevand 8622724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE 8632724bce2SAlexander Kabaevpermit to both remove 8642724bce2SAlexander Kabaev.Fa var 8652724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 8662724bce2SAlexander Kabaevtraversal. 8672724bce2SAlexander Kabaev.Pp 8686c1d0fbfSArchie CobbsThe macro 869afe61c15SRodney W. Grimes.Nm TAILQ_INIT 870afe61c15SRodney W. Grimesinitializes the tail queue referenced by 871afe61c15SRodney W. Grimes.Fa head . 872afe61c15SRodney W. Grimes.Pp 873afe61c15SRodney W. GrimesThe macro 874afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD 875afe61c15SRodney W. Grimesinserts the new element 876afe61c15SRodney W. Grimes.Fa elm 877afe61c15SRodney W. Grimesat the head of the tail queue. 878afe61c15SRodney W. Grimes.Pp 879afe61c15SRodney W. GrimesThe macro 880afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL 881afe61c15SRodney W. Grimesinserts the new element 882afe61c15SRodney W. Grimes.Fa elm 883afe61c15SRodney W. Grimesat the end of the tail queue. 884afe61c15SRodney W. Grimes.Pp 885afe61c15SRodney W. GrimesThe macro 886afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER 887afe61c15SRodney W. Grimesinserts the new element 888afe61c15SRodney W. Grimes.Fa elm 889afe61c15SRodney W. Grimesafter the element 890afe61c15SRodney W. Grimes.Fa listelm . 891afe61c15SRodney W. Grimes.Pp 892afe61c15SRodney W. GrimesThe macro 8937658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE 8947658b0a2SJustin T. Gibbsinserts the new element 8957658b0a2SJustin T. Gibbs.Fa elm 8967658b0a2SJustin T. Gibbsbefore the element 8977658b0a2SJustin T. Gibbs.Fa listelm . 8987658b0a2SJustin T. Gibbs.Pp 8997658b0a2SJustin T. GibbsThe macro 900c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST 901c5c15c16SPoul-Henning Kampreturns the last item on the tail queue. 902c5c15c16SPoul-Henning KampIf the tail queue is empty the return value is undefined. 903c5c15c16SPoul-Henning Kamp.Pp 904c5c15c16SPoul-Henning KampThe macro 905c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT 90679ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last. 90779ea9bc4SAlexey Zelkin.Pp 90879ea9bc4SAlexey ZelkinThe macro 90979ea9bc4SAlexey Zelkin.Nm TAILQ_PREV 91079ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item 91179ea9bc4SAlexey Zelkinis the first. 912c5c15c16SPoul-Henning Kamp.Pp 913c5c15c16SPoul-Henning KampThe macro 914afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE 915afe61c15SRodney W. Grimesremoves the element 916afe61c15SRodney W. Grimes.Fa elm 917afe61c15SRodney W. Grimesfrom the tail queue. 918afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE 919afe61c15SRodney W. Grimes.Bd -literal 92003763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head = 92103763fe0SJake Burkholder TAILQ_HEAD_INITIALIZER(head); 922afe61c15SRodney W. Grimesstruct tailhead *headp; /* Tail queue head. */ 923afe61c15SRodney W. Grimesstruct entry { 924afe61c15SRodney W. Grimes ... 925afe61c15SRodney W. Grimes TAILQ_ENTRY(entry) entries; /* Tail queue. */ 926afe61c15SRodney W. Grimes ... 9277658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np; 928afe61c15SRodney W. Grimes 929afe61c15SRodney W. GrimesTAILQ_INIT(&head); /* Initialize the queue. */ 930afe61c15SRodney W. Grimes 931afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 932afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries); 933afe61c15SRodney W. Grimes 934afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 935afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries); 936afe61c15SRodney W. Grimes 937afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 938afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries); 9397658b0a2SJustin T. Gibbs 9407658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 9413652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries); 9427658b0a2SJustin T. Gibbs 9437658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries); /* Deletion. */ 9447658b0a2SJustin T. Gibbsfree(n2); 945afe61c15SRodney W. Grimes /* Forward traversal. */ 94679ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries) 947afe61c15SRodney W. Grimes np-> ... 9482724bce2SAlexander Kabaev /* Safe forward traversal. */ 9492724bce2SAlexander KabaevTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 9502724bce2SAlexander Kabaev np->do_stuff(); 9512724bce2SAlexander Kabaev ... 9522724bce2SAlexander Kabaev TAILQ_REMOVE(&head, np, entries); 9532724bce2SAlexander Kabaev free(np); 9542724bce2SAlexander Kabaev} 9556c1d0fbfSArchie Cobbs /* Reverse traversal. */ 9566c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 9576c1d0fbfSArchie Cobbs np-> ... 9587658b0a2SJustin T. Gibbs /* TailQ Deletion. */ 959d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) { 960c5c15c16SPoul-Henning Kamp n1 = TAILQ_FIRST(&head); 96179ea9bc4SAlexey Zelkin TAILQ_REMOVE(&head, n1, entries); 9627658b0a2SJustin T. Gibbs free(n1); 9637658b0a2SJustin T. Gibbs} 9647658b0a2SJustin T. Gibbs /* Faster TailQ Deletion. */ 965c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head); 9667658b0a2SJustin T. Gibbswhile (n1 != NULL) { 967c5c15c16SPoul-Henning Kamp n2 = TAILQ_NEXT(n1, entries); 9687658b0a2SJustin T. Gibbs free(n1); 9697658b0a2SJustin T. Gibbs n1 = n2; 9707658b0a2SJustin T. Gibbs} 9712724bce2SAlexander Kabaev 9727658b0a2SJustin T. GibbsTAILQ_INIT(&head); 973afe61c15SRodney W. Grimes.Ed 974afe61c15SRodney W. Grimes.Sh HISTORY 975afe61c15SRodney W. GrimesThe 976afe61c15SRodney W. Grimes.Nm queue 97721421932SMike Pritchardfunctions first appeared in 97821421932SMike Pritchard.Bx 4.4 . 979