| 1 |
.\" Copyright 1993, The Regents of the University of California. |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: BSD-3-Clause |
| 5 |
.\" |
| 6 |
.TH STAILQ 3 2025-09-21 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
.\"SIMPLEQ_CONCAT, |
| 9 |
SIMPLEQ_EMPTY, |
| 10 |
SIMPLEQ_ENTRY, |
| 11 |
SIMPLEQ_FIRST, |
| 12 |
SIMPLEQ_FOREACH, |
| 13 |
.\"SIMPLEQ_FOREACH_FROM, |
| 14 |
.\"SIMPLEQ_FOREACH_FROM_SAFE, |
| 15 |
.\"SIMPLEQ_FOREACH_SAFE, |
| 16 |
SIMPLEQ_HEAD, |
| 17 |
SIMPLEQ_HEAD_INITIALIZER, |
| 18 |
SIMPLEQ_INIT, |
| 19 |
SIMPLEQ_INSERT_AFTER, |
| 20 |
SIMPLEQ_INSERT_HEAD, |
| 21 |
SIMPLEQ_INSERT_TAIL, |
| 22 |
.\"SIMPLEQ_LAST, |
| 23 |
SIMPLEQ_NEXT, |
| 24 |
SIMPLEQ_REMOVE, |
| 25 |
.\"SIMPLEQ_REMOVE_AFTER, |
| 26 |
SIMPLEQ_REMOVE_HEAD, |
| 27 |
.\"SIMPLEQ_SWAP, |
| 28 |
STAILQ_CONCAT, |
| 29 |
STAILQ_EMPTY, |
| 30 |
STAILQ_ENTRY, |
| 31 |
STAILQ_FIRST, |
| 32 |
STAILQ_FOREACH, |
| 33 |
.\"STAILQ_FOREACH_FROM, |
| 34 |
.\"STAILQ_FOREACH_FROM_SAFE, |
| 35 |
.\"STAILQ_FOREACH_SAFE, |
| 36 |
STAILQ_HEAD, |
| 37 |
STAILQ_HEAD_INITIALIZER, |
| 38 |
STAILQ_INIT, |
| 39 |
STAILQ_INSERT_AFTER, |
| 40 |
STAILQ_INSERT_HEAD, |
| 41 |
STAILQ_INSERT_TAIL, |
| 42 |
.\"STAILQ_LAST, |
| 43 |
STAILQ_NEXT, |
| 44 |
STAILQ_REMOVE, |
| 45 |
.\"STAILQ_REMOVE_AFTER, |
| 46 |
STAILQ_REMOVE_HEAD, |
| 47 |
.\"STAILQ_SWAP |
| 48 |
\- implementation of a singly linked tail queue |
| 49 |
.SH LIBRARY |
| 50 |
Standard C library |
| 51 |
.RI ( libc ,\~ \-lc ) |
| 52 |
.SH SYNOPSIS |
| 53 |
.nf |
| 54 |
.B #include <sys/queue.h> |
| 55 |
.P |
| 56 |
.B STAILQ_ENTRY(TYPE); |
| 57 |
.P |
| 58 |
.B STAILQ_HEAD(HEADNAME, TYPE); |
| 59 |
.BI "STAILQ_HEAD STAILQ_HEAD_INITIALIZER(STAILQ_HEAD " head ); |
| 60 |
.BI "void STAILQ_INIT(STAILQ_HEAD *" head ); |
| 61 |
.P |
| 62 |
.BI "int STAILQ_EMPTY(STAILQ_HEAD *" head ); |
| 63 |
.P |
| 64 |
.BI "void STAILQ_INSERT_HEAD(STAILQ_HEAD *" head , |
| 65 |
.BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME ); |
| 66 |
.BI "void STAILQ_INSERT_TAIL(STAILQ_HEAD *" head , |
| 67 |
.BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME ); |
| 68 |
.BI "void STAILQ_INSERT_AFTER(STAILQ_HEAD *" head ", struct TYPE *" listelm , |
| 69 |
.BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME ); |
| 70 |
.P |
| 71 |
.BI "struct TYPE *STAILQ_FIRST(STAILQ_HEAD *" head ); |
| 72 |
.\" .BI "struct TYPE *STAILQ_LAST(STAILQ_HEAD *" head ", struct TYPE *" elm , |
| 73 |
.\" .BI " STAILQ_ENTRY " NAME ); |
| 74 |
.BI "struct TYPE *STAILQ_NEXT(struct TYPE *" elm ", STAILQ_ENTRY " NAME ); |
| 75 |
.P |
| 76 |
.BI "STAILQ_FOREACH(struct TYPE *" var ", STAILQ_HEAD *" head ", STAILQ_ENTRY " NAME ); |
| 77 |
.\" .BI "STAILQ_FOREACH_FROM(struct TYPE *" var ", STAILQ_HEAD *" head , |
| 78 |
.\" .BI " STAILQ_ENTRY " NAME ); |
| 79 |
.\" .P |
| 80 |
.\" .BI "STAILQ_FOREACH_SAFE(struct TYPE *" var ", STAILQ_HEAD *" head , |
| 81 |
.\" .BI " STAILQ_ENTRY " NAME ", struct TYPE *" temp_var ); |
| 82 |
.\" .BI "STAILQ_FOREACH_FROM_SAFE(struct TYPE *" var ", STAILQ_HEAD *" head , |
| 83 |
.\" .BI " STAILQ_ENTRY " NAME ", struct TYPE *" temp_var ); |
| 84 |
.P |
| 85 |
.BI "void STAILQ_REMOVE(STAILQ_HEAD *" head ", struct TYPE *" elm ", TYPE," |
| 86 |
.BI " STAILQ_ENTRY " NAME ); |
| 87 |
.BI "void STAILQ_REMOVE_HEAD(STAILQ_HEAD *" head , |
| 88 |
.BI " STAILQ_ENTRY " NAME ); |
| 89 |
.\" .BI "void STAILQ_REMOVE_AFTER(STAILQ_HEAD *" head ", struct TYPE *" elm , |
| 90 |
.\" .BI " STAILQ_ENTRY " NAME ); |
| 91 |
.P |
| 92 |
.BI "void STAILQ_CONCAT(STAILQ_HEAD *" head1 ", STAILQ_HEAD *" head2 ); |
| 93 |
.\" .BI "void STAILQ_SWAP(STAILQ_HEAD *" head1 ", STAILQ_HEAD *" head2 , |
| 94 |
.\" .BI " STAILQ_ENTRY " NAME ); |
| 95 |
.fi |
| 96 |
.IR Note : |
| 97 |
Identical macros prefixed with SIMPLEQ instead of STAILQ exist; |
| 98 |
see VERSIONS. |
| 99 |
.SH DESCRIPTION |
| 100 |
These macros define and operate on singly linked tail queues. |
| 101 |
.P |
| 102 |
In the macro definitions, |
| 103 |
.I TYPE |
| 104 |
is the name of a user-defined structure, |
| 105 |
that must contain a field of type |
| 106 |
.IR STAILQ_ENTRY , |
| 107 |
named |
| 108 |
.IR NAME . |
| 109 |
The argument |
| 110 |
.I HEADNAME |
| 111 |
is the name of a user-defined structure that must be declared |
| 112 |
using the macro |
| 113 |
.BR STAILQ_HEAD (). |
| 114 |
.SS Creation |
| 115 |
A singly linked tail queue is headed by a structure defined by the |
| 116 |
.BR STAILQ_HEAD () |
| 117 |
macro. |
| 118 |
This structure contains a pair of pointers, |
| 119 |
one to the first element in the tail queue and the other to |
| 120 |
the last element in the tail queue. |
| 121 |
The elements are singly linked for minimum space and pointer |
| 122 |
manipulation overhead at the expense of O(n) removal for arbitrary elements. |
| 123 |
New elements can be added to the tail queue after an existing element, |
| 124 |
at the head of the tail queue, or at the end of the tail queue. |
| 125 |
A |
| 126 |
.I STAILQ_HEAD |
| 127 |
structure is declared as follows: |
| 128 |
.P |
| 129 |
.in +4 |
| 130 |
.EX |
| 131 |
STAILQ_HEAD(HEADNAME, TYPE) head; |
| 132 |
.EE |
| 133 |
.in |
| 134 |
.P |
| 135 |
where |
| 136 |
.I struct HEADNAME |
| 137 |
is the structure to be defined, and |
| 138 |
.I struct TYPE |
| 139 |
is the type of the elements to be linked into the tail queue. |
| 140 |
A pointer to the head of the tail queue can later be declared as: |
| 141 |
.P |
| 142 |
.in +4 |
| 143 |
.EX |
| 144 |
struct HEADNAME *headp; |
| 145 |
.EE |
| 146 |
.in |
| 147 |
.P |
| 148 |
(The names |
| 149 |
.I head |
| 150 |
and |
| 151 |
.I headp |
| 152 |
are user selectable.) |
| 153 |
.P |
| 154 |
.BR STAILQ_ENTRY () |
| 155 |
declares a structure that connects the elements in the tail queue. |
| 156 |
.P |
| 157 |
.BR STAILQ_HEAD_INITIALIZER () |
| 158 |
evaluates to an initializer for the tail queue |
| 159 |
.IR head . |
| 160 |
.P |
| 161 |
.BR STAILQ_INIT () |
| 162 |
initializes the tail queue referenced by |
| 163 |
.IR head . |
| 164 |
.P |
| 165 |
.BR STAILQ_EMPTY () |
| 166 |
evaluates to true if there are no items on the tail queue. |
| 167 |
.SS Insertion |
| 168 |
.BR STAILQ_INSERT_HEAD () |
| 169 |
inserts the new element |
| 170 |
.I elm |
| 171 |
at the head of the tail queue. |
| 172 |
.P |
| 173 |
.BR STAILQ_INSERT_TAIL () |
| 174 |
inserts the new element |
| 175 |
.I elm |
| 176 |
at the end of the tail queue. |
| 177 |
.P |
| 178 |
.BR STAILQ_INSERT_AFTER () |
| 179 |
inserts the new element |
| 180 |
.I elm |
| 181 |
after the element |
| 182 |
.IR listelm . |
| 183 |
.SS Traversal |
| 184 |
.BR STAILQ_FIRST () |
| 185 |
returns the first item on the tail queue or NULL if the tail queue is empty. |
| 186 |
.\" .P |
| 187 |
.\" .BR STAILQ_LAST () |
| 188 |
.\" returns the last item on the tail queue. |
| 189 |
.\" If the tail queue is empty the return value is NULL . |
| 190 |
.P |
| 191 |
.BR STAILQ_NEXT () |
| 192 |
returns the next item on the tail queue, or NULL this item is the last. |
| 193 |
.P |
| 194 |
.BR STAILQ_FOREACH () |
| 195 |
traverses the tail queue referenced by |
| 196 |
.I head |
| 197 |
in the forward direction, |
| 198 |
assigning each element in turn to |
| 199 |
.IR var . |
| 200 |
.\" .P |
| 201 |
.\" .BR STAILQ_FOREACH_FROM () |
| 202 |
.\" behaves identically to |
| 203 |
.\" .BR STAILQ_FOREACH () |
| 204 |
.\" when |
| 205 |
.\" .I var |
| 206 |
.\" is NULL, else it treats |
| 207 |
.\" .I var |
| 208 |
.\" as a previously found STAILQ element and begins the loop at |
| 209 |
.\" .I var |
| 210 |
.\" instead of the first element in the STAILQ referenced by |
| 211 |
.\" .IR head . |
| 212 |
.\" .P |
| 213 |
.\" .BR STAILQ_FOREACH_SAFE () |
| 214 |
.\" traverses the tail queue referenced by |
| 215 |
.\" .I head |
| 216 |
.\" in the forward direction, assigning each element |
| 217 |
.\" in turn to |
| 218 |
.\" .IR var . |
| 219 |
.\" However, unlike |
| 220 |
.\" .BR STAILQ_FOREACH () |
| 221 |
.\" here it is permitted to both remove |
| 222 |
.\" .I var |
| 223 |
.\" as well as free it from within the loop safely without interfering with the |
| 224 |
.\" traversal. |
| 225 |
.\" .P |
| 226 |
.\" .BR STAILQ_FOREACH_FROM_SAFE () |
| 227 |
.\" behaves identically to |
| 228 |
.\" .BR STAILQ_FOREACH_SAFE () |
| 229 |
.\" when |
| 230 |
.\" .I var |
| 231 |
.\" is NULL, else it treats |
| 232 |
.\" .I var |
| 233 |
.\" as a previously found STAILQ element and begins the loop at |
| 234 |
.\" .I var |
| 235 |
.\" instead of the first element in the STAILQ referenced by |
| 236 |
.\" .IR head . |
| 237 |
.SS Removal |
| 238 |
.BR STAILQ_REMOVE () |
| 239 |
removes the element |
| 240 |
.I elm |
| 241 |
from the tail queue. |
| 242 |
.P |
| 243 |
.BR STAILQ_REMOVE_HEAD () |
| 244 |
removes the element at the head of the tail queue. |
| 245 |
For optimum efficiency, |
| 246 |
elements being removed from the head of the tail queue should |
| 247 |
use this macro explicitly rather than the generic |
| 248 |
.BR STAILQ_REMOVE () |
| 249 |
macro. |
| 250 |
.\" .P |
| 251 |
.\" .BR STAILQ_REMOVE_AFTER () |
| 252 |
.\" removes the element after |
| 253 |
.\" .I elm |
| 254 |
.\" from the tail queue. |
| 255 |
.\" Unlike |
| 256 |
.\" .BR STAILQ_REMOVE (), |
| 257 |
.\" this macro does not traverse the entire tail queue. |
| 258 |
.SS Other features |
| 259 |
.BR STAILQ_CONCAT () |
| 260 |
concatenates the tail queue headed by |
| 261 |
.I head2 |
| 262 |
onto the end of the one headed by |
| 263 |
.I head1 |
| 264 |
removing all entries from the former. |
| 265 |
.\" .P |
| 266 |
.\" .BR STAILQ_SWAP () |
| 267 |
.\" swaps the contents of |
| 268 |
.\" .I head1 |
| 269 |
.\" and |
| 270 |
.\" .IR head2 . |
| 271 |
.SH RETURN VALUE |
| 272 |
.BR STAILQ_EMPTY () |
| 273 |
returns nonzero if the queue is empty, |
| 274 |
and zero if the queue contains at least one entry. |
| 275 |
.P |
| 276 |
.BR STAILQ_FIRST (), |
| 277 |
and |
| 278 |
.BR STAILQ_NEXT () |
| 279 |
return a pointer to the first or next |
| 280 |
.I TYPE |
| 281 |
structure, respectively. |
| 282 |
.P |
| 283 |
.BR STAILQ_HEAD_INITIALIZER () |
| 284 |
returns an initializer that can be assigned to the queue |
| 285 |
.IR head . |
| 286 |
.SH VERSIONS |
| 287 |
Some BSDs provide SIMPLEQ instead of STAILQ. |
| 288 |
They are identical, but for historical reasons |
| 289 |
they were named differently on different BSDs. |
| 290 |
STAILQ originated on FreeBSD, and SIMPLEQ originated on NetBSD. |
| 291 |
For compatibility reasons, some systems provide both sets of macros. |
| 292 |
glibc provides both STAILQ and SIMPLEQ, |
| 293 |
which are identical except for a missing SIMPLEQ equivalent to |
| 294 |
.BR STAILQ_CONCAT (). |
| 295 |
.SH BUGS |
| 296 |
.BR STAILQ_FOREACH () |
| 297 |
doesn't allow |
| 298 |
.I var |
| 299 |
to be removed or freed within the loop, |
| 300 |
as it would interfere with the traversal. |
| 301 |
.BR STAILQ_FOREACH_SAFE (), |
| 302 |
which is present on the BSDs but is not present in glibc, |
| 303 |
fixes this limitation by allowing |
| 304 |
.I var |
| 305 |
to safely be removed from the list and freed from within the loop |
| 306 |
without interfering with the traversal. |
| 307 |
.SH STANDARDS |
| 308 |
BSD. |
| 309 |
.SH HISTORY |
| 310 |
4.4BSD. |
| 311 |
.SH EXAMPLES |
| 312 |
.\" SRC BEGIN (stailq.c) |
| 313 |
.EX |
| 314 |
#include <stddef.h> |
| 315 |
#include <stdio.h> |
| 316 |
#include <stdlib.h> |
| 317 |
#include <sys/queue.h> |
| 318 |
\& |
| 319 |
struct entry { |
| 320 |
int data; |
| 321 |
STAILQ_ENTRY(entry) entries; /* Singly linked tail queue */ |
| 322 |
}; |
| 323 |
\& |
| 324 |
STAILQ_HEAD(stailhead, entry); |
| 325 |
\& |
| 326 |
int |
| 327 |
main(void) |
| 328 |
{ |
| 329 |
struct entry *n1, *n2, *n3, *np; |
| 330 |
struct stailhead head; /* Singly linked tail queue |
| 331 |
head */ |
| 332 |
\& |
| 333 |
STAILQ_INIT(&head); /* Initialize the queue */ |
| 334 |
\& |
| 335 |
n1 = malloc(sizeof(struct entry)); /* Insert at the head */ |
| 336 |
STAILQ_INSERT_HEAD(&head, n1, entries); |
| 337 |
\& |
| 338 |
n1 = malloc(sizeof(struct entry)); /* Insert at the tail */ |
| 339 |
STAILQ_INSERT_TAIL(&head, n1, entries); |
| 340 |
\& |
| 341 |
n2 = malloc(sizeof(struct entry)); /* Insert after */ |
| 342 |
STAILQ_INSERT_AFTER(&head, n1, n2, entries); |
| 343 |
\& |
| 344 |
STAILQ_REMOVE(&head, n2, entry, entries); /* Deletion */ |
| 345 |
free(n2); |
| 346 |
\& |
| 347 |
n3 = STAILQ_FIRST(&head); |
| 348 |
STAILQ_REMOVE_HEAD(&head, entries); /* Deletion from the head */ |
| 349 |
free(n3); |
| 350 |
\& |
| 351 |
n1 = STAILQ_FIRST(&head); |
| 352 |
n1\->data = 0; |
| 353 |
for (unsigned int i = 1; i < 5; i++) { |
| 354 |
n1 = malloc(sizeof(struct entry)); |
| 355 |
STAILQ_INSERT_HEAD(&head, n1, entries); |
| 356 |
n1\->data = i; |
| 357 |
} |
| 358 |
/* Forward traversal */ |
| 359 |
STAILQ_FOREACH(np, &head, entries) |
| 360 |
printf("%i\[rs]n", np\->data); |
| 361 |
/* TailQ deletion */ |
| 362 |
n1 = STAILQ_FIRST(&head); |
| 363 |
while (n1 != NULL) { |
| 364 |
n2 = STAILQ_NEXT(n1, entries); |
| 365 |
free(n1); |
| 366 |
n1 = n2; |
| 367 |
} |
| 368 |
STAILQ_INIT(&head); |
| 369 |
\& |
| 370 |
exit(EXIT_SUCCESS); |
| 371 |
} |
| 372 |
.EE |
| 373 |
.\" SRC END |
| 374 |
.SH SEE ALSO |
| 375 |
.BR insque (3), |
| 376 |
.BR queue (7) |