| 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 TAILQ 3 2025-05-17 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
TAILQ_CONCAT, |
| 9 |
TAILQ_EMPTY, |
| 10 |
TAILQ_ENTRY, |
| 11 |
TAILQ_FIRST, |
| 12 |
TAILQ_FOREACH, |
| 13 |
.\"TAILQ_FOREACH_FROM, |
| 14 |
.\"TAILQ_FOREACH_FROM_SAFE, |
| 15 |
TAILQ_FOREACH_REVERSE, |
| 16 |
.\"TAILQ_FOREACH_REVERSE_FROM, |
| 17 |
.\"TAILQ_FOREACH_REVERSE_FROM_SAFE, |
| 18 |
.\"TAILQ_FOREACH_REVERSE_SAFE, |
| 19 |
.\"TAILQ_FOREACH_SAFE, |
| 20 |
TAILQ_HEAD, |
| 21 |
TAILQ_HEAD_INITIALIZER, |
| 22 |
TAILQ_INIT, |
| 23 |
TAILQ_INSERT_AFTER, |
| 24 |
TAILQ_INSERT_BEFORE, |
| 25 |
TAILQ_INSERT_HEAD, |
| 26 |
TAILQ_INSERT_TAIL, |
| 27 |
TAILQ_LAST, |
| 28 |
TAILQ_NEXT, |
| 29 |
TAILQ_PREV, |
| 30 |
TAILQ_REMOVE |
| 31 |
.\"TAILQ_SWAP |
| 32 |
\- implementation of a doubly linked tail queue |
| 33 |
.SH LIBRARY |
| 34 |
Standard C library |
| 35 |
.RI ( libc ,\~ \-lc ) |
| 36 |
.SH SYNOPSIS |
| 37 |
.nf |
| 38 |
.B #include <sys/queue.h> |
| 39 |
.P |
| 40 |
.B TAILQ_ENTRY(TYPE); |
| 41 |
.P |
| 42 |
.B TAILQ_HEAD(HEADNAME, TYPE); |
| 43 |
.BI "TAILQ_HEAD TAILQ_HEAD_INITIALIZER(TAILQ_HEAD " head ); |
| 44 |
.BI "void TAILQ_INIT(TAILQ_HEAD *" head ); |
| 45 |
.P |
| 46 |
.BI "int TAILQ_EMPTY(TAILQ_HEAD *" head ); |
| 47 |
.P |
| 48 |
.BI "void TAILQ_INSERT_HEAD(TAILQ_HEAD *" head , |
| 49 |
.BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME ); |
| 50 |
.BI "void TAILQ_INSERT_TAIL(TAILQ_HEAD *" head , |
| 51 |
.BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME ); |
| 52 |
.BI "void TAILQ_INSERT_BEFORE(struct TYPE *" listelm , |
| 53 |
.BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME ); |
| 54 |
.BI "void TAILQ_INSERT_AFTER(TAILQ_HEAD *" head ", struct TYPE *" listelm , |
| 55 |
.BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME ); |
| 56 |
.P |
| 57 |
.BI "struct TYPE *TAILQ_FIRST(TAILQ_HEAD *" head ); |
| 58 |
.BI "struct TYPE *TAILQ_LAST(TAILQ_HEAD *" head ", HEADNAME);" |
| 59 |
.BI "struct TYPE *TAILQ_PREV(struct TYPE *" elm ", HEADNAME, TAILQ_ENTRY " NAME ); |
| 60 |
.BI "struct TYPE *TAILQ_NEXT(struct TYPE *" elm ", TAILQ_ENTRY " NAME ); |
| 61 |
.P |
| 62 |
.BI "TAILQ_FOREACH(struct TYPE *" var ", TAILQ_HEAD *" head , |
| 63 |
.BI " TAILQ_ENTRY " NAME ); |
| 64 |
.\" .BI "TAILQ_FOREACH_FROM(struct TYPE *" var ", TAILQ_HEAD *" head , |
| 65 |
.\" .BI " TAILQ_ENTRY " NAME ); |
| 66 |
.BI "TAILQ_FOREACH_REVERSE(struct TYPE *" var ", TAILQ_HEAD *" head ", HEADNAME," |
| 67 |
.BI " TAILQ_ENTRY " NAME ); |
| 68 |
.\" .BI "TAILQ_FOREACH_REVERSE_FROM(struct TYPE *" var ", TAILQ_HEAD *" head ", HEADNAME," |
| 69 |
.\" .BI " TAILQ_ENTRY " NAME ); |
| 70 |
.\" .P |
| 71 |
.\" .BI "TAILQ_FOREACH_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head , |
| 72 |
.\" .BI " TAILQ_ENTRY " NAME , |
| 73 |
.\" .BI " struct TYPE *" temp_var ); |
| 74 |
.\" .BI "TAILQ_FOREACH_FROM_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head , |
| 75 |
.\" .BI " TAILQ_ENTRY " NAME , |
| 76 |
.\" .BI " struct TYPE *" temp_var ); |
| 77 |
.\" .BI "TAILQ_FOREACH_REVERSE_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head , |
| 78 |
.\" .BI " HEADNAME, TAILQ_ENTRY " NAME , |
| 79 |
.\" .BI " struct TYPE *" temp_var ); |
| 80 |
.\" .BI "TAILQ_FOREACH_REVERSE_FROM_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head , |
| 81 |
.\" .BI " HEADNAME, TAILQ_ENTRY " NAME , |
| 82 |
.\" .BI " struct TYPE *" temp_var ); |
| 83 |
.P |
| 84 |
.BI "void TAILQ_REMOVE(TAILQ_HEAD *" head ", struct TYPE *" elm , |
| 85 |
.BI " TAILQ_ENTRY " NAME ); |
| 86 |
.P |
| 87 |
.BI "void TAILQ_CONCAT(TAILQ_HEAD *" head1 ", TAILQ_HEAD *" head2 , |
| 88 |
.BI " TAILQ_ENTRY " NAME ); |
| 89 |
.\" .BI "void TAILQ_SWAP(TAILQ_HEAD *" head1 ", TAILQ_HEAD *" head2 ", TYPE," |
| 90 |
.\" .BI " TAILQ_ENTRY " NAME ); |
| 91 |
.fi |
| 92 |
.SH DESCRIPTION |
| 93 |
These macros define and operate on doubly linked tail queues. |
| 94 |
.P |
| 95 |
In the macro definitions, |
| 96 |
.I TYPE |
| 97 |
is the name of a user defined structure, |
| 98 |
that must contain a field of type |
| 99 |
.IR TAILQ_ENTRY , |
| 100 |
named |
| 101 |
.IR NAME . |
| 102 |
The argument |
| 103 |
.I HEADNAME |
| 104 |
is the name of a user defined structure that must be declared |
| 105 |
using the macro |
| 106 |
.BR TAILQ_HEAD (). |
| 107 |
.SS Creation |
| 108 |
A tail queue is headed by a structure defined by the |
| 109 |
.BR TAILQ_HEAD () |
| 110 |
macro. |
| 111 |
This structure contains a pair of pointers, |
| 112 |
one to the first element in the queue |
| 113 |
and the other to the last element in the queue. |
| 114 |
The elements are doubly linked |
| 115 |
so that an arbitrary element can be removed without traversing the queue. |
| 116 |
New elements can be added to the queue |
| 117 |
after an existing element, |
| 118 |
before an existing element, |
| 119 |
at the head of the queue, |
| 120 |
or at the end of the queue. |
| 121 |
A |
| 122 |
.I TAILQ_HEAD |
| 123 |
structure is declared as follows: |
| 124 |
.P |
| 125 |
.in +4 |
| 126 |
.EX |
| 127 |
TAILQ_HEAD(HEADNAME, TYPE) head; |
| 128 |
.EE |
| 129 |
.in |
| 130 |
.P |
| 131 |
where |
| 132 |
.I struct HEADNAME |
| 133 |
is the structure to be defined, and |
| 134 |
.I struct TYPE |
| 135 |
is the type of the elements to be linked into the queue. |
| 136 |
A pointer to the head of the queue can later be declared as: |
| 137 |
.P |
| 138 |
.in +4 |
| 139 |
.EX |
| 140 |
struct HEADNAME *headp; |
| 141 |
.EE |
| 142 |
.in |
| 143 |
.P |
| 144 |
(The names |
| 145 |
.I head |
| 146 |
and |
| 147 |
.I headp |
| 148 |
are user selectable.) |
| 149 |
.P |
| 150 |
.BR TAILQ_ENTRY () |
| 151 |
declares a structure that connects the elements in the queue. |
| 152 |
.P |
| 153 |
.BR TAILQ_HEAD_INITIALIZER () |
| 154 |
evaluates to an initializer for the queue |
| 155 |
.IR head . |
| 156 |
.P |
| 157 |
.BR TAILQ_INIT () |
| 158 |
initializes the queue referenced by |
| 159 |
.P |
| 160 |
.BR TAILQ_EMPTY () |
| 161 |
evaluates to true if there are no items on the queue. |
| 162 |
.IR head . |
| 163 |
.SS Insertion |
| 164 |
.BR TAILQ_INSERT_HEAD () |
| 165 |
inserts the new element |
| 166 |
.I elm |
| 167 |
at the head of the queue. |
| 168 |
.P |
| 169 |
.BR TAILQ_INSERT_TAIL () |
| 170 |
inserts the new element |
| 171 |
.I elm |
| 172 |
at the end of the queue. |
| 173 |
.P |
| 174 |
.BR TAILQ_INSERT_BEFORE () |
| 175 |
inserts the new element |
| 176 |
.I elm |
| 177 |
before the element |
| 178 |
.IR listelm . |
| 179 |
.P |
| 180 |
.BR TAILQ_INSERT_AFTER () |
| 181 |
inserts the new element |
| 182 |
.I elm |
| 183 |
after the element |
| 184 |
.IR listelm . |
| 185 |
.SS Traversal |
| 186 |
.BR TAILQ_FIRST () |
| 187 |
returns the first item on the queue, or NULL if the queue is empty. |
| 188 |
.P |
| 189 |
.BR TAILQ_LAST () |
| 190 |
returns the last item on the queue. |
| 191 |
If the queue is empty the return value is NULL. |
| 192 |
.P |
| 193 |
.BR TAILQ_PREV () |
| 194 |
returns the previous item on the queue, or NULL if this item is the first. |
| 195 |
.P |
| 196 |
.BR TAILQ_NEXT () |
| 197 |
returns the next item on the queue, or NULL if this item is the last. |
| 198 |
.P |
| 199 |
.BR TAILQ_FOREACH () |
| 200 |
traverses the queue referenced by |
| 201 |
.I head |
| 202 |
in the forward direction, |
| 203 |
assigning each element in turn to |
| 204 |
.IR var . |
| 205 |
.I var |
| 206 |
is set to NULL if the loop completes normally, |
| 207 |
or if there were no elements. |
| 208 |
.\" .P |
| 209 |
.\" .BR TAILQ_FOREACH_FROM () |
| 210 |
.\" behaves identically to |
| 211 |
.\" .BR TAILQ_FOREACH () |
| 212 |
.\" when |
| 213 |
.\" .I var |
| 214 |
.\" is NULL, else it treats |
| 215 |
.\" .I var |
| 216 |
.\" as a previously found TAILQ element and begins the loop at |
| 217 |
.\" .I var |
| 218 |
.\" instead of the first element in the TAILQ referenced by |
| 219 |
.\" .IR head . |
| 220 |
.P |
| 221 |
.BR TAILQ_FOREACH_REVERSE () |
| 222 |
traverses the queue referenced by |
| 223 |
.I head |
| 224 |
in the reverse direction, |
| 225 |
assigning each element in turn to |
| 226 |
.IR var . |
| 227 |
.\" .P |
| 228 |
.\" .BR TAILQ_FOREACH_REVERSE_FROM () |
| 229 |
.\" behaves identically to |
| 230 |
.\" .BR TAILQ_FOREACH_REVERSE () |
| 231 |
.\" when |
| 232 |
.\" .I var |
| 233 |
.\" is NULL, else it treats |
| 234 |
.\" .I var |
| 235 |
.\" as a previously found TAILQ element and begins the reverse loop at |
| 236 |
.\" .I var |
| 237 |
.\" instead of the last element in the TAILQ referenced by |
| 238 |
.\" .IR head . |
| 239 |
.\" .P |
| 240 |
.\" .BR TAILQ_FOREACH_SAFE () |
| 241 |
.\" and |
| 242 |
.\" .BR TAILQ_FOREACH_REVERSE_SAFE () |
| 243 |
.\" traverse the list referenced by |
| 244 |
.\" .I head |
| 245 |
.\" in the forward or reverse direction respectively, |
| 246 |
.\" assigning each element in turn to |
| 247 |
.\" .IR var . |
| 248 |
.\" However, unlike their unsafe counterparts, |
| 249 |
.\" .BR TAILQ_FOREACH () |
| 250 |
.\" and |
| 251 |
.\" .BR TAILQ_FOREACH_REVERSE () |
| 252 |
.\" permit to both remove |
| 253 |
.\" .I var |
| 254 |
.\" as well as free it from within the loop safely without interfering with the |
| 255 |
.\" traversal. |
| 256 |
.\" .P |
| 257 |
.\" .BR TAILQ_FOREACH_FROM_SAFE () |
| 258 |
.\" behaves identically to |
| 259 |
.\" .BR TAILQ_FOREACH_SAFE () |
| 260 |
.\" when |
| 261 |
.\" .I var |
| 262 |
.\" is NULL, else it treats |
| 263 |
.\" .I var |
| 264 |
.\" as a previously found TAILQ element and begins the loop at |
| 265 |
.\" .I var |
| 266 |
.\" instead of the first element in the TAILQ referenced by |
| 267 |
.\" .IR head . |
| 268 |
.\" .P |
| 269 |
.\" .BR TAILQ_FOREACH_REVERSE_FROM_SAFE () |
| 270 |
.\" behaves identically to |
| 271 |
.\" .BR TAILQ_FOREACH_REVERSE_SAFE () |
| 272 |
.\" when |
| 273 |
.\" .I var |
| 274 |
.\" is NULL, else it treats |
| 275 |
.\" .I var |
| 276 |
.\" as a previously found TAILQ element and begins the reverse loop at |
| 277 |
.\" .I var |
| 278 |
.\" instead of the last element in the TAILQ referenced by |
| 279 |
.\" .IR head . |
| 280 |
.SS Removal |
| 281 |
.BR TAILQ_REMOVE () |
| 282 |
removes the element |
| 283 |
.I elm |
| 284 |
from the queue. |
| 285 |
.SS Other features |
| 286 |
.\" .BR TAILQ_SWAP () |
| 287 |
.\" swaps the contents of |
| 288 |
.\" .I head1 |
| 289 |
.\" and |
| 290 |
.\" .IR head2 . |
| 291 |
.\" .P |
| 292 |
.BR TAILQ_CONCAT () |
| 293 |
concatenates the queue headed by |
| 294 |
.I head2 |
| 295 |
onto the end of the one headed by |
| 296 |
.I head1 |
| 297 |
removing all entries from the former. |
| 298 |
.SH RETURN VALUE |
| 299 |
.BR TAILQ_EMPTY () |
| 300 |
returns nonzero if the queue is empty, |
| 301 |
and zero if the queue contains at least one entry. |
| 302 |
.P |
| 303 |
.BR TAILQ_FIRST (), |
| 304 |
.BR TAILQ_LAST (), |
| 305 |
.BR TAILQ_PREV (), |
| 306 |
and |
| 307 |
.BR TAILQ_NEXT () |
| 308 |
return a pointer to the first, last, previous, or next |
| 309 |
.I TYPE |
| 310 |
structure, respectively. |
| 311 |
.P |
| 312 |
.BR TAILQ_HEAD_INITIALIZER () |
| 313 |
returns an initializer that can be assigned to the queue |
| 314 |
.IR head . |
| 315 |
.SH STANDARDS |
| 316 |
BSD. |
| 317 |
.SH HISTORY |
| 318 |
4.4BSD. |
| 319 |
.SH CAVEATS |
| 320 |
.BR TAILQ_FOREACH () |
| 321 |
and |
| 322 |
.BR TAILQ_FOREACH_REVERSE () |
| 323 |
don't allow |
| 324 |
.I var |
| 325 |
to be removed or freed within the loop, |
| 326 |
as it would interfere with the traversal. |
| 327 |
.BR TAILQ_FOREACH_SAFE () |
| 328 |
and |
| 329 |
.BR TAILQ_FOREACH_REVERSE_SAFE (), |
| 330 |
which are present on the BSDs but are not present in glibc, |
| 331 |
fix this limitation by allowing |
| 332 |
.I var |
| 333 |
to safely be removed from the list and freed from within the loop |
| 334 |
without interfering with the traversal. |
| 335 |
.SH EXAMPLES |
| 336 |
.\" SRC BEGIN (tailq.c) |
| 337 |
.EX |
| 338 |
#include <stddef.h> |
| 339 |
#include <stdio.h> |
| 340 |
#include <stdlib.h> |
| 341 |
#include <sys/queue.h> |
| 342 |
\& |
| 343 |
struct entry { |
| 344 |
int data; |
| 345 |
TAILQ_ENTRY(entry) entries; /* Tail queue */ |
| 346 |
}; |
| 347 |
\& |
| 348 |
TAILQ_HEAD(tailhead, entry); |
| 349 |
\& |
| 350 |
int |
| 351 |
main(void) |
| 352 |
{ |
| 353 |
struct entry *n1, *n2, *n3, *np; |
| 354 |
struct tailhead head; /* Tail queue head */ |
| 355 |
int i; |
| 356 |
\& |
| 357 |
TAILQ_INIT(&head); /* Initialize the queue */ |
| 358 |
\& |
| 359 |
n1 = malloc(sizeof(struct entry)); /* Insert at the head */ |
| 360 |
TAILQ_INSERT_HEAD(&head, n1, entries); |
| 361 |
\& |
| 362 |
n1 = malloc(sizeof(struct entry)); /* Insert at the tail */ |
| 363 |
TAILQ_INSERT_TAIL(&head, n1, entries); |
| 364 |
\& |
| 365 |
n2 = malloc(sizeof(struct entry)); /* Insert after */ |
| 366 |
TAILQ_INSERT_AFTER(&head, n1, n2, entries); |
| 367 |
\& |
| 368 |
n3 = malloc(sizeof(struct entry)); /* Insert before */ |
| 369 |
TAILQ_INSERT_BEFORE(n2, n3, entries); |
| 370 |
\& |
| 371 |
TAILQ_REMOVE(&head, n2, entries); /* Deletion */ |
| 372 |
free(n2); |
| 373 |
/* Forward traversal */ |
| 374 |
i = 0; |
| 375 |
TAILQ_FOREACH(np, &head, entries) |
| 376 |
np\->data = i++; |
| 377 |
/* Reverse traversal */ |
| 378 |
TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries) |
| 379 |
printf("%i\[rs]n", np\->data); |
| 380 |
/* TailQ deletion */ |
| 381 |
n1 = TAILQ_FIRST(&head); |
| 382 |
while (n1 != NULL) { |
| 383 |
n2 = TAILQ_NEXT(n1, entries); |
| 384 |
free(n1); |
| 385 |
n1 = n2; |
| 386 |
} |
| 387 |
TAILQ_INIT(&head); |
| 388 |
\& |
| 389 |
exit(EXIT_SUCCESS); |
| 390 |
} |
| 391 |
.EE |
| 392 |
.\" SRC END |
| 393 |
.SH SEE ALSO |
| 394 |
.BR insque (3), |
| 395 |
.BR queue (7) |