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 37afe61c15SRodney W. Grimes.Os BSD 4 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 , 434a775e8fSJustin T. Gibbs.Nm SLIST_HEAD , 4403763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER , 454a775e8fSJustin T. Gibbs.Nm SLIST_INIT , 464a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER , 474a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD , 48aea88892SPoul-Henning Kamp.Nm SLIST_NEXT , 494a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD , 504a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE , 5179ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY , 524a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY , 5379ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST , 5479ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH , 554a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD , 5603763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER , 574a775e8fSJustin T. Gibbs.Nm STAILQ_INIT , 584a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER , 594a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD , 604a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL , 6179ea9bc4SAlexey Zelkin.Nm STAILQ_LAST , 6279ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT , 634a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD , 644a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE , 6579ea9bc4SAlexey Zelkin.Nm LIST_EMPTY , 66afe61c15SRodney W. Grimes.Nm LIST_ENTRY , 6779ea9bc4SAlexey Zelkin.Nm LIST_FIRST , 6879ea9bc4SAlexey Zelkin.Nm LIST_FOREACH , 69afe61c15SRodney W. Grimes.Nm LIST_HEAD , 7003763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER , 71afe61c15SRodney W. Grimes.Nm LIST_INIT , 72afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER , 737658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE , 74afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD , 7579ea9bc4SAlexey Zelkin.Nm LIST_NEXT , 76afe61c15SRodney W. Grimes.Nm LIST_REMOVE , 77c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY , 78afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY , 79c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST , 8079ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH , 816c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE , 82afe61c15SRodney W. Grimes.Nm TAILQ_HEAD , 8303763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER , 84afe61c15SRodney W. Grimes.Nm TAILQ_INIT , 85afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER , 867658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE , 87afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD , 88afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL , 89c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST , 90c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT , 9179ea9bc4SAlexey Zelkin.Nm TAILQ_PREV , 92afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE , 934a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues, 9424b85d18SPoul-Henning Kamplists and tail queues 95afe61c15SRodney W. Grimes.Sh SYNOPSIS 96afe61c15SRodney W. Grimes.Fd #include <sys/queue.h> 978f20a914SMike Pritchard.\" 98aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head" 994a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE" 100aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head" 10179ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1024a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE" 10303763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head" 1044a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head" 1054a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 1064a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 107aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 1084a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1094a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 1108f20a914SMike Pritchard.\" 11179ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 1124a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE" 11379ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head" 11479ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1154a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE" 11603763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" 1174a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head" 1184a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 1194a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 1204a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 12179ea9bc4SAlexey Zelkin.Fn STAILQ_LAST "STAILQ_HEAD *head" 12279ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 12302a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1244a775e8fSJustin T. Gibbs.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 1258f20a914SMike Pritchard.\" 12679ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head" 127afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE" 12879ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head" 12979ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 130afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE" 13103763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head" 132afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head" 1334a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 1344a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 135afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 13679ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 137afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 1388f20a914SMike Pritchard.\" 139c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 140afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE" 141c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head" 14279ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 1436c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 144afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE" 14503763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head" 146afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head" 147afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 1483652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 149afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 150afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 15179ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 152c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 15379ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 154afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 1558f20a914SMike Pritchard.\" 156afe61c15SRodney W. Grimes.Sh DESCRIPTION 1574a775e8fSJustin T. GibbsThese macros define and operate on five types of data structures: 1584a775e8fSJustin T. Gibbssingly-linked lists, singly-linked tail queues, lists, tail queues, 1594a775e8fSJustin T. Gibbsand circular queues. 1604a775e8fSJustin T. GibbsAll five structures support the following functionality: 161afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 162afe61c15SRodney W. Grimes.It 163afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list. 164afe61c15SRodney W. Grimes.It 165afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list. 166afe61c15SRodney W. Grimes.It 1674a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list. 1687658b0a2SJustin T. Gibbs.It 1694a775e8fSJustin T. GibbsO(n) removal of any entry in the list. 170afe61c15SRodney W. Grimes.It 171afe61c15SRodney W. GrimesForward traversal through the list. 172afe61c15SRodney W. Grimes.El 173afe61c15SRodney W. Grimes.Pp 1744a775e8fSJustin T. GibbsSingly-linked lists are the simplest of the five data structures 1754a775e8fSJustin T. Gibbsand support only the above functionality. 1764a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets 1774a775e8fSJustin T. Gibbsand few or no removals, 1784a775e8fSJustin T. Gibbsor for implementing a LIFO queue. 1794a775e8fSJustin T. Gibbs.Pp 1804a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality: 1814a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 1824a775e8fSJustin T. Gibbs.It 1834a775e8fSJustin T. GibbsEntries can be added at the end of a list. 1844a775e8fSJustin T. Gibbs.El 1854a775e8fSJustin T. GibbsHowever: 1864a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 1874a775e8fSJustin T. Gibbs.It 1884a775e8fSJustin T. GibbsAll list insertions must specify the head of the list. 1894a775e8fSJustin T. Gibbs.It 1904a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one. 1914a775e8fSJustin T. Gibbs.It 1924a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower 1934a775e8fSJustin T. Gibbsthan singly-linked lists. 1944a775e8fSJustin T. Gibbs.El 1954a775e8fSJustin T. Gibbs.Pp 1964a775e8fSJustin T. GibbsSingly-linked tailqs are ideal for applications with large datasets and 1974a775e8fSJustin T. Gibbsfew or no removals, 1984a775e8fSJustin T. Gibbsor for implementing a FIFO queue. 1994a775e8fSJustin T. Gibbs.Pp 2004a775e8fSJustin T. GibbsAll doubly linked types of data structures (lists, tail queues, and circle 2014a775e8fSJustin T. Gibbsqueues) additionally allow: 2024a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2034a775e8fSJustin T. Gibbs.It 2044a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list. 2054a775e8fSJustin T. Gibbs.It 2064a775e8fSJustin T. GibbsO(1) removal of any entry in the list. 2074a775e8fSJustin T. Gibbs.El 2084a775e8fSJustin T. GibbsHowever: 2094a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2104a775e8fSJustin T. Gibbs.It 2114a775e8fSJustin T. GibbsEach elements requires two pointers rather than one. 2124a775e8fSJustin T. Gibbs.It 2134a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about 2144a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures. 2154a775e8fSJustin T. Gibbs.El 2164a775e8fSJustin T. Gibbs.Pp 2174a775e8fSJustin T. GibbsLinked lists are the simplest of the doubly linked data structures and support 2184a775e8fSJustin T. Gibbsonly the above functionality over singly-linked lists. 219afe61c15SRodney W. Grimes.Pp 220afe61c15SRodney W. GrimesTail queues add the following functionality: 221afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 222afe61c15SRodney W. Grimes.It 223afe61c15SRodney W. GrimesEntries can be added at the end of a list. 2246c1d0fbfSArchie Cobbs.It 2256c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head. 226afe61c15SRodney W. Grimes.El 227afe61c15SRodney W. GrimesHowever: 228afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 229afe61c15SRodney W. Grimes.It 230afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list. 231afe61c15SRodney W. Grimes.It 232afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one. 233afe61c15SRodney W. Grimes.It 234afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower 2354a775e8fSJustin T. Gibbsthan singly-linked lists. 236afe61c15SRodney W. Grimes.El 237afe61c15SRodney W. Grimes.Pp 238afe61c15SRodney W. GrimesIn the macro definitions, 239afe61c15SRodney W. Grimes.Fa TYPE 240afe61c15SRodney W. Grimesis the name of a user defined structure, 241afe61c15SRodney W. Grimesthat must contain a field of type 2424a775e8fSJustin T. Gibbs.Li SLIST_ENTRY , 2434a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY , 244afe61c15SRodney W. Grimes.Li LIST_ENTRY , 245afe61c15SRodney W. Grimesor 24624b85d18SPoul-Henning Kamp.Li TAILQ_ENTRY , 247afe61c15SRodney W. Grimesnamed 248afe61c15SRodney W. Grimes.Fa NAME . 249afe61c15SRodney W. GrimesThe argument 250afe61c15SRodney W. Grimes.Fa HEADNAME 251afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared 252afe61c15SRodney W. Grimesusing the macros 2534a775e8fSJustin T. Gibbs.Li SLIST_HEAD , 2544a775e8fSJustin T. Gibbs.Li STAILQ_HEAD , 255afe61c15SRodney W. Grimes.Li LIST_HEAD , 256afe61c15SRodney W. Grimesor 25724b85d18SPoul-Henning Kamp.Li TAILQ_HEAD . 258afe61c15SRodney W. GrimesSee the examples below for further explanation of how these 259afe61c15SRodney W. Grimesmacros are used. 2604a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS 2614a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the 2624a775e8fSJustin T. Gibbs.Nm SLIST_HEAD 2634a775e8fSJustin T. Gibbsmacro. 2644a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element 2654a775e8fSJustin T. Gibbson the list. 2664a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation 2674a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements. 2684a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or 2694a775e8fSJustin T. Gibbsat the head of the list. 2704a775e8fSJustin T. GibbsAn 2714a775e8fSJustin T. Gibbs.Fa SLIST_HEAD 2724a775e8fSJustin T. Gibbsstructure is declared as follows: 2734a775e8fSJustin T. Gibbs.Bd -literal -offset indent 2744a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head; 2754a775e8fSJustin T. Gibbs.Ed 2768f20a914SMike Pritchard.Pp 2774a775e8fSJustin T. Gibbswhere 2784a775e8fSJustin T. Gibbs.Fa HEADNAME 2794a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 2804a775e8fSJustin T. Gibbs.Fa TYPE 2814a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list. 2824a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as: 2834a775e8fSJustin T. Gibbs.Bd -literal -offset indent 2844a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 2854a775e8fSJustin T. Gibbs.Ed 2868f20a914SMike Pritchard.Pp 2874a775e8fSJustin T. Gibbs(The names 2884a775e8fSJustin T. Gibbs.Li head 2894a775e8fSJustin T. Gibbsand 2904a775e8fSJustin T. Gibbs.Li headp 2914a775e8fSJustin T. Gibbsare user selectable.) 2924a775e8fSJustin T. Gibbs.Pp 2934a775e8fSJustin T. GibbsThe macro 29403763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER 29503763fe0SJake Burkholderevaluates to an initializer for the list 29603763fe0SJake Burkholder.Fa head . 29703763fe0SJake Burkholder.Pp 29803763fe0SJake BurkholderThe macro 29979ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY 30079ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list. 30179ea9bc4SAlexey Zelkin.Pp 30279ea9bc4SAlexey ZelkinThe macro 3034a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY 3044a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 3054a775e8fSJustin T. Gibbsthe list. 3064a775e8fSJustin T. Gibbs.Pp 3074a775e8fSJustin T. GibbsThe macro 30879ea9bc4SAlexey Zelkin.Nm SLIST_FIRST 30979ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty. 31079ea9bc4SAlexey Zelkin.Pp 31179ea9bc4SAlexey ZelkinThe macro 31279ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH 31379ea9bc4SAlexey Zelkintraverses the list referenced by 31479ea9bc4SAlexey Zelkin.Fa head 31579ea9bc4SAlexey Zelkinin the forward direction, assigning each element in 31679ea9bc4SAlexey Zelkinturn to 31779ea9bc4SAlexey Zelkin.Fa var . 31879ea9bc4SAlexey Zelkin.Pp 31979ea9bc4SAlexey ZelkinThe macro 3204a775e8fSJustin T. Gibbs.Nm SLIST_INIT 3214a775e8fSJustin T. Gibbsinitializes the list referenced by 3224a775e8fSJustin T. Gibbs.Fa head . 3234a775e8fSJustin T. Gibbs.Pp 3244a775e8fSJustin T. GibbsThe macro 3254a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD 3264a775e8fSJustin T. Gibbsinserts the new element 3274a775e8fSJustin T. Gibbs.Fa elm 3284a775e8fSJustin T. Gibbsat the head of the list. 3294a775e8fSJustin T. Gibbs.Pp 3304a775e8fSJustin T. GibbsThe macro 3314a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER 3324a775e8fSJustin T. Gibbsinserts the new element 3334a775e8fSJustin T. Gibbs.Fa elm 3344a775e8fSJustin T. Gibbsafter the element 3354a775e8fSJustin T. Gibbs.Fa listelm . 3364a775e8fSJustin T. Gibbs.Pp 3374a775e8fSJustin T. GibbsThe macro 33879ea9bc4SAlexey Zelkin.Nm SLIST_NEXT 33979ea9bc4SAlexey Zelkinreturns the next element in the list. 34079ea9bc4SAlexey Zelkin.Pp 34179ea9bc4SAlexey ZelkinThe macro 3424a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD 3434a775e8fSJustin T. Gibbsremoves the element 3444a775e8fSJustin T. Gibbs.Fa elm 3454a775e8fSJustin T. Gibbsfrom the head of the list. 3464a775e8fSJustin T. GibbsFor optimum efficiency, 3474a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use 3484a775e8fSJustin T. Gibbsthis macro instead of the generic 3494a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE 3504a775e8fSJustin T. Gibbsmacro. 3514a775e8fSJustin T. Gibbs.Pp 3524a775e8fSJustin T. GibbsThe macro 3534a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE 3544a775e8fSJustin T. Gibbsremoves the element 3554a775e8fSJustin T. Gibbs.Fa elm 3564a775e8fSJustin T. Gibbsfrom the list. 3574a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE 3584a775e8fSJustin T. Gibbs.Bd -literal 35903763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head = 36003763fe0SJake Burkholder SLIST_HEAD_INITIALIZER(head); 3614a775e8fSJustin T. Gibbsstruct slisthead *headp; /* Singly-linked List head. */ 3624a775e8fSJustin T. Gibbsstruct entry { 3634a775e8fSJustin T. Gibbs ... 3644a775e8fSJustin T. Gibbs SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 3654a775e8fSJustin T. Gibbs ... 3664a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 3674a775e8fSJustin T. Gibbs 3684a775e8fSJustin T. GibbsSLIST_INIT(&head); /* Initialize the list. */ 3694a775e8fSJustin T. Gibbs 3704a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 3714a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries); 3724a775e8fSJustin T. Gibbs 3734a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 3744a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries); 3754a775e8fSJustin T. Gibbs 3764a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 3774a775e8fSJustin T. Gibbsfree(n2); 3784a775e8fSJustin T. Gibbs 37979ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head); 38003763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 3814a775e8fSJustin T. Gibbsfree(n3); 3824a775e8fSJustin T. Gibbs /* Forward traversal. */ 38379ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries) 3844a775e8fSJustin T. Gibbs np-> ... 3854a775e8fSJustin T. Gibbs 38679ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) { /* List Deletion. */ 38779ea9bc4SAlexey Zelkin n1 = SLIST_FIRST(&head); 3884a775e8fSJustin T. Gibbs SLIST_REMOVE_HEAD(&head, entries); 3894a775e8fSJustin T. Gibbs free(n1); 3904a775e8fSJustin T. Gibbs} 3914a775e8fSJustin T. Gibbs.Ed 3924a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES 3934a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the 3944a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD 3954a775e8fSJustin T. Gibbsmacro. 3964a775e8fSJustin T. GibbsThis structure contains a pair of pointers, 3974a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to 3984a775e8fSJustin T. Gibbsthe last element in the tail queue. 3994a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer 4004a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary 4014a775e8fSJustin T. Gibbselements. 4024a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element, 4034a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue. 4044a775e8fSJustin T. GibbsA 4054a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD 4064a775e8fSJustin T. Gibbsstructure is declared as follows: 4074a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4084a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head; 4094a775e8fSJustin T. Gibbs.Ed 4108f20a914SMike Pritchard.Pp 4114a775e8fSJustin T. Gibbswhere 4124a775e8fSJustin T. Gibbs.Li HEADNAME 4134a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 4144a775e8fSJustin T. Gibbs.Li TYPE 4154a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue. 4164a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as: 4174a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4184a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 4194a775e8fSJustin T. Gibbs.Ed 4208f20a914SMike Pritchard.Pp 4214a775e8fSJustin T. Gibbs(The names 4224a775e8fSJustin T. Gibbs.Li head 4234a775e8fSJustin T. Gibbsand 4244a775e8fSJustin T. Gibbs.Li headp 4254a775e8fSJustin T. Gibbsare user selectable.) 4264a775e8fSJustin T. Gibbs.Pp 4274a775e8fSJustin T. GibbsThe macro 42803763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER 42903763fe0SJake Burkholderevaluates to an initializer for the tail queue 43003763fe0SJake Burkholder.Fa head . 43103763fe0SJake Burkholder.Pp 43203763fe0SJake BurkholderThe macro 43379ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY 43479ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue. 43579ea9bc4SAlexey Zelkin.Pp 43679ea9bc4SAlexey ZelkinThe macro 4374a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY 4384a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 4394a775e8fSJustin T. Gibbsthe tail queue. 4404a775e8fSJustin T. Gibbs.Pp 4414a775e8fSJustin T. GibbsThe macro 44279ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST 44379ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue 44479ea9bc4SAlexey Zelkinis empty. 44579ea9bc4SAlexey Zelkin.Pp 44679ea9bc4SAlexey ZelkinThe macro 44779ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH 44879ea9bc4SAlexey Zelkintraverses the tail queue referenced by 44979ea9bc4SAlexey Zelkin.Fa head 45079ea9bc4SAlexey Zelkinin the forward direction, assigning each element 45179ea9bc4SAlexey Zelkinin turn to 45279ea9bc4SAlexey Zelkin.Fa var . 45379ea9bc4SAlexey Zelkin.Pp 45479ea9bc4SAlexey ZelkinThe macro 4554a775e8fSJustin T. Gibbs.Nm STAILQ_INIT 4564a775e8fSJustin T. Gibbsinitializes the tail queue referenced by 4574a775e8fSJustin T. Gibbs.Fa head . 4584a775e8fSJustin T. Gibbs.Pp 4594a775e8fSJustin T. GibbsThe macro 4604a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD 4614a775e8fSJustin T. Gibbsinserts the new element 4624a775e8fSJustin T. Gibbs.Fa elm 4634a775e8fSJustin T. Gibbsat the head of the tail queue. 4644a775e8fSJustin T. Gibbs.Pp 4654a775e8fSJustin T. GibbsThe macro 4664a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL 4674a775e8fSJustin T. Gibbsinserts the new element 4684a775e8fSJustin T. Gibbs.Fa elm 4694a775e8fSJustin T. Gibbsat the end of the tail queue. 4704a775e8fSJustin T. Gibbs.Pp 4714a775e8fSJustin T. GibbsThe macro 4724a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER 4734a775e8fSJustin T. Gibbsinserts the new element 4744a775e8fSJustin T. Gibbs.Fa elm 4754a775e8fSJustin T. Gibbsafter the element 4764a775e8fSJustin T. Gibbs.Fa listelm . 4774a775e8fSJustin T. Gibbs.Pp 4784a775e8fSJustin T. GibbsThe macro 47979ea9bc4SAlexey Zelkin.Nm STAILQ_LAST 48079ea9bc4SAlexey Zelkinreturns the last item on the tail queue. 48179ea9bc4SAlexey ZelkinIf the tail queue is empty the return value is undefined. 48279ea9bc4SAlexey Zelkin.Pp 48379ea9bc4SAlexey ZelkinThe macro 48479ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT 48579ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last. 48679ea9bc4SAlexey Zelkin.Pp 48779ea9bc4SAlexey ZelkinThe macro 4884a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD 489ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue. 4904a775e8fSJustin T. GibbsFor optimum efficiency, 4914a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should 4924a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic 4934a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE 4944a775e8fSJustin T. Gibbsmacro. 4954a775e8fSJustin T. Gibbs.Pp 4964a775e8fSJustin T. GibbsThe macro 4974a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE 4984a775e8fSJustin T. Gibbsremoves the element 4994a775e8fSJustin T. Gibbs.Fa elm 5004a775e8fSJustin T. Gibbsfrom the tail queue. 5014a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE 5024a775e8fSJustin T. Gibbs.Bd -literal 50303763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head = 50403763fe0SJake Burkholder STAILQ_HEAD_INITIALIZER(head); 5054a775e8fSJustin T. Gibbsstruct stailhead *headp; /* Singly-linked tail queue head. */ 5064a775e8fSJustin T. Gibbsstruct entry { 5074a775e8fSJustin T. Gibbs ... 5084a775e8fSJustin T. Gibbs STAILQ_ENTRY(entry) entries; /* Tail queue. */ 5094a775e8fSJustin T. Gibbs ... 5104a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 5114a775e8fSJustin T. Gibbs 5124a775e8fSJustin T. GibbsSTAILQ_INIT(&head); /* Initialize the queue. */ 5134a775e8fSJustin T. Gibbs 5144a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 5154a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries); 5164a775e8fSJustin T. Gibbs 5174a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 5184a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries); 5194a775e8fSJustin T. Gibbs 5204a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 5214a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries); 5224a775e8fSJustin T. Gibbs /* Deletion. */ 5234a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries); 5244a775e8fSJustin T. Gibbsfree(n2); 52503763fe0SJake Burkholder /* Deletion from the head. */ 52679ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head); 5274a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries); 5284a775e8fSJustin T. Gibbsfree(n3); 5294a775e8fSJustin T. Gibbs /* Forward traversal. */ 53079ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries) 5314a775e8fSJustin T. Gibbs np-> ... 5324a775e8fSJustin T. Gibbs /* TailQ Deletion. */ 53379ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) { 53403763fe0SJake Burkholder n1 = STAILQ_FIRST(&head); 535266e33fdSJohn Baldwin STAILQ_REMOVE_HEAD(&head, entries); 5364a775e8fSJustin T. Gibbs free(n1); 5374a775e8fSJustin T. Gibbs} 5384a775e8fSJustin T. Gibbs /* Faster TailQ Deletion. */ 53979ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head); 5404a775e8fSJustin T. Gibbswhile (n1 != NULL) { 54179ea9bc4SAlexey Zelkin n2 = STAILQ_NEXT(n1, entries); 5424a775e8fSJustin T. Gibbs free(n1); 5434a775e8fSJustin T. Gibbs n1 = n2; 5444a775e8fSJustin T. Gibbs} 5454a775e8fSJustin T. GibbsSTAILQ_INIT(&head); 5464a775e8fSJustin T. Gibbs.Ed 547afe61c15SRodney W. Grimes.Sh LISTS 548afe61c15SRodney W. GrimesA list is headed by a structure defined by the 549afe61c15SRodney W. Grimes.Nm LIST_HEAD 550afe61c15SRodney W. Grimesmacro. 551afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element 552afe61c15SRodney W. Grimeson the list. 553afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 554afe61c15SRodney W. Grimesremoved without traversing the list. 5554a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element, 5564a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list. 557afe61c15SRodney W. GrimesA 558afe61c15SRodney W. Grimes.Fa LIST_HEAD 559afe61c15SRodney W. Grimesstructure is declared as follows: 560afe61c15SRodney W. Grimes.Bd -literal -offset indent 561afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head; 562afe61c15SRodney W. Grimes.Ed 5638f20a914SMike Pritchard.Pp 564afe61c15SRodney W. Grimeswhere 565afe61c15SRodney W. Grimes.Fa HEADNAME 566afe61c15SRodney W. Grimesis the name of the structure to be defined, and 567afe61c15SRodney W. Grimes.Fa TYPE 568afe61c15SRodney W. Grimesis the type of the elements to be linked into the list. 569afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as: 570afe61c15SRodney W. Grimes.Bd -literal -offset indent 571afe61c15SRodney W. Grimesstruct HEADNAME *headp; 572afe61c15SRodney W. Grimes.Ed 5738f20a914SMike Pritchard.Pp 574afe61c15SRodney W. Grimes(The names 575afe61c15SRodney W. Grimes.Li head 576afe61c15SRodney W. Grimesand 577afe61c15SRodney W. Grimes.Li headp 578afe61c15SRodney W. Grimesare user selectable.) 579afe61c15SRodney W. Grimes.Pp 580afe61c15SRodney W. GrimesThe macro 58103763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER 58203763fe0SJake Burkholderevaluates to an initializer for the list 58303763fe0SJake Burkholder.Fa head . 58403763fe0SJake Burkholder.Pp 58503763fe0SJake BurkholderThe macro 58679ea9bc4SAlexey Zelkin.Nm LIST_EMPTY 58779ea9bc4SAlexey Zelkinevaluates to true if their are no elements in the list. 58879ea9bc4SAlexey Zelkin.Pp 58979ea9bc4SAlexey ZelkinThe macro 590afe61c15SRodney W. Grimes.Nm LIST_ENTRY 591afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 592afe61c15SRodney W. Grimesthe list. 593afe61c15SRodney W. Grimes.Pp 594afe61c15SRodney W. GrimesThe macro 59579ea9bc4SAlexey Zelkin.Nm LIST_FIRST 59679ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list 59779ea9bc4SAlexey Zelkinis empty. 59879ea9bc4SAlexey Zelkin.Pp 59979ea9bc4SAlexey ZelkinThe macro 60079ea9bc4SAlexey Zelkin.Nm LIST_FOREACH 60179ea9bc4SAlexey Zelkintraverses the list referenced by 60279ea9bc4SAlexey Zelkin.Fa head 60379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 60479ea9bc4SAlexey Zelkin.Fa var . 60579ea9bc4SAlexey Zelkin.Pp 60679ea9bc4SAlexey ZelkinThe macro 607afe61c15SRodney W. Grimes.Nm LIST_INIT 608afe61c15SRodney W. Grimesinitializes the list referenced by 609afe61c15SRodney W. Grimes.Fa head . 610afe61c15SRodney W. Grimes.Pp 611afe61c15SRodney W. GrimesThe macro 612afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD 613afe61c15SRodney W. Grimesinserts the new element 614afe61c15SRodney W. Grimes.Fa elm 615afe61c15SRodney W. Grimesat the head of the list. 616afe61c15SRodney W. Grimes.Pp 617afe61c15SRodney W. GrimesThe macro 618afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER 619afe61c15SRodney W. Grimesinserts the new element 620afe61c15SRodney W. Grimes.Fa elm 621afe61c15SRodney W. Grimesafter the element 622afe61c15SRodney W. Grimes.Fa listelm . 623afe61c15SRodney W. Grimes.Pp 624afe61c15SRodney W. GrimesThe macro 6257658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE 6267658b0a2SJustin T. Gibbsinserts the new element 6277658b0a2SJustin T. Gibbs.Fa elm 6287658b0a2SJustin T. Gibbsbefore the element 6297658b0a2SJustin T. Gibbs.Fa listelm . 6307658b0a2SJustin T. Gibbs.Pp 6317658b0a2SJustin T. GibbsThe macro 63279ea9bc4SAlexey Zelkin.Nm LIST_NEXT 63379ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last. 63479ea9bc4SAlexey Zelkin.Pp 63579ea9bc4SAlexey ZelkinThe macro 636afe61c15SRodney W. Grimes.Nm LIST_REMOVE 637afe61c15SRodney W. Grimesremoves the element 638afe61c15SRodney W. Grimes.Fa elm 639afe61c15SRodney W. Grimesfrom the list. 640afe61c15SRodney W. Grimes.Sh LIST EXAMPLE 641afe61c15SRodney W. Grimes.Bd -literal 64203763fe0SJake BurkholderLIST_HEAD(listhead, entry) head = 64303763fe0SJake Burkholder LIST_HEAD_INITIALIZER(head); 644afe61c15SRodney W. Grimesstruct listhead *headp; /* List head. */ 645afe61c15SRodney W. Grimesstruct entry { 646afe61c15SRodney W. Grimes ... 647afe61c15SRodney W. Grimes LIST_ENTRY(entry) entries; /* List. */ 648afe61c15SRodney W. Grimes ... 6497658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np; 650afe61c15SRodney W. Grimes 651afe61c15SRodney W. GrimesLIST_INIT(&head); /* Initialize the list. */ 652afe61c15SRodney W. Grimes 653afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 654afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries); 655afe61c15SRodney W. Grimes 656afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 657afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries); 6587658b0a2SJustin T. Gibbs 6597658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 6607658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries); 6617658b0a2SJustin T. Gibbs 6627658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries); /* Deletion. */ 6637658b0a2SJustin T. Gibbsfree(n2); 664afe61c15SRodney W. Grimes /* Forward traversal. */ 66579ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries) 666afe61c15SRodney W. Grimes np-> ... 667afe61c15SRodney W. Grimes 66879ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) { /* List Deletion. */ 66979ea9bc4SAlexey Zelkin n1 = LIST_FIRST(&head); 6707658b0a2SJustin T. Gibbs LIST_REMOVE(n1, entries); 6717658b0a2SJustin T. Gibbs free(n1); 6727658b0a2SJustin T. Gibbs} 6737658b0a2SJustin T. Gibbs 67403763fe0SJake Burkholdern1 = LIST_FIRST(&head); /* Faster List Deletion. */ 6757658b0a2SJustin T. Gibbswhile (n1 != NULL) { 67679ea9bc4SAlexey Zelkin n2 = LIST_NEXT(n1, entries); 6777658b0a2SJustin T. Gibbs free(n1); 6787658b0a2SJustin T. Gibbs n1 = n2; 6797658b0a2SJustin T. Gibbs} 6807658b0a2SJustin T. GibbsLIST_INIT(&head); 681afe61c15SRodney W. Grimes.Ed 682afe61c15SRodney W. Grimes.Sh TAIL QUEUES 683afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the 684afe61c15SRodney W. Grimes.Nm TAILQ_HEAD 685afe61c15SRodney W. Grimesmacro. 686afe61c15SRodney W. GrimesThis structure contains a pair of pointers, 687afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to 688afe61c15SRodney W. Grimesthe last element in the tail queue. 689afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 690afe61c15SRodney W. Grimesremoved without traversing the tail queue. 691afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element, 6924a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue, 6934a775e8fSJustin T. Gibbsor at the end of the tail queue. 694afe61c15SRodney W. GrimesA 695afe61c15SRodney W. Grimes.Fa TAILQ_HEAD 696afe61c15SRodney W. Grimesstructure is declared as follows: 697afe61c15SRodney W. Grimes.Bd -literal -offset indent 698afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head; 699afe61c15SRodney W. Grimes.Ed 7008f20a914SMike Pritchard.Pp 701afe61c15SRodney W. Grimeswhere 702afe61c15SRodney W. Grimes.Li HEADNAME 703afe61c15SRodney W. Grimesis the name of the structure to be defined, and 704afe61c15SRodney W. Grimes.Li TYPE 705afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue. 706afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as: 707afe61c15SRodney W. Grimes.Bd -literal -offset indent 708afe61c15SRodney W. Grimesstruct HEADNAME *headp; 709afe61c15SRodney W. Grimes.Ed 7108f20a914SMike Pritchard.Pp 711afe61c15SRodney W. Grimes(The names 712afe61c15SRodney W. Grimes.Li head 713afe61c15SRodney W. Grimesand 714afe61c15SRodney W. Grimes.Li headp 715afe61c15SRodney W. Grimesare user selectable.) 716afe61c15SRodney W. Grimes.Pp 717afe61c15SRodney W. GrimesThe macro 71803763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER 71903763fe0SJake Burkholderevaluates to an initializer for the tail queue 72003763fe0SJake Burkholder.Fa head . 72103763fe0SJake Burkholder.Pp 72203763fe0SJake BurkholderThe macro 723c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY 724c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue. 725c5c15c16SPoul-Henning Kamp.Pp 726c5c15c16SPoul-Henning KampThe macro 727afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY 728afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 729afe61c15SRodney W. Grimesthe tail queue. 730afe61c15SRodney W. Grimes.Pp 731afe61c15SRodney W. GrimesThe macro 732c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST 733c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue 734c5c15c16SPoul-Henning Kampis empty. 735c5c15c16SPoul-Henning Kamp.Pp 736c5c15c16SPoul-Henning KampThe macro 73779ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH 73879ea9bc4SAlexey Zelkintraverses the tail queue referenced by 73979ea9bc4SAlexey Zelkin.Fa head 74079ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 74179ea9bc4SAlexey Zelkin.Fa var . 74279ea9bc4SAlexey Zelkin.Pp 74379ea9bc4SAlexey ZelkinThe macro 7446c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE 7456c1d0fbfSArchie Cobbstraverses the tail queue referenced by 7466c1d0fbfSArchie Cobbs.Fa head 7476c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to 7486c1d0fbfSArchie Cobbs.Fa var . 7496c1d0fbfSArchie Cobbs.Pp 7506c1d0fbfSArchie CobbsThe macro 751afe61c15SRodney W. Grimes.Nm TAILQ_INIT 752afe61c15SRodney W. Grimesinitializes the tail queue referenced by 753afe61c15SRodney W. Grimes.Fa head . 754afe61c15SRodney W. Grimes.Pp 755afe61c15SRodney W. GrimesThe macro 756afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD 757afe61c15SRodney W. Grimesinserts the new element 758afe61c15SRodney W. Grimes.Fa elm 759afe61c15SRodney W. Grimesat the head of the tail queue. 760afe61c15SRodney W. Grimes.Pp 761afe61c15SRodney W. GrimesThe macro 762afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL 763afe61c15SRodney W. Grimesinserts the new element 764afe61c15SRodney W. Grimes.Fa elm 765afe61c15SRodney W. Grimesat the end of the tail queue. 766afe61c15SRodney W. Grimes.Pp 767afe61c15SRodney W. GrimesThe macro 768afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER 769afe61c15SRodney W. Grimesinserts the new element 770afe61c15SRodney W. Grimes.Fa elm 771afe61c15SRodney W. Grimesafter the element 772afe61c15SRodney W. Grimes.Fa listelm . 773afe61c15SRodney W. Grimes.Pp 774afe61c15SRodney W. GrimesThe macro 7757658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE 7767658b0a2SJustin T. Gibbsinserts the new element 7777658b0a2SJustin T. Gibbs.Fa elm 7787658b0a2SJustin T. Gibbsbefore the element 7797658b0a2SJustin T. Gibbs.Fa listelm . 7807658b0a2SJustin T. Gibbs.Pp 7817658b0a2SJustin T. GibbsThe macro 782c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST 783c5c15c16SPoul-Henning Kampreturns the last item on the tail queue. 784c5c15c16SPoul-Henning KampIf the tail queue is empty the return value is undefined. 785c5c15c16SPoul-Henning Kamp.Pp 786c5c15c16SPoul-Henning KampThe macro 787c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT 78879ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last. 78979ea9bc4SAlexey Zelkin.Pp 79079ea9bc4SAlexey ZelkinThe macro 79179ea9bc4SAlexey Zelkin.Nm TAILQ_PREV 79279ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item 79379ea9bc4SAlexey Zelkinis the first. 794c5c15c16SPoul-Henning Kamp.Pp 795c5c15c16SPoul-Henning KampThe macro 796afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE 797afe61c15SRodney W. Grimesremoves the element 798afe61c15SRodney W. Grimes.Fa elm 799afe61c15SRodney W. Grimesfrom the tail queue. 800afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE 801afe61c15SRodney W. Grimes.Bd -literal 80203763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head = 80303763fe0SJake Burkholder TAILQ_HEAD_INITIALIZER(head); 804afe61c15SRodney W. Grimesstruct tailhead *headp; /* Tail queue head. */ 805afe61c15SRodney W. Grimesstruct entry { 806afe61c15SRodney W. Grimes ... 807afe61c15SRodney W. Grimes TAILQ_ENTRY(entry) entries; /* Tail queue. */ 808afe61c15SRodney W. Grimes ... 8097658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np; 810afe61c15SRodney W. Grimes 811afe61c15SRodney W. GrimesTAILQ_INIT(&head); /* Initialize the queue. */ 812afe61c15SRodney W. Grimes 813afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 814afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries); 815afe61c15SRodney W. Grimes 816afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 817afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries); 818afe61c15SRodney W. Grimes 819afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 820afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries); 8217658b0a2SJustin T. Gibbs 8227658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 8233652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries); 8247658b0a2SJustin T. Gibbs 8257658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries); /* Deletion. */ 8267658b0a2SJustin T. Gibbsfree(n2); 827afe61c15SRodney W. Grimes /* Forward traversal. */ 82879ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries) 829afe61c15SRodney W. Grimes np-> ... 8306c1d0fbfSArchie Cobbs /* Reverse traversal. */ 8316c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 8326c1d0fbfSArchie Cobbs np-> ... 8337658b0a2SJustin T. Gibbs /* TailQ Deletion. */ 834d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) { 835c5c15c16SPoul-Henning Kamp n1 = TAILQ_FIRST(&head); 83679ea9bc4SAlexey Zelkin TAILQ_REMOVE(&head, n1, entries); 8377658b0a2SJustin T. Gibbs free(n1); 8387658b0a2SJustin T. Gibbs} 8397658b0a2SJustin T. Gibbs /* Faster TailQ Deletion. */ 840c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head); 8417658b0a2SJustin T. Gibbswhile (n1 != NULL) { 842c5c15c16SPoul-Henning Kamp n2 = TAILQ_NEXT(n1, entries); 8437658b0a2SJustin T. Gibbs free(n1); 8447658b0a2SJustin T. Gibbs n1 = n2; 8457658b0a2SJustin T. Gibbs} 8467658b0a2SJustin T. GibbsTAILQ_INIT(&head); 847afe61c15SRodney W. Grimes.Ed 848afe61c15SRodney W. Grimes.Sh HISTORY 849afe61c15SRodney W. GrimesThe 850afe61c15SRodney W. Grimes.Nm queue 85121421932SMike Pritchardfunctions first appeared in 85221421932SMike Pritchard.Bx 4.4 . 853