1.\" $NetBSD: queue.3,v 1.61 2020/10/20 23:27:57 kamil Exp $ 2.\" 3.\" Copyright (c) 2000, 2002 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25.\" POSSIBILITY OF SUCH DAMAGE. 26.\" 27.\" Copyright (c) 1993 The Regents of the University of California. 28.\" All rights reserved. 29.\" 30.\" Redistribution and use in source and binary forms, with or without 31.\" modification, are permitted provided that the following conditions 32.\" are met: 33.\" 1. Redistributions of source code must retain the above copyright 34.\" notice, this list of conditions and the following disclaimer. 35.\" 2. Redistributions in binary form must reproduce the above copyright 36.\" notice, this list of conditions and the following disclaimer in the 37.\" documentation and/or other materials provided with the distribution. 38.\" 3. Neither the name of the University nor the names of its contributors 39.\" may be used to endorse or promote products derived from this software 40.\" without specific prior written permission. 41.\" 42.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 43.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 46.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52.\" SUCH DAMAGE. 53.\" 54.\" @(#)queue.3 8.1 (Berkeley) 12/13/93 55.\" 56.Dd October 1, 2017 57.Dt QUEUE 3 58.Os 59.Sh NAME 60.Nm SLIST_HEAD , 61.Nm SLIST_HEAD_INITIALIZER , 62.Nm SLIST_ENTRY , 63.Nm SLIST_FIRST , 64.Nm SLIST_EMPTY , 65.Nm SLIST_NEXT , 66.Nm SLIST_FOREACH , 67.Nm SLIST_FOREACH_SAFE , 68.Nm SLIST_INIT , 69.Nm SLIST_INSERT_AFTER , 70.Nm SLIST_INSERT_HEAD , 71.Nm SLIST_REMOVE_AFTER , 72.Nm SLIST_REMOVE_HEAD , 73.Nm SLIST_REMOVE , 74.Nm LIST_HEAD , 75.Nm LIST_HEAD_INITIALIZER , 76.Nm LIST_ENTRY , 77.Nm LIST_FIRST , 78.Nm LIST_EMPTY , 79.Nm LIST_NEXT , 80.Nm LIST_FOREACH , 81.Nm LIST_FOREACH_SAFE , 82.Nm LIST_INIT , 83.Nm LIST_INSERT_AFTER , 84.Nm LIST_INSERT_BEFORE , 85.Nm LIST_INSERT_HEAD , 86.Nm LIST_REMOVE , 87.Nm LIST_REPLACE , 88.Nm LIST_MOVE , 89.Nm SIMPLEQ_HEAD , 90.Nm SIMPLEQ_HEAD_INITIALIZER , 91.Nm SIMPLEQ_ENTRY , 92.Nm SIMPLEQ_FIRST , 93.Nm SIMPLEQ_EMPTY , 94.Nm SIMPLEQ_NEXT , 95.Nm SIMPLEQ_LAST , 96.Nm SIMPLEQ_FOREACH , 97.Nm SIMPLEQ_FOREACH_SAFE , 98.Nm SIMPLEQ_INIT , 99.Nm SIMPLEQ_INSERT_AFTER , 100.Nm SIMPLEQ_INSERT_HEAD , 101.Nm SIMPLEQ_INSERT_TAIL , 102.Nm SIMPLEQ_REMOVE_AFTER , 103.Nm SIMPLEQ_REMOVE_HEAD , 104.Nm SIMPLEQ_REMOVE , 105.Nm SIMPLEQ_CONCAT , 106.Nm TAILQ_HEAD , 107.Nm TAILQ_HEAD_INITIALIZER , 108.Nm TAILQ_ENTRY , 109.Nm TAILQ_FIRST , 110.Nm TAILQ_NEXT , 111.Nm TAILQ_LAST , 112.Nm TAILQ_PREV , 113.Nm TAILQ_EMPTY , 114.Nm TAILQ_FOREACH , 115.Nm TAILQ_FOREACH_SAFE , 116.Nm TAILQ_FOREACH_REVERSE , 117.Nm TAILQ_FOREACH_REVERSE_SAFE , 118.Nm TAILQ_INIT , 119.Nm TAILQ_INSERT_AFTER , 120.Nm TAILQ_INSERT_BEFORE , 121.Nm TAILQ_INSERT_HEAD , 122.Nm TAILQ_INSERT_TAIL , 123.Nm TAILQ_REMOVE , 124.Nm TAILQ_REPLACE , 125.Nm TAILQ_CONCAT , 126.Nm STAILQ_HEAD , 127.Nm STAILQ_HEAD_INITIALIZER , 128.Nm STAILQ_ENTRY , 129.Nm STAILQ_FIRST , 130.Nm STAILQ_EMPTY , 131.Nm STAILQ_NEXT , 132.Nm STAILQ_LAST , 133.Nm STAILQ_FOREACH , 134.Nm STAILQ_FOREACH_SAFE , 135.Nm STAILQ_INIT , 136.Nm STAILQ_INSERT_AFTER , 137.Nm STAILQ_INSERT_HEAD , 138.Nm STAILQ_INSERT_TAIL , 139.Nm STAILQ_REMOVE_HEAD , 140.Nm STAILQ_REMOVE , 141.Nm STAILQ_CONCAT 142.Nd implementations of singly-linked lists, lists, simple queues, tail queues, and singly-linked tail queues 143.Sh SYNOPSIS 144.In sys/queue.h 145.Pp 146.Fn SLIST_HEAD "HEADNAME" "TYPE" 147.Fn SLIST_HEAD_INITIALIZER "head" 148.Fn SLIST_ENTRY "TYPE" 149.Ft TYPE * 150.Fn SLIST_FIRST "SLIST_HEAD *head" 151.Ft int 152.Fn SLIST_EMPTY "SLIST_HEAD *head" 153.Ft TYPE * 154.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME" 155.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" 156.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *tmp" 157.Fn SLIST_INIT "SLIST_HEAD *head" 158.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME" 159.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME" 160.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME" 161.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME" 162.Pp 163.Fn LIST_HEAD "HEADNAME" "TYPE" 164.Fn LIST_HEAD_INITIALIZER "head" 165.Fn LIST_ENTRY "TYPE" 166.Ft TYPE * 167.Fn LIST_FIRST "LIST_HEAD *head" 168.Ft TYPE * 169.Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME" 170.Ft int 171.Fn LIST_EMPTY "LIST_HEAD *head" 172.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" 173.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *tmp" 174.Fn LIST_INIT "LIST_HEAD *head" 175.Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 176.Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME" 177.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME" 178.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME" 179.Fn LIST_REPLACE "TYPE *elm" "TYPE *new" "LIST_ENTRY NAME" 180.Fn LIST_MOVE "LIST_HEAD *head1" "LIST_HEAD *head2" "LIST_ENTRY NAME" 181.Pp 182.Fn SIMPLEQ_HEAD "HEADNAME" "TYPE" 183.Fn SIMPLEQ_HEAD_INITIALIZER "head" 184.Fn SIMPLEQ_ENTRY "TYPE" 185.Ft TYPE * 186.Fn SIMPLEQ_FIRST "SIMPLEQ_HEAD *head" 187.Ft int 188.Fn SIMPLEQ_EMPTY "SIMPLEQ_HEAD *head" 189.Ft TYPE * 190.Fn SIMPLEQ_NEXT "TYPE *elm" "SIMPLEQ_ENTRY NAME" 191.Ft TYPE * 192.Fn SIMPLEQ_LAST "SIMPLEQ_HEAD *head" "TYPE *elm" "SIMPLEQ_ENTRY NAME" 193.Fn SIMPLEQ_FOREACH "TYPE *var" "SIMPLEQ_HEAD *head" "SIMPLEQ_ENTRY NAME" 194.Fn SIMPLEQ_FOREACH_SAFE "TYPE *var" "SIMPLEQ_HEAD *head" "SIMPLEQ_ENTRY NAME" "TYPE *tmp" 195.Fn SIMPLEQ_INIT "SIMPLEQ_HEAD *head" 196.Fn SIMPLEQ_INSERT_HEAD "SIMPLEQ_HEAD *head" "TYPE *elm" "SIMPLEQ_ENTRY NAME" 197.Fn SIMPLEQ_INSERT_TAIL "SIMPLEQ_HEAD *head" "TYPE *elm" "SIMPLEQ_ENTRY NAME" 198.Fn SIMPLEQ_INSERT_AFTER "SIMPLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "SIMPLEQ_ENTRY NAME" 199.Fn SIMPLEQ_REMOVE_HEAD "SIMPLEQ_HEAD *head" "SIMPLEQ_ENTRY NAME" 200.Fn SIMPLEQ_REMOVE_AFTER "SIMPLEQ_HEAD *head" "TYPE *elm" "SIMPLEQ_ENTRY NAME" 201.Fn SIMPLEQ_REMOVE "SIMPLEQ_HEAD *head" "TYPE *elm" "TYPE" "SIMPLEQ_ENTRY NAME" 202.Fn SIMPLEQ_CONCAT "SIMPLEQ_HEAD *head1" "SIMPLEQ_HEAD *head2" 203.Pp 204.Fn TAILQ_HEAD "HEADNAME" "TYPE" 205.Fn TAILQ_HEAD_INITIALIZER "head" 206.Fn TAILQ_ENTRY "TYPE" 207.Ft TYPE * 208.Fn TAILQ_FIRST "TAILQ_HEAD *head" 209.Ft TYPE * 210.Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME" 211.Ft TYPE * 212.Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME" 213.Ft TYPE * 214.Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME" 215.Ft int 216.Fn TAILQ_EMPTY "TAILQ_HEAD *head" 217.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" 218.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *tmp" 219.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" 220.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *tmp" 221.Fn TAILQ_INIT "TAILQ_HEAD *head" 222.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 223.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 224.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 225.Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME" 226.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME" 227.Fn TAILQ_REPLACE "TAILQ_HEAD *head" "TYPE *elm" "TYPE *new" "TAILQ_ENTRY NAME" 228.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME" 229.Pp 230.Fn STAILQ_HEAD "HEADNAME" "TYPE" 231.Fn STAILQ_HEAD_INITIALIZER "head" 232.Fn STAILQ_ENTRY "TYPE" 233.Ft TYPE * 234.Fn STAILQ_FIRST "STAILQ_HEAD *head" 235.Ft int 236.Fn STAILQ_EMPTY "STAILQ_HEAD *head" 237.Ft TYPE * 238.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" 239.Ft TYPE * 240.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 241.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 242.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *tmp" 243.Fn STAILQ_INIT "STAILQ_HEAD *head" 244.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 245.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" 246.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" 247.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" 248.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" 249.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" 250.Sh DESCRIPTION 251These macros define and operate on five types of data structures: 252singly-linked lists, simple queues, lists, tail queues, and singly-linked 253tail queues. 254All five structures support the following functionality: 255.Bl -enum -compact -offset indent 256.It 257Insertion of a new entry at the head of the list. 258.It 259Insertion of a new entry after any element in the list. 260.It 261Removal of any entry in the list. 262.It 263Forward traversal through the list. 264.El 265.Pp 266Singly-linked lists are the simplest of the four data structures and 267support only the above functionality. 268Singly-linked lists are ideal for applications with large datasets and 269few or no removals, 270or for implementing a LIFO queue. 271.Pp 272Simple queues add the following functionality: 273.Bl -enum -compact -offset indent 274.It 275Entries can be added at the end of a list. 276.It 277They may be concatenated. 278.El 279However: 280.Bl -enum -compact -offset indent 281.It 282Entries may not be added before any element in the list. 283.It 284All list insertions and removals must specify the head of the list. 285.It 286Each head entry requires two pointers rather than one. 287.El 288.Pp 289Simple queues are ideal for applications with large datasets and few or 290no removals, or for implementing a FIFO queue. 291.Pp 292All doubly linked types of data structures (lists and tail queues) 293additionally allow: 294.Bl -enum -compact -offset indent 295.It 296Insertion of a new entry before any element in the list. 297.It 298O(1) removal of any entry in the list. 299.El 300However: 301.Bl -enum -compact -offset indent 302.It 303Each element requires two pointers rather than one. 304.It 305Code size and execution time of operations (except for removal) is about 306twice that of the singly-linked data-structures. 307.El 308.Pp 309Linked lists are the simplest of the doubly linked data structures and 310support only the above functionality over singly-linked lists. 311.Pp 312Tail queues add the following functionality: 313.Bl -enum -compact -offset indent 314.It 315Entries can be added at the end of a list. 316.It 317They may be concatenated. 318.El 319However: 320.Bl -enum -compact -offset indent 321.It 322All list insertions and removals, except insertion before another element, must 323specify the head of the list. 324.It 325Each head entry requires two pointers rather than one. 326.It 327Code size is about 15% greater and operations run about 20% slower 328than lists. 329.El 330.Pp 331Circular queues add the following functionality: 332.Bl -enum -compact -offset indent 333.It 334Entries can be added at the end of a list. 335.It 336They may be traversed backwards, from tail to head. 337.El 338However: 339.Bl -enum -compact -offset indent 340.It 341All list insertions and removals must specify the head of the list. 342.It 343Each head entry requires two pointers rather than one. 344.It 345The termination condition for traversal is more complex. 346.It 347Code size is about 40% greater and operations run about 45% slower 348than lists. 349.El 350.Pp 351In the macro definitions, 352.Fa TYPE 353is the name of a user defined structure, 354that must contain a field of type 355.Li SLIST_ENTRY , 356.Li LIST_ENTRY , 357.Li SIMPLEQ_ENTRY , 358.Li TAILQ_ENTRY , 359or 360.Li STAILQ_ENTRY , 361named 362.Fa NAME . 363The argument 364.Fa HEADNAME 365is the name of a user defined structure that must be declared 366using the macros 367.Li LIST_HEAD , 368.Li SIMPLEQ_HEAD , 369.Li SLIST_HEAD , 370or 371.Li TAILQ_HEAD . 372See the examples below for further explanation of how these 373macros are used. 374.Ss Summary of Operations 375The following table summarizes the supported macros for each type 376of data structure. 377.Pp 378.TS 379box tab(:); 380l | c | c | c | c | c 381l | c | c | c | c | c 382l | c | c | c | c | c 383l | c | c | c | c | c 384l | c | c | c | c | c 385l | c | c | c | c | c. 386:SLIST:LIST:SIMPLEQ:TAILQ:STAILQ 387_ 388_FIRST:+:+:+:+:+ 389_EMPTY:+:+:+:+:+ 390_NEXT:+:+:+:+:+ 391_PREV:-:-:-:+:- 392_LAST:-:-:+:+:+ 393_FOREACH:+:+:+:+:+ 394_FOREACH_SAFE:+:+:+:+:+ 395_FOREACH_REVERSE:-:-:-:+:- 396_FOREACH_REVERSE_SAFE:-:-:-:+:- 397_INSERT_HEAD:+:+:+:+:+ 398_INSERT_AFTER:+:+:+:+:+ 399_INSERT_BEFORE:-:+:-:+:- 400_INSERT_TAIL:-:-:+:+:+ 401_REMOVE:+:+:+:+:+ 402_REMOVE_HEAD:+:-:+:-:+ 403_REMOVE_AFTER:-:-:+:-:+ 404_REPLACE:-:+:-:+:- 405_CONCAT:-:-:+:+:+ 406.TE 407.Sh SINGLY-LINKED LISTS 408A singly-linked list is headed by a structure defined by the 409.Fn SLIST_HEAD 410macro. 411This structure contains a single pointer to the first element 412on the list. 413The elements are singly linked for minimum space and pointer manipulation 414overhead at the expense of O(n) removal for arbitrary elements. 415New elements can be added to the list after an existing element or 416at the head of the list. 417An 418.Fa SLIST_HEAD 419structure is declared as follows: 420.Bd -literal -offset indent 421SLIST_HEAD(HEADNAME, TYPE) head; 422.Ed 423.Pp 424where 425.Fa HEADNAME 426is the name of the structure to be defined, and 427.Fa TYPE 428is the type of the elements to be linked into the list. 429A pointer to the head of the list can later be declared as: 430.Bd -literal -offset indent 431struct HEADNAME *headp; 432.Ed 433.Pp 434(The names 435.Li head 436and 437.Li headp 438are user selectable.) 439.Pp 440The macro 441.Fn SLIST_HEAD_INITIALIZER 442evaluates to an initializer for the list 443.Fa head . 444.Pp 445The macro 446.Fn SLIST_ENTRY 447declares a structure that connects the elements in 448the list. 449.Pp 450The macro 451.Fn SLIST_FIRST 452returns the first element in the list or NULL if the list is empty. 453.Pp 454The macro 455.Fn SLIST_EMPTY 456evaluates to true if there are no elements in the list. 457.Pp 458The macro 459.Fn SLIST_NEXT 460returns the next element in the list. 461.Pp 462.Fn SLIST_FOREACH 463traverses the list referenced by 464.Fa head 465in the forward direction, assigning each element in 466turn to 467.Fa var . 468.Pp 469The SAFE version uses 470.Fa tmp 471to hold the next element, so 472.Fa var 473may be freed or removed from the list. 474.Pp 475The macro 476.Fn SLIST_INIT 477initializes the list referenced by 478.Fa head . 479.Pp 480The macro 481.Fn SLIST_INSERT_HEAD 482inserts the new element 483.Fa elm 484at the head of the list. 485.Pp 486The macro 487.Fn SLIST_INSERT_AFTER 488inserts the new element 489.Fa elm 490after the element 491.Fa listelm . 492.Pp 493The macro 494.Fn SLIST_REMOVE 495removes the element 496.Fa elm 497from the list. 498.Pp 499The macro 500.Fn SLIST_REMOVE_HEAD 501removes the first element from the head of the list. 502For optimum efficiency, 503elements being removed from the head of the list should explicitly use 504this macro instead of the generic 505.Fn SLIST_REMOVE 506macro. 507.Pp 508The macro 509.Fn SLIST_REMOVE_AFTER 510removes the element after the one specified. 511For optimum efficiency, 512elements being removed after a specified one should explicitly use 513this macro instead of the generic 514.Fn SLIST_REMOVE 515.Sh SINGLY-LINKED LIST EXAMPLE 516.Bd -literal 517SLIST_HEAD(slisthead, entry) head = 518 SLIST_HEAD_INITIALIZER(head); 519struct slisthead *headp; /* Singly-linked List head. */ 520struct entry { 521 ... 522 SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 523 ... 524} *n1, *n2, *n3, *np; 525 526SLIST_INIT(&head); /* Initialize the list. */ 527 528n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 529SLIST_INSERT_HEAD(&head, n1, entries); 530 531n2 = malloc(sizeof(struct entry)); /* Insert after. */ 532SLIST_INSERT_AFTER(n1, n2, entries); 533 534SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */ 535free(n2); 536 537n3 = SLIST_FIRST(&head); 538SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ 539free(n3); 540 541SLIST_FOREACH(np, &head, entries) /* Forward traversal. */ 542 np-> ... 543 544while (!SLIST_EMPTY(&head)) { /* List Deletion. */ 545 n1 = SLIST_FIRST(&head); 546 SLIST_REMOVE_HEAD(&head, entries); 547 free(n1); 548} 549.Ed 550.Sh LISTS 551A list is headed by a structure defined by the 552.Fn LIST_HEAD 553macro. 554This structure contains a single pointer to the first element 555on the list. 556The elements are doubly linked so that an arbitrary element can be 557removed without traversing the list. 558New elements can be added to the list after an existing element, 559before an existing element, or at the head of the list. 560A 561.Fa LIST_HEAD 562structure is declared as follows: 563.Bd -literal -offset indent 564LIST_HEAD(HEADNAME, TYPE) head; 565.Ed 566.Pp 567where 568.Fa HEADNAME 569is the name of the structure to be defined, and 570.Fa TYPE 571is the type of the elements to be linked into the list. 572A pointer to the head of the list can later be declared as: 573.Bd -literal -offset indent 574struct HEADNAME *headp; 575.Ed 576.Pp 577(The names 578.Li head 579and 580.Li headp 581are user selectable.) 582.Pp 583The macro 584.Fn LIST_ENTRY 585declares a structure that connects the elements in 586the list. 587.Pp 588The macro 589.Fn LIST_HEAD_INITIALIZER 590provides a value which can be used to initialize a list head at 591compile time, and is used at the point that the list head 592variable is declared, like: 593.Bd -literal -offset indent 594struct HEADNAME head = LIST_HEAD_INITIALIZER(head); 595.Ed 596.Pp 597The macro 598.Fn LIST_FIRST 599returns the first element of the list 600.Fa head . 601.Pp 602The macro 603.Fn LIST_EMPTY 604returns true if the list 605.Fa head 606has no elements. 607.Pp 608The macro 609.Fn LIST_NEXT 610returns the element after the element 611.Fa elm . 612.Pp 613The macro 614.Fn LIST_FOREACH 615traverses the list referenced by 616.Fa head 617in the forward direction, assigning each element in turn to 618.Fa var . 619.Pp 620The SAFE version uses 621.Fa tmp 622to hold the next element, so 623.Fa var 624may be freed or removed from the list. 625.Pp 626The macro 627.Fn LIST_INIT 628initializes the list referenced by 629.Fa head . 630.Pp 631The macro 632.Fn LIST_INSERT_AFTER 633inserts the new element 634.Fa elm 635after the element 636.Fa listelm . 637.Pp 638The macro 639.Fn LIST_INSERT_BEFORE 640inserts the new element 641.Fa elm 642before the element 643.Fa listelm . 644.Pp 645The macro 646.Fn LIST_INSERT_HEAD 647inserts the new element 648.Fa elm 649at the head of the list. 650.Pp 651The macro 652.Fn LIST_REMOVE 653removes the element 654.Fa elm 655from the list. 656.Pp 657The macro 658.Fn LIST_REPLACE 659replaces the element 660.Fa elm 661with 662.Fa new 663in the list. 664.Pp 665The macro 666.Fn LIST_MOVE 667moves the list headed by 668.Fa head1 669onto the list headed by 670.Fa head2 , 671always making the former empty. 672.Sh LIST EXAMPLE 673.Bd -literal 674LIST_HEAD(listhead, entry) head; 675struct listhead *headp; /* List head. */ 676struct entry { 677 ... 678 LIST_ENTRY(entry) entries; /* List. */ 679 ... 680} *n1, *n2, *np; 681 682LIST_INIT(&head); /* Initialize the list. */ 683 684n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 685LIST_INSERT_HEAD(&head, n1, entries); 686 687n2 = malloc(sizeof(struct entry)); /* Insert after. */ 688LIST_INSERT_AFTER(n1, n2, entries); 689 690n2 = malloc(sizeof(struct entry)); /* Insert before. */ 691LIST_INSERT_BEFORE(n1, n2, entries); 692 693LIST_FOREACH(np, &head, entries) /* Forward traversal. */ 694 np-> ... 695 696while (LIST_FIRST(&head) != NULL) /* Delete. */ 697 LIST_REMOVE(LIST_FIRST(&head), entries); 698if (LIST_EMPTY(&head)) /* Test for emptiness. */ 699 printf("nothing to do\\n"); 700.Ed 701.Sh SIMPLE QUEUES 702A simple queue is headed by a structure defined by the 703.Fn SIMPLEQ_HEAD 704macro. 705This structure contains a pair of pointers, 706one to the first element in the simple queue and the other to 707the last element in the simple queue. 708The elements are singly linked for minimum space and pointer manipulation 709overhead at the expense of O(n) removal for arbitrary elements. 710New elements can be added to the queue after an existing element, 711at the head of the queue, or at the end of the queue. 712A 713.Fa SIMPLEQ_HEAD 714structure is declared as follows: 715.Bd -literal -offset indent 716SIMPLEQ_HEAD(HEADNAME, TYPE) head; 717.Ed 718.Pp 719where 720.Li HEADNAME 721is the name of the structure to be defined, and 722.Li TYPE 723is the type of the elements to be linked into the simple queue. 724A pointer to the head of the simple queue can later be declared as: 725.Bd -literal -offset indent 726struct HEADNAME *headp; 727.Ed 728.Pp 729(The names 730.Li head 731and 732.Li headp 733are user selectable.) 734.Pp 735The macro 736.Fn SIMPLEQ_ENTRY 737declares a structure that connects the elements in 738the simple queue. 739.Pp 740The macro 741.Fn SIMPLEQ_HEAD_INITIALIZER 742provides a value which can be used to initialize a simple queue head at 743compile time, and is used at the point that the simple queue head 744variable is declared, like: 745.Bd -literal -offset indent 746struct HEADNAME head = SIMPLEQ_HEAD_INITIALIZER(head); 747.Ed 748.Pp 749The macro 750.Fn SIMPLEQ_FIRST 751returns the first element of the simple queue 752.Fa head . 753.Pp 754The macro 755.Fn SIMPLEQ_EMPTY 756returns true if the simple queue 757.Fa head 758has no elements. 759.Pp 760The macro 761.Fn SIMPLEQ_NEXT 762returns the element after the element 763.Fa elm . 764.Pp 765The macro 766.Fn SIMPLEQ_LAST 767returns the last item on the simple queue. 768If the simple queue is empty the return value is 769.Dv NULL . 770.Pp 771The macro 772.Fn SIMPLEQ_FOREACH 773traverses the simple queue referenced by 774.Fa head 775in the forward direction, assigning each element 776in turn to 777.Fa var . 778.Pp 779The SAFE version uses 780.Fa tmp 781to hold the next element, so 782.Fa var 783may be freed or removed from the list. 784.Pp 785The macro 786.Fn SIMPLEQ_INIT 787initializes the simple queue referenced by 788.Fa head . 789.Pp 790The macro 791.Fn SIMPLEQ_INSERT_HEAD 792inserts the new element 793.Fa elm 794at the head of the simple queue. 795.Pp 796The macro 797.Fn SIMPLEQ_INSERT_TAIL 798inserts the new element 799.Fa elm 800at the end of the simple queue. 801.Pp 802The macro 803.Fn SIMPLEQ_INSERT_AFTER 804inserts the new element 805.Fa elm 806after the element 807.Fa listelm . 808.Pp 809The macro 810.Fn SIMPLEQ_REMOVE_HEAD 811removes the first element from the head of the simple queue. 812For optimum efficiency, 813elements being removed from the head of the queue should explicitly use 814this macro instead of the generic 815.Fn SIMPLEQ_REMOVE 816macro. 817.Pp 818The macro 819.Fn SIMPLEQ_REMOVE_AFTER 820removes the element after the one specified from the simple queue. 821For optimum efficiency, 822elements being removed after specified elements should explicitly use 823this macro instead of the generic 824.Fn SIMPLEQ_REMOVE 825macro. 826.Pp 827The macro 828.Fn SIMPLEQ_REMOVE 829removes 830.Fa elm 831from the simple queue. 832.Pp 833The macro 834.Fn SIMPLEQ_CONCAT 835concatenates the simple queue headed by 836.Fa head2 837onto the end of the one headed by 838.Fa head1 , 839removing all entries from the former. 840.Sh SIMPLE QUEUE EXAMPLE 841.Bd -literal 842SIMPLEQ_HEAD(simplehead, entry) head; 843struct simplehead *headp; /* Simple queue head. */ 844struct entry { 845 ... 846 SIMPLEQ_ENTRY(entry) entries; /* Simple queue. */ 847 ... 848} *n1, *n2, *np; 849 850SIMPLEQ_INIT(&head); /* Initialize the queue. */ 851 852n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 853SIMPLEQ_INSERT_HEAD(&head, n1, entries); 854 855n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 856SIMPLEQ_INSERT_TAIL(&head, n1, entries); 857 858n2 = malloc(sizeof(struct entry)); /* Insert after. */ 859SIMPLEQ_INSERT_AFTER(&head, n1, n2, entries); 860 861SIMPLEQ_FOREACH(np, &head, entries) /* Forward traversal. */ 862 np-> ... 863 864while (SIMPLEQ_FIRST(&head) != NULL) /* Delete. */ 865 SIMPLEQ_REMOVE_HEAD(&head, entries); 866if (SIMPLEQ_EMPTY(&head)) /* Test for emptiness. */ 867 printf("nothing to do\\n"); 868.Ed 869.Sh TAIL QUEUES 870A tail queue is headed by a structure defined by the 871.Fn TAILQ_HEAD 872macro. 873This structure contains a pair of pointers, 874one to the first element in the tail queue and the other to 875the last element in the tail queue. 876The elements are doubly linked so that an arbitrary element can be 877removed without traversing the tail queue. 878New elements can be added to the queue after an existing element, 879before an existing element, at the head of the queue, or at the end 880the queue. 881A 882.Fa TAILQ_HEAD 883structure is declared as follows: 884.Bd -literal -offset indent 885TAILQ_HEAD(HEADNAME, TYPE) head; 886.Ed 887.Pp 888where 889.Li HEADNAME 890is the name of the structure to be defined, and 891.Li TYPE 892is the type of the elements to be linked into the tail queue. 893A pointer to the head of the tail queue can later be declared as: 894.Bd -literal -offset indent 895struct HEADNAME *headp; 896.Ed 897.Pp 898(The names 899.Li head 900and 901.Li headp 902are user selectable.) 903.Pp 904The macro 905.Fn TAILQ_ENTRY 906declares a structure that connects the elements in 907the tail queue. 908.Pp 909The macro 910.Fn TAILQ_HEAD_INITIALIZER 911provides a value which can be used to initialize a tail queue head at 912compile time, and is used at the point that the tail queue head 913variable is declared, like: 914.Bd -literal -offset indent 915struct HEADNAME head = TAILQ_HEAD_INITIALIZER(head); 916.Ed 917.Pp 918The macro 919.Fn TAILQ_FIRST 920returns the first element of the tail queue 921.Fa head . 922.Pp 923The macro 924.Fn TAILQ_NEXT 925returns the element after the element 926.Fa elm . 927.Pp 928The macro 929.Fn TAILQ_LAST 930returns the last item on the tail queue. 931If the tail queue is empty the return value is 932.Dv NULL . 933.Pp 934The macro 935.Fn TAILQ_PREV 936returns the previous item on the tail queue, from the one specified. 937If the tail queue is empty the return value is 938.Dv NULL . 939.Pp 940The macro 941.Fn TAILQ_EMPTY 942returns true if the tail queue 943.Fa head 944has no elements. 945.Pp 946The macros 947.Fn TAILQ_FOREACH , 948.Fn TAILQ_FOREACH_REVERSE , 949.Fn TAILQ_FOREACH_SAFE , 950and 951.Fn TAILQ_FOREACH_REVERSE_SAFE 952traverse the tail queue referenced by 953.Fa head 954in the forward or reverse direction, assigning each element in turn to 955.Fa var . 956.Pp 957The SAFE versions use 958.Fa tmp 959to hold the next element, so 960.Fa var 961may be freed or removed from the list. 962.Pp 963The macro 964.Fn TAILQ_INIT 965initializes the tail queue referenced by 966.Fa head . 967.Pp 968The macro 969.Fn TAILQ_INSERT_HEAD 970inserts the new element 971.Fa elm 972at the head of the tail queue. 973.Pp 974The macro 975.Fn TAILQ_INSERT_TAIL 976inserts the new element 977.Fa elm 978at the end of the tail queue. 979.Pp 980The macro 981.Fn TAILQ_INSERT_AFTER 982inserts the new element 983.Fa elm 984after the element 985.Fa listelm . 986.Pp 987The macro 988.Fn TAILQ_INSERT_BEFORE 989inserts the new element 990.Fa elm 991before the element 992.Fa listelm . 993.Pp 994The macro 995.Fn TAILQ_REMOVE 996removes the element 997.Fa elm 998from the tail queue. 999.Pp 1000The macro 1001.Fn TAILQ_REPLACE 1002replaces the element 1003.Fa elm 1004with the 1005.Fa new 1006one specified in the tail queue. 1007.Pp 1008The macro 1009.Fn TAILQ_CONCAT 1010concatenates the tail queue headed by 1011.Fa head2 1012onto the end of the one headed by 1013.Fa head1 , 1014removing all entries from the former. 1015.Sh TAIL QUEUE EXAMPLE 1016.Bd -literal 1017TAILQ_HEAD(tailhead, entry) head; 1018struct tailhead *headp; /* Tail queue head. */ 1019struct entry { 1020 ... 1021 TAILQ_ENTRY(entry) entries; /* Tail queue. */ 1022 ... 1023} *n1, *n2, *np; 1024 1025TAILQ_INIT(&head); /* Initialize the queue. */ 1026 1027n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1028TAILQ_INSERT_HEAD(&head, n1, entries); 1029 1030n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 1031TAILQ_INSERT_TAIL(&head, n1, entries); 1032 1033n2 = malloc(sizeof(struct entry)); /* Insert after. */ 1034TAILQ_INSERT_AFTER(&head, n1, n2, entries); 1035 1036n2 = malloc(sizeof(struct entry)); /* Insert before. */ 1037TAILQ_INSERT_BEFORE(n1, n2, entries); 1038 1039TAILQ_FOREACH(np, &head, entries) /* Forward traversal. */ 1040 np-> ... 1041 /* Reverse traversal. */ 1042TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) 1043 np-> ... 1044 1045while (TAILQ_FIRST(&head) != NULL) /* Delete. */ 1046 TAILQ_REMOVE(&head, TAILQ_FIRST(&head), entries); 1047if (TAILQ_EMPTY(&head)) /* Test for emptiness. */ 1048 printf("nothing to do\\n"); 1049.Ed 1050.Sh SINGLY LINKED TAIL QUEUES 1051The macros prefixed with 1052.Do Nm STAILQ_ Dc ( 1053.Fn STAILQ_HEAD , 1054.Fn STAILQ_HEAD_INITIALIZER , 1055.Fn STAILQ_ENTRY , 1056.Fn STAILQ_FOREACH , 1057.Fn STAILQ_FOREACH_SAFE , 1058.Fn STAILQ_FIRST , 1059.Fn STAILQ_EMPTY , 1060.Fn STAILQ_NEXT , 1061.Fn STAILQ_LAST , 1062.Fn STAILQ_INIT , 1063.Fn STAILQ_INSERT_HEAD , 1064.Fn STAILQ_INSERT_TAIL , 1065.Fn STAILQ_INSERT_AFTER , 1066.Fn STAILQ_REMOVE_HEAD , 1067.Fn STAILQ_REMOVE , 1068and 1069.Fn STAILQ_CONCAT ) 1070are functionally identical to these simple queue functions, 1071and are provided for compatibility with 1072.Fx . 1073.Sh NOTES 1074Some of these macros or functions perform no error checking, 1075and invalid usage leads to undefined behaviour. 1076In the case of macros or functions that expect their arguments 1077to be elements that are present in the list or queue, passing an element 1078that is not present is invalid. 1079.Sh HISTORY 1080The 1081.Nm queue 1082functions first appeared in 1083.Bx 4.4 . 1084The 1085.Nm SIMPLEQ 1086functions first appeared in 1087.Nx 1.2 . 1088The 1089.Nm SLIST 1090and 1091.Nm STAILQ 1092functions first appeared in 1093.Fx 2.1.5 . 1094.Pp 1095The 1096.Nm CIRCLEQ 1097functions first appeared in 1098.Bx 4.4 1099and were deprecated in 1100.Nx 7 1101and removed in 1102.Nx 10 1103due to the pointer aliasing violations. 1104