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.\" 28afe61c15SRodney W. Grimes.\" @(#)queue.3 8.2 (Berkeley) 1/24/94 297f3dea24SPeter Wemm.\" $FreeBSD$ 30afe61c15SRodney W. Grimes.\" 31*65d31997SKirk McKusick.Dd August 15, 2016 32afe61c15SRodney W. Grimes.Dt QUEUE 3 333d45e180SRuslan Ermilov.Os 34afe61c15SRodney W. Grimes.Sh NAME 3538b622e1SHans Petter Selasky.Nm SLIST_CLASS_ENTRY , 3638b622e1SHans Petter Selasky.Nm SLIST_CLASS_HEAD , 37*65d31997SKirk McKusick.Nm SLIST_CONCAT , 38aea88892SPoul-Henning Kamp.Nm SLIST_EMPTY , 394a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY , 40aea88892SPoul-Henning Kamp.Nm SLIST_FIRST , 4179ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH , 427ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM , 437ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE , 4438b622e1SHans Petter Selasky.Nm SLIST_FOREACH_SAFE , 454a775e8fSJustin T. Gibbs.Nm SLIST_HEAD , 4603763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER , 474a775e8fSJustin T. Gibbs.Nm SLIST_INIT , 484a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER , 494a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD , 50aea88892SPoul-Henning Kamp.Nm SLIST_NEXT , 5138b622e1SHans Petter Selasky.Nm SLIST_REMOVE , 523d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER , 534a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD , 54359295cdSMatthew D Fleming.Nm SLIST_SWAP , 5538b622e1SHans Petter Selasky.Nm STAILQ_CLASS_ENTRY , 5638b622e1SHans Petter Selasky.Nm STAILQ_CLASS_HEAD , 57d57e28adSThomas Moestl.Nm STAILQ_CONCAT , 5879ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY , 594a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY , 6079ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST , 6179ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH , 627ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM , 637ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE , 6438b622e1SHans Petter Selasky.Nm STAILQ_FOREACH_SAFE , 654a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD , 6603763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER , 674a775e8fSJustin T. Gibbs.Nm STAILQ_INIT , 684a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER , 694a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD , 704a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL , 7179ea9bc4SAlexey Zelkin.Nm STAILQ_LAST , 7279ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT , 7338b622e1SHans Petter Selasky.Nm STAILQ_REMOVE , 743d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER , 754a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD , 76359295cdSMatthew D Fleming.Nm STAILQ_SWAP , 7738b622e1SHans Petter Selasky.Nm LIST_CLASS_ENTRY , 7838b622e1SHans Petter Selasky.Nm LIST_CLASS_HEAD , 79*65d31997SKirk McKusick.Nm LIST_CONCAT , 8079ea9bc4SAlexey Zelkin.Nm LIST_EMPTY , 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 , 96359295cdSMatthew D Fleming.Nm LIST_SWAP , 9738b622e1SHans Petter Selasky.Nm TAILQ_CLASS_ENTRY , 9838b622e1SHans Petter Selasky.Nm TAILQ_CLASS_HEAD , 99d57e28adSThomas Moestl.Nm TAILQ_CONCAT , 100c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY , 101afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY , 102c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST , 10379ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH , 1047ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM , 1057ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE , 1066c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE , 1077ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM , 1087ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE , 10938b622e1SHans Petter Selasky.Nm TAILQ_FOREACH_REVERSE_SAFE , 11038b622e1SHans Petter Selasky.Nm TAILQ_FOREACH_SAFE , 111afe61c15SRodney W. Grimes.Nm TAILQ_HEAD , 11203763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER , 113afe61c15SRodney W. Grimes.Nm TAILQ_INIT , 114afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER , 1157658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE , 116afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD , 117afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL , 118c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST , 119c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT , 12079ea9bc4SAlexey Zelkin.Nm TAILQ_PREV , 121359295cdSMatthew D Fleming.Nm TAILQ_REMOVE , 122359295cdSMatthew D Fleming.Nm TAILQ_SWAP 1234a775e8fSJustin T. Gibbs.Nd implementations of singly-linked lists, singly-linked tail queues, 12424b85d18SPoul-Henning Kamplists and tail queues 125afe61c15SRodney W. Grimes.Sh SYNOPSIS 12632eef9aeSRuslan Ermilov.In sys/queue.h 1278f20a914SMike Pritchard.\" 12838b622e1SHans Petter Selasky.Fn SLIST_CLASS_ENTRY "CLASSTYPE" 12938b622e1SHans Petter Selasky.Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE" 130*65d31997SKirk McKusick.Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME" 131aea88892SPoul-Henning Kamp.Fn SLIST_EMPTY "SLIST_HEAD *head" 1324a775e8fSJustin T. Gibbs.Fn SLIST_ENTRY "TYPE" 133aea88892SPoul-Henning Kamp.Fn SLIST_FIRST "SLIST_HEAD *head" 13479ea9bc4SAlexey Zelkin.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1357ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 1367ecb4019SLawrence Stewart.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 13738b622e1SHans Petter Selasky.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 1384a775e8fSJustin T. Gibbs.Fn SLIST_HEAD "HEADNAME" "TYPE" 13903763fe0SJake Burkholder.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head" 1404a775e8fSJustin T. Gibbs.Fn SLIST_INIT "SLIST_HEAD *head" 1414a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 1424a775e8fSJustin T. Gibbs.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 143aea88892SPoul-Henning Kamp.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 14438b622e1SHans Petter Selasky.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 1453d98b75bSEd Schouten.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME" 1464a775e8fSJustin T. Gibbs.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 14760fa6e9fSBenjamin Kaduk.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" 1488f20a914SMike Pritchard.\" 14938b622e1SHans Petter Selasky.Fn STAILQ_CLASS_ENTRY "CLASSTYPE" 15038b622e1SHans Petter Selasky.Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE" 151d57e28adSThomas Moestl.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" 15279ea9bc4SAlexey Zelkin.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 1534a775e8fSJustin T. Gibbs.Fn STAILQ_ENTRY "TYPE" 15479ea9bc4SAlexey Zelkin.Fn STAILQ_FIRST "STAILQ_HEAD *head" 15579ea9bc4SAlexey Zelkin.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1567ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 1577ecb4019SLawrence Stewart.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 15838b622e1SHans Petter Selasky.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 1594a775e8fSJustin T. Gibbs.Fn STAILQ_HEAD "HEADNAME" "TYPE" 16003763fe0SJake Burkholder.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" 1614a775e8fSJustin T. Gibbs.Fn STAILQ_INIT "STAILQ_HEAD *head" 1624a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 1634a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 1644a775e8fSJustin T. Gibbs.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 165d3f649deSBrooks Davis.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 16679ea9bc4SAlexey Zelkin.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 16738b622e1SHans Petter Selasky.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 1683d98b75bSEd Schouten.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 16902a98699SBruce Evans.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 17060fa6e9fSBenjamin Kaduk.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE" 1718f20a914SMike Pritchard.\" 17238b622e1SHans Petter Selasky.Fn LIST_CLASS_ENTRY "CLASSTYPE" 17338b622e1SHans Petter Selasky.Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE" 174*65d31997SKirk McKusick.Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 17579ea9bc4SAlexey Zelkin.Fn LIST_EMPTY "LIST_HEAD *head" 176afe61c15SRodney W. Grimes.Fn LIST_ENTRY "TYPE" 17779ea9bc4SAlexey Zelkin.Fn LIST_FIRST "LIST_HEAD *head" 17879ea9bc4SAlexey Zelkin.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 1797ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 1807ecb4019SLawrence Stewart.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 18138b622e1SHans Petter Selasky.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 182afe61c15SRodney W. Grimes.Fn LIST_HEAD "HEADNAME" "TYPE" 18303763fe0SJake Burkholder.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head" 184afe61c15SRodney W. Grimes.Fn LIST_INIT "LIST_HEAD *head" 1854a775e8fSJustin T. Gibbs.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 1864a775e8fSJustin T. Gibbs.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 187afe61c15SRodney W. Grimes.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 18879ea9bc4SAlexey Zelkin.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 1894170b083SEd Schouten.Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME" 190afe61c15SRodney W. Grimes.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 19190df28ccSBenjamin Kaduk.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 1928f20a914SMike Pritchard.\" 19338b622e1SHans Petter Selasky.Fn TAILQ_CLASS_ENTRY "CLASSTYPE" 19438b622e1SHans Petter Selasky.Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE" 195d57e28adSThomas Moestl.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME" 196c5c15c16SPoul-Henning Kamp.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 197afe61c15SRodney W. Grimes.Fn TAILQ_ENTRY "TYPE" 198c5c15c16SPoul-Henning Kamp.Fn TAILQ_FIRST "TAILQ_HEAD *head" 19979ea9bc4SAlexey Zelkin.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 2007ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 2017ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 2026c1d0fbfSArchie Cobbs.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 2037ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 2047ecb4019SLawrence Stewart.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 20538b622e1SHans Petter Selasky.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 20638b622e1SHans Petter Selasky.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 207afe61c15SRodney W. Grimes.Fn TAILQ_HEAD "HEADNAME" "TYPE" 20803763fe0SJake Burkholder.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head" 209afe61c15SRodney W. Grimes.Fn TAILQ_INIT "TAILQ_HEAD *head" 210afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 2113652ff55SJustin T. Gibbs.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 212afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 213afe61c15SRodney W. Grimes.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 21479ea9bc4SAlexey Zelkin.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 215c5c15c16SPoul-Henning Kamp.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 21679ea9bc4SAlexey Zelkin.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 217afe61c15SRodney W. Grimes.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 21890df28ccSBenjamin Kaduk.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME" 2198f20a914SMike Pritchard.\" 220afe61c15SRodney W. Grimes.Sh DESCRIPTION 22138b622e1SHans Petter SelaskyThese macros define and operate on four types of data structures which 22238b622e1SHans Petter Selaskycan be used in both C and C++ source code: 22338b622e1SHans Petter Selasky.Bl -enum -compact -offset indent 22438b622e1SHans Petter Selasky.It 22538b622e1SHans Petter SelaskyLists 22638b622e1SHans Petter Selasky.It 22738b622e1SHans Petter SelaskySingly-linked lists 22838b622e1SHans Petter Selasky.It 22938b622e1SHans Petter SelaskySingly-linked tail queues 23038b622e1SHans Petter Selasky.It 23138b622e1SHans Petter SelaskyTail queues 23238b622e1SHans Petter Selasky.El 233b86388c1SDima DorfmanAll four structures support the following functionality: 234afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 235afe61c15SRodney W. Grimes.It 236afe61c15SRodney W. GrimesInsertion of a new entry at the head of the list. 237afe61c15SRodney W. Grimes.It 238afe61c15SRodney W. GrimesInsertion of a new entry after any element in the list. 239afe61c15SRodney W. Grimes.It 2404a775e8fSJustin T. GibbsO(1) removal of an entry from the head of the list. 2417658b0a2SJustin T. Gibbs.It 242afe61c15SRodney W. GrimesForward traversal through the list. 243359295cdSMatthew D Fleming.It 244f9257802SRalf S. EngelschallSwapping the contents of two lists. 245afe61c15SRodney W. Grimes.El 246afe61c15SRodney W. Grimes.Pp 247d5fd66e1SEd MasteSingly-linked lists are the simplest of the four data structures 2484a775e8fSJustin T. Gibbsand support only the above functionality. 2494a775e8fSJustin T. GibbsSingly-linked lists are ideal for applications with large datasets 2504a775e8fSJustin T. Gibbsand few or no removals, 2514a775e8fSJustin T. Gibbsor for implementing a LIFO queue. 252ed741d4eSDavid E. O'BrienSingly-linked lists add the following functionality: 253ed741d4eSDavid E. O'Brien.Bl -enum -compact -offset indent 254ed741d4eSDavid E. O'Brien.It 255ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 256*65d31997SKirk McKusick.It 257*65d31997SKirk McKusickO(n) concatenation of two lists. 258ed741d4eSDavid E. O'Brien.El 2594a775e8fSJustin T. Gibbs.Pp 2604a775e8fSJustin T. GibbsSingly-linked tail queues add the following functionality: 2614a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2624a775e8fSJustin T. Gibbs.It 2634a775e8fSJustin T. GibbsEntries can be added at the end of a list. 264d57e28adSThomas Moestl.It 265ed741d4eSDavid E. O'BrienO(n) removal of any entry in the list. 266ed741d4eSDavid E. O'Brien.It 267d57e28adSThomas MoestlThey may be concatenated. 2684a775e8fSJustin T. Gibbs.El 2694a775e8fSJustin T. GibbsHowever: 2704a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2714a775e8fSJustin T. Gibbs.It 2724a775e8fSJustin T. GibbsAll list insertions must specify the head of the list. 2734a775e8fSJustin T. Gibbs.It 2744a775e8fSJustin T. GibbsEach head entry requires two pointers rather than one. 2754a775e8fSJustin T. Gibbs.It 2764a775e8fSJustin T. GibbsCode size is about 15% greater and operations run about 20% slower 2774a775e8fSJustin T. Gibbsthan singly-linked lists. 2784a775e8fSJustin T. Gibbs.El 2794a775e8fSJustin T. Gibbs.Pp 280f9257802SRalf S. EngelschallSingly-linked tail queues are ideal for applications with large datasets and 2814a775e8fSJustin T. Gibbsfew or no removals, 2824a775e8fSJustin T. Gibbsor for implementing a FIFO queue. 2834a775e8fSJustin T. Gibbs.Pp 284b86388c1SDima DorfmanAll doubly linked types of data structures (lists and tail queues) 285b86388c1SDima Dorfmanadditionally allow: 2864a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2874a775e8fSJustin T. Gibbs.It 2884a775e8fSJustin T. GibbsInsertion of a new entry before any element in the list. 2894a775e8fSJustin T. Gibbs.It 2904a775e8fSJustin T. GibbsO(1) removal of any entry in the list. 2914a775e8fSJustin T. Gibbs.El 2924a775e8fSJustin T. GibbsHowever: 2934a775e8fSJustin T. Gibbs.Bl -enum -compact -offset indent 2944a775e8fSJustin T. Gibbs.It 295ad035dafSChristian BruefferEach element requires two pointers rather than one. 2964a775e8fSJustin T. Gibbs.It 2974a775e8fSJustin T. GibbsCode size and execution time of operations (except for removal) is about 2984a775e8fSJustin T. Gibbstwice that of the singly-linked data-structures. 2994a775e8fSJustin T. Gibbs.El 3004a775e8fSJustin T. Gibbs.Pp 3014170b083SEd SchoutenLinked lists are the simplest of the doubly linked data structures. 3024170b083SEd SchoutenThey add the following functionality over the above: 3034170b083SEd Schouten.Bl -enum -compact -offset indent 3044170b083SEd Schouten.It 305*65d31997SKirk McKusickO(n) concatenation of two lists. 306*65d31997SKirk McKusick.It 3074170b083SEd SchoutenThey may be traversed backwards. 3084170b083SEd Schouten.El 3094170b083SEd SchoutenHowever: 3104170b083SEd Schouten.Bl -enum -compact -offset indent 3114170b083SEd Schouten.It 3124170b083SEd SchoutenTo traverse backwards, an entry to begin the traversal and the list in 3134170b083SEd Schoutenwhich it is contained must be specified. 3144170b083SEd Schouten.El 315afe61c15SRodney W. Grimes.Pp 316afe61c15SRodney W. GrimesTail queues add the following functionality: 317afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 318afe61c15SRodney W. Grimes.It 319afe61c15SRodney W. GrimesEntries can be added at the end of a list. 3206c1d0fbfSArchie Cobbs.It 3216c1d0fbfSArchie CobbsThey may be traversed backwards, from tail to head. 322d57e28adSThomas Moestl.It 323d57e28adSThomas MoestlThey may be concatenated. 324afe61c15SRodney W. Grimes.El 325afe61c15SRodney W. GrimesHowever: 326afe61c15SRodney W. Grimes.Bl -enum -compact -offset indent 327afe61c15SRodney W. Grimes.It 328afe61c15SRodney W. GrimesAll list insertions and removals must specify the head of the list. 329afe61c15SRodney W. Grimes.It 330afe61c15SRodney W. GrimesEach head entry requires two pointers rather than one. 331afe61c15SRodney W. Grimes.It 332afe61c15SRodney W. GrimesCode size is about 15% greater and operations run about 20% slower 3334a775e8fSJustin T. Gibbsthan singly-linked lists. 334afe61c15SRodney W. Grimes.El 335afe61c15SRodney W. Grimes.Pp 336afe61c15SRodney W. GrimesIn the macro definitions, 337afe61c15SRodney W. Grimes.Fa TYPE 33838b622e1SHans Petter Selaskyis the name of a user defined structure. 33938b622e1SHans Petter SelaskyThe structure must contain a field called 34038b622e1SHans Petter Selasky.Fa NAME 34138b622e1SHans Petter Selaskywhich is of type 3424a775e8fSJustin T. Gibbs.Li SLIST_ENTRY , 3434a775e8fSJustin T. Gibbs.Li STAILQ_ENTRY , 344afe61c15SRodney W. Grimes.Li LIST_ENTRY , 345afe61c15SRodney W. Grimesor 34638b622e1SHans Petter Selasky.Li TAILQ_ENTRY . 34738b622e1SHans Petter SelaskyIn the macro definitions, 34838b622e1SHans Petter Selasky.Fa CLASSTYPE 34938b622e1SHans Petter Selaskyis the name of a user defined class. 35038b622e1SHans Petter SelaskyThe class must contain a field called 35138b622e1SHans Petter Selasky.Fa NAME 35238b622e1SHans Petter Selaskywhich is of type 35338b622e1SHans Petter Selasky.Li SLIST_CLASS_ENTRY , 35438b622e1SHans Petter Selasky.Li STAILQ_CLASS_ENTRY , 35538b622e1SHans Petter Selasky.Li LIST_CLASS_ENTRY , 35638b622e1SHans Petter Selaskyor 35738b622e1SHans Petter Selasky.Li TAILQ_CLASS_ENTRY . 358afe61c15SRodney W. GrimesThe argument 359afe61c15SRodney W. Grimes.Fa HEADNAME 360afe61c15SRodney W. Grimesis the name of a user defined structure that must be declared 361afe61c15SRodney W. Grimesusing the macros 3624a775e8fSJustin T. Gibbs.Li SLIST_HEAD , 36338b622e1SHans Petter Selasky.Li SLIST_CLASS_HEAD , 3644a775e8fSJustin T. Gibbs.Li STAILQ_HEAD , 36538b622e1SHans Petter Selasky.Li STAILQ_CLASS_HEAD , 366afe61c15SRodney W. Grimes.Li LIST_HEAD , 36738b622e1SHans Petter Selasky.Li LIST_CLASS_HEAD , 36838b622e1SHans Petter Selasky.Li TAILQ_HEAD , 369afe61c15SRodney W. Grimesor 37038b622e1SHans Petter Selasky.Li TAILQ_CLASS_HEAD . 371afe61c15SRodney W. GrimesSee the examples below for further explanation of how these 372afe61c15SRodney W. Grimesmacros are used. 3734a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LISTS 3744a775e8fSJustin T. GibbsA singly-linked list is headed by a structure defined by the 3754a775e8fSJustin T. Gibbs.Nm SLIST_HEAD 3764a775e8fSJustin T. Gibbsmacro. 3774a775e8fSJustin T. GibbsThis structure contains a single pointer to the first element 3784a775e8fSJustin T. Gibbson the list. 3794a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer manipulation 3804a775e8fSJustin T. Gibbsoverhead at the expense of O(n) removal for arbitrary elements. 3814a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element or 3824a775e8fSJustin T. Gibbsat the head of the list. 3834a775e8fSJustin T. GibbsAn 3844a775e8fSJustin T. Gibbs.Fa SLIST_HEAD 3854a775e8fSJustin T. Gibbsstructure is declared as follows: 3864a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3874a775e8fSJustin T. GibbsSLIST_HEAD(HEADNAME, TYPE) head; 3884a775e8fSJustin T. Gibbs.Ed 3898f20a914SMike Pritchard.Pp 3904a775e8fSJustin T. Gibbswhere 3914a775e8fSJustin T. Gibbs.Fa HEADNAME 3924a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 3934a775e8fSJustin T. Gibbs.Fa TYPE 3944a775e8fSJustin T. Gibbsis the type of the elements to be linked into the list. 3954a775e8fSJustin T. GibbsA pointer to the head of the list can later be declared as: 3964a775e8fSJustin T. Gibbs.Bd -literal -offset indent 3974a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 3984a775e8fSJustin T. Gibbs.Ed 3998f20a914SMike Pritchard.Pp 4004a775e8fSJustin T. Gibbs(The names 4014a775e8fSJustin T. Gibbs.Li head 4024a775e8fSJustin T. Gibbsand 4034a775e8fSJustin T. Gibbs.Li headp 4044a775e8fSJustin T. Gibbsare user selectable.) 4054a775e8fSJustin T. Gibbs.Pp 4064a775e8fSJustin T. GibbsThe macro 40703763fe0SJake Burkholder.Nm SLIST_HEAD_INITIALIZER 40803763fe0SJake Burkholderevaluates to an initializer for the list 40903763fe0SJake Burkholder.Fa head . 41003763fe0SJake Burkholder.Pp 41103763fe0SJake BurkholderThe macro 412*65d31997SKirk McKusick.Nm SLIST_CONCAT 413*65d31997SKirk McKusickconcatenates the list headed by 414*65d31997SKirk McKusick.Fa head2 415*65d31997SKirk McKusickonto the end of the one headed by 416*65d31997SKirk McKusick.Fa head1 417*65d31997SKirk McKusickremoving all entries from the former. 418*65d31997SKirk McKusickUse of this macro should be avoided as it traverses the entirety of the 419*65d31997SKirk McKusick.Fa head1 420*65d31997SKirk McKusicklist. 421*65d31997SKirk McKusickA singly-linked tail queue should be used if this macro is needed in 422*65d31997SKirk McKusickhigh-usage code paths or to operate on long lists. 423*65d31997SKirk McKusick.Pp 424*65d31997SKirk McKusickThe macro 42579ea9bc4SAlexey Zelkin.Nm SLIST_EMPTY 42679ea9bc4SAlexey Zelkinevaluates to true if there are no elements in the list. 42779ea9bc4SAlexey Zelkin.Pp 42879ea9bc4SAlexey ZelkinThe macro 4294a775e8fSJustin T. Gibbs.Nm SLIST_ENTRY 4304a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 4314a775e8fSJustin T. Gibbsthe list. 4324a775e8fSJustin T. Gibbs.Pp 4334a775e8fSJustin T. GibbsThe macro 43479ea9bc4SAlexey Zelkin.Nm SLIST_FIRST 43579ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list is empty. 43679ea9bc4SAlexey Zelkin.Pp 43779ea9bc4SAlexey ZelkinThe macro 43879ea9bc4SAlexey Zelkin.Nm SLIST_FOREACH 43979ea9bc4SAlexey Zelkintraverses the list referenced by 44079ea9bc4SAlexey Zelkin.Fa head 44179ea9bc4SAlexey Zelkinin the forward direction, assigning each element in 44279ea9bc4SAlexey Zelkinturn to 44379ea9bc4SAlexey Zelkin.Fa var . 44479ea9bc4SAlexey Zelkin.Pp 44579ea9bc4SAlexey ZelkinThe macro 4467ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM 4477ecb4019SLawrence Stewartbehaves identically to 4487ecb4019SLawrence Stewart.Nm SLIST_FOREACH 4497ecb4019SLawrence Stewartwhen 4507ecb4019SLawrence Stewart.Fa var 4517ecb4019SLawrence Stewartis NULL, else it treats 4527ecb4019SLawrence Stewart.Fa var 4537ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at 4547ecb4019SLawrence Stewart.Fa var 4557ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by 4567ecb4019SLawrence Stewart.Fa head . 4577ecb4019SLawrence Stewart.Pp 4587ecb4019SLawrence StewartThe macro 4592724bce2SAlexander Kabaev.Nm SLIST_FOREACH_SAFE 4602724bce2SAlexander Kabaevtraverses the list referenced by 4612724bce2SAlexander Kabaev.Fa head 4622724bce2SAlexander Kabaevin the forward direction, assigning each element in 4632724bce2SAlexander Kabaevturn to 4642724bce2SAlexander Kabaev.Fa var . 4652724bce2SAlexander KabaevHowever, unlike 4662724bce2SAlexander Kabaev.Fn SLIST_FOREACH 4672724bce2SAlexander Kabaevhere it is permitted to both remove 4682724bce2SAlexander Kabaev.Fa var 4692724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 4702724bce2SAlexander Kabaevtraversal. 4712724bce2SAlexander Kabaev.Pp 4722724bce2SAlexander KabaevThe macro 4737ecb4019SLawrence Stewart.Nm SLIST_FOREACH_FROM_SAFE 4747ecb4019SLawrence Stewartbehaves identically to 4757ecb4019SLawrence Stewart.Nm SLIST_FOREACH_SAFE 4767ecb4019SLawrence Stewartwhen 4777ecb4019SLawrence Stewart.Fa var 4787ecb4019SLawrence Stewartis NULL, else it treats 4797ecb4019SLawrence Stewart.Fa var 4807ecb4019SLawrence Stewartas a previously found SLIST element and begins the loop at 4817ecb4019SLawrence Stewart.Fa var 4827ecb4019SLawrence Stewartinstead of the first element in the SLIST referenced by 4837ecb4019SLawrence Stewart.Fa head . 4847ecb4019SLawrence Stewart.Pp 4857ecb4019SLawrence StewartThe macro 4864a775e8fSJustin T. Gibbs.Nm SLIST_INIT 4874a775e8fSJustin T. Gibbsinitializes the list referenced by 4884a775e8fSJustin T. Gibbs.Fa head . 4894a775e8fSJustin T. Gibbs.Pp 4904a775e8fSJustin T. GibbsThe macro 4914a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_HEAD 4924a775e8fSJustin T. Gibbsinserts the new element 4934a775e8fSJustin T. Gibbs.Fa elm 4944a775e8fSJustin T. Gibbsat the head of the list. 4954a775e8fSJustin T. Gibbs.Pp 4964a775e8fSJustin T. GibbsThe macro 4974a775e8fSJustin T. Gibbs.Nm SLIST_INSERT_AFTER 4984a775e8fSJustin T. Gibbsinserts the new element 4994a775e8fSJustin T. Gibbs.Fa elm 5004a775e8fSJustin T. Gibbsafter the element 5014a775e8fSJustin T. Gibbs.Fa listelm . 5024a775e8fSJustin T. Gibbs.Pp 5034a775e8fSJustin T. GibbsThe macro 50479ea9bc4SAlexey Zelkin.Nm SLIST_NEXT 50579ea9bc4SAlexey Zelkinreturns the next element in the list. 50679ea9bc4SAlexey Zelkin.Pp 50779ea9bc4SAlexey ZelkinThe macro 5083d98b75bSEd Schouten.Nm SLIST_REMOVE_AFTER 5093d98b75bSEd Schoutenremoves the element after 5103d98b75bSEd Schouten.Fa elm 511bc106255SEitan Adlerfrom the list. 512bc106255SEitan AdlerUnlike 5133d98b75bSEd Schouten.Fa SLIST_REMOVE , 5143d98b75bSEd Schoutenthis macro does not traverse the entire list. 5153d98b75bSEd Schouten.Pp 5163d98b75bSEd SchoutenThe macro 5174a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE_HEAD 5184a775e8fSJustin T. Gibbsremoves the element 5194a775e8fSJustin T. Gibbs.Fa elm 5204a775e8fSJustin T. Gibbsfrom the head of the list. 5214a775e8fSJustin T. GibbsFor optimum efficiency, 5224a775e8fSJustin T. Gibbselements being removed from the head of the list should explicitly use 5234a775e8fSJustin T. Gibbsthis macro instead of the generic 5244a775e8fSJustin T. Gibbs.Fa SLIST_REMOVE 5254a775e8fSJustin T. Gibbsmacro. 5264a775e8fSJustin T. Gibbs.Pp 5274a775e8fSJustin T. GibbsThe macro 5284a775e8fSJustin T. Gibbs.Nm SLIST_REMOVE 5294a775e8fSJustin T. Gibbsremoves the element 5304a775e8fSJustin T. Gibbs.Fa elm 5314a775e8fSJustin T. Gibbsfrom the list. 532*65d31997SKirk McKusickUse of this macro should be avoided as it traverses the entire list. 533*65d31997SKirk McKusickA doubly-linked list should be used if this macro is needed in 534*65d31997SKirk McKusickhigh-usage code paths or to operate on long lists. 535359295cdSMatthew D Fleming.Pp 536359295cdSMatthew D FlemingThe macro 537359295cdSMatthew D Fleming.Nm SLIST_SWAP 538359295cdSMatthew D Flemingswaps the contents of 539359295cdSMatthew D Fleming.Fa head1 540359295cdSMatthew D Flemingand 541359295cdSMatthew D Fleming.Fa head2 . 5424a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED LIST EXAMPLE 5434a775e8fSJustin T. Gibbs.Bd -literal 54403763fe0SJake BurkholderSLIST_HEAD(slisthead, entry) head = 54503763fe0SJake Burkholder SLIST_HEAD_INITIALIZER(head); 5464a775e8fSJustin T. Gibbsstruct slisthead *headp; /* Singly-linked List head. */ 5474a775e8fSJustin T. Gibbsstruct entry { 5484a775e8fSJustin T. Gibbs ... 5494a775e8fSJustin T. Gibbs SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 5504a775e8fSJustin T. Gibbs ... 5514a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 5524a775e8fSJustin T. Gibbs 5534a775e8fSJustin T. GibbsSLIST_INIT(&head); /* Initialize the list. */ 5544a775e8fSJustin T. Gibbs 5554a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 5564a775e8fSJustin T. GibbsSLIST_INSERT_HEAD(&head, n1, entries); 5574a775e8fSJustin T. Gibbs 5584a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 5594a775e8fSJustin T. GibbsSLIST_INSERT_AFTER(n1, n2, entries); 5604a775e8fSJustin T. Gibbs 5614a775e8fSJustin T. GibbsSLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 5624a775e8fSJustin T. Gibbsfree(n2); 5634a775e8fSJustin T. Gibbs 56479ea9bc4SAlexey Zelkinn3 = SLIST_FIRST(&head); 56503763fe0SJake BurkholderSLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 5664a775e8fSJustin T. Gibbsfree(n3); 5674a775e8fSJustin T. Gibbs /* Forward traversal. */ 56879ea9bc4SAlexey ZelkinSLIST_FOREACH(np, &head, entries) 5694a775e8fSJustin T. Gibbs np-> ... 5702724bce2SAlexander Kabaev /* Safe forward traversal. */ 5712724bce2SAlexander KabaevSLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 5722724bce2SAlexander Kabaev np->do_stuff(); 5732724bce2SAlexander Kabaev ... 5742724bce2SAlexander Kabaev SLIST_REMOVE(&head, np, entry, entries); 5752724bce2SAlexander Kabaev free(np); 5762724bce2SAlexander Kabaev} 5774a775e8fSJustin T. Gibbs 57879ea9bc4SAlexey Zelkinwhile (!SLIST_EMPTY(&head)) { /* List Deletion. */ 57979ea9bc4SAlexey Zelkin n1 = SLIST_FIRST(&head); 5804a775e8fSJustin T. Gibbs SLIST_REMOVE_HEAD(&head, entries); 5814a775e8fSJustin T. Gibbs free(n1); 5824a775e8fSJustin T. Gibbs} 5834a775e8fSJustin T. Gibbs.Ed 5844a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUES 5854a775e8fSJustin T. GibbsA singly-linked tail queue is headed by a structure defined by the 5864a775e8fSJustin T. Gibbs.Nm STAILQ_HEAD 5874a775e8fSJustin T. Gibbsmacro. 5884a775e8fSJustin T. GibbsThis structure contains a pair of pointers, 5894a775e8fSJustin T. Gibbsone to the first element in the tail queue and the other to 5904a775e8fSJustin T. Gibbsthe last element in the tail queue. 5914a775e8fSJustin T. GibbsThe elements are singly linked for minimum space and pointer 5924a775e8fSJustin T. Gibbsmanipulation overhead at the expense of O(n) removal for arbitrary 5934a775e8fSJustin T. Gibbselements. 5944a775e8fSJustin T. GibbsNew elements can be added to the tail queue after an existing element, 5954a775e8fSJustin T. Gibbsat the head of the tail queue, or at the end of the tail queue. 5964a775e8fSJustin T. GibbsA 5974a775e8fSJustin T. Gibbs.Fa STAILQ_HEAD 5984a775e8fSJustin T. Gibbsstructure is declared as follows: 5994a775e8fSJustin T. Gibbs.Bd -literal -offset indent 6004a775e8fSJustin T. GibbsSTAILQ_HEAD(HEADNAME, TYPE) head; 6014a775e8fSJustin T. Gibbs.Ed 6028f20a914SMike Pritchard.Pp 6034a775e8fSJustin T. Gibbswhere 6044a775e8fSJustin T. Gibbs.Li HEADNAME 6054a775e8fSJustin T. Gibbsis the name of the structure to be defined, and 6064a775e8fSJustin T. Gibbs.Li TYPE 6074a775e8fSJustin T. Gibbsis the type of the elements to be linked into the tail queue. 6084a775e8fSJustin T. GibbsA pointer to the head of the tail queue can later be declared as: 6094a775e8fSJustin T. Gibbs.Bd -literal -offset indent 6104a775e8fSJustin T. Gibbsstruct HEADNAME *headp; 6114a775e8fSJustin T. Gibbs.Ed 6128f20a914SMike Pritchard.Pp 6134a775e8fSJustin T. Gibbs(The names 6144a775e8fSJustin T. Gibbs.Li head 6154a775e8fSJustin T. Gibbsand 6164a775e8fSJustin T. Gibbs.Li headp 6174a775e8fSJustin T. Gibbsare user selectable.) 6184a775e8fSJustin T. Gibbs.Pp 6194a775e8fSJustin T. GibbsThe macro 62003763fe0SJake Burkholder.Nm STAILQ_HEAD_INITIALIZER 62103763fe0SJake Burkholderevaluates to an initializer for the tail queue 62203763fe0SJake Burkholder.Fa head . 62303763fe0SJake Burkholder.Pp 62403763fe0SJake BurkholderThe macro 625d57e28adSThomas Moestl.Nm STAILQ_CONCAT 626d57e28adSThomas Moestlconcatenates the tail queue headed by 627d57e28adSThomas Moestl.Fa head2 628d57e28adSThomas Moestlonto the end of the one headed by 629d57e28adSThomas Moestl.Fa head1 630d57e28adSThomas Moestlremoving all entries from the former. 631d57e28adSThomas Moestl.Pp 632d57e28adSThomas MoestlThe macro 63379ea9bc4SAlexey Zelkin.Nm STAILQ_EMPTY 63479ea9bc4SAlexey Zelkinevaluates to true if there are no items on the tail queue. 63579ea9bc4SAlexey Zelkin.Pp 63679ea9bc4SAlexey ZelkinThe macro 6374a775e8fSJustin T. Gibbs.Nm STAILQ_ENTRY 6384a775e8fSJustin T. Gibbsdeclares a structure that connects the elements in 6394a775e8fSJustin T. Gibbsthe tail queue. 6404a775e8fSJustin T. Gibbs.Pp 6414a775e8fSJustin T. GibbsThe macro 64279ea9bc4SAlexey Zelkin.Nm STAILQ_FIRST 64379ea9bc4SAlexey Zelkinreturns the first item on the tail queue or NULL if the tail queue 64479ea9bc4SAlexey Zelkinis empty. 64579ea9bc4SAlexey Zelkin.Pp 64679ea9bc4SAlexey ZelkinThe macro 64779ea9bc4SAlexey Zelkin.Nm STAILQ_FOREACH 64879ea9bc4SAlexey Zelkintraverses the tail queue referenced by 64979ea9bc4SAlexey Zelkin.Fa head 65079ea9bc4SAlexey Zelkinin the forward direction, assigning each element 65179ea9bc4SAlexey Zelkinin turn to 65279ea9bc4SAlexey Zelkin.Fa var . 65379ea9bc4SAlexey Zelkin.Pp 65479ea9bc4SAlexey ZelkinThe macro 6557ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM 6567ecb4019SLawrence Stewartbehaves identically to 6577ecb4019SLawrence Stewart.Nm STAILQ_FOREACH 6587ecb4019SLawrence Stewartwhen 6597ecb4019SLawrence Stewart.Fa var 6607ecb4019SLawrence Stewartis NULL, else it treats 6617ecb4019SLawrence Stewart.Fa var 6627ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at 6637ecb4019SLawrence Stewart.Fa var 6647ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by 6657ecb4019SLawrence Stewart.Fa head . 6667ecb4019SLawrence Stewart.Pp 6677ecb4019SLawrence StewartThe macro 6682724bce2SAlexander Kabaev.Nm STAILQ_FOREACH_SAFE 6692724bce2SAlexander Kabaevtraverses the tail queue referenced by 6702724bce2SAlexander Kabaev.Fa head 6712724bce2SAlexander Kabaevin the forward direction, assigning each element 6722724bce2SAlexander Kabaevin turn to 6732724bce2SAlexander Kabaev.Fa var . 6742724bce2SAlexander KabaevHowever, unlike 6752724bce2SAlexander Kabaev.Fn STAILQ_FOREACH 6762724bce2SAlexander Kabaevhere it is permitted to both remove 6772724bce2SAlexander Kabaev.Fa var 6782724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 6792724bce2SAlexander Kabaevtraversal. 6802724bce2SAlexander Kabaev.Pp 6812724bce2SAlexander KabaevThe macro 6827ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_FROM_SAFE 6837ecb4019SLawrence Stewartbehaves identically to 6847ecb4019SLawrence Stewart.Nm STAILQ_FOREACH_SAFE 6857ecb4019SLawrence Stewartwhen 6867ecb4019SLawrence Stewart.Fa var 6877ecb4019SLawrence Stewartis NULL, else it treats 6887ecb4019SLawrence Stewart.Fa var 6897ecb4019SLawrence Stewartas a previously found STAILQ element and begins the loop at 6907ecb4019SLawrence Stewart.Fa var 6917ecb4019SLawrence Stewartinstead of the first element in the STAILQ referenced by 6927ecb4019SLawrence Stewart.Fa head . 6937ecb4019SLawrence Stewart.Pp 6947ecb4019SLawrence StewartThe macro 6954a775e8fSJustin T. Gibbs.Nm STAILQ_INIT 6964a775e8fSJustin T. Gibbsinitializes the tail queue referenced by 6974a775e8fSJustin T. Gibbs.Fa head . 6984a775e8fSJustin T. Gibbs.Pp 6994a775e8fSJustin T. GibbsThe macro 7004a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_HEAD 7014a775e8fSJustin T. Gibbsinserts the new element 7024a775e8fSJustin T. Gibbs.Fa elm 7034a775e8fSJustin T. Gibbsat the head of the tail queue. 7044a775e8fSJustin T. Gibbs.Pp 7054a775e8fSJustin T. GibbsThe macro 7064a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_TAIL 7074a775e8fSJustin T. Gibbsinserts the new element 7084a775e8fSJustin T. Gibbs.Fa elm 7094a775e8fSJustin T. Gibbsat the end of the tail queue. 7104a775e8fSJustin T. Gibbs.Pp 7114a775e8fSJustin T. GibbsThe macro 7124a775e8fSJustin T. Gibbs.Nm STAILQ_INSERT_AFTER 7134a775e8fSJustin T. Gibbsinserts the new element 7144a775e8fSJustin T. Gibbs.Fa elm 7154a775e8fSJustin T. Gibbsafter the element 7164a775e8fSJustin T. Gibbs.Fa listelm . 7174a775e8fSJustin T. Gibbs.Pp 7184a775e8fSJustin T. GibbsThe macro 71979ea9bc4SAlexey Zelkin.Nm STAILQ_LAST 72079ea9bc4SAlexey Zelkinreturns the last item on the tail queue. 721982ba1cbSKirk McKusickIf the tail queue is empty the return value is 722982ba1cbSKirk McKusick.Dv NULL . 72379ea9bc4SAlexey Zelkin.Pp 72479ea9bc4SAlexey ZelkinThe macro 72579ea9bc4SAlexey Zelkin.Nm STAILQ_NEXT 72679ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL this item is the last. 72779ea9bc4SAlexey Zelkin.Pp 72879ea9bc4SAlexey ZelkinThe macro 7293d98b75bSEd Schouten.Nm STAILQ_REMOVE_AFTER 7303d98b75bSEd Schoutenremoves the element after 7313d98b75bSEd Schouten.Fa elm 732bc106255SEitan Adlerfrom the tail queue. 733bc106255SEitan AdlerUnlike 7343d98b75bSEd Schouten.Fa STAILQ_REMOVE , 7353d98b75bSEd Schoutenthis macro does not traverse the entire tail queue. 7363d98b75bSEd Schouten.Pp 7373d98b75bSEd SchoutenThe macro 7384a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE_HEAD 739ce900f03SAlfred Perlsteinremoves the element at the head of the tail queue. 7404a775e8fSJustin T. GibbsFor optimum efficiency, 7414a775e8fSJustin T. Gibbselements being removed from the head of the tail queue should 7424a775e8fSJustin T. Gibbsuse this macro explicitly rather than the generic 7434a775e8fSJustin T. Gibbs.Fa STAILQ_REMOVE 7444a775e8fSJustin T. Gibbsmacro. 7454a775e8fSJustin T. Gibbs.Pp 7464a775e8fSJustin T. GibbsThe macro 7474a775e8fSJustin T. Gibbs.Nm STAILQ_REMOVE 7484a775e8fSJustin T. Gibbsremoves the element 7494a775e8fSJustin T. Gibbs.Fa elm 7504a775e8fSJustin T. Gibbsfrom the tail queue. 751*65d31997SKirk McKusickUse of this macro should be avoided as it traverses the entire list. 752*65d31997SKirk McKusickA doubly-linked tail queue should be used if this macro is needed in 753*65d31997SKirk McKusickhigh-usage code paths or to operate on long tail queues. 754359295cdSMatthew D Fleming.Pp 755359295cdSMatthew D FlemingThe macro 756359295cdSMatthew D Fleming.Nm STAILQ_SWAP 757359295cdSMatthew D Flemingswaps the contents of 758359295cdSMatthew D Fleming.Fa head1 759359295cdSMatthew D Flemingand 760359295cdSMatthew D Fleming.Fa head2 . 7614a775e8fSJustin T. Gibbs.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE 7624a775e8fSJustin T. Gibbs.Bd -literal 76303763fe0SJake BurkholderSTAILQ_HEAD(stailhead, entry) head = 76403763fe0SJake Burkholder STAILQ_HEAD_INITIALIZER(head); 7654a775e8fSJustin T. Gibbsstruct stailhead *headp; /* Singly-linked tail queue head. */ 7664a775e8fSJustin T. Gibbsstruct entry { 7674a775e8fSJustin T. Gibbs ... 7684a775e8fSJustin T. Gibbs STAILQ_ENTRY(entry) entries; /* Tail queue. */ 7694a775e8fSJustin T. Gibbs ... 7704a775e8fSJustin T. Gibbs} *n1, *n2, *n3, *np; 7714a775e8fSJustin T. Gibbs 7724a775e8fSJustin T. GibbsSTAILQ_INIT(&head); /* Initialize the queue. */ 7734a775e8fSJustin T. Gibbs 7744a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 7754a775e8fSJustin T. GibbsSTAILQ_INSERT_HEAD(&head, n1, entries); 7764a775e8fSJustin T. Gibbs 7774a775e8fSJustin T. Gibbsn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 7784a775e8fSJustin T. GibbsSTAILQ_INSERT_TAIL(&head, n1, entries); 7794a775e8fSJustin T. Gibbs 7804a775e8fSJustin T. Gibbsn2 = malloc(sizeof(struct entry)); /* Insert after. */ 7814a775e8fSJustin T. GibbsSTAILQ_INSERT_AFTER(&head, n1, n2, entries); 7824a775e8fSJustin T. Gibbs /* Deletion. */ 7834a775e8fSJustin T. GibbsSTAILQ_REMOVE(&head, n2, entry, entries); 7844a775e8fSJustin T. Gibbsfree(n2); 78503763fe0SJake Burkholder /* Deletion from the head. */ 78679ea9bc4SAlexey Zelkinn3 = STAILQ_FIRST(&head); 7874a775e8fSJustin T. GibbsSTAILQ_REMOVE_HEAD(&head, entries); 7884a775e8fSJustin T. Gibbsfree(n3); 7894a775e8fSJustin T. Gibbs /* Forward traversal. */ 79079ea9bc4SAlexey ZelkinSTAILQ_FOREACH(np, &head, entries) 7914a775e8fSJustin T. Gibbs np-> ... 7922724bce2SAlexander Kabaev /* Safe forward traversal. */ 7932724bce2SAlexander KabaevSTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 7942724bce2SAlexander Kabaev np->do_stuff(); 7952724bce2SAlexander Kabaev ... 7962724bce2SAlexander Kabaev STAILQ_REMOVE(&head, np, entry, entries); 7972724bce2SAlexander Kabaev free(np); 7982724bce2SAlexander Kabaev} 7994a775e8fSJustin T. Gibbs /* TailQ Deletion. */ 80079ea9bc4SAlexey Zelkinwhile (!STAILQ_EMPTY(&head)) { 80103763fe0SJake Burkholder n1 = STAILQ_FIRST(&head); 802266e33fdSJohn Baldwin STAILQ_REMOVE_HEAD(&head, entries); 8034a775e8fSJustin T. Gibbs free(n1); 8044a775e8fSJustin T. Gibbs} 8054a775e8fSJustin T. Gibbs /* Faster TailQ Deletion. */ 80679ea9bc4SAlexey Zelkinn1 = STAILQ_FIRST(&head); 8074a775e8fSJustin T. Gibbswhile (n1 != NULL) { 80879ea9bc4SAlexey Zelkin n2 = STAILQ_NEXT(n1, entries); 8094a775e8fSJustin T. Gibbs free(n1); 8104a775e8fSJustin T. Gibbs n1 = n2; 8114a775e8fSJustin T. Gibbs} 8124a775e8fSJustin T. GibbsSTAILQ_INIT(&head); 8134a775e8fSJustin T. Gibbs.Ed 814afe61c15SRodney W. Grimes.Sh LISTS 815afe61c15SRodney W. GrimesA list is headed by a structure defined by the 816afe61c15SRodney W. Grimes.Nm LIST_HEAD 817afe61c15SRodney W. Grimesmacro. 818afe61c15SRodney W. GrimesThis structure contains a single pointer to the first element 819afe61c15SRodney W. Grimeson the list. 820afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 821afe61c15SRodney W. Grimesremoved without traversing the list. 8224a775e8fSJustin T. GibbsNew elements can be added to the list after an existing element, 8234a775e8fSJustin T. Gibbsbefore an existing element, or at the head of the list. 824afe61c15SRodney W. GrimesA 825afe61c15SRodney W. Grimes.Fa LIST_HEAD 826afe61c15SRodney W. Grimesstructure is declared as follows: 827afe61c15SRodney W. Grimes.Bd -literal -offset indent 828afe61c15SRodney W. GrimesLIST_HEAD(HEADNAME, TYPE) head; 829afe61c15SRodney W. Grimes.Ed 8308f20a914SMike Pritchard.Pp 831afe61c15SRodney W. Grimeswhere 832afe61c15SRodney W. Grimes.Fa HEADNAME 833afe61c15SRodney W. Grimesis the name of the structure to be defined, and 834afe61c15SRodney W. Grimes.Fa TYPE 835afe61c15SRodney W. Grimesis the type of the elements to be linked into the list. 836afe61c15SRodney W. GrimesA pointer to the head of the list can later be declared as: 837afe61c15SRodney W. Grimes.Bd -literal -offset indent 838afe61c15SRodney W. Grimesstruct HEADNAME *headp; 839afe61c15SRodney W. Grimes.Ed 8408f20a914SMike Pritchard.Pp 841afe61c15SRodney W. Grimes(The names 842afe61c15SRodney W. Grimes.Li head 843afe61c15SRodney W. Grimesand 844afe61c15SRodney W. Grimes.Li headp 845afe61c15SRodney W. Grimesare user selectable.) 846afe61c15SRodney W. Grimes.Pp 847afe61c15SRodney W. GrimesThe macro 84803763fe0SJake Burkholder.Nm LIST_HEAD_INITIALIZER 84903763fe0SJake Burkholderevaluates to an initializer for the list 85003763fe0SJake Burkholder.Fa head . 85103763fe0SJake Burkholder.Pp 85203763fe0SJake BurkholderThe macro 853*65d31997SKirk McKusick.Nm LIST_CONCAT 854*65d31997SKirk McKusickconcatenates the list headed by 855*65d31997SKirk McKusick.Fa head2 856*65d31997SKirk McKusickonto the end of the one headed by 857*65d31997SKirk McKusick.Fa head1 858*65d31997SKirk McKusickremoving all entries from the former. 859*65d31997SKirk McKusickUse of this macro should be avoided as it traverses the entirety of the 860*65d31997SKirk McKusick.Fa head1 861*65d31997SKirk McKusicklist. 862*65d31997SKirk McKusickA tail queue should be used if this macro is needed in 863*65d31997SKirk McKusickhigh-usage code paths or to operate on long lists. 864*65d31997SKirk McKusick.Pp 865*65d31997SKirk McKusickThe macro 86679ea9bc4SAlexey Zelkin.Nm LIST_EMPTY 867ddbb94adSTom Rhodesevaluates to true if there are no elements in the list. 86879ea9bc4SAlexey Zelkin.Pp 86979ea9bc4SAlexey ZelkinThe macro 870afe61c15SRodney W. Grimes.Nm LIST_ENTRY 871afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 872afe61c15SRodney W. Grimesthe list. 873afe61c15SRodney W. Grimes.Pp 874afe61c15SRodney W. GrimesThe macro 87579ea9bc4SAlexey Zelkin.Nm LIST_FIRST 87679ea9bc4SAlexey Zelkinreturns the first element in the list or NULL if the list 87779ea9bc4SAlexey Zelkinis empty. 87879ea9bc4SAlexey Zelkin.Pp 87979ea9bc4SAlexey ZelkinThe macro 88079ea9bc4SAlexey Zelkin.Nm LIST_FOREACH 88179ea9bc4SAlexey Zelkintraverses the list referenced by 88279ea9bc4SAlexey Zelkin.Fa head 88379ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 88479ea9bc4SAlexey Zelkin.Fa var . 88579ea9bc4SAlexey Zelkin.Pp 88679ea9bc4SAlexey ZelkinThe macro 8877ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM 8887ecb4019SLawrence Stewartbehaves identically to 8897ecb4019SLawrence Stewart.Nm LIST_FOREACH 8907ecb4019SLawrence Stewartwhen 8917ecb4019SLawrence Stewart.Fa var 8927ecb4019SLawrence Stewartis NULL, else it treats 8937ecb4019SLawrence Stewart.Fa var 8947ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at 8957ecb4019SLawrence Stewart.Fa var 8967ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by 8977ecb4019SLawrence Stewart.Fa head . 8987ecb4019SLawrence Stewart.Pp 8997ecb4019SLawrence StewartThe macro 9004250a68eSBosko Milekic.Nm LIST_FOREACH_SAFE 9014250a68eSBosko Milekictraverses the list referenced by 9024250a68eSBosko Milekic.Fa head 9034250a68eSBosko Milekicin the forward direction, assigning each element in turn to 9044250a68eSBosko Milekic.Fa var . 9054250a68eSBosko MilekicHowever, unlike 9064250a68eSBosko Milekic.Fn LIST_FOREACH 9074250a68eSBosko Milekichere it is permitted to both remove 9084250a68eSBosko Milekic.Fa var 9094250a68eSBosko Milekicas well as free it from within the loop safely without interfering with the 9104250a68eSBosko Milekictraversal. 9114250a68eSBosko Milekic.Pp 9124250a68eSBosko MilekicThe macro 9137ecb4019SLawrence Stewart.Nm LIST_FOREACH_FROM_SAFE 9147ecb4019SLawrence Stewartbehaves identically to 9157ecb4019SLawrence Stewart.Nm LIST_FOREACH_SAFE 9167ecb4019SLawrence Stewartwhen 9177ecb4019SLawrence Stewart.Fa var 9187ecb4019SLawrence Stewartis NULL, else it treats 9197ecb4019SLawrence Stewart.Fa var 9207ecb4019SLawrence Stewartas a previously found LIST element and begins the loop at 9217ecb4019SLawrence Stewart.Fa var 9227ecb4019SLawrence Stewartinstead of the first element in the LIST referenced by 9237ecb4019SLawrence Stewart.Fa head . 9247ecb4019SLawrence Stewart.Pp 9257ecb4019SLawrence StewartThe macro 926afe61c15SRodney W. Grimes.Nm LIST_INIT 927afe61c15SRodney W. Grimesinitializes the list referenced by 928afe61c15SRodney W. Grimes.Fa head . 929afe61c15SRodney W. Grimes.Pp 930afe61c15SRodney W. GrimesThe macro 931afe61c15SRodney W. Grimes.Nm LIST_INSERT_HEAD 932afe61c15SRodney W. Grimesinserts the new element 933afe61c15SRodney W. Grimes.Fa elm 934afe61c15SRodney W. Grimesat the head of the list. 935afe61c15SRodney W. Grimes.Pp 936afe61c15SRodney W. GrimesThe macro 937afe61c15SRodney W. Grimes.Nm LIST_INSERT_AFTER 938afe61c15SRodney W. Grimesinserts the new element 939afe61c15SRodney W. Grimes.Fa elm 940afe61c15SRodney W. Grimesafter the element 941afe61c15SRodney W. Grimes.Fa listelm . 942afe61c15SRodney W. Grimes.Pp 943afe61c15SRodney W. GrimesThe macro 9447658b0a2SJustin T. Gibbs.Nm LIST_INSERT_BEFORE 9457658b0a2SJustin T. Gibbsinserts the new element 9467658b0a2SJustin T. Gibbs.Fa elm 9477658b0a2SJustin T. Gibbsbefore the element 9487658b0a2SJustin T. Gibbs.Fa listelm . 9497658b0a2SJustin T. Gibbs.Pp 9507658b0a2SJustin T. GibbsThe macro 95179ea9bc4SAlexey Zelkin.Nm LIST_NEXT 95279ea9bc4SAlexey Zelkinreturns the next element in the list, or NULL if this is the last. 95379ea9bc4SAlexey Zelkin.Pp 95479ea9bc4SAlexey ZelkinThe macro 9554170b083SEd Schouten.Nm LIST_PREV 9564170b083SEd Schoutenreturns the previous element in the list, or NULL if this is the first. 9574170b083SEd SchoutenList 9584170b083SEd Schouten.Fa head 9594170b083SEd Schoutenmust contain element 9604170b083SEd Schouten.Fa elm . 9614170b083SEd Schouten.Pp 9624170b083SEd SchoutenThe macro 963afe61c15SRodney W. Grimes.Nm LIST_REMOVE 964afe61c15SRodney W. Grimesremoves the element 965afe61c15SRodney W. Grimes.Fa elm 966afe61c15SRodney W. Grimesfrom the list. 967359295cdSMatthew D Fleming.Pp 968359295cdSMatthew D FlemingThe macro 969359295cdSMatthew D Fleming.Nm LIST_SWAP 970359295cdSMatthew D Flemingswaps the contents of 971359295cdSMatthew D Fleming.Fa head1 972359295cdSMatthew D Flemingand 973359295cdSMatthew D Fleming.Fa head2 . 974afe61c15SRodney W. Grimes.Sh LIST EXAMPLE 975afe61c15SRodney W. Grimes.Bd -literal 97603763fe0SJake BurkholderLIST_HEAD(listhead, entry) head = 97703763fe0SJake Burkholder LIST_HEAD_INITIALIZER(head); 978afe61c15SRodney W. Grimesstruct listhead *headp; /* List head. */ 979afe61c15SRodney W. Grimesstruct entry { 980afe61c15SRodney W. Grimes ... 981afe61c15SRodney W. Grimes LIST_ENTRY(entry) entries; /* List. */ 982afe61c15SRodney W. Grimes ... 9834250a68eSBosko Milekic} *n1, *n2, *n3, *np, *np_temp; 984afe61c15SRodney W. Grimes 985afe61c15SRodney W. GrimesLIST_INIT(&head); /* Initialize the list. */ 986afe61c15SRodney W. Grimes 987afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 988afe61c15SRodney W. GrimesLIST_INSERT_HEAD(&head, n1, entries); 989afe61c15SRodney W. Grimes 990afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 991afe61c15SRodney W. GrimesLIST_INSERT_AFTER(n1, n2, entries); 9927658b0a2SJustin T. Gibbs 9937658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 9947658b0a2SJustin T. GibbsLIST_INSERT_BEFORE(n2, n3, entries); 9957658b0a2SJustin T. Gibbs 9967658b0a2SJustin T. GibbsLIST_REMOVE(n2, entries); /* Deletion. */ 9977658b0a2SJustin T. Gibbsfree(n2); 998afe61c15SRodney W. Grimes /* Forward traversal. */ 99979ea9bc4SAlexey ZelkinLIST_FOREACH(np, &head, entries) 1000afe61c15SRodney W. Grimes np-> ... 1001afe61c15SRodney W. Grimes 10024250a68eSBosko Milekic /* Safe forward traversal. */ 10034250a68eSBosko MilekicLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 10044250a68eSBosko Milekic np->do_stuff(); 10054250a68eSBosko Milekic ... 10064250a68eSBosko Milekic LIST_REMOVE(np, entries); 10074250a68eSBosko Milekic free(np); 10084250a68eSBosko Milekic} 10094250a68eSBosko Milekic 101079ea9bc4SAlexey Zelkinwhile (!LIST_EMPTY(&head)) { /* List Deletion. */ 101179ea9bc4SAlexey Zelkin n1 = LIST_FIRST(&head); 10127658b0a2SJustin T. Gibbs LIST_REMOVE(n1, entries); 10137658b0a2SJustin T. Gibbs free(n1); 10147658b0a2SJustin T. Gibbs} 10157658b0a2SJustin T. Gibbs 101603763fe0SJake Burkholdern1 = LIST_FIRST(&head); /* Faster List Deletion. */ 10177658b0a2SJustin T. Gibbswhile (n1 != NULL) { 101879ea9bc4SAlexey Zelkin n2 = LIST_NEXT(n1, entries); 10197658b0a2SJustin T. Gibbs free(n1); 10207658b0a2SJustin T. Gibbs n1 = n2; 10217658b0a2SJustin T. Gibbs} 10227658b0a2SJustin T. GibbsLIST_INIT(&head); 1023afe61c15SRodney W. Grimes.Ed 1024afe61c15SRodney W. Grimes.Sh TAIL QUEUES 1025afe61c15SRodney W. GrimesA tail queue is headed by a structure defined by the 1026afe61c15SRodney W. Grimes.Nm TAILQ_HEAD 1027afe61c15SRodney W. Grimesmacro. 1028afe61c15SRodney W. GrimesThis structure contains a pair of pointers, 1029afe61c15SRodney W. Grimesone to the first element in the tail queue and the other to 1030afe61c15SRodney W. Grimesthe last element in the tail queue. 1031afe61c15SRodney W. GrimesThe elements are doubly linked so that an arbitrary element can be 1032afe61c15SRodney W. Grimesremoved without traversing the tail queue. 1033afe61c15SRodney W. GrimesNew elements can be added to the tail queue after an existing element, 10344a775e8fSJustin T. Gibbsbefore an existing element, at the head of the tail queue, 10354a775e8fSJustin T. Gibbsor at the end of the tail queue. 1036afe61c15SRodney W. GrimesA 1037afe61c15SRodney W. Grimes.Fa TAILQ_HEAD 1038afe61c15SRodney W. Grimesstructure is declared as follows: 1039afe61c15SRodney W. Grimes.Bd -literal -offset indent 1040afe61c15SRodney W. GrimesTAILQ_HEAD(HEADNAME, TYPE) head; 1041afe61c15SRodney W. Grimes.Ed 10428f20a914SMike Pritchard.Pp 1043afe61c15SRodney W. Grimeswhere 1044afe61c15SRodney W. Grimes.Li HEADNAME 1045afe61c15SRodney W. Grimesis the name of the structure to be defined, and 1046afe61c15SRodney W. Grimes.Li TYPE 1047afe61c15SRodney W. Grimesis the type of the elements to be linked into the tail queue. 1048afe61c15SRodney W. GrimesA pointer to the head of the tail queue can later be declared as: 1049afe61c15SRodney W. Grimes.Bd -literal -offset indent 1050afe61c15SRodney W. Grimesstruct HEADNAME *headp; 1051afe61c15SRodney W. Grimes.Ed 10528f20a914SMike Pritchard.Pp 1053afe61c15SRodney W. Grimes(The names 1054afe61c15SRodney W. Grimes.Li head 1055afe61c15SRodney W. Grimesand 1056afe61c15SRodney W. Grimes.Li headp 1057afe61c15SRodney W. Grimesare user selectable.) 1058afe61c15SRodney W. Grimes.Pp 1059afe61c15SRodney W. GrimesThe macro 106003763fe0SJake Burkholder.Nm TAILQ_HEAD_INITIALIZER 106103763fe0SJake Burkholderevaluates to an initializer for the tail queue 106203763fe0SJake Burkholder.Fa head . 106303763fe0SJake Burkholder.Pp 106403763fe0SJake BurkholderThe macro 1065d57e28adSThomas Moestl.Nm TAILQ_CONCAT 1066d57e28adSThomas Moestlconcatenates the tail queue headed by 1067d57e28adSThomas Moestl.Fa head2 1068d57e28adSThomas Moestlonto the end of the one headed by 1069d57e28adSThomas Moestl.Fa head1 1070d57e28adSThomas Moestlremoving all entries from the former. 1071d57e28adSThomas Moestl.Pp 1072d57e28adSThomas MoestlThe macro 1073c5c15c16SPoul-Henning Kamp.Nm TAILQ_EMPTY 1074c5c15c16SPoul-Henning Kampevaluates to true if there are no items on the tail queue. 1075c5c15c16SPoul-Henning Kamp.Pp 1076c5c15c16SPoul-Henning KampThe macro 1077afe61c15SRodney W. Grimes.Nm TAILQ_ENTRY 1078afe61c15SRodney W. Grimesdeclares a structure that connects the elements in 1079afe61c15SRodney W. Grimesthe tail queue. 1080afe61c15SRodney W. Grimes.Pp 1081afe61c15SRodney W. GrimesThe macro 1082c5c15c16SPoul-Henning Kamp.Nm TAILQ_FIRST 1083c5c15c16SPoul-Henning Kampreturns the first item on the tail queue or NULL if the tail queue 1084c5c15c16SPoul-Henning Kampis empty. 1085c5c15c16SPoul-Henning Kamp.Pp 1086c5c15c16SPoul-Henning KampThe macro 108779ea9bc4SAlexey Zelkin.Nm TAILQ_FOREACH 108879ea9bc4SAlexey Zelkintraverses the tail queue referenced by 108979ea9bc4SAlexey Zelkin.Fa head 109079ea9bc4SAlexey Zelkinin the forward direction, assigning each element in turn to 109179ea9bc4SAlexey Zelkin.Fa var . 1092fb5293cfSRuslan Ermilov.Fa var 1093fb5293cfSRuslan Ermilovis set to 1094fb5293cfSRuslan Ermilov.Dv NULL 1095fb5293cfSRuslan Ermilovif the loop completes normally, or if there were no elements. 109679ea9bc4SAlexey Zelkin.Pp 109779ea9bc4SAlexey ZelkinThe macro 10987ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM 10997ecb4019SLawrence Stewartbehaves identically to 11007ecb4019SLawrence Stewart.Nm TAILQ_FOREACH 11017ecb4019SLawrence Stewartwhen 11027ecb4019SLawrence Stewart.Fa var 11037ecb4019SLawrence Stewartis NULL, else it treats 11047ecb4019SLawrence Stewart.Fa var 11057ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at 11067ecb4019SLawrence Stewart.Fa var 11077ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by 11087ecb4019SLawrence Stewart.Fa head . 11097ecb4019SLawrence Stewart.Pp 11107ecb4019SLawrence StewartThe macro 11116c1d0fbfSArchie Cobbs.Nm TAILQ_FOREACH_REVERSE 11126c1d0fbfSArchie Cobbstraverses the tail queue referenced by 11136c1d0fbfSArchie Cobbs.Fa head 11146c1d0fbfSArchie Cobbsin the reverse direction, assigning each element in turn to 11156c1d0fbfSArchie Cobbs.Fa var . 11166c1d0fbfSArchie Cobbs.Pp 11177ecb4019SLawrence StewartThe macro 11187ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM 11197ecb4019SLawrence Stewartbehaves identically to 11207ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE 11217ecb4019SLawrence Stewartwhen 11227ecb4019SLawrence Stewart.Fa var 11237ecb4019SLawrence Stewartis NULL, else it treats 11247ecb4019SLawrence Stewart.Fa var 11257ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at 11267ecb4019SLawrence Stewart.Fa var 11277ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by 11287ecb4019SLawrence Stewart.Fa head . 11297ecb4019SLawrence Stewart.Pp 11302724bce2SAlexander KabaevThe macros 11312724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_SAFE 11322724bce2SAlexander Kabaevand 11332724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE_SAFE 11342724bce2SAlexander Kabaevtraverse the list referenced by 11352724bce2SAlexander Kabaev.Fa head 11362724bce2SAlexander Kabaevin the forward or reverse direction respectively, 11372724bce2SAlexander Kabaevassigning each element in turn to 11382724bce2SAlexander Kabaev.Fa var . 11393b96c71fSMike PritchardHowever, unlike their unsafe counterparts, 11402724bce2SAlexander Kabaev.Nm TAILQ_FOREACH 11412724bce2SAlexander Kabaevand 11422724bce2SAlexander Kabaev.Nm TAILQ_FOREACH_REVERSE 11432724bce2SAlexander Kabaevpermit to both remove 11442724bce2SAlexander Kabaev.Fa var 11452724bce2SAlexander Kabaevas well as free it from within the loop safely without interfering with the 11462724bce2SAlexander Kabaevtraversal. 11472724bce2SAlexander Kabaev.Pp 11486c1d0fbfSArchie CobbsThe macro 11497ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_FROM_SAFE 11507ecb4019SLawrence Stewartbehaves identically to 11517ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_SAFE 11527ecb4019SLawrence Stewartwhen 11537ecb4019SLawrence Stewart.Fa var 11547ecb4019SLawrence Stewartis NULL, else it treats 11557ecb4019SLawrence Stewart.Fa var 11567ecb4019SLawrence Stewartas a previously found TAILQ element and begins the loop at 11577ecb4019SLawrence Stewart.Fa var 11587ecb4019SLawrence Stewartinstead of the first element in the TAILQ referenced by 11597ecb4019SLawrence Stewart.Fa head . 11607ecb4019SLawrence Stewart.Pp 11617ecb4019SLawrence StewartThe macro 11627ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE 11637ecb4019SLawrence Stewartbehaves identically to 11647ecb4019SLawrence Stewart.Nm TAILQ_FOREACH_REVERSE_SAFE 11657ecb4019SLawrence Stewartwhen 11667ecb4019SLawrence Stewart.Fa var 11677ecb4019SLawrence Stewartis NULL, else it treats 11687ecb4019SLawrence Stewart.Fa var 11697ecb4019SLawrence Stewartas a previously found TAILQ element and begins the reverse loop at 11707ecb4019SLawrence Stewart.Fa var 11717ecb4019SLawrence Stewartinstead of the last element in the TAILQ referenced by 11727ecb4019SLawrence Stewart.Fa head . 11737ecb4019SLawrence Stewart.Pp 11747ecb4019SLawrence StewartThe macro 1175afe61c15SRodney W. Grimes.Nm TAILQ_INIT 1176afe61c15SRodney W. Grimesinitializes the tail queue referenced by 1177afe61c15SRodney W. Grimes.Fa head . 1178afe61c15SRodney W. Grimes.Pp 1179afe61c15SRodney W. GrimesThe macro 1180afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_HEAD 1181afe61c15SRodney W. Grimesinserts the new element 1182afe61c15SRodney W. Grimes.Fa elm 1183afe61c15SRodney W. Grimesat the head of the tail queue. 1184afe61c15SRodney W. Grimes.Pp 1185afe61c15SRodney W. GrimesThe macro 1186afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_TAIL 1187afe61c15SRodney W. Grimesinserts the new element 1188afe61c15SRodney W. Grimes.Fa elm 1189afe61c15SRodney W. Grimesat the end of the tail queue. 1190afe61c15SRodney W. Grimes.Pp 1191afe61c15SRodney W. GrimesThe macro 1192afe61c15SRodney W. Grimes.Nm TAILQ_INSERT_AFTER 1193afe61c15SRodney W. Grimesinserts the new element 1194afe61c15SRodney W. Grimes.Fa elm 1195afe61c15SRodney W. Grimesafter the element 1196afe61c15SRodney W. Grimes.Fa listelm . 1197afe61c15SRodney W. Grimes.Pp 1198afe61c15SRodney W. GrimesThe macro 11997658b0a2SJustin T. Gibbs.Nm TAILQ_INSERT_BEFORE 12007658b0a2SJustin T. Gibbsinserts the new element 12017658b0a2SJustin T. Gibbs.Fa elm 12027658b0a2SJustin T. Gibbsbefore the element 12037658b0a2SJustin T. Gibbs.Fa listelm . 12047658b0a2SJustin T. Gibbs.Pp 12057658b0a2SJustin T. GibbsThe macro 1206c5c15c16SPoul-Henning Kamp.Nm TAILQ_LAST 1207c5c15c16SPoul-Henning Kampreturns the last item on the tail queue. 1208982ba1cbSKirk McKusickIf the tail queue is empty the return value is 1209982ba1cbSKirk McKusick.Dv NULL . 1210c5c15c16SPoul-Henning Kamp.Pp 1211c5c15c16SPoul-Henning KampThe macro 1212c5c15c16SPoul-Henning Kamp.Nm TAILQ_NEXT 121379ea9bc4SAlexey Zelkinreturns the next item on the tail queue, or NULL if this item is the last. 121479ea9bc4SAlexey Zelkin.Pp 121579ea9bc4SAlexey ZelkinThe macro 121679ea9bc4SAlexey Zelkin.Nm TAILQ_PREV 121779ea9bc4SAlexey Zelkinreturns the previous item on the tail queue, or NULL if this item 121879ea9bc4SAlexey Zelkinis the first. 1219c5c15c16SPoul-Henning Kamp.Pp 1220c5c15c16SPoul-Henning KampThe macro 1221afe61c15SRodney W. Grimes.Nm TAILQ_REMOVE 1222afe61c15SRodney W. Grimesremoves the element 1223afe61c15SRodney W. Grimes.Fa elm 1224afe61c15SRodney W. Grimesfrom the tail queue. 1225359295cdSMatthew D Fleming.Pp 1226359295cdSMatthew D FlemingThe macro 1227359295cdSMatthew D Fleming.Nm TAILQ_SWAP 1228359295cdSMatthew D Flemingswaps the contents of 1229359295cdSMatthew D Fleming.Fa head1 1230359295cdSMatthew D Flemingand 1231359295cdSMatthew D Fleming.Fa head2 . 1232afe61c15SRodney W. Grimes.Sh TAIL QUEUE EXAMPLE 1233afe61c15SRodney W. Grimes.Bd -literal 123403763fe0SJake BurkholderTAILQ_HEAD(tailhead, entry) head = 123503763fe0SJake Burkholder TAILQ_HEAD_INITIALIZER(head); 1236afe61c15SRodney W. Grimesstruct tailhead *headp; /* Tail queue head. */ 1237afe61c15SRodney W. Grimesstruct entry { 1238afe61c15SRodney W. Grimes ... 1239afe61c15SRodney W. Grimes TAILQ_ENTRY(entry) entries; /* Tail queue. */ 1240afe61c15SRodney W. Grimes ... 12417658b0a2SJustin T. Gibbs} *n1, *n2, *n3, *np; 1242afe61c15SRodney W. Grimes 1243afe61c15SRodney W. GrimesTAILQ_INIT(&head); /* Initialize the queue. */ 1244afe61c15SRodney W. Grimes 1245afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1246afe61c15SRodney W. GrimesTAILQ_INSERT_HEAD(&head, n1, entries); 1247afe61c15SRodney W. Grimes 1248afe61c15SRodney W. Grimesn1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 1249afe61c15SRodney W. GrimesTAILQ_INSERT_TAIL(&head, n1, entries); 1250afe61c15SRodney W. Grimes 1251afe61c15SRodney W. Grimesn2 = malloc(sizeof(struct entry)); /* Insert after. */ 1252afe61c15SRodney W. GrimesTAILQ_INSERT_AFTER(&head, n1, n2, entries); 12537658b0a2SJustin T. Gibbs 12547658b0a2SJustin T. Gibbsn3 = malloc(sizeof(struct entry)); /* Insert before. */ 12553652ff55SJustin T. GibbsTAILQ_INSERT_BEFORE(n2, n3, entries); 12567658b0a2SJustin T. Gibbs 12577658b0a2SJustin T. GibbsTAILQ_REMOVE(&head, n2, entries); /* Deletion. */ 12587658b0a2SJustin T. Gibbsfree(n2); 1259afe61c15SRodney W. Grimes /* Forward traversal. */ 126079ea9bc4SAlexey ZelkinTAILQ_FOREACH(np, &head, entries) 1261afe61c15SRodney W. Grimes np-> ... 12622724bce2SAlexander Kabaev /* Safe forward traversal. */ 12632724bce2SAlexander KabaevTAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 12642724bce2SAlexander Kabaev np->do_stuff(); 12652724bce2SAlexander Kabaev ... 12662724bce2SAlexander Kabaev TAILQ_REMOVE(&head, np, entries); 12672724bce2SAlexander Kabaev free(np); 12682724bce2SAlexander Kabaev} 12696c1d0fbfSArchie Cobbs /* Reverse traversal. */ 12706c1d0fbfSArchie CobbsTAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 12716c1d0fbfSArchie Cobbs np-> ... 12727658b0a2SJustin T. Gibbs /* TailQ Deletion. */ 1273d6c63818SMark Murraywhile (!TAILQ_EMPTY(&head)) { 1274c5c15c16SPoul-Henning Kamp n1 = TAILQ_FIRST(&head); 127579ea9bc4SAlexey Zelkin TAILQ_REMOVE(&head, n1, entries); 12767658b0a2SJustin T. Gibbs free(n1); 12777658b0a2SJustin T. Gibbs} 12787658b0a2SJustin T. Gibbs /* Faster TailQ Deletion. */ 1279c5c15c16SPoul-Henning Kampn1 = TAILQ_FIRST(&head); 12807658b0a2SJustin T. Gibbswhile (n1 != NULL) { 1281c5c15c16SPoul-Henning Kamp n2 = TAILQ_NEXT(n1, entries); 12827658b0a2SJustin T. Gibbs free(n1); 12837658b0a2SJustin T. Gibbs n1 = n2; 12847658b0a2SJustin T. Gibbs} 12857658b0a2SJustin T. GibbsTAILQ_INIT(&head); 1286afe61c15SRodney W. Grimes.Ed 1287b9ec8f3bSSimon L. B. Nielsen.Sh SEE ALSO 1288b9ec8f3bSSimon L. B. Nielsen.Xr tree 3 1289afe61c15SRodney W. Grimes.Sh HISTORY 1290afe61c15SRodney W. GrimesThe 1291afe61c15SRodney W. Grimes.Nm queue 129221421932SMike Pritchardfunctions first appeared in 129321421932SMike Pritchard.Bx 4.4 . 1294