1.\" $NetBSD: queue.3,v 1.49 2014/05/18 15:45:08 wiz 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 May 17, 2014 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" 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 before or 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.Nm 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.Nm SLIST_HEAD_INITIALIZER 442evaluates to an initializer for the list 443.Fa head . 444.Pp 445The macro 446.Nm SLIST_ENTRY 447declares a structure that connects the elements in 448the list. 449.Pp 450The macro 451.Nm SLIST_FIRST 452returns the first element in the list or NULL if the list is empty. 453.Pp 454The macro 455.Pp 456The macro 457.Nm SLIST_EMPTY 458evaluates to true if there are no elements in the list. 459.Pp 460The macro 461.Nm SLIST_NEXT 462returns the next element in the list. 463.Pp 464.Nm SLIST_FOREACH 465traverses the list referenced by 466.Fa head 467in the forward direction, assigning each element in 468turn to 469.Fa var . 470.Pp 471The SAFE version uses 472.Fa tmp 473to hold the next element, so 474.Fa var 475may be freed or removed from the list. 476.Pp 477The macro 478.Nm SLIST_INIT 479initializes the list referenced by 480.Fa head . 481.Pp 482The macro 483.Nm SLIST_INSERT_HEAD 484inserts the new element 485.Fa elm 486at the head of the list. 487.Pp 488The macro 489.Nm SLIST_INSERT_AFTER 490inserts the new element 491.Fa elm 492after the element 493.Fa listelm . 494.Pp 495The macro 496.Nm SLIST_REMOVE 497removes the element 498.Fa elm 499from the list. 500.Pp 501The macro 502.Nm SLIST_REMOVE_HEAD 503removes the first element from the head of the list. 504For optimum efficiency, 505elements being removed from the head of the list should explicitly use 506this macro instead of the generic 507.Nm SLIST_REMOVE 508macro. 509.Pp 510The macros 511.Nm SLIST_REMOVE_AFTER 512removes the element after the one specified. 513For optimum efficiency, 514elements being removed after a specified one should explicitly use 515this macro instead of the generic 516.Nm SLIST_REMOVE 517.Sh SINGLY-LINKED LIST EXAMPLE 518.Bd -literal 519SLIST_HEAD(slisthead, entry) head = 520 SLIST_HEAD_INITIALIZER(head); 521struct slisthead *headp; /* Singly-linked List head. */ 522struct entry { 523 ... 524 SLIST_ENTRY(entry) entries; /* Singly-linked List. */ 525 ... 526} *n1, *n2, *n3, *np; 527 528SLIST_INIT(\*[Am]head); /* Initialize the list. */ 529 530n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 531SLIST_INSERT_HEAD(\*[Am]head, n1, entries); 532 533n2 = malloc(sizeof(struct entry)); /* Insert after. */ 534SLIST_INSERT_AFTER(n1, n2, entries); 535 536SLIST_REMOVE(\*[Am]head, n2, entry, entries);/* Deletion. */ 537free(n2); 538 539n3 = SLIST_FIRST(\*[Am]head); 540SLIST_REMOVE_HEAD(\*[Am]head, entries); /* Deletion from the head. */ 541free(n3); 542 /* Forward traversal. */ 543SLIST_FOREACH(np, \*[Am]head, entries) 544 np-\*[Gt] ... 545 546while (!SLIST_EMPTY(\*[Am]head)) { /* List Deletion. */ 547 n1 = SLIST_FIRST(\*[Am]head); 548 SLIST_REMOVE_HEAD(\*[Am]head, entries); 549 free(n1); 550} 551.Ed 552.Sh LISTS 553A list is headed by a structure defined by the 554.Nm LIST_HEAD 555macro. 556This structure contains a single pointer to the first element 557on the list. 558The elements are doubly linked so that an arbitrary element can be 559removed without traversing the list. 560New elements can be added to the list after an existing element, 561before an existing element, or at the head of the list. 562A 563.Fa LIST_HEAD 564structure is declared as follows: 565.Bd -literal -offset indent 566LIST_HEAD(HEADNAME, TYPE) head; 567.Ed 568.Pp 569where 570.Fa HEADNAME 571is the name of the structure to be defined, and 572.Fa TYPE 573is the type of the elements to be linked into the list. 574A pointer to the head of the list can later be declared as: 575.Bd -literal -offset indent 576struct HEADNAME *headp; 577.Ed 578.Pp 579(The names 580.Li head 581and 582.Li headp 583are user selectable.) 584.Pp 585The macro 586.Nm LIST_ENTRY 587declares a structure that connects the elements in 588the list. 589.Pp 590The macro 591.Nm LIST_HEAD_INITIALIZER 592provides a value which can be used to initialize a list head at 593compile time, and is used at the point that the list head 594variable is declared, like: 595.Bd -literal -offset indent 596struct HEADNAME head = LIST_HEAD_INITIALIZER(head); 597.Ed 598.Pp 599The macro 600.Nm LIST_FIRST 601returns the first element of the list 602.Fa head . 603.Pp 604The macro 605.Nm LIST_EMPTY 606return true if the list 607.Fa head 608has no elements. 609.Pp 610The macro 611.Nm LIST_NEXT 612returns the element after the element 613.Fa elm . 614.Pp 615The macro 616.Nm LIST_FOREACH 617traverses the list referenced by 618.Fa head 619in the forward direction, assigning each element in turn to 620.Fa var . 621.Pp 622The SAFE version uses 623.Fa tmp 624to hold the next element, so 625.Fa var 626may be freed or removed from the list. 627.Pp 628The macro 629.Nm LIST_INIT 630initializes the list referenced by 631.Fa head . 632.Pp 633The macro 634.Nm LIST_INSERT_AFTER 635inserts the new element 636.Fa elm 637after the element 638.Fa listelm . 639.Pp 640The macro 641.Nm LIST_INSERT_BEFORE 642inserts the new element 643.Fa elm 644before the element 645.Fa listelm . 646.Pp 647The macro 648.Nm LIST_INSERT_HEAD 649inserts the new element 650.Fa elm 651at the head of the list. 652.Pp 653The macro 654.Nm LIST_REMOVE 655removes the element 656.Fa elm 657from the list. 658.Pp 659The macro 660.Nm LIST_REPLACE 661replaces the element 662.Fa elm 663with 664.Fa new 665in the list. 666.Pp 667The macro 668.Nm LIST_MOVE 669moves the list headed by 670.Fa head1 671onto the list headed by 672.Fa head2 , 673always making the former empty. 674.Sh LIST EXAMPLE 675.Bd -literal 676LIST_HEAD(listhead, entry) head; 677struct listhead *headp; /* List head. */ 678struct entry { 679 ... 680 LIST_ENTRY(entry) entries; /* List. */ 681 ... 682} *n1, *n2, *np; 683 684LIST_INIT(\*[Am]head); /* Initialize the list. */ 685 686n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 687LIST_INSERT_HEAD(\*[Am]head, n1, entries); 688 689n2 = malloc(sizeof(struct entry)); /* Insert after. */ 690LIST_INSERT_AFTER(n1, n2, entries); 691 692n2 = malloc(sizeof(struct entry)); /* Insert before. */ 693LIST_INSERT_BEFORE(n1, n2, entries); 694 /* Forward traversal. */ 695LIST_FOREACH(np, \*[Am]head, entries) 696 np-\*[Gt] ... 697 /* Delete. */ 698while (LIST_FIRST(\*[Am]head) != NULL) 699 LIST_REMOVE(LIST_FIRST(\*[Am]head), entries); 700if (LIST_EMPTY(\*[Am]head)) /* Test for emptiness. */ 701 printf("nothing to do\\n"); 702.Ed 703.Sh SIMPLE QUEUES 704A simple queue is headed by a structure defined by the 705.Nm SIMPLEQ_HEAD 706macro. 707This structure contains a pair of pointers, 708one to the first element in the simple queue and the other to 709the last element in the simple queue. 710The elements are singly linked for minimum space and pointer manipulation 711overhead at the expense of O(n) removal for arbitrary elements. 712New elements can be added to the queue after an existing element, 713at the head of the queue, or at the end of the queue. 714A 715.Fa SIMPLEQ_HEAD 716structure is declared as follows: 717.Bd -literal -offset indent 718SIMPLEQ_HEAD(HEADNAME, TYPE) head; 719.Ed 720.Pp 721where 722.Li HEADNAME 723is the name of the structure to be defined, and 724.Li TYPE 725is the type of the elements to be linked into the simple queue. 726A pointer to the head of the simple queue can later be declared as: 727.Bd -literal -offset indent 728struct HEADNAME *headp; 729.Ed 730.Pp 731(The names 732.Li head 733and 734.Li headp 735are user selectable.) 736.Pp 737The macro 738.Nm SIMPLEQ_ENTRY 739declares a structure that connects the elements in 740the simple queue. 741.Pp 742The macro 743.Nm SIMPLEQ_HEAD_INITIALIZER 744provides a value which can be used to initialize a simple queue head at 745compile time, and is used at the point that the simple queue head 746variable is declared, like: 747.Bd -literal -offset indent 748struct HEADNAME head = SIMPLEQ_HEAD_INITIALIZER(head); 749.Ed 750.Pp 751The macro 752.Nm SIMPLEQ_FIRST 753returns the first element of the simple queue 754.Fa head . 755.Pp 756The macro 757.Nm SIMPLEQ_EMPTY 758return true if the simple queue 759.Fa head 760has no elements. 761.Pp 762The macro 763.Nm SIMPLEQ_NEXT 764returns the element after the element 765.Fa elm . 766.Pp 767The macro 768.Nm SIMPLEQ_LAST 769returns the last item on the tail queue. 770If the tail queue is empty the return value is 771.Dv NULL . 772.Pp 773The macro 774.Nm SIMPLEQ_FOREACH 775traverses the tail queue referenced by 776.Fa head 777in the forward direction, assigning each element 778in turn to 779.Fa var . 780.Pp 781The SAFE version uses 782.Fa tmp 783to hold the next element, so 784.Fa var 785may be freed or removed from the list. 786.Pp 787The macro 788.Nm SIMPLEQ_INIT 789initializes the simple queue referenced by 790.Fa head . 791.Pp 792The macro 793.Nm SIMPLEQ_INSERT_HEAD 794inserts the new element 795.Fa elm 796at the head of the simple queue. 797.Pp 798The macro 799.Nm SIMPLEQ_INSERT_TAIL 800inserts the new element 801.Fa elm 802at the end of the simple queue. 803.Pp 804The macro 805.Nm SIMPLEQ_INSERT_AFTER 806inserts the new element 807.Fa elm 808after the element 809.Fa listelm . 810.Pp 811The macro 812.Nm SIMPLEQ_REMOVE_HEAD 813removes the first element from the head of the simple queue. 814For optimum efficiency, 815elements being removed from the head of the queue should explicitly use 816this macro instead of the generic 817.Nm SIMPLQ_REMOVE 818macro. 819.Pp 820The macro 821.Nm SIMPLEQ_REMOVE_AFTER 822removes the element after the one specified from the simple queue. 823For optimum efficiency, 824elements being removed after specified elements should explicitly use 825this macro instead of the generic 826.Nm SIMPLQ_REMOVE 827macro. 828.Pp 829The macro 830.Nm SIMPLEQ_REMOVE 831removes 832.Fa elm 833from the simple queue. 834.Pp 835The macro 836.Nm SIMPLEQ_CONCAT 837concatenates the tail queue headed by 838.Fa head2 839onto the end of the one headed by 840.Fa head1 841removing all entries from the former. 842.Sh SIMPLE QUEUE EXAMPLE 843.Bd -literal 844SIMPLEQ_HEAD(simplehead, entry) head; 845struct simplehead *headp; /* Simple queue head. */ 846struct entry { 847 ... 848 SIMPLEQ_ENTRY(entry) entries; /* Simple queue. */ 849 ... 850} *n1, *n2, *np; 851 852SIMPLEQ_INIT(\*[Am]head); /* Initialize the queue. */ 853 854n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 855SIMPLEQ_INSERT_HEAD(\*[Am]head, n1, entries); 856 857n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 858SIMPLEQ_INSERT_TAIL(\*[Am]head, n1, entries); 859 860n2 = malloc(sizeof(struct entry)); /* Insert after. */ 861SIMPLEQ_INSERT_AFTER(\*[Am]head, n1, n2, entries); 862 /* Forward traversal. */ 863SIMPLEQ_FOREACH(np, \*[Am]head, entries) 864 np-\*[Gt] ... 865 /* Delete. */ 866while (SIMPLEQ_FIRST(\*[Am]head) != NULL) 867 SIMPLEQ_REMOVE_HEAD(\*[Am]head, entries); 868if (SIMPLEQ_EMPTY(\*[Am]head)) /* Test for emptiness. */ 869 printf("nothing to do\\n"); 870.Ed 871.Sh TAIL QUEUES 872A tail queue is headed by a structure defined by the 873.Nm TAILQ_HEAD 874macro. 875This structure contains a pair of pointers, 876one to the first element in the tail queue and the other to 877the last element in the tail queue. 878The elements are doubly linked so that an arbitrary element can be 879removed without traversing the tail queue. 880New elements can be added to the queue after an existing element, 881before an existing element, at the head of the queue, or at the end 882the queue. 883A 884.Fa TAILQ_HEAD 885structure is declared as follows: 886.Bd -literal -offset indent 887TAILQ_HEAD(HEADNAME, TYPE) head; 888.Ed 889.Pp 890where 891.Li HEADNAME 892is the name of the structure to be defined, and 893.Li TYPE 894is the type of the elements to be linked into the tail queue. 895A pointer to the head of the tail queue can later be declared as: 896.Bd -literal -offset indent 897struct HEADNAME *headp; 898.Ed 899.Pp 900(The names 901.Li head 902and 903.Li headp 904are user selectable.) 905.Pp 906The macro 907.Nm TAILQ_ENTRY 908declares a structure that connects the elements in 909the tail queue. 910.Pp 911The macro 912.Nm TAILQ_HEAD_INITIALIZER 913provides a value which can be used to initialize a tail queue head at 914compile time, and is used at the point that the tail queue head 915variable is declared, like: 916.Bd -literal -offset indent 917struct HEADNAME head = TAILQ_HEAD_INITIALIZER(head); 918.Ed 919.Pp 920The macro 921.Nm TAILQ_FIRST 922returns the first element of the tail queue 923.Fa head . 924.Pp 925The macro 926.Nm TAILQ_NEXT 927returns the element after the element 928.Fa elm . 929.Pp 930The macro 931.Nm TAILQ_LAST 932returns the last item on the tail queue. 933If the tail queue is empty the return value is 934.Dv NULL . 935.Pp 936The macro 937.Nm TAILQ_PREV 938returns the previous item on the tail queue, from the one specified. 939If the tail queue is empty the return value is 940.Dv NULL . 941.Pp 942The macro 943.Nm TAILQ_EMPTY 944return true if the tail queue 945.Fa head 946has no elements. 947.Pp 948The macros 949.Nm TAILQ_FOREACH , 950.Nm TAILQ_FOREACH_REVERSE , 951.Nm TAILQ_FOREACH_SAFE , 952and 953.Nm TAILQ_FOREACH_REVERSE_SAFE 954traverse the tail queue referenced by 955.Fa head 956in the forward or reverse direction direction, assigning each element in turn to 957.Fa var . 958.Pp 959The SAFE versions use 960.Fa tmp 961to hold the next element, so 962.Fa var 963may be freed or removed from the list. 964.Pp 965The macro 966.Nm TAILQ_INIT 967initializes the tail queue referenced by 968.Fa head . 969.Pp 970The macro 971.Nm TAILQ_INSERT_HEAD 972inserts the new element 973.Fa elm 974at the head of the tail queue. 975.Pp 976The macro 977.Nm TAILQ_INSERT_TAIL 978inserts the new element 979.Fa elm 980at the end of the tail queue. 981.Pp 982The macro 983.Nm TAILQ_INSERT_AFTER 984inserts the new element 985.Fa elm 986after the element 987.Fa listelm . 988.Pp 989The macro 990.Nm TAILQ_INSERT_BEFORE 991inserts the new element 992.Fa elm 993before the element 994.Fa listelm . 995.Pp 996The macro 997.Nm TAILQ_REMOVE 998removes the element 999.Fa elm 1000from the tail queue. 1001.Pp 1002The macro 1003.Nm TAILQ_REPLACE 1004replace the element 1005.Fa elm 1006with the 1007.Fa new 1008one specified in the tail queue. 1009.Pp 1010The macro 1011.Nm TAILQ_CONCAT 1012concatenates the tail queue headed by 1013.Fa head2 1014onto the end of the one headed by 1015.Fa head1 1016removing all entries from the former. 1017.Sh TAIL QUEUE EXAMPLE 1018.Bd -literal 1019TAILQ_HEAD(tailhead, entry) head; 1020struct tailhead *headp; /* Tail queue head. */ 1021struct entry { 1022 ... 1023 TAILQ_ENTRY(entry) entries; /* Tail queue. */ 1024 ... 1025} *n1, *n2, *np; 1026 1027TAILQ_INIT(\*[Am]head); /* Initialize the queue. */ 1028 1029n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ 1030TAILQ_INSERT_HEAD(\*[Am]head, n1, entries); 1031 1032n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ 1033TAILQ_INSERT_TAIL(\*[Am]head, n1, entries); 1034 1035n2 = malloc(sizeof(struct entry)); /* Insert after. */ 1036TAILQ_INSERT_AFTER(\*[Am]head, n1, n2, entries); 1037 1038n2 = malloc(sizeof(struct entry)); /* Insert before. */ 1039TAILQ_INSERT_BEFORE(n1, n2, entries); 1040 /* Forward traversal. */ 1041TAILQ_FOREACH(np, \*[Am]head, entries) 1042 np-\*[Gt] ... 1043 /* Reverse traversal. */ 1044TAILQ_FOREACH_REVERSE(np, \*[Am]head, tailhead, entries) 1045 np-\*[Gt] ... 1046 /* Delete. */ 1047while (TAILQ_FIRST(\*[Am]head) != NULL) 1048 TAILQ_REMOVE(\*[Am]head, TAILQ_FIRST(\*[Am]head), entries); 1049if (TAILQ_EMPTY(\*[Am]head)) /* Test for emptiness. */ 1050 printf("nothing to do\\n"); 1051.Ed 1052.Sh SINGLY LINKED TAIL QUEUES 1053The macros prefixed with 1054.Dq Nm STAILQ_ 1055.Nm ( STAILQ_HEAD , 1056.Nm STAILQ_HEAD_INITIALIZER , 1057.Nm STAILQ_ENTRY , 1058.Nm STAILQ_FOREACH , 1059.Nm STAILQ_FOREACH_SAFE , 1060.Nm STAILQ_FIRST , 1061.Nm STAILQ_EMPTY , 1062.Nm STAILQ_NEXT , 1063.Nm STAILQ_LAST , 1064.Nm STAILQ_INIT , 1065.Nm STAILQ_INSERT_HEAD , 1066.Nm STAILQ_INSERT_TAIL , 1067.Nm STAILQ_INSERT_AFTER , 1068.Nm STAILQ_REMOVE_HEAD , 1069.Nm STAILQ_REMOVE , 1070and 1071.Nm STAILQ_CONCAT ) 1072are functionally identical to these simple queue functions, 1073and are provided for compatibility with 1074.Fx . 1075.Sh NOTES 1076Some of these macros or functions perform no error checking, 1077and invalid usage leads to undefined behaviour. 1078In the case of macros or functions that expect their arguments 1079to be elements that are present in the list or queue, passing an element 1080that is not present is invalid. 1081.Sh HISTORY 1082The 1083.Nm queue 1084functions first appeared in 1085.Bx 4.4 . 1086The 1087.Nm SIMPLEQ 1088functions first appeared in 1089.Nx 1.2 . 1090The 1091.Nm SLIST 1092and 1093.Nm STAILQ 1094functions first appeared in 1095.Fx 2.1.5 . 1096