.\" Copyright (c) 1993 .\" The Regents of the University of California. All rights reserved. .\" and Copyright (c) 2020 by Alejandro Colomar .\" .\" %%%LICENSE_START(BSD_3_CLAUSE_UCB) .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" %%%LICENSE_END .\" .\" .TH STAILQ 3 2020-10-21 "GNU" "Linux Programmer's Manual" .SH NAME .Nm STAILQ_CONCAT , .Nm STAILQ_EMPTY , .Nm STAILQ_ENTRY , .Nm STAILQ_FIRST , .Nm STAILQ_FOREACH , .\" .Nm STAILQ_FOREACH_FROM , .\" .Nm STAILQ_FOREACH_FROM_SAFE , .\" .Nm STAILQ_FOREACH_SAFE , .Nm STAILQ_HEAD , .Nm STAILQ_HEAD_INITIALIZER , .Nm STAILQ_INIT , .Nm STAILQ_INSERT_AFTER , .Nm STAILQ_INSERT_HEAD , .Nm STAILQ_INSERT_TAIL , .\" .Nm STAILQ_LAST , .Nm STAILQ_NEXT , .Nm STAILQ_REMOVE , .\" .Nm STAILQ_REMOVE_AFTER , .Nm STAILQ_REMOVE_HEAD , .\" .Nm STAILQ_SWAP , .SH SYNOPSIS .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" .Fn STAILQ_EMPTY "STAILQ_HEAD *head" .Fn STAILQ_ENTRY "TYPE" .Fn STAILQ_FIRST "STAILQ_HEAD *head" .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" .\" .Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" .\" .Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" .\" .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var" .Fn STAILQ_HEAD "HEADNAME" "TYPE" .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head" .Fn STAILQ_INIT "STAILQ_HEAD *head" .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME" .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" .\" .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME" .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME" .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME" .\" .Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME" .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" .\" .Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME" .\" .SH DESCRIPTION .Ss Singly-linked tail queues A singly-linked tail queue is headed by a structure defined by the .Nm STAILQ_HEAD macro. This structure contains a pair of pointers, one to the first element in the tail queue and the other to the last element in the tail queue. The elements are singly linked for minimum space and pointer manipulation overhead at the expense of O(n) removal for arbitrary elements. New elements can be added to the tail queue after an existing element, at the head of the tail queue, or at the end of the tail queue. A .Fa STAILQ_HEAD structure is declared as follows: .Bd -literal -offset indent STAILQ_HEAD(HEADNAME, TYPE) head; .Ed .Pp where .Li HEADNAME is the name of the structure to be defined, and .Li TYPE is the type of the elements to be linked into the tail queue. A pointer to the head of the tail queue can later be declared as: .Bd -literal -offset indent struct HEADNAME *headp; .Ed .Pp (The names .Li head and .Li headp are user selectable.) .Pp The macro .Nm STAILQ_HEAD_INITIALIZER evaluates to an initializer for the tail queue .Fa head . .Pp The macro .Nm STAILQ_CONCAT concatenates the tail queue headed by .Fa head2 onto the end of the one headed by .Fa head1 removing all entries from the former. .Pp The macro .Nm STAILQ_EMPTY evaluates to true if there are no items on the tail queue. .Pp The macro .Nm STAILQ_ENTRY declares a structure that connects the elements in the tail queue. .Pp The macro .Nm STAILQ_FIRST returns the first item on the tail queue or NULL if the tail queue is empty. .Pp The macro .Nm STAILQ_FOREACH traverses the tail queue referenced by .Fa head in the forward direction, assigning each element in turn to .Fa var . .\" .Pp .\" The macro .\" .Nm STAILQ_FOREACH_FROM .\" behaves identically to .\" .Nm STAILQ_FOREACH .\" when .\" .Fa var .\" is NULL, else it treats .\" .Fa var .\" as a previously found STAILQ element and begins the loop at .\" .Fa var .\" instead of the first element in the STAILQ referenced by .\" .Fa head . .\" .Pp .\" The macro .\" .Nm STAILQ_FOREACH_SAFE .\" traverses the tail queue referenced by .\" .Fa head .\" in the forward direction, assigning each element .\" in turn to .\" .Fa var . .\" However, unlike .\" .Fn STAILQ_FOREACH .\" here it is permitted to both remove .\" .Fa var .\" as well as free it from within the loop safely without interfering with the .\" traversal. .\" .Pp .\" The macro .\" .Nm STAILQ_FOREACH_FROM_SAFE .\" behaves identically to .\" .Nm STAILQ_FOREACH_SAFE .\" when .\" .Fa var .\" is NULL, else it treats .\" .Fa var .\" as a previously found STAILQ element and begins the loop at .\" .Fa var .\" instead of the first element in the STAILQ referenced by .\" .Fa head . .Pp The macro .Nm STAILQ_INIT initializes the tail queue referenced by .Fa head . .Pp The macro .Nm STAILQ_INSERT_HEAD inserts the new element .Fa elm at the head of the tail queue. .Pp The macro .Nm STAILQ_INSERT_TAIL inserts the new element .Fa elm at the end of the tail queue. .Pp The macro .Nm STAILQ_INSERT_AFTER inserts the new element .Fa elm after the element .Fa listelm . .\" .Pp .\" The macro .\" .Nm STAILQ_LAST .\" returns the last item on the tail queue. .\" If the tail queue is empty the return value is .\" .Dv NULL . .Pp The macro .Nm STAILQ_NEXT returns the next item on the tail queue, or NULL this item is the last. .\" .Pp .\" The macro .\" .Nm STAILQ_REMOVE_AFTER .\" removes the element after .\" .Fa elm .\" from the tail queue. .\" Unlike .\" .Fa STAILQ_REMOVE , .\" this macro does not traverse the entire tail queue. .Pp The macro .Nm STAILQ_REMOVE_HEAD removes the element at the head of the tail queue. For optimum efficiency, elements being removed from the head of the tail queue should use this macro explicitly rather than the generic .Fa STAILQ_REMOVE macro. .Pp The macro .Nm STAILQ_REMOVE removes the element .Fa elm from the tail queue. .\" .Pp .\" The macro .\" .Nm STAILQ_SWAP .\" swaps the contents of .\" .Fa head1 .\" and .\" .Fa head2 . .Pp See the EXAMPLES section below for an example program using a singly-linked tail queue. .SH RETURN VALUE .SH CONFORMING TO .SH BUGS .SH EXAMPLES .SH SEE ALSO