1afe61c15SRodney W. Grimes.\" Copyright (c) 1993 2afe61c15SRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 3afe61c15SRodney W. Grimes.\" 4afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 5afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions 6afe61c15SRodney W. Grimes.\" are met: 7afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 8afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 9afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 10afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 11afe61c15SRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12dda5b397SEitan Adler.\" 3. Neither the name of the University nor the names of its contributors 13afe61c15SRodney W. Grimes.\" may be used to endorse or promote products derived from this software 14afe61c15SRodney W. Grimes.\" without specific prior written permission. 15afe61c15SRodney W. Grimes.\" 16afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19afe61c15SRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26afe61c15SRodney W. Grimes.\" SUCH DAMAGE. 27afe61c15SRodney W. Grimes.\" 28*d2870b86SMark Johnston.Dd February 10, 2025 29afe61c15SRodney W. Grimes.Dt QUEUE 3 303d45e180SRuslan Ermilov.Os 31afe61c15SRodney W. Grimes.Sh NAME 3238b622e1SHans Petter Selasky.Nm SLIST_CLASS_ENTRY , 3338b622e1SHans Petter Selasky.Nm SLIST_CLASS_HEAD , 3465d31997SKirk McKusick.Nm SLIST_CONCAT , 35aea88892SPoul-Henning Kamp.Nm SLIST_EMPTY , 36*d2870b86SMark Johnston.Nm SLIST_EMPTY_ATOMIC , 374a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY , 38aea88892SPoul-Henning Kamp.Nm SLIST_FIRST , 3979ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH , 407ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM , 417ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE , 4238b622e1SHans Petter Selasky.Nm SLIST_FOREACH_SAFE , 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 , 4938b622e1SHans Petter Selasky.Nm SLIST_REMOVE , 503d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER , 514a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD , 52359295cdSMatthew D Fleming.Nm SLIST_SWAP , 5338b622e1SHans Petter Selasky.Nm STAILQ_CLASS_ENTRY , 5438b622e1SHans Petter Selasky.Nm STAILQ_CLASS_HEAD , 55d57e28adSThomas Moestl.Nm STAILQ_CONCAT , 5679ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY , 57*d2870b86SMark Johnston.Nm STAILQ_EMPTY_ATOMIC , 584a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY , 5979ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST , 6079ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH , 617ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM , 627ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE , 6338b622e1SHans Petter Selasky.Nm STAILQ_FOREACH_SAFE , 644a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD , 6503763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER , 664a775e8fSJustin T. Gibbs.Nm STAILQ_INIT , 674a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER , 684a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD , 694a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL , 7079ea9bc4SAlexey Zelkin.Nm STAILQ_LAST , 7179ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT , 7238b622e1SHans Petter Selasky.Nm STAILQ_REMOVE , 733d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER , 744a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD , 75359295cdSMatthew D Fleming.Nm STAILQ_SWAP , 7638b622e1SHans Petter Selasky.Nm LIST_CLASS_ENTRY , 7738b622e1SHans Petter Selasky.Nm LIST_CLASS_HEAD , 7865d31997SKirk McKusick.Nm LIST_CONCAT , 7979ea9bc4SAlexey Zelkin.Nm LIST_EMPTY , 80*d2870b86SMark Johnston.Nm LIST_EMPTY_ATOMIC , 81afe61c15SRodney W. Grimes.Nm LIST_ENTRY , 8279ea9bc4SAlexey Zelkin.Nm LIST_FIRST , 8379ea9bc4SAlexey Zelkin.Nm LIST_FOREACH , 847ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM , 857ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM_SAFE , 8638b622e1SHans Petter Selasky.Nm LIST_FOREACH_SAFE , 87afe61c15SRodney W. Grimes.Nm LIST_HEAD , 8803763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER , 89afe61c15SRodney W. Grimes.Nm LIST_INIT , 90afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER , 917658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE , 92afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD , 9379ea9bc4SAlexey Zelkin.Nm LIST_NEXT , 944170b083SEd Schouten.Nm LIST_PREV , 95afe61c15SRodney W. Grimes.Nm LIST_REMOVE , 967f479deeSDag-Erling Smørgrav.Nm LIST_REPLACE , 97359295cdSMatthew D Fleming.Nm LIST_SWAP , 9838b622e1SHans Petter Selasky.Nm TAILQ_CLASS_ENTRY , 9938b622e1SHans Petter Selasky.Nm TAILQ_CLASS_HEAD , 100d57e28adSThomas Moestl.Nm TAILQ_CONCAT , 101c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY , 102*d2870b86SMark Johnston.Nm TAILQ_EMPTY_ATOMIC , 103afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY , 104c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST , 10579ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH , 1067ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM , 1077ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE , 1086c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE , 1097ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM , 1107ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE , 11138b622e1SHans Petter Selasky.Nm TAILQ_FOREACH_REVERSE_SAFE , 11238b622e1SHans Petter Selasky.Nm TAILQ_FOREACH_SAFE , 113afe61c15SRodney W. Grimes.Nm TAILQ_HEAD , 11403763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER , 115afe61c15SRodney W. Grimes.Nm TAILQ_INIT , 116afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER , 1177658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE , 118afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD , 119afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL , 120c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST , 121c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT , 12279ea9bc4SAlexey Zelkin.Nm TAILQ_PREV , 123359295cdSMatthew D Fleming.Nm TAILQ_REMOVE , 1247f479deeSDag-Erling Smørgrav.Nm TAILQ_REPLACE , 125359295cdSMatthew D Fleming.Nm TAILQ_SWAP 1264a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues, 12724b85d18SPoul-Henning Kamplists and tail queues 128afe61c15SRodney W. Grimes.Sh SYNOPSIS 12932eef9aeSRuslan Ermilov.In sys/queue.h 1308f20a914SMike Pritchard.\" 13138b622e1SHans Petter Selasky.Fn SLIST_CLASS_ENTRY "CLASSTYPE" 13238b622e1SHans Petter Selasky.Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE" 13365d31997SKirk McKusick.Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME" 134aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head" 135*d2870b86SMark Johnston.Fn SLIST_EMPTY_ATOMIC "SLIST_HEAD *head" 1364a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE" 137aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head" 13879ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1397ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1407ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 14138b622e1SHans Petter Selasky.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 1424a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE" 14303763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head" 1444a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head" 1454a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 1464a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 147aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 14838b622e1SHans Petter Selasky.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 1493d98b75bSEd Schouten.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME" 1504a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 15160fa6e9fSBenjamin Kaduk.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" 1528f20a914SMike Pritchard.\" 15338b622e1SHans Petter Selasky.Fn STAILQ_CLASS_ENTRY "CLASSTYPE" 15438b622e1SHans Petter Selasky.Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE" 155d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" 15679ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 157*d2870b86SMark Johnston.Fn STAILQ_EMPTY_ATOMIC "STAILQ_HEAD *head" 1584a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE" 15979ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head" 16079ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1617ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1627ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 16338b622e1SHans Petter Selasky.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 1644a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE" 16503763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" 1664a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head" 1674a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 1684a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 1694a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 170d3f649deSBrooks Davis.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 17179ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 17238b622e1SHans Petter Selasky.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 1733d98b75bSEd Schouten.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 17402a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 17560fa6e9fSBenjamin Kaduk.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE" 1768f20a914SMike Pritchard.\" 17738b622e1SHans Petter Selasky.Fn LIST_CLASS_ENTRY "CLASSTYPE" 17838b622e1SHans Petter Selasky.Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE" 17965d31997SKirk McKusick.Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 18079ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head" 181*d2870b86SMark Johnston.Fn LIST_EMPTY_ATOMIC "LIST_HEAD *head" 182afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE" 18379ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head" 18479ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 1857ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 1867ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 18738b622e1SHans Petter Selasky.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 188afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE" 18903763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head" 190afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head" 1914a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 1924a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 193afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 19479ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 1954170b083SEd Schouten.Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME" 196afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 1977f479deeSDag-Erling Smørgrav.Fn LIST_REPLACE "TYPE *elm" "TYPE *new" "LIST_ENTRY NAME" 19890df28ccSBenjamin Kaduk.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 1998f20a914SMike Pritchard.\" 20038b622e1SHans Petter Selasky.Fn TAILQ_CLASS_ENTRY "CLASSTYPE" 20138b622e1SHans Petter Selasky.Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE" 202d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME" 203c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 204*d2870b86SMark Johnston.Fn TAILQ_EMPTY_ATOMIC "TAILQ_HEAD *head" 205afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE" 206c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head" 20779ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 2087ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 2097ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 2106c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 2117ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 2127ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 21338b622e1SHans Petter Selasky.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 21438b622e1SHans Petter Selasky.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 215afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE" 21603763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head" 217afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head" 218afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 2193652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 220afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 221afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 22279ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 223c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 22479ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 225afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 2267f479deeSDag-Erling Smørgrav.Fn TAILQ_REPLACE "TAILQ_HEAD *head" "TYPE *elm" "TYPE *new" "TAILQ_ENTRY NAME" 22790df28ccSBenjamin Kaduk.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME" 2288f20a914SMike Pritchard.\" 229afe61c15SRodney W. Grimes.Sh DESCRIPTION 23038b622e1SHans Petter SelaskyThese macros define and operate on four types of data structures which 23138b622e1SHans Petter Selaskycan be used in both C and C++ source code: 23238b622e1SHans Petter Selasky.Bl -enum -compact -offset indent 23338b622e1SHans Petter Selasky.It 23438b622e1SHans Petter SelaskyLists 23538b622e1SHans Petter Selasky.It 23638b622e1SHans Petter SelaskySingly-linked lists 23738b622e1SHans Petter Selasky.It 23838b622e1SHans Petter SelaskySingly-linked tail queues 23938b622e1SHans Petter Selasky.It 24038b622e1SHans Petter SelaskyTail queues 24138b622e1SHans Petter Selasky.El 242b86388c1SDima DorfmanAll four structures support the following functionality: 243afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 244afe61c15SRodney W. Grimes.It 245afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list. 246afe61c15SRodney W. Grimes.It 247afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list. 248afe61c15SRodney W. Grimes.It 2494a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list. 2507658b0a2SJustin T. Gibbs.It 251afe61c15SRodney W. GrimesForward traversal through the list. 252359295cdSMatthew D Fleming.It 253f9257802SRalf S. EngelschallSwapping the contents of two lists. 254afe61c15SRodney W. Grimes.El 255afe61c15SRodney W. Grimes.Pp 256d5fd66e1SEd MasteSingly-linked lists are the simplest of the four data structures 2574a775e8fSJustin T. Gibbsand support only the above functionality. 2584a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets 2594a775e8fSJustin T. Gibbsand few or no removals, 2604a775e8fSJustin T. Gibbsor for implementing a LIFO queue. 261ed741d4eSDavid E. O'BrienSingly-linked lists add the following functionality: 262ed741d4eSDavid E. O'Brien.Bl -enum -compact -offset indent 263ed741d4eSDavid E. O'Brien.It 264ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 26565d31997SKirk McKusick.It 26665d31997SKirk McKusickO(n) concatenation of two lists. 267ed741d4eSDavid E. O'Brien.El 2684a775e8fSJustin T. Gibbs.Pp 2694a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality: 2704a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2714a775e8fSJustin T. Gibbs.It 2724a775e8fSJustin T. GibbsEntries can be added at the end of a list. 273d57e28adSThomas Moestl.It 274ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 275ed741d4eSDavid E. O'Brien.It 276d57e28adSThomas MoestlThey may be concatenated. 2774a775e8fSJustin T. Gibbs.El 2784a775e8fSJustin T. GibbsHowever: 2794a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2804a775e8fSJustin T. Gibbs.It 2814a775e8fSJustin T. GibbsAll list insertions must specify the head of the list. 2824a775e8fSJustin T. Gibbs.It 2834a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one. 2844a775e8fSJustin T. Gibbs.It 2854a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower 2864a775e8fSJustin T. Gibbsthan singly-linked lists. 2874a775e8fSJustin T. Gibbs.El 2884a775e8fSJustin T. Gibbs.Pp 289f9257802SRalf S. EngelschallSingly-linked tail queues are ideal for applications with large datasets and 2904a775e8fSJustin T. Gibbsfew or no removals, 2914a775e8fSJustin T. Gibbsor for implementing a FIFO queue. 2924a775e8fSJustin T. Gibbs.Pp 293b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues) 294b86388c1SDima Dorfmanadditionally allow: 2954a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2964a775e8fSJustin T. Gibbs.It 2974a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list. 2984a775e8fSJustin T. Gibbs.It 2994a775e8fSJustin T. GibbsO(1) removal of any entry in the list. 3004a775e8fSJustin T. Gibbs.El 3014a775e8fSJustin T. GibbsHowever: 3024a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 3034a775e8fSJustin T. Gibbs.It 304ad035dafSChristian BruefferEach element requires two pointers rather than one. 3054a775e8fSJustin T. Gibbs.It 3064a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about 3074a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures. 3084a775e8fSJustin T. Gibbs.El 3094a775e8fSJustin T. Gibbs.Pp 3104170b083SEd SchoutenLinked lists are the simplest of the doubly linked data structures. 3114170b083SEd SchoutenThey add the following functionality over the above: 3124170b083SEd Schouten.Bl -enum -compact -offset indent 3134170b083SEd Schouten.It 31465d31997SKirk McKusickO(n) concatenation of two lists. 31565d31997SKirk McKusick.It 3164170b083SEd SchoutenThey may be traversed backwards. 3174170b083SEd Schouten.El 3184170b083SEd SchoutenHowever: 3194170b083SEd Schouten.Bl -enum -compact -offset indent 3204170b083SEd Schouten.It 3214170b083SEd SchoutenTo traverse backwards, an entry to begin the traversal and the list in 3224170b083SEd Schoutenwhich it is contained must be specified. 3234170b083SEd Schouten.El 324afe61c15SRodney W. Grimes.Pp 325afe61c15SRodney W. GrimesTail queues add the following functionality: 326afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 327afe61c15SRodney W. Grimes.It 328afe61c15SRodney W. GrimesEntries can be added at the end of a list. 3296c1d0fbfSArchie Cobbs.It 3306c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head. 331d57e28adSThomas Moestl.It 332d57e28adSThomas MoestlThey may be concatenated. 333afe61c15SRodney W. Grimes.El 334afe61c15SRodney W. GrimesHowever: 335afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 336afe61c15SRodney W. Grimes.It 337afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list. 338afe61c15SRodney W. Grimes.It 339afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one. 340afe61c15SRodney W. Grimes.It 341afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower 3424a775e8fSJustin T. Gibbsthan singly-linked lists. 343afe61c15SRodney W. Grimes.El 344afe61c15SRodney W. Grimes.Pp 345afe61c15SRodney W. GrimesIn the macro definitions, 346afe61c15SRodney W. Grimes.Fa TYPE 34738b622e1SHans Petter Selaskyis the name of a user defined structure. 34838b622e1SHans Petter SelaskyThe structure must contain a field called 34938b622e1SHans Petter Selasky.Fa NAME 35038b622e1SHans Petter Selaskywhich is of type 3514a775e8fSJustin T. Gibbs.Li SLIST_ENTRY , 3524a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY , 353afe61c15SRodney W. Grimes.Li LIST_ENTRY , 354afe61c15SRodney W. Grimesor 35538b622e1SHans Petter Selasky.Li TAILQ_ENTRY . 35638b622e1SHans Petter SelaskyIn the macro definitions, 35738b622e1SHans Petter Selasky.Fa CLASSTYPE 35838b622e1SHans Petter Selaskyis the name of a user defined class. 35938b622e1SHans Petter SelaskyThe class must contain a field called 36038b622e1SHans Petter Selasky.Fa NAME 36138b622e1SHans Petter Selaskywhich is of type 36238b622e1SHans Petter Selasky.Li SLIST_CLASS_ENTRY , 36338b622e1SHans Petter Selasky.Li STAILQ_CLASS_ENTRY , 36438b622e1SHans Petter Selasky.Li LIST_CLASS_ENTRY , 36538b622e1SHans Petter Selaskyor 36638b622e1SHans Petter Selasky.Li TAILQ_CLASS_ENTRY . 367afe61c15SRodney W. GrimesThe argument 368afe61c15SRodney W. Grimes.Fa HEADNAME 369afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared 370afe61c15SRodney W. Grimesusing the macros 3714a775e8fSJustin T. Gibbs.Li SLIST_HEAD , 37238b622e1SHans Petter Selasky.Li SLIST_CLASS_HEAD , 3734a775e8fSJustin T. Gibbs.Li STAILQ_HEAD , 37438b622e1SHans Petter Selasky.Li STAILQ_CLASS_HEAD , 375afe61c15SRodney W. Grimes.Li LIST_HEAD , 37638b622e1SHans Petter Selasky.Li LIST_CLASS_HEAD , 37738b622e1SHans Petter Selasky.Li TAILQ_HEAD , 378afe61c15SRodney W. Grimesor 37938b622e1SHans Petter Selasky.Li TAILQ_CLASS_HEAD . 380afe61c15SRodney W. GrimesSee the examples below for further explanation of how these 381afe61c15SRodney W. Grimesmacros are used. 3824a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS 3834a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the 3844a775e8fSJustin T. Gibbs.Nm SLIST_HEAD 3854a775e8fSJustin T. Gibbsmacro. 3864a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element 3874a775e8fSJustin T. Gibbson the list. 3884a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation 3894a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements. 3904a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or 3914a775e8fSJustin T. Gibbsat the head of the list. 3924a775e8fSJustin T. GibbsAn 3934a775e8fSJustin T. Gibbs.Fa SLIST_HEAD 3944a775e8fSJustin T. Gibbsstructure is declared as follows: 3954a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3964a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head; 3974a775e8fSJustin T. Gibbs.Ed 3988f20a914SMike Pritchard.Pp 3994a775e8fSJustin T. Gibbswhere 4004a775e8fSJustin T. Gibbs.Fa HEADNAME 4014a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 4024a775e8fSJustin T. Gibbs.Fa TYPE 4034a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list. 4044a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as: 4054a775e8fSJustin T. Gibbs.Bd -literal -offset indent 4064a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 4074a775e8fSJustin T. Gibbs.Ed 4088f20a914SMike Pritchard.Pp 4094a775e8fSJustin T. Gibbs(The names 4104a775e8fSJustin T. Gibbs.Li head 4114a775e8fSJustin T. Gibbsand 4124a775e8fSJustin T. Gibbs.Li headp 4134a775e8fSJustin T. Gibbsare user selectable.) 4144a775e8fSJustin T. Gibbs.Pp 4154a775e8fSJustin T. GibbsThe macro 41603763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER 41703763fe0SJake Burkholderevaluates to an initializer for the list 41803763fe0SJake Burkholder.Fa head . 41903763fe0SJake Burkholder.Pp 42003763fe0SJake BurkholderThe macro 42165d31997SKirk McKusick.Nm SLIST_CONCAT 42265d31997SKirk McKusickconcatenates the list headed by 42365d31997SKirk McKusick.Fa head2 42465d31997SKirk McKusickonto the end of the one headed by 42565d31997SKirk McKusick.Fa head1 42665d31997SKirk McKusickremoving all entries from the former. 42765d31997SKirk McKusickUse of this macro should be avoided as it traverses the entirety of the 42865d31997SKirk McKusick.Fa head1 42965d31997SKirk McKusicklist. 43065d31997SKirk McKusickA singly-linked tail queue should be used if this macro is needed in 43165d31997SKirk McKusickhigh-usage code paths or to operate on long lists. 43265d31997SKirk McKusick.Pp 43365d31997SKirk McKusickThe macro 43479ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY 43579ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list. 436*d2870b86SMark JohnstonThe 437*d2870b86SMark Johnston.Nm SLIST_EMPTY_ATOMIC 438*d2870b86SMark Johnstonvariant has the same behavior, but can be safely used in contexts where it is 439*d2870b86SMark Johnstonpossible that a different thread is concurrently updating the list. 44079ea9bc4SAlexey Zelkin.Pp 44179ea9bc4SAlexey ZelkinThe macro 4424a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY 4434a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 4444a775e8fSJustin T. Gibbsthe list. 4454a775e8fSJustin T. Gibbs.Pp 4464a775e8fSJustin T. GibbsThe macro 44779ea9bc4SAlexey Zelkin.Nm SLIST_FIRST 44879ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty. 44979ea9bc4SAlexey Zelkin.Pp 45079ea9bc4SAlexey ZelkinThe macro 45179ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH 45279ea9bc4SAlexey Zelkintraverses the list referenced by 45379ea9bc4SAlexey Zelkin.Fa head 45479ea9bc4SAlexey Zelkinin the forward direction, assigning each element in 45579ea9bc4SAlexey Zelkinturn to 45679ea9bc4SAlexey Zelkin.Fa var . 45779ea9bc4SAlexey Zelkin.Pp 45879ea9bc4SAlexey ZelkinThe macro 4597ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM 4607ecb4019SLawrence Stewartbehaves identically to 4617ecb4019SLawrence Stewart.Nm SLIST_FOREACH 4627ecb4019SLawrence Stewartwhen 4637ecb4019SLawrence Stewart.Fa var 4647ecb4019SLawrence Stewartis NULL, else it treats 4657ecb4019SLawrence Stewart.Fa var 4667ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at 4677ecb4019SLawrence Stewart.Fa var 4687ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by 4697ecb4019SLawrence Stewart.Fa head . 4707ecb4019SLawrence Stewart.Pp 4717ecb4019SLawrence StewartThe macro 4722724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE 4732724bce2SAlexander Kabaevtraverses the list referenced by 4742724bce2SAlexander Kabaev.Fa head 4752724bce2SAlexander Kabaevin the forward direction, assigning each element in 4762724bce2SAlexander Kabaevturn to 4772724bce2SAlexander Kabaev.Fa var . 4782724bce2SAlexander KabaevHowever, unlike 4792724bce2SAlexander Kabaev.Fn SLIST_FOREACH 4802724bce2SAlexander Kabaevhere it is permitted to both remove 4812724bce2SAlexander Kabaev.Fa var 4822724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 4832724bce2SAlexander Kabaevtraversal. 4842724bce2SAlexander Kabaev.Pp 4852724bce2SAlexander KabaevThe macro 4867ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE 4877ecb4019SLawrence Stewartbehaves identically to 4887ecb4019SLawrence Stewart.Nm SLIST_FOREACH_SAFE 4897ecb4019SLawrence Stewartwhen 4907ecb4019SLawrence Stewart.Fa var 4917ecb4019SLawrence Stewartis NULL, else it treats 4927ecb4019SLawrence Stewart.Fa var 4937ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at 4947ecb4019SLawrence Stewart.Fa var 4957ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by 4967ecb4019SLawrence Stewart.Fa head . 4977ecb4019SLawrence Stewart.Pp 4987ecb4019SLawrence StewartThe macro 4994a775e8fSJustin T. Gibbs.Nm SLIST_INIT 5004a775e8fSJustin T. Gibbsinitializes the list referenced by 5014a775e8fSJustin T. Gibbs.Fa head . 5024a775e8fSJustin T. Gibbs.Pp 5034a775e8fSJustin T. GibbsThe macro 5044a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD 5054a775e8fSJustin T. Gibbsinserts the new element 5064a775e8fSJustin T. Gibbs.Fa elm 5074a775e8fSJustin T. Gibbsat the head of the list. 5084a775e8fSJustin T. Gibbs.Pp 5094a775e8fSJustin T. GibbsThe macro 5104a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER 5114a775e8fSJustin T. Gibbsinserts the new element 5124a775e8fSJustin T. Gibbs.Fa elm 5134a775e8fSJustin T. Gibbsafter the element 5144a775e8fSJustin T. Gibbs.Fa listelm . 5154a775e8fSJustin T. Gibbs.Pp 5164a775e8fSJustin T. GibbsThe macro 51779ea9bc4SAlexey Zelkin.Nm SLIST_NEXT 51879ea9bc4SAlexey Zelkinreturns the next element in the list. 51979ea9bc4SAlexey Zelkin.Pp 52079ea9bc4SAlexey ZelkinThe macro 5213d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER 5223d98b75bSEd Schoutenremoves the element after 5233d98b75bSEd Schouten.Fa elm 524bc106255SEitan Adlerfrom the list. 525bc106255SEitan AdlerUnlike 5263d98b75bSEd Schouten.Fa SLIST_REMOVE , 5273d98b75bSEd Schoutenthis macro does not traverse the entire list. 5283d98b75bSEd Schouten.Pp 5293d98b75bSEd SchoutenThe macro 5304a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD 5314a775e8fSJustin T. Gibbsremoves the element 5324a775e8fSJustin T. Gibbs.Fa elm 5334a775e8fSJustin T. Gibbsfrom the head of the list. 5344a775e8fSJustin T. GibbsFor optimum efficiency, 5354a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use 5364a775e8fSJustin T. Gibbsthis macro instead of the generic 5374a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE 5384a775e8fSJustin T. Gibbsmacro. 5394a775e8fSJustin T. Gibbs.Pp 5404a775e8fSJustin T. GibbsThe macro 5414a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE 5424a775e8fSJustin T. Gibbsremoves the element 5434a775e8fSJustin T. Gibbs.Fa elm 5444a775e8fSJustin T. Gibbsfrom the list. 54565d31997SKirk McKusickUse of this macro should be avoided as it traverses the entire list. 54665d31997SKirk McKusickA doubly-linked list should be used if this macro is needed in 54765d31997SKirk McKusickhigh-usage code paths or to operate on long lists. 548359295cdSMatthew D Fleming.Pp 549359295cdSMatthew D FlemingThe macro 550359295cdSMatthew D Fleming.Nm SLIST_SWAP 551359295cdSMatthew D Flemingswaps the contents of 552359295cdSMatthew D Fleming.Fa head1 553359295cdSMatthew D Flemingand 554359295cdSMatthew D Fleming.Fa head2 . 5554a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE 5564a775e8fSJustin T. Gibbs.Bd -literal 55703763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head = 55803763fe0SJake Burkholder SLIST_HEAD_INITIALIZER(head); 5594a775e8fSJustin T. Gibbsstruct slisthead *headp; /* Singly-linked List head. */ 5604a775e8fSJustin T. Gibbsstruct entry { 5614a775e8fSJustin T. Gibbs ... 5624a775e8fSJustin T. Gibbs SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 5634a775e8fSJustin T. Gibbs ... 5644a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 5654a775e8fSJustin T. Gibbs 5664a775e8fSJustin T. GibbsSLIST_INIT(&head); /* Initialize the list. */ 5674a775e8fSJustin T. Gibbs 5684a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 5694a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries); 5704a775e8fSJustin T. Gibbs 5714a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 5724a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries); 5734a775e8fSJustin T. Gibbs 5744a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 5754a775e8fSJustin T. Gibbsfree(n2); 5764a775e8fSJustin T. Gibbs 57779ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head); 57803763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 5794a775e8fSJustin T. Gibbsfree(n3); 5804a775e8fSJustin T. Gibbs /* Forward traversal. */ 58179ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries) 5824a775e8fSJustin T. Gibbs np-> ... 5832724bce2SAlexander Kabaev /* Safe forward traversal. */ 5842724bce2SAlexander KabaevSLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 5852724bce2SAlexander Kabaev np->do_stuff(); 5862724bce2SAlexander Kabaev ... 5872724bce2SAlexander Kabaev SLIST_REMOVE(&head, np, entry, entries); 5882724bce2SAlexander Kabaev free(np); 5892724bce2SAlexander Kabaev} 5904a775e8fSJustin T. Gibbs 59179ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) { /* List Deletion. */ 59279ea9bc4SAlexey Zelkin n1 = SLIST_FIRST(&head); 5934a775e8fSJustin T. Gibbs SLIST_REMOVE_HEAD(&head, entries); 5944a775e8fSJustin T. Gibbs free(n1); 5954a775e8fSJustin T. Gibbs} 5964a775e8fSJustin T. Gibbs.Ed 5974a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES 5984a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the 5994a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD 6004a775e8fSJustin T. Gibbsmacro. 6014a775e8fSJustin T. GibbsThis structure contains a pair of pointers, 6024a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to 6034a775e8fSJustin T. Gibbsthe last element in the tail queue. 6044a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer 6054a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary 6064a775e8fSJustin T. Gibbselements. 6074a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element, 6084a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue. 6094a775e8fSJustin T. GibbsA 6104a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD 6114a775e8fSJustin T. Gibbsstructure is declared as follows: 6124a775e8fSJustin T. Gibbs.Bd -literal -offset indent 6134a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head; 6144a775e8fSJustin T. Gibbs.Ed 6158f20a914SMike Pritchard.Pp 6164a775e8fSJustin T. Gibbswhere 6174a775e8fSJustin T. Gibbs.Li HEADNAME 6184a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 6194a775e8fSJustin T. Gibbs.Li TYPE 6204a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue. 6214a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as: 6224a775e8fSJustin T. Gibbs.Bd -literal -offset indent 6234a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 6244a775e8fSJustin T. Gibbs.Ed 6258f20a914SMike Pritchard.Pp 6264a775e8fSJustin T. Gibbs(The names 6274a775e8fSJustin T. Gibbs.Li head 6284a775e8fSJustin T. Gibbsand 6294a775e8fSJustin T. Gibbs.Li headp 6304a775e8fSJustin T. Gibbsare user selectable.) 6314a775e8fSJustin T. Gibbs.Pp 6324a775e8fSJustin T. GibbsThe macro 63303763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER 63403763fe0SJake Burkholderevaluates to an initializer for the tail queue 63503763fe0SJake Burkholder.Fa head . 63603763fe0SJake Burkholder.Pp 63703763fe0SJake BurkholderThe macro 638d57e28adSThomas Moestl.Nm STAILQ_CONCAT 639d57e28adSThomas Moestlconcatenates the tail queue headed by 640d57e28adSThomas Moestl.Fa head2 641d57e28adSThomas Moestlonto the end of the one headed by 642d57e28adSThomas Moestl.Fa head1 643d57e28adSThomas Moestlremoving all entries from the former. 644d57e28adSThomas Moestl.Pp 645d57e28adSThomas MoestlThe macro 64679ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY 64779ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue. 648*d2870b86SMark JohnstonThe 649*d2870b86SMark Johnston.Nm STAILQ_EMPTY_ATOMIC 650*d2870b86SMark Johnstonvariant has the same behavior, but can be safely used in contexts where it is 651*d2870b86SMark Johnstonpossible that a different thread is concurrently updating the queue. 65279ea9bc4SAlexey Zelkin.Pp 65379ea9bc4SAlexey ZelkinThe macro 6544a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY 6554a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 6564a775e8fSJustin T. Gibbsthe tail queue. 6574a775e8fSJustin T. Gibbs.Pp 6584a775e8fSJustin T. GibbsThe macro 65979ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST 66079ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue 66179ea9bc4SAlexey Zelkinis empty. 66279ea9bc4SAlexey Zelkin.Pp 66379ea9bc4SAlexey ZelkinThe macro 66479ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH 66579ea9bc4SAlexey Zelkintraverses the tail queue referenced by 66679ea9bc4SAlexey Zelkin.Fa head 66779ea9bc4SAlexey Zelkinin the forward direction, assigning each element 66879ea9bc4SAlexey Zelkinin turn to 66979ea9bc4SAlexey Zelkin.Fa var . 67079ea9bc4SAlexey Zelkin.Pp 67179ea9bc4SAlexey ZelkinThe macro 6727ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM 6737ecb4019SLawrence Stewartbehaves identically to 6747ecb4019SLawrence Stewart.Nm STAILQ_FOREACH 6757ecb4019SLawrence Stewartwhen 6767ecb4019SLawrence Stewart.Fa var 6777ecb4019SLawrence Stewartis NULL, else it treats 6787ecb4019SLawrence Stewart.Fa var 6797ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at 6807ecb4019SLawrence Stewart.Fa var 6817ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by 6827ecb4019SLawrence Stewart.Fa head . 6837ecb4019SLawrence Stewart.Pp 6847ecb4019SLawrence StewartThe macro 6852724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE 6862724bce2SAlexander Kabaevtraverses the tail queue referenced by 6872724bce2SAlexander Kabaev.Fa head 6882724bce2SAlexander Kabaevin the forward direction, assigning each element 6892724bce2SAlexander Kabaevin turn to 6902724bce2SAlexander Kabaev.Fa var . 6912724bce2SAlexander KabaevHowever, unlike 6922724bce2SAlexander Kabaev.Fn STAILQ_FOREACH 6932724bce2SAlexander Kabaevhere it is permitted to both remove 6942724bce2SAlexander Kabaev.Fa var 6952724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 6962724bce2SAlexander Kabaevtraversal. 6972724bce2SAlexander Kabaev.Pp 6982724bce2SAlexander KabaevThe macro 6997ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE 7007ecb4019SLawrence Stewartbehaves identically to 7017ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_SAFE 7027ecb4019SLawrence Stewartwhen 7037ecb4019SLawrence Stewart.Fa var 7047ecb4019SLawrence Stewartis NULL, else it treats 7057ecb4019SLawrence Stewart.Fa var 7067ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at 7077ecb4019SLawrence Stewart.Fa var 7087ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by 7097ecb4019SLawrence Stewart.Fa head . 7107ecb4019SLawrence Stewart.Pp 7117ecb4019SLawrence StewartThe macro 7124a775e8fSJustin T. Gibbs.Nm STAILQ_INIT 7134a775e8fSJustin T. Gibbsinitializes the tail queue referenced by 7144a775e8fSJustin T. Gibbs.Fa head . 7154a775e8fSJustin T. Gibbs.Pp 7164a775e8fSJustin T. GibbsThe macro 7174a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD 7184a775e8fSJustin T. Gibbsinserts the new element 7194a775e8fSJustin T. Gibbs.Fa elm 7204a775e8fSJustin T. Gibbsat the head of the tail queue. 7214a775e8fSJustin T. Gibbs.Pp 7224a775e8fSJustin T. GibbsThe macro 7234a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL 7244a775e8fSJustin T. Gibbsinserts the new element 7254a775e8fSJustin T. Gibbs.Fa elm 7264a775e8fSJustin T. Gibbsat the end of the tail queue. 7274a775e8fSJustin T. Gibbs.Pp 7284a775e8fSJustin T. GibbsThe macro 7294a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER 7304a775e8fSJustin T. Gibbsinserts the new element 7314a775e8fSJustin T. Gibbs.Fa elm 7324a775e8fSJustin T. Gibbsafter the element 7334a775e8fSJustin T. Gibbs.Fa listelm . 7344a775e8fSJustin T. Gibbs.Pp 7354a775e8fSJustin T. GibbsThe macro 73679ea9bc4SAlexey Zelkin.Nm STAILQ_LAST 73779ea9bc4SAlexey Zelkinreturns the last item on the tail queue. 738982ba1cbSKirk McKusickIf the tail queue is empty the return value is 739982ba1cbSKirk McKusick.Dv NULL . 74079ea9bc4SAlexey Zelkin.Pp 74179ea9bc4SAlexey ZelkinThe macro 74279ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT 74379ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last. 74479ea9bc4SAlexey Zelkin.Pp 74579ea9bc4SAlexey ZelkinThe macro 7463d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER 7473d98b75bSEd Schoutenremoves the element after 7483d98b75bSEd Schouten.Fa elm 749bc106255SEitan Adlerfrom the tail queue. 750bc106255SEitan AdlerUnlike 7513d98b75bSEd Schouten.Fa STAILQ_REMOVE , 7523d98b75bSEd Schoutenthis macro does not traverse the entire tail queue. 7533d98b75bSEd Schouten.Pp 7543d98b75bSEd SchoutenThe macro 7554a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD 756ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue. 7574a775e8fSJustin T. GibbsFor optimum efficiency, 7584a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should 7594a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic 7604a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE 7614a775e8fSJustin T. Gibbsmacro. 7624a775e8fSJustin T. Gibbs.Pp 7634a775e8fSJustin T. GibbsThe macro 7644a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE 7654a775e8fSJustin T. Gibbsremoves the element 7664a775e8fSJustin T. Gibbs.Fa elm 7674a775e8fSJustin T. Gibbsfrom the tail queue. 76865d31997SKirk McKusickUse of this macro should be avoided as it traverses the entire list. 76965d31997SKirk McKusickA doubly-linked tail queue should be used if this macro is needed in 77065d31997SKirk McKusickhigh-usage code paths or to operate on long tail queues. 771359295cdSMatthew D Fleming.Pp 772359295cdSMatthew D FlemingThe macro 773359295cdSMatthew D Fleming.Nm STAILQ_SWAP 774359295cdSMatthew D Flemingswaps the contents of 775359295cdSMatthew D Fleming.Fa head1 776359295cdSMatthew D Flemingand 777359295cdSMatthew D Fleming.Fa head2 . 7784a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE 7794a775e8fSJustin T. Gibbs.Bd -literal 78003763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head = 78103763fe0SJake Burkholder STAILQ_HEAD_INITIALIZER(head); 7824a775e8fSJustin T. Gibbsstruct stailhead *headp; /* Singly-linked tail queue head. */ 7834a775e8fSJustin T. Gibbsstruct entry { 7844a775e8fSJustin T. Gibbs ... 7854a775e8fSJustin T. Gibbs STAILQ_ENTRY(entry) entries; /* Tail queue. */ 7864a775e8fSJustin T. Gibbs ... 7874a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 7884a775e8fSJustin T. Gibbs 7894a775e8fSJustin T. GibbsSTAILQ_INIT(&head); /* Initialize the queue. */ 7904a775e8fSJustin T. Gibbs 7914a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 7924a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries); 7934a775e8fSJustin T. Gibbs 7944a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 7954a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries); 7964a775e8fSJustin T. Gibbs 7974a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 7984a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries); 7994a775e8fSJustin T. Gibbs /* Deletion. */ 8004a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries); 8014a775e8fSJustin T. Gibbsfree(n2); 80203763fe0SJake Burkholder /* Deletion from the head. */ 80379ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head); 8044a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries); 8054a775e8fSJustin T. Gibbsfree(n3); 8064a775e8fSJustin T. Gibbs /* Forward traversal. */ 80779ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries) 8084a775e8fSJustin T. Gibbs np-> ... 8092724bce2SAlexander Kabaev /* Safe forward traversal. */ 8102724bce2SAlexander KabaevSTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 8112724bce2SAlexander Kabaev np->do_stuff(); 8122724bce2SAlexander Kabaev ... 8132724bce2SAlexander Kabaev STAILQ_REMOVE(&head, np, entry, entries); 8142724bce2SAlexander Kabaev free(np); 8152724bce2SAlexander Kabaev} 8164a775e8fSJustin T. Gibbs /* TailQ Deletion. */ 81779ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) { 81803763fe0SJake Burkholder n1 = STAILQ_FIRST(&head); 819266e33fdSJohn Baldwin STAILQ_REMOVE_HEAD(&head, entries); 8204a775e8fSJustin T. Gibbs free(n1); 8214a775e8fSJustin T. Gibbs} 8224a775e8fSJustin T. Gibbs /* Faster TailQ Deletion. */ 82379ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head); 8244a775e8fSJustin T. Gibbswhile (n1 != NULL) { 82579ea9bc4SAlexey Zelkin n2 = STAILQ_NEXT(n1, entries); 8264a775e8fSJustin T. Gibbs free(n1); 8274a775e8fSJustin T. Gibbs n1 = n2; 8284a775e8fSJustin T. Gibbs} 8294a775e8fSJustin T. GibbsSTAILQ_INIT(&head); 8304a775e8fSJustin T. Gibbs.Ed 831afe61c15SRodney W. Grimes.Sh LISTS 832afe61c15SRodney W. GrimesA list is headed by a structure defined by the 833afe61c15SRodney W. Grimes.Nm LIST_HEAD 834afe61c15SRodney W. Grimesmacro. 835afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element 836afe61c15SRodney W. Grimeson the list. 837afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 838afe61c15SRodney W. Grimesremoved without traversing the list. 8394a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element, 8404a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list. 841afe61c15SRodney W. GrimesA 842afe61c15SRodney W. Grimes.Fa LIST_HEAD 843afe61c15SRodney W. Grimesstructure is declared as follows: 844afe61c15SRodney W. Grimes.Bd -literal -offset indent 845afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head; 846afe61c15SRodney W. Grimes.Ed 8478f20a914SMike Pritchard.Pp 848afe61c15SRodney W. Grimeswhere 849afe61c15SRodney W. Grimes.Fa HEADNAME 850afe61c15SRodney W. Grimesis the name of the structure to be defined, and 851afe61c15SRodney W. Grimes.Fa TYPE 852afe61c15SRodney W. Grimesis the type of the elements to be linked into the list. 853afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as: 854afe61c15SRodney W. Grimes.Bd -literal -offset indent 855afe61c15SRodney W. Grimesstruct HEADNAME *headp; 856afe61c15SRodney W. Grimes.Ed 8578f20a914SMike Pritchard.Pp 858afe61c15SRodney W. Grimes(The names 859afe61c15SRodney W. Grimes.Li head 860afe61c15SRodney W. Grimesand 861afe61c15SRodney W. Grimes.Li headp 862afe61c15SRodney W. Grimesare user selectable.) 863afe61c15SRodney W. Grimes.Pp 864afe61c15SRodney W. GrimesThe macro 86503763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER 86603763fe0SJake Burkholderevaluates to an initializer for the list 86703763fe0SJake Burkholder.Fa head . 86803763fe0SJake Burkholder.Pp 86903763fe0SJake BurkholderThe macro 87065d31997SKirk McKusick.Nm LIST_CONCAT 87165d31997SKirk McKusickconcatenates the list headed by 87265d31997SKirk McKusick.Fa head2 87365d31997SKirk McKusickonto the end of the one headed by 87465d31997SKirk McKusick.Fa head1 87565d31997SKirk McKusickremoving all entries from the former. 87665d31997SKirk McKusickUse of this macro should be avoided as it traverses the entirety of the 87765d31997SKirk McKusick.Fa head1 87865d31997SKirk McKusicklist. 87965d31997SKirk McKusickA tail queue should be used if this macro is needed in 88065d31997SKirk McKusickhigh-usage code paths or to operate on long lists. 88165d31997SKirk McKusick.Pp 88265d31997SKirk McKusickThe macro 88379ea9bc4SAlexey Zelkin.Nm LIST_EMPTY 884ddbb94adSTom Rhodesevaluates to true if there are no elements in the list. 885*d2870b86SMark JohnstonThe 886*d2870b86SMark Johnston.Nm LIST_EMPTY_ATOMIC 887*d2870b86SMark Johnstonvariant has the same behavior, but can be safely used in contexts where it is 888*d2870b86SMark Johnstonpossible that a different thread is concurrently updating the list. 88979ea9bc4SAlexey Zelkin.Pp 89079ea9bc4SAlexey ZelkinThe macro 891afe61c15SRodney W. Grimes.Nm LIST_ENTRY 892afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 893afe61c15SRodney W. Grimesthe list. 894afe61c15SRodney W. Grimes.Pp 895afe61c15SRodney W. GrimesThe macro 89679ea9bc4SAlexey Zelkin.Nm LIST_FIRST 89779ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list 89879ea9bc4SAlexey Zelkinis empty. 89979ea9bc4SAlexey Zelkin.Pp 90079ea9bc4SAlexey ZelkinThe macro 90179ea9bc4SAlexey Zelkin.Nm LIST_FOREACH 90279ea9bc4SAlexey Zelkintraverses the list referenced by 90379ea9bc4SAlexey Zelkin.Fa head 90479ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 90579ea9bc4SAlexey Zelkin.Fa var . 90679ea9bc4SAlexey Zelkin.Pp 90779ea9bc4SAlexey ZelkinThe macro 9087ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM 9097ecb4019SLawrence Stewartbehaves identically to 9107ecb4019SLawrence Stewart.Nm LIST_FOREACH 9117ecb4019SLawrence Stewartwhen 9127ecb4019SLawrence Stewart.Fa var 9137ecb4019SLawrence Stewartis NULL, else it treats 9147ecb4019SLawrence Stewart.Fa var 9157ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at 9167ecb4019SLawrence Stewart.Fa var 9177ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by 9187ecb4019SLawrence Stewart.Fa head . 9197ecb4019SLawrence Stewart.Pp 9207ecb4019SLawrence StewartThe macro 9214250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE 9224250a68eSBosko Milekictraverses the list referenced by 9234250a68eSBosko Milekic.Fa head 9244250a68eSBosko Milekicin the forward direction, assigning each element in turn to 9254250a68eSBosko Milekic.Fa var . 9264250a68eSBosko MilekicHowever, unlike 9274250a68eSBosko Milekic.Fn LIST_FOREACH 9284250a68eSBosko Milekichere it is permitted to both remove 9294250a68eSBosko Milekic.Fa var 9304250a68eSBosko Milekicas well as free it from within the loop safely without interfering with the 9314250a68eSBosko Milekictraversal. 9324250a68eSBosko Milekic.Pp 9334250a68eSBosko MilekicThe macro 9347ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM_SAFE 9357ecb4019SLawrence Stewartbehaves identically to 9367ecb4019SLawrence Stewart.Nm LIST_FOREACH_SAFE 9377ecb4019SLawrence Stewartwhen 9387ecb4019SLawrence Stewart.Fa var 9397ecb4019SLawrence Stewartis NULL, else it treats 9407ecb4019SLawrence Stewart.Fa var 9417ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at 9427ecb4019SLawrence Stewart.Fa var 9437ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by 9447ecb4019SLawrence Stewart.Fa head . 9457ecb4019SLawrence Stewart.Pp 9467ecb4019SLawrence StewartThe macro 947afe61c15SRodney W. Grimes.Nm LIST_INIT 948afe61c15SRodney W. Grimesinitializes the list referenced by 949afe61c15SRodney W. Grimes.Fa head . 950afe61c15SRodney W. Grimes.Pp 951afe61c15SRodney W. GrimesThe macro 952afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD 953afe61c15SRodney W. Grimesinserts the new element 954afe61c15SRodney W. Grimes.Fa elm 955afe61c15SRodney W. Grimesat the head of the list. 956afe61c15SRodney W. Grimes.Pp 957afe61c15SRodney W. GrimesThe macro 958afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER 959afe61c15SRodney W. Grimesinserts the new element 960afe61c15SRodney W. Grimes.Fa elm 961afe61c15SRodney W. Grimesafter the element 962afe61c15SRodney W. Grimes.Fa listelm . 963afe61c15SRodney W. Grimes.Pp 964afe61c15SRodney W. GrimesThe macro 9657658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE 9667658b0a2SJustin T. Gibbsinserts the new element 9677658b0a2SJustin T. Gibbs.Fa elm 9687658b0a2SJustin T. Gibbsbefore the element 9697658b0a2SJustin T. Gibbs.Fa listelm . 9707658b0a2SJustin T. Gibbs.Pp 9717658b0a2SJustin T. GibbsThe macro 97279ea9bc4SAlexey Zelkin.Nm LIST_NEXT 97379ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last. 97479ea9bc4SAlexey Zelkin.Pp 97579ea9bc4SAlexey ZelkinThe macro 9764170b083SEd Schouten.Nm LIST_PREV 9774170b083SEd Schoutenreturns the previous element in the list, or NULL if this is the first. 9784170b083SEd SchoutenList 9794170b083SEd Schouten.Fa head 9804170b083SEd Schoutenmust contain element 9814170b083SEd Schouten.Fa elm . 9824170b083SEd Schouten.Pp 9834170b083SEd SchoutenThe macro 984afe61c15SRodney W. Grimes.Nm LIST_REMOVE 985afe61c15SRodney W. Grimesremoves the element 986afe61c15SRodney W. Grimes.Fa elm 987afe61c15SRodney W. Grimesfrom the list. 988359295cdSMatthew D Fleming.Pp 989359295cdSMatthew D FlemingThe macro 9907f479deeSDag-Erling Smørgrav.Fn LIST_REPLACE 9917f479deeSDag-Erling Smørgravreplaces the element 9927f479deeSDag-Erling Smørgrav.Fa elm 9937f479deeSDag-Erling Smørgravwith 9947f479deeSDag-Erling Smørgrav.Fa new 9957f479deeSDag-Erling Smørgravin the list. 9967f479deeSDag-Erling SmørgravThe element 9977f479deeSDag-Erling Smørgrav.Fa new 9987f479deeSDag-Erling Smørgravmust not already be on a list. 9997f479deeSDag-Erling Smørgrav.Pp 10007f479deeSDag-Erling SmørgravThe macro 1001359295cdSMatthew D Fleming.Nm LIST_SWAP 1002359295cdSMatthew D Flemingswaps the contents of 1003359295cdSMatthew D Fleming.Fa head1 1004359295cdSMatthew D Flemingand 1005359295cdSMatthew D Fleming.Fa head2 . 1006afe61c15SRodney W. Grimes.Sh LIST EXAMPLE 1007afe61c15SRodney W. Grimes.Bd -literal 100803763fe0SJake BurkholderLIST_HEAD(listhead, entry) head = 100903763fe0SJake Burkholder LIST_HEAD_INITIALIZER(head); 1010afe61c15SRodney W. Grimesstruct listhead *headp; /* List head. */ 1011afe61c15SRodney W. Grimesstruct entry { 1012afe61c15SRodney W. Grimes ... 1013afe61c15SRodney W. Grimes LIST_ENTRY(entry) entries; /* List. */ 1014afe61c15SRodney W. Grimes ... 10154250a68eSBosko Milekic} *n1, *n2, *n3, *np, *np_temp; 1016afe61c15SRodney W. Grimes 1017afe61c15SRodney W. GrimesLIST_INIT(&head); /* Initialize the list. */ 1018afe61c15SRodney W. Grimes 1019afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1020afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries); 1021afe61c15SRodney W. Grimes 1022afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 1023afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries); 10247658b0a2SJustin T. Gibbs 10257658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 10267658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries); 10277658b0a2SJustin T. Gibbs 10287658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries); /* Deletion. */ 10297658b0a2SJustin T. Gibbsfree(n2); 1030afe61c15SRodney W. Grimes /* Forward traversal. */ 103179ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries) 1032afe61c15SRodney W. Grimes np-> ... 1033afe61c15SRodney W. Grimes 10344250a68eSBosko Milekic /* Safe forward traversal. */ 10354250a68eSBosko MilekicLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 10364250a68eSBosko Milekic np->do_stuff(); 10374250a68eSBosko Milekic ... 10384250a68eSBosko Milekic LIST_REMOVE(np, entries); 10394250a68eSBosko Milekic free(np); 10404250a68eSBosko Milekic} 10414250a68eSBosko Milekic 104279ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) { /* List Deletion. */ 104379ea9bc4SAlexey Zelkin n1 = LIST_FIRST(&head); 10447658b0a2SJustin T. Gibbs LIST_REMOVE(n1, entries); 10457658b0a2SJustin T. Gibbs free(n1); 10467658b0a2SJustin T. Gibbs} 10477658b0a2SJustin T. Gibbs 104803763fe0SJake Burkholdern1 = LIST_FIRST(&head); /* Faster List Deletion. */ 10497658b0a2SJustin T. Gibbswhile (n1 != NULL) { 105079ea9bc4SAlexey Zelkin n2 = LIST_NEXT(n1, entries); 10517658b0a2SJustin T. Gibbs free(n1); 10527658b0a2SJustin T. Gibbs n1 = n2; 10537658b0a2SJustin T. Gibbs} 10547658b0a2SJustin T. GibbsLIST_INIT(&head); 1055afe61c15SRodney W. Grimes.Ed 1056afe61c15SRodney W. Grimes.Sh TAIL QUEUES 1057afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the 1058afe61c15SRodney W. Grimes.Nm TAILQ_HEAD 1059afe61c15SRodney W. Grimesmacro. 1060afe61c15SRodney W. GrimesThis structure contains a pair of pointers, 1061afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to 1062afe61c15SRodney W. Grimesthe last element in the tail queue. 1063afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 1064afe61c15SRodney W. Grimesremoved without traversing the tail queue. 1065afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element, 10664a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue, 10674a775e8fSJustin T. Gibbsor at the end of the tail queue. 1068afe61c15SRodney W. GrimesA 1069afe61c15SRodney W. Grimes.Fa TAILQ_HEAD 1070afe61c15SRodney W. Grimesstructure is declared as follows: 1071afe61c15SRodney W. Grimes.Bd -literal -offset indent 1072afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head; 1073afe61c15SRodney W. Grimes.Ed 10748f20a914SMike Pritchard.Pp 1075afe61c15SRodney W. Grimeswhere 1076afe61c15SRodney W. Grimes.Li HEADNAME 1077afe61c15SRodney W. Grimesis the name of the structure to be defined, and 1078afe61c15SRodney W. Grimes.Li TYPE 1079afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue. 1080afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as: 1081afe61c15SRodney W. Grimes.Bd -literal -offset indent 1082afe61c15SRodney W. Grimesstruct HEADNAME *headp; 1083afe61c15SRodney W. Grimes.Ed 10848f20a914SMike Pritchard.Pp 1085afe61c15SRodney W. Grimes(The names 1086afe61c15SRodney W. Grimes.Li head 1087afe61c15SRodney W. Grimesand 1088afe61c15SRodney W. Grimes.Li headp 1089afe61c15SRodney W. Grimesare user selectable.) 1090afe61c15SRodney W. Grimes.Pp 1091afe61c15SRodney W. GrimesThe macro 109203763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER 109303763fe0SJake Burkholderevaluates to an initializer for the tail queue 109403763fe0SJake Burkholder.Fa head . 109503763fe0SJake Burkholder.Pp 109603763fe0SJake BurkholderThe macro 1097d57e28adSThomas Moestl.Nm TAILQ_CONCAT 1098d57e28adSThomas Moestlconcatenates the tail queue headed by 1099d57e28adSThomas Moestl.Fa head2 1100d57e28adSThomas Moestlonto the end of the one headed by 1101d57e28adSThomas Moestl.Fa head1 1102d57e28adSThomas Moestlremoving all entries from the former. 1103d57e28adSThomas Moestl.Pp 1104d57e28adSThomas MoestlThe macro 1105c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY 1106c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue. 1107*d2870b86SMark JohnstonThe 1108*d2870b86SMark Johnston.Nm TAILQ_EMPTY_ATOMIC 1109*d2870b86SMark Johnstonvariant has the same behavior, but can be safely used in contexts where it is 1110*d2870b86SMark Johnstonpossible that a different thread is concurrently updating the queue. 1111c5c15c16SPoul-Henning Kamp.Pp 1112c5c15c16SPoul-Henning KampThe macro 1113afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY 1114afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 1115afe61c15SRodney W. Grimesthe tail queue. 1116afe61c15SRodney W. Grimes.Pp 1117afe61c15SRodney W. GrimesThe macro 1118c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST 1119c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue 1120c5c15c16SPoul-Henning Kampis empty. 1121c5c15c16SPoul-Henning Kamp.Pp 1122c5c15c16SPoul-Henning KampThe macro 112379ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH 112479ea9bc4SAlexey Zelkintraverses the tail queue referenced by 112579ea9bc4SAlexey Zelkin.Fa head 112679ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 112779ea9bc4SAlexey Zelkin.Fa var . 1128fb5293cfSRuslan Ermilov.Fa var 1129fb5293cfSRuslan Ermilovis set to 1130fb5293cfSRuslan Ermilov.Dv NULL 1131fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements. 113279ea9bc4SAlexey Zelkin.Pp 113379ea9bc4SAlexey ZelkinThe macro 11347ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM 11357ecb4019SLawrence Stewartbehaves identically to 11367ecb4019SLawrence Stewart.Nm TAILQ_FOREACH 11377ecb4019SLawrence Stewartwhen 11387ecb4019SLawrence Stewart.Fa var 11397ecb4019SLawrence Stewartis NULL, else it treats 11407ecb4019SLawrence Stewart.Fa var 11417ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at 11427ecb4019SLawrence Stewart.Fa var 11437ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by 11447ecb4019SLawrence Stewart.Fa head . 11457ecb4019SLawrence Stewart.Pp 11467ecb4019SLawrence StewartThe macro 11476c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE 11486c1d0fbfSArchie Cobbstraverses the tail queue referenced by 11496c1d0fbfSArchie Cobbs.Fa head 11506c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to 11516c1d0fbfSArchie Cobbs.Fa var . 11526c1d0fbfSArchie Cobbs.Pp 11537ecb4019SLawrence StewartThe macro 11547ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM 11557ecb4019SLawrence Stewartbehaves identically to 11567ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE 11577ecb4019SLawrence Stewartwhen 11587ecb4019SLawrence Stewart.Fa var 11597ecb4019SLawrence Stewartis NULL, else it treats 11607ecb4019SLawrence Stewart.Fa var 11617ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at 11627ecb4019SLawrence Stewart.Fa var 11637ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by 11647ecb4019SLawrence Stewart.Fa head . 11657ecb4019SLawrence Stewart.Pp 11662724bce2SAlexander KabaevThe macros 11672724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE 11682724bce2SAlexander Kabaevand 11692724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE 11702724bce2SAlexander Kabaevtraverse the list referenced by 11712724bce2SAlexander Kabaev.Fa head 11722724bce2SAlexander Kabaevin the forward or reverse direction respectively, 11732724bce2SAlexander Kabaevassigning each element in turn to 11742724bce2SAlexander Kabaev.Fa var . 11753b96c71fSMike PritchardHowever, unlike their unsafe counterparts, 11762724bce2SAlexander Kabaev.Nm TAILQ_FOREACH 11772724bce2SAlexander Kabaevand 11782724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE 11792724bce2SAlexander Kabaevpermit to both remove 11802724bce2SAlexander Kabaev.Fa var 11812724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 11822724bce2SAlexander Kabaevtraversal. 11832724bce2SAlexander Kabaev.Pp 11846c1d0fbfSArchie CobbsThe macro 11857ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE 11867ecb4019SLawrence Stewartbehaves identically to 11877ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_SAFE 11887ecb4019SLawrence Stewartwhen 11897ecb4019SLawrence Stewart.Fa var 11907ecb4019SLawrence Stewartis NULL, else it treats 11917ecb4019SLawrence Stewart.Fa var 11927ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at 11937ecb4019SLawrence Stewart.Fa var 11947ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by 11957ecb4019SLawrence Stewart.Fa head . 11967ecb4019SLawrence Stewart.Pp 11977ecb4019SLawrence StewartThe macro 11987ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE 11997ecb4019SLawrence Stewartbehaves identically to 12007ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_SAFE 12017ecb4019SLawrence Stewartwhen 12027ecb4019SLawrence Stewart.Fa var 12037ecb4019SLawrence Stewartis NULL, else it treats 12047ecb4019SLawrence Stewart.Fa var 12057ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at 12067ecb4019SLawrence Stewart.Fa var 12077ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by 12087ecb4019SLawrence Stewart.Fa head . 12097ecb4019SLawrence Stewart.Pp 12107ecb4019SLawrence StewartThe macro 1211afe61c15SRodney W. Grimes.Nm TAILQ_INIT 1212afe61c15SRodney W. Grimesinitializes the tail queue referenced by 1213afe61c15SRodney W. Grimes.Fa head . 1214afe61c15SRodney W. Grimes.Pp 1215afe61c15SRodney W. GrimesThe macro 1216afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD 1217afe61c15SRodney W. Grimesinserts the new element 1218afe61c15SRodney W. Grimes.Fa elm 1219afe61c15SRodney W. Grimesat the head of the tail queue. 1220afe61c15SRodney W. Grimes.Pp 1221afe61c15SRodney W. GrimesThe macro 1222afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL 1223afe61c15SRodney W. Grimesinserts the new element 1224afe61c15SRodney W. Grimes.Fa elm 1225afe61c15SRodney W. Grimesat the end of the tail queue. 1226afe61c15SRodney W. Grimes.Pp 1227afe61c15SRodney W. GrimesThe macro 1228afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER 1229afe61c15SRodney W. Grimesinserts the new element 1230afe61c15SRodney W. Grimes.Fa elm 1231afe61c15SRodney W. Grimesafter the element 1232afe61c15SRodney W. Grimes.Fa listelm . 1233afe61c15SRodney W. Grimes.Pp 1234afe61c15SRodney W. GrimesThe macro 12357658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE 12367658b0a2SJustin T. Gibbsinserts the new element 12377658b0a2SJustin T. Gibbs.Fa elm 12387658b0a2SJustin T. Gibbsbefore the element 12397658b0a2SJustin T. Gibbs.Fa listelm . 12407658b0a2SJustin T. Gibbs.Pp 12417658b0a2SJustin T. GibbsThe macro 1242c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST 1243c5c15c16SPoul-Henning Kampreturns the last item on the tail queue. 1244982ba1cbSKirk McKusickIf the tail queue is empty the return value is 1245982ba1cbSKirk McKusick.Dv NULL . 1246c5c15c16SPoul-Henning Kamp.Pp 1247c5c15c16SPoul-Henning KampThe macro 1248c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT 124979ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last. 125079ea9bc4SAlexey Zelkin.Pp 125179ea9bc4SAlexey ZelkinThe macro 125279ea9bc4SAlexey Zelkin.Nm TAILQ_PREV 125379ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item 125479ea9bc4SAlexey Zelkinis the first. 1255c5c15c16SPoul-Henning Kamp.Pp 1256c5c15c16SPoul-Henning KampThe macro 1257afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE 1258afe61c15SRodney W. Grimesremoves the element 1259afe61c15SRodney W. Grimes.Fa elm 1260afe61c15SRodney W. Grimesfrom the tail queue. 1261359295cdSMatthew D Fleming.Pp 1262359295cdSMatthew D FlemingThe macro 12637f479deeSDag-Erling Smørgrav.Fn TAILQ_REPLACE 12647f479deeSDag-Erling Smørgravreplaces the element 12657f479deeSDag-Erling Smørgrav.Fa elm 12667f479deeSDag-Erling Smørgravwith 12677f479deeSDag-Erling Smørgrav.Fa new 12687f479deeSDag-Erling Smørgravin the tail queue. 12697f479deeSDag-Erling SmørgravThe element 12707f479deeSDag-Erling Smørgrav.Fa new 12717f479deeSDag-Erling Smørgravmust not already be on a list. 12727f479deeSDag-Erling Smørgrav.Pp 12737f479deeSDag-Erling SmørgravThe macro 1274359295cdSMatthew D Fleming.Nm TAILQ_SWAP 1275359295cdSMatthew D Flemingswaps the contents of 1276359295cdSMatthew D Fleming.Fa head1 1277359295cdSMatthew D Flemingand 1278359295cdSMatthew D Fleming.Fa head2 . 1279afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE 1280afe61c15SRodney W. Grimes.Bd -literal 128103763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head = 128203763fe0SJake Burkholder TAILQ_HEAD_INITIALIZER(head); 1283afe61c15SRodney W. Grimesstruct tailhead *headp; /* Tail queue head. */ 1284afe61c15SRodney W. Grimesstruct entry { 1285afe61c15SRodney W. Grimes ... 1286afe61c15SRodney W. Grimes TAILQ_ENTRY(entry) entries; /* Tail queue. */ 1287afe61c15SRodney W. Grimes ... 12887f479deeSDag-Erling Smørgrav} *n1, *n2, *n3, *n4, *np; 1289afe61c15SRodney W. Grimes 1290afe61c15SRodney W. GrimesTAILQ_INIT(&head); /* Initialize the queue. */ 1291afe61c15SRodney W. Grimes 1292afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1293afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries); 1294afe61c15SRodney W. Grimes 1295afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 1296afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries); 1297afe61c15SRodney W. Grimes 1298afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 1299afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries); 13007658b0a2SJustin T. Gibbs 13017658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 13023652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries); 13037658b0a2SJustin T. Gibbs 13047658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries); /* Deletion. */ 13057658b0a2SJustin T. Gibbsfree(n2); 13067f479deeSDag-Erling Smørgrav 13077f479deeSDag-Erling Smørgravn4 = malloc(sizeof(struct entry)); /* Replacement. */ 13087f479deeSDag-Erling SmørgravTAILQ_REPLACE(&head, n3, n4, entries); 13097f479deeSDag-Erling Smørgravfree(n3); 1310afe61c15SRodney W. Grimes /* Forward traversal. */ 131179ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries) 1312afe61c15SRodney W. Grimes np-> ... 13132724bce2SAlexander Kabaev /* Safe forward traversal. */ 13142724bce2SAlexander KabaevTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 13152724bce2SAlexander Kabaev np->do_stuff(); 13162724bce2SAlexander Kabaev ... 13172724bce2SAlexander Kabaev TAILQ_REMOVE(&head, np, entries); 13182724bce2SAlexander Kabaev free(np); 13192724bce2SAlexander Kabaev} 13206c1d0fbfSArchie Cobbs /* Reverse traversal. */ 13216c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 13226c1d0fbfSArchie Cobbs np-> ... 13237658b0a2SJustin T. Gibbs /* TailQ Deletion. */ 1324d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) { 1325c5c15c16SPoul-Henning Kamp n1 = TAILQ_FIRST(&head); 132679ea9bc4SAlexey Zelkin TAILQ_REMOVE(&head, n1, entries); 13277658b0a2SJustin T. Gibbs free(n1); 13287658b0a2SJustin T. Gibbs} 13297658b0a2SJustin T. Gibbs /* Faster TailQ Deletion. */ 1330c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head); 13317658b0a2SJustin T. Gibbswhile (n1 != NULL) { 1332c5c15c16SPoul-Henning Kamp n2 = TAILQ_NEXT(n1, entries); 13337658b0a2SJustin T. Gibbs free(n1); 13347658b0a2SJustin T. Gibbs n1 = n2; 13357658b0a2SJustin T. Gibbs} 13367658b0a2SJustin T. GibbsTAILQ_INIT(&head); 1337afe61c15SRodney W. Grimes.Ed 133806b93667SConrad Meyer.Sh DIAGNOSTICS 133906b93667SConrad MeyerWhen debugging 134006b93667SConrad Meyer.Nm queue(3) , 134106b93667SConrad Meyerit can be useful to trace queue changes. 134206b93667SConrad MeyerTo enable tracing, define the macro 134306b93667SConrad Meyer.Va QUEUE_MACRO_DEBUG_TRACE 134406b93667SConrad Meyerat compile time. 134506b93667SConrad Meyer.Pp 134606b93667SConrad MeyerIt can also be useful to trash pointers that have been unlinked from a queue, 134706b93667SConrad Meyerto detect use after removal. 134806b93667SConrad MeyerTo enable pointer trashing, define the macro 134906b93667SConrad Meyer.Va QUEUE_MACRO_DEBUG_TRASH 135006b93667SConrad Meyerat compile time. 135106b93667SConrad MeyerThe macro 135206b93667SConrad Meyer.Fn QMD_IS_TRASHED "void *ptr" 135306b93667SConrad Meyerreturns true if 135406b93667SConrad Meyer.Fa ptr 135506b93667SConrad Meyerhas been trashed by the 135606b93667SConrad Meyer.Va QUEUE_MACRO_DEBUG_TRASH 135706b93667SConrad Meyeroption. 135806b93667SConrad Meyer.Pp 135906b93667SConrad MeyerIn the kernel (with 136006b93667SConrad Meyer.Va INVARIANTS 136106b93667SConrad Meyerenabled), the 136206b93667SConrad Meyer.Fn SLIST_REMOVE_PREVPTR 136306b93667SConrad Meyermacro is available to aid debugging: 136406b93667SConrad Meyer.Bl -hang -offset indent 136506b93667SConrad Meyer.It Fn SLIST_REMOVE_PREVPTR "TYPE **prev" "TYPE *elm" "SLIST_ENTRY NAME" 136606b93667SConrad Meyer.Pp 136706b93667SConrad MeyerRemoves 136806b93667SConrad Meyer.Fa elm , 136906b93667SConrad Meyerwhich must directly follow the element whose 137006b93667SConrad Meyer.Va &SLIST_NEXT() 137106b93667SConrad Meyeris 137206b93667SConrad Meyer.Fa prev , 137306b93667SConrad Meyerfrom the SLIST. 137406b93667SConrad MeyerThis macro validates that 137506b93667SConrad Meyer.Fa elm 137606b93667SConrad Meyerfollows 137706b93667SConrad Meyer.Fa prev 137806b93667SConrad Meyerin 137906b93667SConrad Meyer.Va INVARIANTS 138006b93667SConrad Meyermode. 138106b93667SConrad Meyer.El 1382b9ec8f3bSSimon L. B. Nielsen.Sh SEE ALSO 1383fad4b12bSEdward Tomasz Napierala.Xr arb 3 , 1384b9ec8f3bSSimon L. B. Nielsen.Xr tree 3 1385afe61c15SRodney W. Grimes.Sh HISTORY 1386afe61c15SRodney W. GrimesThe 1387afe61c15SRodney W. Grimes.Nm queue 138821421932SMike Pritchardfunctions first appeared in 138921421932SMike Pritchard.Bx 4.4 . 1390