2 .\" The Regents of the University of California. All rights reserved.
3 .\" and Copyright (c) 2020 by Alejandro Colomar <alx@kernel.org>
5 .\" SPDX-License-Identifier: BSD-3-Clause
8 .TH TAILQ 3 (date) "Linux man-pages (unreleased)"
15 .\"TAILQ_FOREACH_FROM,
16 .\"TAILQ_FOREACH_FROM_SAFE,
17 TAILQ_FOREACH_REVERSE,
18 .\"TAILQ_FOREACH_REVERSE_FROM,
19 .\"TAILQ_FOREACH_REVERSE_FROM_SAFE,
20 .\"TAILQ_FOREACH_REVERSE_SAFE,
21 .\"TAILQ_FOREACH_SAFE,
23 TAILQ_HEAD_INITIALIZER,
34 \- implementation of a doubly linked tail queue
37 .RI ( libc ", " \-lc )
40 .B #include <sys/queue.h>
44 .B TAILQ_HEAD(HEADNAME, TYPE);
45 .BI "TAILQ_HEAD TAILQ_HEAD_INITIALIZER(TAILQ_HEAD " head );
46 .BI "void TAILQ_INIT(TAILQ_HEAD *" head );
48 .BI "int TAILQ_EMPTY(TAILQ_HEAD *" head );
50 .BI "void TAILQ_INSERT_HEAD(TAILQ_HEAD *" head ,
51 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
52 .BI "void TAILQ_INSERT_TAIL(TAILQ_HEAD *" head ,
53 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
54 .BI "void TAILQ_INSERT_BEFORE(struct TYPE *" listelm ,
55 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
56 .BI "void TAILQ_INSERT_AFTER(TAILQ_HEAD *" head ", struct TYPE *" listelm ,
57 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
59 .BI "struct TYPE *TAILQ_FIRST(TAILQ_HEAD *" head );
60 .BI "struct TYPE *TAILQ_LAST(TAILQ_HEAD *" head ", HEADNAME);"
61 .BI "struct TYPE *TAILQ_PREV(struct TYPE *" elm ", HEADNAME, TAILQ_ENTRY " NAME );
62 .BI "struct TYPE *TAILQ_NEXT(struct TYPE *" elm ", TAILQ_ENTRY " NAME );
64 .BI "TAILQ_FOREACH(struct TYPE *" var ", TAILQ_HEAD *" head ,
65 .BI " TAILQ_ENTRY " NAME );
66 .\" .BI "TAILQ_FOREACH_FROM(struct TYPE *" var ", TAILQ_HEAD *" head ,
67 .\" .BI " TAILQ_ENTRY " NAME );
68 .BI "TAILQ_FOREACH_REVERSE(struct TYPE *" var ", TAILQ_HEAD *" head ", HEADNAME,"
69 .BI " TAILQ_ENTRY " NAME );
70 .\" .BI "TAILQ_FOREACH_REVERSE_FROM(struct TYPE *" var ", TAILQ_HEAD *" head ", HEADNAME,"
71 .\" .BI " TAILQ_ENTRY " NAME );
73 .\" .BI "TAILQ_FOREACH_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
74 .\" .BI " TAILQ_ENTRY " NAME ,
75 .\" .BI " struct TYPE *" temp_var );
76 .\" .BI "TAILQ_FOREACH_FROM_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
77 .\" .BI " TAILQ_ENTRY " NAME ,
78 .\" .BI " struct TYPE *" temp_var );
79 .\" .BI "TAILQ_FOREACH_REVERSE_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
80 .\" .BI " HEADNAME, TAILQ_ENTRY " NAME ,
81 .\" .BI " struct TYPE *" temp_var );
82 .\" .BI "TAILQ_FOREACH_REVERSE_FROM_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
83 .\" .BI " HEADNAME, TAILQ_ENTRY " NAME ,
84 .\" .BI " struct TYPE *" temp_var );
86 .BI "void TAILQ_REMOVE(TAILQ_HEAD *" head ", struct TYPE *" elm ,
87 .BI " TAILQ_ENTRY " NAME );
89 .BI "void TAILQ_CONCAT(TAILQ_HEAD *" head1 ", TAILQ_HEAD *" head2 ,
90 .BI " TAILQ_ENTRY " NAME );
91 .\" .BI "void TAILQ_SWAP(TAILQ_HEAD *" head1 ", TAILQ_HEAD *" head2 ", TYPE,"
92 .\" .BI " TAILQ_ENTRY " NAME );
95 These macros define and operate on doubly linked tail queues.
97 In the macro definitions,
99 is the name of a user defined structure,
100 that must contain a field of type
106 is the name of a user defined structure that must be declared
110 A tail queue is headed by a structure defined by the
113 This structure contains a pair of pointers,
114 one to the first element in the queue
115 and the other to the last element in the queue.
116 The elements are doubly linked
117 so that an arbitrary element can be removed without traversing the queue.
118 New elements can be added to the queue
119 after an existing element,
120 before an existing element,
121 at the head of the queue,
122 or at the end of the queue.
125 structure is declared as follows:
129 TAILQ_HEAD(HEADNAME, TYPE) head;
135 is the structure to be defined, and
137 is the type of the elements to be linked into the queue.
138 A pointer to the head of the queue can later be declared as:
142 struct HEADNAME *headp;
150 are user selectable.)
153 declares a structure that connects the elements in the queue.
155 .BR TAILQ_HEAD_INITIALIZER ()
156 evaluates to an initializer for the queue
160 initializes the queue referenced by
163 evaluates to true if there are no items on the queue.
166 .BR TAILQ_INSERT_HEAD ()
167 inserts the new element
169 at the head of the queue.
171 .BR TAILQ_INSERT_TAIL ()
172 inserts the new element
174 at the end of the queue.
176 .BR TAILQ_INSERT_BEFORE ()
177 inserts the new element
182 .BR TAILQ_INSERT_AFTER ()
183 inserts the new element
189 returns the first item on the queue, or NULL if the queue is empty.
192 returns the last item on the queue.
193 If the queue is empty the return value is NULL.
196 returns the previous item on the queue, or NULL if this item is the first.
199 returns the next item on the queue, or NULL if this item is the last.
202 traverses the queue referenced by
204 in the forward direction,
205 assigning each element in turn to
208 is set to NULL if the loop completes normally,
209 or if there were no elements.
211 .\" .BR TAILQ_FOREACH_FROM ()
212 .\" behaves identically to
213 .\" .BR TAILQ_FOREACH ()
216 .\" is NULL, else it treats
218 .\" as a previously found TAILQ element and begins the loop at
220 .\" instead of the first element in the TAILQ referenced by
223 .BR TAILQ_FOREACH_REVERSE ()
224 traverses the queue referenced by
226 in the reverse direction,
227 assigning each element in turn to
230 .\" .BR TAILQ_FOREACH_REVERSE_FROM ()
231 .\" behaves identically to
232 .\" .BR TAILQ_FOREACH_REVERSE ()
235 .\" is NULL, else it treats
237 .\" as a previously found TAILQ element and begins the reverse loop at
239 .\" instead of the last element in the TAILQ referenced by
242 .\" .BR TAILQ_FOREACH_SAFE ()
244 .\" .BR TAILQ_FOREACH_REVERSE_SAFE ()
245 .\" traverse the list referenced by
247 .\" in the forward or reverse direction respectively,
248 .\" assigning each element in turn to
250 .\" However, unlike their unsafe counterparts,
251 .\" .BR TAILQ_FOREACH ()
253 .\" .BR TAILQ_FOREACH_REVERSE ()
254 .\" permit to both remove
256 .\" as well as free it from within the loop safely without interfering with the
259 .\" .BR TAILQ_FOREACH_FROM_SAFE ()
260 .\" behaves identically to
261 .\" .BR TAILQ_FOREACH_SAFE ()
264 .\" is NULL, else it treats
266 .\" as a previously found TAILQ element and begins the loop at
268 .\" instead of the first element in the TAILQ referenced by
271 .\" .BR TAILQ_FOREACH_REVERSE_FROM_SAFE ()
272 .\" behaves identically to
273 .\" .BR TAILQ_FOREACH_REVERSE_SAFE ()
276 .\" is NULL, else it treats
278 .\" as a previously found TAILQ element and begins the reverse loop at
280 .\" instead of the last element in the TAILQ referenced by
288 .\" .BR TAILQ_SWAP ()
289 .\" swaps the contents of
295 concatenates the queue headed by
297 onto the end of the one headed by
299 removing all entries from the former.
302 returns nonzero if the queue is empty,
303 and zero if the queue contains at least one entry.
310 return a pointer to the first, last, previous, or next
312 structure, respectively.
314 .BR TAILQ_HEAD_INITIALIZER ()
315 returns an initializer that can be assigned to the queue
318 Not in POSIX.1, POSIX.1-2001, or POSIX.1-2008.
320 (TAILQ functions first appeared in 4.4BSD).
324 .BR TAILQ_FOREACH_REVERSE ()
327 to be removed or freed within the loop,
328 as it would interfere with the traversal.
329 .BR TAILQ_FOREACH_SAFE ()
331 .BR TAILQ_FOREACH_REVERSE_SAFE (),
332 which are present on the BSDs but are not present in glibc,
333 fix this limitation by allowing
335 to safely be removed from the list and freed from within the loop
336 without interfering with the traversal.
338 .\" SRC BEGIN (tailq.c)
343 #include <sys/queue.h>
347 TAILQ_ENTRY(entry) entries; /* Tail queue */
350 TAILQ_HEAD(tailhead, entry);
355 struct entry *n1, *n2, *n3, *np;
356 struct tailhead head; /* Tail queue head */
359 TAILQ_INIT(&head); /* Initialize the queue */
361 n1 = malloc(sizeof(struct entry)); /* Insert at the head */
362 TAILQ_INSERT_HEAD(&head, n1, entries);
364 n1 = malloc(sizeof(struct entry)); /* Insert at the tail */
365 TAILQ_INSERT_TAIL(&head, n1, entries);
367 n2 = malloc(sizeof(struct entry)); /* Insert after */
368 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
370 n3 = malloc(sizeof(struct entry)); /* Insert before */
371 TAILQ_INSERT_BEFORE(n2, n3, entries);
373 TAILQ_REMOVE(&head, n2, entries); /* Deletion */
375 /* Forward traversal */
377 TAILQ_FOREACH(np, &head, entries)
379 /* Reverse traversal */
380 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
381 printf("%i\en", np\->data);
383 n1 = TAILQ_FIRST(&head);
385 n2 = TAILQ_NEXT(n1, entries);