1.\" Copyright (c) 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd February 10, 2025 29.Dt QUEUE 3 30.Os 31.Sh NAME 32.Nm SLIST_CLASS_ENTRY , 33.Nm SLIST_CLASS_HEAD , 34.Nm SLIST_CONCAT , 35.Nm SLIST_EMPTY , 36.Nm SLIST_EMPTY_ATOMIC , 37.Nm SLIST_ENTRY , 38.Nm SLIST_FIRST , 39.Nm SLIST_FOREACH , 40.Nm SLIST_FOREACH_FROM , 41.Nm SLIST_FOREACH_FROM_SAFE , 42.Nm SLIST_FOREACH_SAFE , 43.Nm SLIST_HEAD , 44.Nm SLIST_HEAD_INITIALIZER , 45.Nm SLIST_INIT , 46.Nm SLIST_INSERT_AFTER , 47.Nm SLIST_INSERT_HEAD , 48.Nm SLIST_NEXT , 49.Nm SLIST_REMOVE , 50.Nm SLIST_REMOVE_AFTER , 51.Nm SLIST_REMOVE_HEAD , 52.Nm SLIST_SWAP , 53.Nm STAILQ_CLASS_ENTRY , 54.Nm STAILQ_CLASS_HEAD , 55.Nm STAILQ_CONCAT , 56.Nm STAILQ_EMPTY , 57.Nm STAILQ_EMPTY_ATOMIC , 58.Nm STAILQ_ENTRY , 59.Nm STAILQ_FIRST , 60.Nm STAILQ_FOREACH , 61.Nm STAILQ_FOREACH_FROM , 62.Nm STAILQ_FOREACH_FROM_SAFE , 63.Nm STAILQ_FOREACH_SAFE , 64.Nm STAILQ_HEAD , 65.Nm STAILQ_HEAD_INITIALIZER , 66.Nm STAILQ_INIT , 67.Nm STAILQ_INSERT_AFTER , 68.Nm STAILQ_INSERT_HEAD , 69.Nm STAILQ_INSERT_TAIL , 70.Nm STAILQ_LAST , 71.Nm STAILQ_NEXT , 72.Nm STAILQ_REMOVE , 73.Nm STAILQ_REMOVE_AFTER , 74.Nm STAILQ_REMOVE_HEAD , 75.Nm STAILQ_SWAP , 76.Nm LIST_CLASS_ENTRY , 77.Nm LIST_CLASS_HEAD , 78.Nm LIST_CONCAT , 79.Nm LIST_EMPTY , 80.Nm LIST_EMPTY_ATOMIC , 81.Nm LIST_ENTRY , 82.Nm LIST_FIRST , 83.Nm LIST_FOREACH , 84.Nm LIST_FOREACH_FROM , 85.Nm LIST_FOREACH_FROM_SAFE , 86.Nm LIST_FOREACH_SAFE , 87.Nm LIST_HEAD , 88.Nm LIST_HEAD_INITIALIZER , 89.Nm LIST_INIT , 90.Nm LIST_INSERT_AFTER , 91.Nm LIST_INSERT_BEFORE , 92.Nm LIST_INSERT_HEAD , 93.Nm LIST_NEXT , 94.Nm LIST_PREV , 95.Nm LIST_REMOVE , 96.Nm LIST_REPLACE , 97.Nm LIST_SWAP , 98.Nm TAILQ_CLASS_ENTRY , 99.Nm TAILQ_CLASS_HEAD , 100.Nm TAILQ_CONCAT , 101.Nm TAILQ_EMPTY , 102.Nm TAILQ_EMPTY_ATOMIC , 103.Nm TAILQ_ENTRY , 104.Nm TAILQ_FIRST , 105.Nm TAILQ_FOREACH , 106.Nm TAILQ_FOREACH_FROM , 107.Nm TAILQ_FOREACH_FROM_SAFE , 108.Nm TAILQ_FOREACH_REVERSE , 109.Nm TAILQ_FOREACH_REVERSE_FROM , 110.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE , 111.Nm TAILQ_FOREACH_REVERSE_SAFE , 112.Nm TAILQ_FOREACH_SAFE , 113.Nm TAILQ_HEAD , 114.Nm TAILQ_HEAD_INITIALIZER , 115.Nm TAILQ_INIT , 116.Nm TAILQ_INSERT_AFTER , 117.Nm TAILQ_INSERT_BEFORE , 118.Nm TAILQ_INSERT_HEAD , 119.Nm TAILQ_INSERT_TAIL , 120.Nm TAILQ_LAST , 121.Nm TAILQ_NEXT , 122.Nm TAILQ_PREV , 123.Nm TAILQ_REMOVE , 124.Nm TAILQ_REPLACE , 125.Nm TAILQ_SWAP 126.Nd implementations of singly-linked lists, singly-linked tail queues, 127lists and tail queues 128.Sh SYNOPSIS 129.In sys/queue.h 130.\" 131.Fn SLIST_CLASS_ENTRY "CLASSTYPE" 132.Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE" 133.Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME" 134.Fn SLIST_EMPTY "SLIST_HEAD *head" 135.Fn SLIST_EMPTY_ATOMIC "SLIST_HEAD *head" 136.Fn SLIST_ENTRY "TYPE" 137.Fn SLIST_FIRST "SLIST_HEAD *head" 138.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 139.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 140.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 141.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var" 142.Fn SLIST_HEAD "HEADNAME" "TYPE" 143.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head" 144.Fn SLIST_INIT "SLIST_HEAD *head" 145.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 146.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 147.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 148.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 149.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME" 150.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 151.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" 152.\" 153.Fn STAILQ_CLASS_ENTRY "CLASSTYPE" 154.Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE" 155.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" 156.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 157.Fn STAILQ_EMPTY_ATOMIC "STAILQ_HEAD *head" 158.Fn STAILQ_ENTRY "TYPE" 159.Fn STAILQ_FIRST "STAILQ_HEAD *head" 160.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 161.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 162.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 163.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" 164.Fn STAILQ_HEAD "HEADNAME" "TYPE" 165.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" 166.Fn STAILQ_INIT "STAILQ_HEAD *head" 167.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 168.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 169.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 170.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 171.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 172.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 173.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 174.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 175.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE" 176.\" 177.Fn LIST_CLASS_ENTRY "CLASSTYPE" 178.Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE" 179.Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 180.Fn LIST_EMPTY "LIST_HEAD *head" 181.Fn LIST_EMPTY_ATOMIC "LIST_HEAD *head" 182.Fn LIST_ENTRY "TYPE" 183.Fn LIST_FIRST "LIST_HEAD *head" 184.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 185.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 186.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 187.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var" 188.Fn LIST_HEAD "HEADNAME" "TYPE" 189.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head" 190.Fn LIST_INIT "LIST_HEAD *head" 191.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 192.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 193.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 194.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 195.Fn LIST_PREV "TYPE *elm" "LIST_HEAD *head" "TYPE" "LIST_ENTRY NAME" 196.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 197.Fn LIST_REPLACE "TYPE *elm" "TYPE *new" "LIST_ENTRY NAME" 198.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME" 199.\" 200.Fn TAILQ_CLASS_ENTRY "CLASSTYPE" 201.Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE" 202.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME" 203.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 204.Fn TAILQ_EMPTY_ATOMIC "TAILQ_HEAD *head" 205.Fn TAILQ_ENTRY "TYPE" 206.Fn TAILQ_FIRST "TAILQ_HEAD *head" 207.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 208.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 209.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 210.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 211.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 212.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 213.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var" 214.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var" 215.Fn TAILQ_HEAD "HEADNAME" "TYPE" 216.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head" 217.Fn TAILQ_INIT "TAILQ_HEAD *head" 218.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 219.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 220.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 221.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 222.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 223.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 224.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 225.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 226.Fn TAILQ_REPLACE "TAILQ_HEAD *head" "TYPE *elm" "TYPE *new" "TAILQ_ENTRY NAME" 227.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME" 228.\" 229.Sh DESCRIPTION 230These macros define and operate on four types of data structures which 231can be used in both C and C++ source code: 232.Bl -enum -compact -offset indent 233.It 234Lists 235.It 236Singly-linked lists 237.It 238Singly-linked tail queues 239.It 240Tail queues 241.El 242All four structures support the following functionality: 243.Bl -enum -compact -offset indent 244.It 245Insertion of a new entry at the head of the list. 246.It 247Insertion of a new entry after any element in the list. 248.It 249O(1) removal of an entry from the head of the list. 250.It 251Forward traversal through the list. 252.It 253Swapping the contents of two lists. 254.El 255.Pp 256Singly-linked lists are the simplest of the four data structures 257and support only the above functionality. 258Singly-linked lists are ideal for applications with large datasets 259and few or no removals, 260or for implementing a LIFO queue. 261Singly-linked lists add the following functionality: 262.Bl -enum -compact -offset indent 263.It 264O(n) removal of any entry in the list. 265.It 266O(n) concatenation of two lists. 267.El 268.Pp 269Singly-linked tail queues add the following functionality: 270.Bl -enum -compact -offset indent 271.It 272Entries can be added at the end of a list. 273.It 274O(n) removal of any entry in the list. 275.It 276They may be concatenated. 277.El 278However: 279.Bl -enum -compact -offset indent 280.It 281All list insertions must specify the head of the list. 282.It 283Each head entry requires two pointers rather than one. 284.It 285Code size is about 15% greater and operations run about 20% slower 286than singly-linked lists. 287.El 288.Pp 289Singly-linked tail queues are ideal for applications with large datasets and 290few or no removals, 291or for implementing a FIFO queue. 292.Pp 293All doubly linked types of data structures (lists and tail queues) 294additionally allow: 295.Bl -enum -compact -offset indent 296.It 297Insertion of a new entry before any element in the list. 298.It 299O(1) removal of any entry in the list. 300.El 301However: 302.Bl -enum -compact -offset indent 303.It 304Each element requires two pointers rather than one. 305.It 306Code size and execution time of operations (except for removal) is about 307twice that of the singly-linked data-structures. 308.El 309.Pp 310Linked lists are the simplest of the doubly linked data structures. 311They add the following functionality over the above: 312.Bl -enum -compact -offset indent 313.It 314O(n) concatenation of two lists. 315.It 316They may be traversed backwards. 317.El 318However: 319.Bl -enum -compact -offset indent 320.It 321To traverse backwards, an entry to begin the traversal and the list in 322which it is contained must be specified. 323.El 324.Pp 325Tail queues add the following functionality: 326.Bl -enum -compact -offset indent 327.It 328Entries can be added at the end of a list. 329.It 330They may be traversed backwards, from tail to head. 331.It 332They may be concatenated. 333.El 334However: 335.Bl -enum -compact -offset indent 336.It 337All list insertions and removals must specify the head of the list. 338.It 339Each head entry requires two pointers rather than one. 340.It 341Code size is about 15% greater and operations run about 20% slower 342than singly-linked lists. 343.El 344.Pp 345In the macro definitions, 346.Fa TYPE 347is the name of a user defined structure. 348The structure must contain a field called 349.Fa NAME 350which is of type 351.Li SLIST_ENTRY , 352.Li STAILQ_ENTRY , 353.Li LIST_ENTRY , 354or 355.Li TAILQ_ENTRY . 356In the macro definitions, 357.Fa CLASSTYPE 358is the name of a user defined class. 359The class must contain a field called 360.Fa NAME 361which is of type 362.Li SLIST_CLASS_ENTRY , 363.Li STAILQ_CLASS_ENTRY , 364.Li LIST_CLASS_ENTRY , 365or 366.Li TAILQ_CLASS_ENTRY . 367The argument 368.Fa HEADNAME 369is the name of a user defined structure that must be declared 370using the macros 371.Li SLIST_HEAD , 372.Li SLIST_CLASS_HEAD , 373.Li STAILQ_HEAD , 374.Li STAILQ_CLASS_HEAD , 375.Li LIST_HEAD , 376.Li LIST_CLASS_HEAD , 377.Li TAILQ_HEAD , 378or 379.Li TAILQ_CLASS_HEAD . 380See the examples below for further explanation of how these 381macros are used. 382.Sh SINGLY-LINKED LISTS 383A singly-linked list is headed by a structure defined by the 384.Nm SLIST_HEAD 385macro. 386This structure contains a single pointer to the first element 387on the list. 388The elements are singly linked for minimum space and pointer manipulation 389overhead at the expense of O(n) removal for arbitrary elements. 390New elements can be added to the list after an existing element or 391at the head of the list. 392An 393.Fa SLIST_HEAD 394structure is declared as follows: 395.Bd -literal -offset indent 396SLIST_HEAD(HEADNAME, TYPE) head; 397.Ed 398.Pp 399where 400.Fa HEADNAME 401is the name of the structure to be defined, and 402.Fa TYPE 403is the type of the elements to be linked into the list. 404A pointer to the head of the list can later be declared as: 405.Bd -literal -offset indent 406struct HEADNAME *headp; 407.Ed 408.Pp 409(The names 410.Li head 411and 412.Li headp 413are user selectable.) 414.Pp 415The macro 416.Nm SLIST_HEAD_INITIALIZER 417evaluates to an initializer for the list 418.Fa head . 419.Pp 420The macro 421.Nm SLIST_CONCAT 422concatenates the list headed by 423.Fa head2 424onto the end of the one headed by 425.Fa head1 426removing all entries from the former. 427Use of this macro should be avoided as it traverses the entirety of the 428.Fa head1 429list. 430A singly-linked tail queue should be used if this macro is needed in 431high-usage code paths or to operate on long lists. 432.Pp 433The macro 434.Nm SLIST_EMPTY 435evaluates to true if there are no elements in the list. 436The 437.Nm SLIST_EMPTY_ATOMIC 438variant has the same behavior, but can be safely used in contexts where it is 439possible that a different thread is concurrently updating the list. 440.Pp 441The macro 442.Nm SLIST_ENTRY 443declares a structure that connects the elements in 444the list. 445.Pp 446The macro 447.Nm SLIST_FIRST 448returns the first element in the list or NULL if the list is empty. 449.Pp 450The macro 451.Nm SLIST_FOREACH 452traverses the list referenced by 453.Fa head 454in the forward direction, assigning each element in 455turn to 456.Fa var . 457.Pp 458The macro 459.Nm SLIST_FOREACH_FROM 460behaves identically to 461.Nm SLIST_FOREACH 462when 463.Fa var 464is NULL, else it treats 465.Fa var 466as a previously found SLIST element and begins the loop at 467.Fa var 468instead of the first element in the SLIST referenced by 469.Fa head . 470.Pp 471The macro 472.Nm SLIST_FOREACH_SAFE 473traverses the list referenced by 474.Fa head 475in the forward direction, assigning each element in 476turn to 477.Fa var . 478However, unlike 479.Fn SLIST_FOREACH 480here it is permitted to both remove 481.Fa var 482as well as free it from within the loop safely without interfering with the 483traversal. 484.Pp 485The macro 486.Nm SLIST_FOREACH_FROM_SAFE 487behaves identically to 488.Nm SLIST_FOREACH_SAFE 489when 490.Fa var 491is NULL, else it treats 492.Fa var 493as a previously found SLIST element and begins the loop at 494.Fa var 495instead of the first element in the SLIST referenced by 496.Fa head . 497.Pp 498The macro 499.Nm SLIST_INIT 500initializes the list referenced by 501.Fa head . 502.Pp 503The macro 504.Nm SLIST_INSERT_HEAD 505inserts the new element 506.Fa elm 507at the head of the list. 508.Pp 509The macro 510.Nm SLIST_INSERT_AFTER 511inserts the new element 512.Fa elm 513after the element 514.Fa listelm . 515.Pp 516The macro 517.Nm SLIST_NEXT 518returns the next element in the list. 519.Pp 520The macro 521.Nm SLIST_REMOVE_AFTER 522removes the element after 523.Fa elm 524from the list. 525Unlike 526.Fa SLIST_REMOVE , 527this macro does not traverse the entire list. 528.Pp 529The macro 530.Nm SLIST_REMOVE_HEAD 531removes the element 532.Fa elm 533from the head of the list. 534For optimum efficiency, 535elements being removed from the head of the list should explicitly use 536this macro instead of the generic 537.Fa SLIST_REMOVE 538macro. 539.Pp 540The macro 541.Nm SLIST_REMOVE 542removes the element 543.Fa elm 544from the list. 545Use of this macro should be avoided as it traverses the entire list. 546A doubly-linked list should be used if this macro is needed in 547high-usage code paths or to operate on long lists. 548.Pp 549The macro 550.Nm SLIST_SWAP 551swaps the contents of 552.Fa head1 553and 554.Fa head2 . 555.Sh SINGLY-LINKED LIST EXAMPLE 556.Bd -literal 557SLIST_HEAD(slisthead, entry) head = 558 SLIST_HEAD_INITIALIZER(head); 559struct slisthead *headp; /* Singly-linked List head. */ 560struct entry { 561 ... 562 SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 563 ... 564} *n1, *n2, *n3, *np; 565 566SLIST_INIT(&head); /* Initialize the list. */ 567 568n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 569SLIST_INSERT_HEAD(&head, n1, entries); 570 571n2 = malloc(sizeof(struct entry)); /* Insert after. */ 572SLIST_INSERT_AFTER(n1, n2, entries); 573 574SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 575free(n2); 576 577n3 = SLIST_FIRST(&head); 578SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 579free(n3); 580 /* Forward traversal. */ 581SLIST_FOREACH(np, &head, entries) 582 np-> ... 583 /* Safe forward traversal. */ 584SLIST_FOREACH_SAFE(np, &head, entries, np_temp) { 585 np->do_stuff(); 586 ... 587 SLIST_REMOVE(&head, np, entry, entries); 588 free(np); 589} 590 591while (!SLIST_EMPTY(&head)) { /* List Deletion. */ 592 n1 = SLIST_FIRST(&head); 593 SLIST_REMOVE_HEAD(&head, entries); 594 free(n1); 595} 596.Ed 597.Sh SINGLY-LINKED TAIL QUEUES 598A singly-linked tail queue is headed by a structure defined by the 599.Nm STAILQ_HEAD 600macro. 601This structure contains a pair of pointers, 602one to the first element in the tail queue and the other to 603the last element in the tail queue. 604The elements are singly linked for minimum space and pointer 605manipulation overhead at the expense of O(n) removal for arbitrary 606elements. 607New elements can be added to the tail queue after an existing element, 608at the head of the tail queue, or at the end of the tail queue. 609A 610.Fa STAILQ_HEAD 611structure is declared as follows: 612.Bd -literal -offset indent 613STAILQ_HEAD(HEADNAME, TYPE) head; 614.Ed 615.Pp 616where 617.Li HEADNAME 618is the name of the structure to be defined, and 619.Li TYPE 620is the type of the elements to be linked into the tail queue. 621A pointer to the head of the tail queue can later be declared as: 622.Bd -literal -offset indent 623struct HEADNAME *headp; 624.Ed 625.Pp 626(The names 627.Li head 628and 629.Li headp 630are user selectable.) 631.Pp 632The macro 633.Nm STAILQ_HEAD_INITIALIZER 634evaluates to an initializer for the tail queue 635.Fa head . 636.Pp 637The macro 638.Nm STAILQ_CONCAT 639concatenates the tail queue headed by 640.Fa head2 641onto the end of the one headed by 642.Fa head1 643removing all entries from the former. 644.Pp 645The macro 646.Nm STAILQ_EMPTY 647evaluates to true if there are no items on the tail queue. 648The 649.Nm STAILQ_EMPTY_ATOMIC 650variant has the same behavior, but can be safely used in contexts where it is 651possible that a different thread is concurrently updating the queue. 652.Pp 653The macro 654.Nm STAILQ_ENTRY 655declares a structure that connects the elements in 656the tail queue. 657.Pp 658The macro 659.Nm STAILQ_FIRST 660returns the first item on the tail queue or NULL if the tail queue 661is empty. 662.Pp 663The macro 664.Nm STAILQ_FOREACH 665traverses the tail queue referenced by 666.Fa head 667in the forward direction, assigning each element 668in turn to 669.Fa var . 670.Pp 671The macro 672.Nm STAILQ_FOREACH_FROM 673behaves identically to 674.Nm STAILQ_FOREACH 675when 676.Fa var 677is NULL, else it treats 678.Fa var 679as a previously found STAILQ element and begins the loop at 680.Fa var 681instead of the first element in the STAILQ referenced by 682.Fa head . 683.Pp 684The macro 685.Nm STAILQ_FOREACH_SAFE 686traverses the tail queue referenced by 687.Fa head 688in the forward direction, assigning each element 689in turn to 690.Fa var . 691However, unlike 692.Fn STAILQ_FOREACH 693here it is permitted to both remove 694.Fa var 695as well as free it from within the loop safely without interfering with the 696traversal. 697.Pp 698The macro 699.Nm STAILQ_FOREACH_FROM_SAFE 700behaves identically to 701.Nm STAILQ_FOREACH_SAFE 702when 703.Fa var 704is NULL, else it treats 705.Fa var 706as a previously found STAILQ element and begins the loop at 707.Fa var 708instead of the first element in the STAILQ referenced by 709.Fa head . 710.Pp 711The macro 712.Nm STAILQ_INIT 713initializes the tail queue referenced by 714.Fa head . 715.Pp 716The macro 717.Nm STAILQ_INSERT_HEAD 718inserts the new element 719.Fa elm 720at the head of the tail queue. 721.Pp 722The macro 723.Nm STAILQ_INSERT_TAIL 724inserts the new element 725.Fa elm 726at the end of the tail queue. 727.Pp 728The macro 729.Nm STAILQ_INSERT_AFTER 730inserts the new element 731.Fa elm 732after the element 733.Fa listelm . 734.Pp 735The macro 736.Nm STAILQ_LAST 737returns the last item on the tail queue. 738If the tail queue is empty the return value is 739.Dv NULL . 740.Pp 741The macro 742.Nm STAILQ_NEXT 743returns the next item on the tail queue, or NULL this item is the last. 744.Pp 745The macro 746.Nm STAILQ_REMOVE_AFTER 747removes the element after 748.Fa elm 749from the tail queue. 750Unlike 751.Fa STAILQ_REMOVE , 752this macro does not traverse the entire tail queue. 753.Pp 754The macro 755.Nm STAILQ_REMOVE_HEAD 756removes the element at the head of the tail queue. 757For optimum efficiency, 758elements being removed from the head of the tail queue should 759use this macro explicitly rather than the generic 760.Fa STAILQ_REMOVE 761macro. 762.Pp 763The macro 764.Nm STAILQ_REMOVE 765removes the element 766.Fa elm 767from the tail queue. 768Use of this macro should be avoided as it traverses the entire list. 769A doubly-linked tail queue should be used if this macro is needed in 770high-usage code paths or to operate on long tail queues. 771.Pp 772The macro 773.Nm STAILQ_SWAP 774swaps the contents of 775.Fa head1 776and 777.Fa head2 . 778.Sh SINGLY-LINKED TAIL QUEUE EXAMPLE 779.Bd -literal 780STAILQ_HEAD(stailhead, entry) head = 781 STAILQ_HEAD_INITIALIZER(head); 782struct stailhead *headp; /* Singly-linked tail queue head. */ 783struct entry { 784 ... 785 STAILQ_ENTRY(entry) entries; /* Tail queue. */ 786 ... 787} *n1, *n2, *n3, *np; 788 789STAILQ_INIT(&head); /* Initialize the queue. */ 790 791n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 792STAILQ_INSERT_HEAD(&head, n1, entries); 793 794n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 795STAILQ_INSERT_TAIL(&head, n1, entries); 796 797n2 = malloc(sizeof(struct entry)); /* Insert after. */ 798STAILQ_INSERT_AFTER(&head, n1, n2, entries); 799 /* Deletion. */ 800STAILQ_REMOVE(&head, n2, entry, entries); 801free(n2); 802 /* Deletion from the head. */ 803n3 = STAILQ_FIRST(&head); 804STAILQ_REMOVE_HEAD(&head, entries); 805free(n3); 806 /* Forward traversal. */ 807STAILQ_FOREACH(np, &head, entries) 808 np-> ... 809 /* Safe forward traversal. */ 810STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 811 np->do_stuff(); 812 ... 813 STAILQ_REMOVE(&head, np, entry, entries); 814 free(np); 815} 816 /* TailQ Deletion. */ 817while (!STAILQ_EMPTY(&head)) { 818 n1 = STAILQ_FIRST(&head); 819 STAILQ_REMOVE_HEAD(&head, entries); 820 free(n1); 821} 822 /* Faster TailQ Deletion. */ 823n1 = STAILQ_FIRST(&head); 824while (n1 != NULL) { 825 n2 = STAILQ_NEXT(n1, entries); 826 free(n1); 827 n1 = n2; 828} 829STAILQ_INIT(&head); 830.Ed 831.Sh LISTS 832A list is headed by a structure defined by the 833.Nm LIST_HEAD 834macro. 835This structure contains a single pointer to the first element 836on the list. 837The elements are doubly linked so that an arbitrary element can be 838removed without traversing the list. 839New elements can be added to the list after an existing element, 840before an existing element, or at the head of the list. 841A 842.Fa LIST_HEAD 843structure is declared as follows: 844.Bd -literal -offset indent 845LIST_HEAD(HEADNAME, TYPE) head; 846.Ed 847.Pp 848where 849.Fa HEADNAME 850is the name of the structure to be defined, and 851.Fa TYPE 852is the type of the elements to be linked into the list. 853A pointer to the head of the list can later be declared as: 854.Bd -literal -offset indent 855struct HEADNAME *headp; 856.Ed 857.Pp 858(The names 859.Li head 860and 861.Li headp 862are user selectable.) 863.Pp 864The macro 865.Nm LIST_HEAD_INITIALIZER 866evaluates to an initializer for the list 867.Fa head . 868.Pp 869The macro 870.Nm LIST_CONCAT 871concatenates the list headed by 872.Fa head2 873onto the end of the one headed by 874.Fa head1 875removing all entries from the former. 876Use of this macro should be avoided as it traverses the entirety of the 877.Fa head1 878list. 879A tail queue should be used if this macro is needed in 880high-usage code paths or to operate on long lists. 881.Pp 882The macro 883.Nm LIST_EMPTY 884evaluates to true if there are no elements in the list. 885The 886.Nm LIST_EMPTY_ATOMIC 887variant has the same behavior, but can be safely used in contexts where it is 888possible that a different thread is concurrently updating the list. 889.Pp 890The macro 891.Nm LIST_ENTRY 892declares a structure that connects the elements in 893the list. 894.Pp 895The macro 896.Nm LIST_FIRST 897returns the first element in the list or NULL if the list 898is empty. 899.Pp 900The macro 901.Nm LIST_FOREACH 902traverses the list referenced by 903.Fa head 904in the forward direction, assigning each element in turn to 905.Fa var . 906.Pp 907The macro 908.Nm LIST_FOREACH_FROM 909behaves identically to 910.Nm LIST_FOREACH 911when 912.Fa var 913is NULL, else it treats 914.Fa var 915as a previously found LIST element and begins the loop at 916.Fa var 917instead of the first element in the LIST referenced by 918.Fa head . 919.Pp 920The macro 921.Nm LIST_FOREACH_SAFE 922traverses the list referenced by 923.Fa head 924in the forward direction, assigning each element in turn to 925.Fa var . 926However, unlike 927.Fn LIST_FOREACH 928here it is permitted to both remove 929.Fa var 930as well as free it from within the loop safely without interfering with the 931traversal. 932.Pp 933The macro 934.Nm LIST_FOREACH_FROM_SAFE 935behaves identically to 936.Nm LIST_FOREACH_SAFE 937when 938.Fa var 939is NULL, else it treats 940.Fa var 941as a previously found LIST element and begins the loop at 942.Fa var 943instead of the first element in the LIST referenced by 944.Fa head . 945.Pp 946The macro 947.Nm LIST_INIT 948initializes the list referenced by 949.Fa head . 950.Pp 951The macro 952.Nm LIST_INSERT_HEAD 953inserts the new element 954.Fa elm 955at the head of the list. 956.Pp 957The macro 958.Nm LIST_INSERT_AFTER 959inserts the new element 960.Fa elm 961after the element 962.Fa listelm . 963.Pp 964The macro 965.Nm LIST_INSERT_BEFORE 966inserts the new element 967.Fa elm 968before the element 969.Fa listelm . 970.Pp 971The macro 972.Nm LIST_NEXT 973returns the next element in the list, or NULL if this is the last. 974.Pp 975The macro 976.Nm LIST_PREV 977returns the previous element in the list, or NULL if this is the first. 978List 979.Fa head 980must contain element 981.Fa elm . 982.Pp 983The macro 984.Nm LIST_REMOVE 985removes the element 986.Fa elm 987from the list. 988.Pp 989The macro 990.Fn LIST_REPLACE 991replaces the element 992.Fa elm 993with 994.Fa new 995in the list. 996The element 997.Fa new 998must not already be on a list. 999.Pp 1000The macro 1001.Nm LIST_SWAP 1002swaps the contents of 1003.Fa head1 1004and 1005.Fa head2 . 1006.Sh LIST EXAMPLE 1007.Bd -literal 1008LIST_HEAD(listhead, entry) head = 1009 LIST_HEAD_INITIALIZER(head); 1010struct listhead *headp; /* List head. */ 1011struct entry { 1012 ... 1013 LIST_ENTRY(entry) entries; /* List. */ 1014 ... 1015} *n1, *n2, *n3, *np, *np_temp; 1016 1017LIST_INIT(&head); /* Initialize the list. */ 1018 1019n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1020LIST_INSERT_HEAD(&head, n1, entries); 1021 1022n2 = malloc(sizeof(struct entry)); /* Insert after. */ 1023LIST_INSERT_AFTER(n1, n2, entries); 1024 1025n3 = malloc(sizeof(struct entry)); /* Insert before. */ 1026LIST_INSERT_BEFORE(n2, n3, entries); 1027 1028LIST_REMOVE(n2, entries); /* Deletion. */ 1029free(n2); 1030 /* Forward traversal. */ 1031LIST_FOREACH(np, &head, entries) 1032 np-> ... 1033 1034 /* Safe forward traversal. */ 1035LIST_FOREACH_SAFE(np, &head, entries, np_temp) { 1036 np->do_stuff(); 1037 ... 1038 LIST_REMOVE(np, entries); 1039 free(np); 1040} 1041 1042while (!LIST_EMPTY(&head)) { /* List Deletion. */ 1043 n1 = LIST_FIRST(&head); 1044 LIST_REMOVE(n1, entries); 1045 free(n1); 1046} 1047 1048n1 = LIST_FIRST(&head); /* Faster List Deletion. */ 1049while (n1 != NULL) { 1050 n2 = LIST_NEXT(n1, entries); 1051 free(n1); 1052 n1 = n2; 1053} 1054LIST_INIT(&head); 1055.Ed 1056.Sh TAIL QUEUES 1057A tail queue is headed by a structure defined by the 1058.Nm TAILQ_HEAD 1059macro. 1060This structure contains a pair of pointers, 1061one to the first element in the tail queue and the other to 1062the last element in the tail queue. 1063The elements are doubly linked so that an arbitrary element can be 1064removed without traversing the tail queue. 1065New elements can be added to the tail queue after an existing element, 1066before an existing element, at the head of the tail queue, 1067or at the end of the tail queue. 1068A 1069.Fa TAILQ_HEAD 1070structure is declared as follows: 1071.Bd -literal -offset indent 1072TAILQ_HEAD(HEADNAME, TYPE) head; 1073.Ed 1074.Pp 1075where 1076.Li HEADNAME 1077is the name of the structure to be defined, and 1078.Li TYPE 1079is the type of the elements to be linked into the tail queue. 1080A pointer to the head of the tail queue can later be declared as: 1081.Bd -literal -offset indent 1082struct HEADNAME *headp; 1083.Ed 1084.Pp 1085(The names 1086.Li head 1087and 1088.Li headp 1089are user selectable.) 1090.Pp 1091The macro 1092.Nm TAILQ_HEAD_INITIALIZER 1093evaluates to an initializer for the tail queue 1094.Fa head . 1095.Pp 1096The macro 1097.Nm TAILQ_CONCAT 1098concatenates the tail queue headed by 1099.Fa head2 1100onto the end of the one headed by 1101.Fa head1 1102removing all entries from the former. 1103.Pp 1104The macro 1105.Nm TAILQ_EMPTY 1106evaluates to true if there are no items on the tail queue. 1107The 1108.Nm TAILQ_EMPTY_ATOMIC 1109variant has the same behavior, but can be safely used in contexts where it is 1110possible that a different thread is concurrently updating the queue. 1111.Pp 1112The macro 1113.Nm TAILQ_ENTRY 1114declares a structure that connects the elements in 1115the tail queue. 1116.Pp 1117The macro 1118.Nm TAILQ_FIRST 1119returns the first item on the tail queue or NULL if the tail queue 1120is empty. 1121.Pp 1122The macro 1123.Nm TAILQ_FOREACH 1124traverses the tail queue referenced by 1125.Fa head 1126in the forward direction, assigning each element in turn to 1127.Fa var . 1128.Fa var 1129is set to 1130.Dv NULL 1131if the loop completes normally, or if there were no elements. 1132.Pp 1133The macro 1134.Nm TAILQ_FOREACH_FROM 1135behaves identically to 1136.Nm TAILQ_FOREACH 1137when 1138.Fa var 1139is NULL, else it treats 1140.Fa var 1141as a previously found TAILQ element and begins the loop at 1142.Fa var 1143instead of the first element in the TAILQ referenced by 1144.Fa head . 1145.Pp 1146The macro 1147.Nm TAILQ_FOREACH_REVERSE 1148traverses the tail queue referenced by 1149.Fa head 1150in the reverse direction, assigning each element in turn to 1151.Fa var . 1152.Pp 1153The macro 1154.Nm TAILQ_FOREACH_REVERSE_FROM 1155behaves identically to 1156.Nm TAILQ_FOREACH_REVERSE 1157when 1158.Fa var 1159is NULL, else it treats 1160.Fa var 1161as a previously found TAILQ element and begins the reverse loop at 1162.Fa var 1163instead of the last element in the TAILQ referenced by 1164.Fa head . 1165.Pp 1166The macros 1167.Nm TAILQ_FOREACH_SAFE 1168and 1169.Nm TAILQ_FOREACH_REVERSE_SAFE 1170traverse the list referenced by 1171.Fa head 1172in the forward or reverse direction respectively, 1173assigning each element in turn to 1174.Fa var . 1175However, unlike their unsafe counterparts, 1176.Nm TAILQ_FOREACH 1177and 1178.Nm TAILQ_FOREACH_REVERSE 1179permit to both remove 1180.Fa var 1181as well as free it from within the loop safely without interfering with the 1182traversal. 1183.Pp 1184The macro 1185.Nm TAILQ_FOREACH_FROM_SAFE 1186behaves identically to 1187.Nm TAILQ_FOREACH_SAFE 1188when 1189.Fa var 1190is NULL, else it treats 1191.Fa var 1192as a previously found TAILQ element and begins the loop at 1193.Fa var 1194instead of the first element in the TAILQ referenced by 1195.Fa head . 1196.Pp 1197The macro 1198.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE 1199behaves identically to 1200.Nm TAILQ_FOREACH_REVERSE_SAFE 1201when 1202.Fa var 1203is NULL, else it treats 1204.Fa var 1205as a previously found TAILQ element and begins the reverse loop at 1206.Fa var 1207instead of the last element in the TAILQ referenced by 1208.Fa head . 1209.Pp 1210The macro 1211.Nm TAILQ_INIT 1212initializes the tail queue referenced by 1213.Fa head . 1214.Pp 1215The macro 1216.Nm TAILQ_INSERT_HEAD 1217inserts the new element 1218.Fa elm 1219at the head of the tail queue. 1220.Pp 1221The macro 1222.Nm TAILQ_INSERT_TAIL 1223inserts the new element 1224.Fa elm 1225at the end of the tail queue. 1226.Pp 1227The macro 1228.Nm TAILQ_INSERT_AFTER 1229inserts the new element 1230.Fa elm 1231after the element 1232.Fa listelm . 1233.Pp 1234The macro 1235.Nm TAILQ_INSERT_BEFORE 1236inserts the new element 1237.Fa elm 1238before the element 1239.Fa listelm . 1240.Pp 1241The macro 1242.Nm TAILQ_LAST 1243returns the last item on the tail queue. 1244If the tail queue is empty the return value is 1245.Dv NULL . 1246.Pp 1247The macro 1248.Nm TAILQ_NEXT 1249returns the next item on the tail queue, or NULL if this item is the last. 1250.Pp 1251The macro 1252.Nm TAILQ_PREV 1253returns the previous item on the tail queue, or NULL if this item 1254is the first. 1255.Pp 1256The macro 1257.Nm TAILQ_REMOVE 1258removes the element 1259.Fa elm 1260from the tail queue. 1261.Pp 1262The macro 1263.Fn TAILQ_REPLACE 1264replaces the element 1265.Fa elm 1266with 1267.Fa new 1268in the tail queue. 1269The element 1270.Fa new 1271must not already be on a list. 1272.Pp 1273The macro 1274.Nm TAILQ_SWAP 1275swaps the contents of 1276.Fa head1 1277and 1278.Fa head2 . 1279.Sh TAIL QUEUE EXAMPLE 1280.Bd -literal 1281TAILQ_HEAD(tailhead, entry) head = 1282 TAILQ_HEAD_INITIALIZER(head); 1283struct tailhead *headp; /* Tail queue head. */ 1284struct entry { 1285 ... 1286 TAILQ_ENTRY(entry) entries; /* Tail queue. */ 1287 ... 1288} *n1, *n2, *n3, *n4, *np; 1289 1290TAILQ_INIT(&head); /* Initialize the queue. */ 1291 1292n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1293TAILQ_INSERT_HEAD(&head, n1, entries); 1294 1295n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 1296TAILQ_INSERT_TAIL(&head, n1, entries); 1297 1298n2 = malloc(sizeof(struct entry)); /* Insert after. */ 1299TAILQ_INSERT_AFTER(&head, n1, n2, entries); 1300 1301n3 = malloc(sizeof(struct entry)); /* Insert before. */ 1302TAILQ_INSERT_BEFORE(n2, n3, entries); 1303 1304TAILQ_REMOVE(&head, n2, entries); /* Deletion. */ 1305free(n2); 1306 1307n4 = malloc(sizeof(struct entry)); /* Replacement. */ 1308TAILQ_REPLACE(&head, n3, n4, entries); 1309free(n3); 1310 /* Forward traversal. */ 1311TAILQ_FOREACH(np, &head, entries) 1312 np-> ... 1313 /* Safe forward traversal. */ 1314TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) { 1315 np->do_stuff(); 1316 ... 1317 TAILQ_REMOVE(&head, np, entries); 1318 free(np); 1319} 1320 /* Reverse traversal. */ 1321TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 1322 np-> ... 1323 /* TailQ Deletion. */ 1324while (!TAILQ_EMPTY(&head)) { 1325 n1 = TAILQ_FIRST(&head); 1326 TAILQ_REMOVE(&head, n1, entries); 1327 free(n1); 1328} 1329 /* Faster TailQ Deletion. */ 1330n1 = TAILQ_FIRST(&head); 1331while (n1 != NULL) { 1332 n2 = TAILQ_NEXT(n1, entries); 1333 free(n1); 1334 n1 = n2; 1335} 1336TAILQ_INIT(&head); 1337.Ed 1338.Sh DIAGNOSTICS 1339When debugging 1340.Nm queue(3) , 1341it can be useful to trace queue changes. 1342To enable tracing, define the macro 1343.Va QUEUE_MACRO_DEBUG_TRACE 1344at compile time. 1345.Pp 1346It can also be useful to trash pointers that have been unlinked from a queue, 1347to detect use after removal. 1348To enable pointer trashing, define the macro 1349.Va QUEUE_MACRO_DEBUG_TRASH 1350at compile time. 1351The macro 1352.Fn QMD_IS_TRASHED "void *ptr" 1353returns true if 1354.Fa ptr 1355has been trashed by the 1356.Va QUEUE_MACRO_DEBUG_TRASH 1357option. 1358.Pp 1359In the kernel (with 1360.Va INVARIANTS 1361enabled), the 1362.Fn SLIST_REMOVE_PREVPTR 1363macro is available to aid debugging: 1364.Bl -hang -offset indent 1365.It Fn SLIST_REMOVE_PREVPTR "TYPE **prev" "TYPE *elm" "SLIST_ENTRY NAME" 1366.Pp 1367Removes 1368.Fa elm , 1369which must directly follow the element whose 1370.Va &SLIST_NEXT() 1371is 1372.Fa prev , 1373from the SLIST. 1374This macro validates that 1375.Fa elm 1376follows 1377.Fa prev 1378in 1379.Va INVARIANTS 1380mode. 1381.El 1382.Sh SEE ALSO 1383.Xr arb 3 , 1384.Xr tree 3 1385.Sh HISTORY 1386The 1387.Nm queue 1388functions first appeared in 1389.Bx 4.4 . 1390