2 .\" The Regents of the University of California. All rights reserved.
3 .\" and Copyright (c) 2020 by Alejandro Colomar <colomar.6.4.3@gmail.com>
5 .\" %%%LICENSE_START(BSD_3_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .TH TAILQ 3 2021-03-22 "GNU" "Linux Programmer's Manual"
39 .\"TAILQ_FOREACH_FROM,
40 .\"TAILQ_FOREACH_FROM_SAFE,
41 TAILQ_FOREACH_REVERSE,
42 .\"TAILQ_FOREACH_REVERSE_FROM,
43 .\"TAILQ_FOREACH_REVERSE_FROM_SAFE,
44 .\"TAILQ_FOREACH_REVERSE_SAFE,
45 .\"TAILQ_FOREACH_SAFE,
47 TAILQ_HEAD_INITIALIZER,
58 \- implementation of a doubly linked tail queue
61 .B #include <sys/queue.h>
65 .B TAILQ_HEAD(HEADNAME, TYPE);
66 .BI "TAILQ_HEAD TAILQ_HEAD_INITIALIZER(TAILQ_HEAD " head );
67 .BI "void TAILQ_INIT(TAILQ_HEAD *" head );
69 .BI "int TAILQ_EMPTY(TAILQ_HEAD *" head );
71 .BI "void TAILQ_INSERT_HEAD(TAILQ_HEAD *" head ,
72 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
73 .BI "void TAILQ_INSERT_TAIL(TAILQ_HEAD *" head ,
74 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
75 .BI "void TAILQ_INSERT_BEFORE(struct TYPE *" listelm ,
76 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
77 .BI "void TAILQ_INSERT_AFTER(TAILQ_HEAD *" head ", struct TYPE *" listelm ,
78 .BI " struct TYPE *" elm ", TAILQ_ENTRY " NAME );
80 .BI "struct TYPE *TAILQ_FIRST(TAILQ_HEAD *" head );
81 .BI "struct TYPE *TAILQ_LAST(TAILQ_HEAD *" head ", HEADNAME);"
82 .BI "struct TYPE *TAILQ_PREV(struct TYPE *" elm ", HEADNAME, TAILQ_ENTRY " NAME );
83 .BI "struct TYPE *TAILQ_NEXT(struct TYPE *" elm ", TAILQ_ENTRY " NAME );
85 .BI "TAILQ_FOREACH(struct TYPE *" var ", TAILQ_HEAD *" head ,
86 .BI " TAILQ_ENTRY " NAME );
87 .\" .BI "TAILQ_FOREACH_FROM(struct TYPE *" var ", TAILQ_HEAD *" head ,
88 .\" .BI " TAILQ_ENTRY " NAME );
89 .BI "TAILQ_FOREACH_REVERSE(struct TYPE *" var ", TAILQ_HEAD *" head ", HEADNAME,"
90 .BI " TAILQ_ENTRY " NAME );
91 .\" .BI "TAILQ_FOREACH_REVERSE_FROM(struct TYPE *" var ", TAILQ_HEAD *" head ", HEADNAME,"
92 .\" .BI " TAILQ_ENTRY " NAME );
94 .\" .BI "TAILQ_FOREACH_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
95 .\" .BI " TAILQ_ENTRY " NAME ,
96 .\" .BI " struct TYPE *" temp_var );
97 .\" .BI "TAILQ_FOREACH_FROM_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
98 .\" .BI " TAILQ_ENTRY " NAME ,
99 .\" .BI " struct TYPE *" temp_var );
100 .\" .BI "TAILQ_FOREACH_REVERSE_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
101 .\" .BI " HEADNAME, TAILQ_ENTRY " NAME ,
102 .\" .BI " struct TYPE *" temp_var );
103 .\" .BI "TAILQ_FOREACH_REVERSE_FROM_SAFE(struct TYPE *" var ", TAILQ_HEAD *" head ,
104 .\" .BI " HEADNAME, TAILQ_ENTRY " NAME ,
105 .\" .BI " struct TYPE *" temp_var );
107 .BI "void TAILQ_REMOVE(TAILQ_HEAD *" head ", struct TYPE *" elm ,
108 .BI " TAILQ_ENTRY " NAME );
110 .BI "void TAILQ_CONCAT(TAILQ_HEAD *" head1 ", TAILQ_HEAD *" head2 ,
111 .BI " TAILQ_ENTRY " NAME );
112 .\" .BI "void TAILQ_SWAP(TAILQ_HEAD *" head1 ", TAILQ_HEAD *" head2 ", TYPE,"
113 .\" .BI " TAILQ_ENTRY " NAME );
116 These macros define and operate on doubly linked tail queues.
118 In the macro definitions,
120 is the name of a user defined structure,
121 that must contain a field of type
127 is the name of a user defined structure that must be declared
131 A tail queue is headed by a structure defined by the
134 This structure contains a pair of pointers,
135 one to the first element in the queue
136 and the other to the last element in the queue.
137 The elements are doubly linked
138 so that an arbitrary element can be removed without traversing the queue.
139 New elements can be added to the queue
140 after an existing element,
141 before an existing element,
142 at the head of the queue,
143 or at the end of the queue.
146 structure is declared as follows:
150 TAILQ_HEAD(HEADNAME, TYPE) head;
156 is the structure to be defined, and
158 is the type of the elements to be linked into the queue.
159 A pointer to the head of the queue can later be declared as:
163 struct HEADNAME *headp;
171 are user selectable.)
174 declares a structure that connects the elements in the queue.
176 .BR TAILQ_HEAD_INITIALIZER ()
177 evaluates to an initializer for the queue
181 initializes the queue referenced by
184 evaluates to true if there are no items on the queue.
187 .BR TAILQ_INSERT_HEAD ()
188 inserts the new element
190 at the head of the queue.
192 .BR TAILQ_INSERT_TAIL ()
193 inserts the new element
195 at the end of the queue.
197 .BR TAILQ_INSERT_BEFORE ()
198 inserts the new element
203 .BR TAILQ_INSERT_AFTER ()
204 inserts the new element
210 returns the first item on the queue, or NULL if the queue is empty.
213 returns the last item on the queue.
214 If the queue is empty the return value is NULL.
217 returns the previous item on the queue, or NULL if this item is the first.
220 returns the next item on the queue, or NULL if this item is the last.
223 traverses the queue referenced by
225 in the forward direction,
226 assigning each element in turn to
229 is set to NULL if the loop completes normally,
230 or if there were no elements.
232 .\" .BR TAILQ_FOREACH_FROM ()
233 .\" behaves identically to
234 .\" .BR TAILQ_FOREACH ()
237 .\" is NULL, else it treats
239 .\" as a previously found TAILQ element and begins the loop at
241 .\" instead of the first element in the TAILQ referenced by
244 .BR TAILQ_FOREACH_REVERSE ()
245 traverses the queue referenced by
247 in the reverse direction,
248 assigning each element in turn to
251 .\" .BR TAILQ_FOREACH_REVERSE_FROM ()
252 .\" behaves identically to
253 .\" .BR TAILQ_FOREACH_REVERSE ()
256 .\" is NULL, else it treats
258 .\" as a previously found TAILQ element and begins the reverse loop at
260 .\" instead of the last element in the TAILQ referenced by
263 .\" .BR TAILQ_FOREACH_SAFE ()
265 .\" .BR TAILQ_FOREACH_REVERSE_SAFE ()
266 .\" traverse the list referenced by
268 .\" in the forward or reverse direction respectively,
269 .\" assigning each element in turn to
271 .\" However, unlike their unsafe counterparts,
272 .\" .BR TAILQ_FOREACH ()
274 .\" .BR TAILQ_FOREACH_REVERSE ()
275 .\" permit to both remove
277 .\" as well as free it from within the loop safely without interfering with the
280 .\" .BR TAILQ_FOREACH_FROM_SAFE ()
281 .\" behaves identically to
282 .\" .BR TAILQ_FOREACH_SAFE ()
285 .\" is NULL, else it treats
287 .\" as a previously found TAILQ element and begins the loop at
289 .\" instead of the first element in the TAILQ referenced by
292 .\" .BR TAILQ_FOREACH_REVERSE_FROM_SAFE ()
293 .\" behaves identically to
294 .\" .BR TAILQ_FOREACH_REVERSE_SAFE ()
297 .\" is NULL, else it treats
299 .\" as a previously found TAILQ element and begins the reverse loop at
301 .\" instead of the last element in the TAILQ referenced by
309 .\" .BR TAILQ_SWAP ()
310 .\" swaps the contents of
316 concatenates the queue headed by
318 onto the end of the one headed by
320 removing all entries from the former.
323 returns nonzero if the queue is empty,
324 and zero if the queue contains at least one entry.
331 return a pointer to the first, last, previous, or next
333 structure, respectively.
335 .BR TAILQ_HEAD_INITIALIZER ()
336 returns an initializer that can be assigned to the queue
339 Not in POSIX.1, POSIX.1-2001, or POSIX.1-2008.
341 (TAILQ functions first appeared in 4.4BSD).
345 .BR TAILQ_FOREACH_REVERSE ()
348 to be removed or freed within the loop,
349 as it would interfere with the traversal.
350 .BR TAILQ_FOREACH_SAFE ()
352 .BR TAILQ_FOREACH_REVERSE_SAFE (),
353 which are present on the BSDs but are not present in glibc,
354 fix this limitation by allowing
356 to safely be removed from the list and freed from within the loop
357 without interfering with the traversal.
363 #include <sys/queue.h>
367 TAILQ_ENTRY(entry) entries; /* Tail queue */
370 TAILQ_HEAD(tailhead, entry);
375 struct entry *n1, *n2, *n3, *np;
376 struct tailhead head; /* Tail queue head */
379 TAILQ_INIT(&head); /* Initialize the queue */
381 n1 = malloc(sizeof(struct entry)); /* Insert at the head */
382 TAILQ_INSERT_HEAD(&head, n1, entries);
384 n1 = malloc(sizeof(struct entry)); /* Insert at the tail */
385 TAILQ_INSERT_TAIL(&head, n1, entries);
387 n2 = malloc(sizeof(struct entry)); /* Insert after */
388 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
390 n3 = malloc(sizeof(struct entry)); /* Insert before */
391 TAILQ_INSERT_BEFORE(n2, n3, entries);
393 TAILQ_REMOVE(&head, n2, entries); /* Deletion */
395 /* Forward traversal */
397 TAILQ_FOREACH(np, &head, entries)
399 /* Reverse traversal */
400 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
401 printf("%i\en", np\->data);
403 n1 = TAILQ_FIRST(&head);
405 n2 = TAILQ_NEXT(n1, entries);