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 STAILQ 3 2021-03-22 "GNU" "Linux Programmer's Manual"
39 .\"SIMPLEQ_FOREACH_FROM,
40 .\"SIMPLEQ_FOREACH_FROM_SAFE,
41 .\"SIMPLEQ_FOREACH_SAFE,
43 SIMPLEQ_HEAD_INITIALIZER,
51 .\"SIMPLEQ_REMOVE_AFTER,
59 .\"STAILQ_FOREACH_FROM,
60 .\"STAILQ_FOREACH_FROM_SAFE,
61 .\"STAILQ_FOREACH_SAFE,
63 STAILQ_HEAD_INITIALIZER,
71 .\"STAILQ_REMOVE_AFTER,
74 \- implementation of a singly linked tail queue
77 .B #include <sys/queue.h>
79 .B STAILQ_ENTRY(TYPE);
81 .B STAILQ_HEAD(HEADNAME, TYPE);
82 .BI "STAILQ_HEAD STAILQ_HEAD_INITIALIZER(STAILQ_HEAD " head );
83 .BI "void STAILQ_INIT(STAILQ_HEAD *" head );
85 .BI "int STAILQ_EMPTY(STAILQ_HEAD *" head );
87 .BI "void STAILQ_INSERT_HEAD(STAILQ_HEAD *" head ,
88 .BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME );
89 .BI "void STAILQ_INSERT_TAIL(STAILQ_HEAD *" head ,
90 .BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME );
91 .BI "void STAILQ_INSERT_AFTER(STAILQ_HEAD *" head ", struct TYPE *" listelm ,
92 .BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME );
94 .BI "struct TYPE *STAILQ_FIRST(STAILQ_HEAD *" head );
95 .\" .BI "struct TYPE *STAILQ_LAST(STAILQ_HEAD *" head ", struct TYPE *" elm ,
96 .\" .BI " STAILQ_ENTRY " NAME );
97 .BI "struct TYPE *STAILQ_NEXT(struct TYPE *" elm ", STAILQ_ENTRY " NAME );
99 .BI "STAILQ_FOREACH(struct TYPE *" var ", STAILQ_HEAD *" head ", STAILQ_ENTRY " NAME );
100 .\" .BI "STAILQ_FOREACH_FROM(struct TYPE *" var ", STAILQ_HEAD *" head ,
101 .\" .BI " STAILQ_ENTRY " NAME );
103 .\" .BI "STAILQ_FOREACH_SAFE(struct TYPE *" var ", STAILQ_HEAD *" head ,
104 .\" .BI " STAILQ_ENTRY " NAME ", struct TYPE *" temp_var );
105 .\" .BI "STAILQ_FOREACH_FROM_SAFE(struct TYPE *" var ", STAILQ_HEAD *" head ,
106 .\" .BI " STAILQ_ENTRY " NAME ", struct TYPE *" temp_var );
108 .BI "void STAILQ_REMOVE(STAILQ_HEAD *" head ", struct TYPE *" elm ", TYPE,"
109 .BI " STAILQ_ENTRY " NAME );
110 .BI "void STAILQ_REMOVE_HEAD(STAILQ_HEAD *" head ,
111 .BI " STAILQ_ENTRY " NAME );
112 .\" .BI "void STAILQ_REMOVE_AFTER(STAILQ_HEAD *" head ", struct TYPE *" elm ,
113 .\" .BI " STAILQ_ENTRY " NAME );
115 .BI "void STAILQ_CONCAT(STAILQ_HEAD *" head1 ", STAILQ_HEAD *" head2 );
116 .\" .BI "void STAILQ_SWAP(STAILQ_HEAD *" head1 ", STAILQ_HEAD *" head2 ,
117 .\" .BI " STAILQ_ENTRY " NAME );
120 Identical macros prefixed with SIMPLEQ instead of STAILQ exist; see NOTES.
122 These macros define and operate on singly linked tail queues.
124 In the macro definitions,
126 is the name of a user-defined structure,
127 that must contain a field of type
133 is the name of a user-defined structure that must be declared
137 A singly linked tail queue is headed by a structure defined by the
140 This structure contains a pair of pointers,
141 one to the first element in the tail queue and the other to
142 the last element in the tail queue.
143 The elements are singly linked for minimum space and pointer
144 manipulation overhead at the expense of O(n) removal for arbitrary elements.
145 New elements can be added to the tail queue after an existing element,
146 at the head of the tail queue, or at the end of the tail queue.
149 structure is declared as follows:
153 STAILQ_HEAD(HEADNAME, TYPE) head;
159 is the structure to be defined, and
161 is the type of the elements to be linked into the tail queue.
162 A pointer to the head of the tail queue can later be declared as:
166 struct HEADNAME *headp;
174 are user selectable.)
177 declares a structure that connects the elements in the tail queue.
179 .BR STAILQ_HEAD_INITIALIZER ()
180 evaluates to an initializer for the tail queue
184 initializes the tail queue referenced by
188 evaluates to true if there are no items on the tail queue.
190 .BR STAILQ_INSERT_HEAD ()
191 inserts the new element
193 at the head of the tail queue.
195 .BR STAILQ_INSERT_TAIL ()
196 inserts the new element
198 at the end of the tail queue.
200 .BR STAILQ_INSERT_AFTER ()
201 inserts the new element
207 returns the first item on the tail queue or NULL if the tail queue is empty.
209 .\" .BR STAILQ_LAST ()
210 .\" returns the last item on the tail queue.
211 .\" If the tail queue is empty the return value is NULL .
214 returns the next item on the tail queue, or NULL this item is the last.
216 .BR STAILQ_FOREACH ()
217 traverses the tail queue referenced by
219 in the forward direction,
220 assigning each element in turn to
223 .\" .BR STAILQ_FOREACH_FROM ()
224 .\" behaves identically to
225 .\" .BR STAILQ_FOREACH ()
228 .\" is NULL, else it treats
230 .\" as a previously found STAILQ element and begins the loop at
232 .\" instead of the first element in the STAILQ referenced by
235 .\" .BR STAILQ_FOREACH_SAFE ()
236 .\" traverses the tail queue referenced by
238 .\" in the forward direction, assigning each element
242 .\" .BR STAILQ_FOREACH ()
243 .\" here it is permitted to both remove
245 .\" as well as free it from within the loop safely without interfering with the
248 .\" .BR STAILQ_FOREACH_FROM_SAFE ()
249 .\" behaves identically to
250 .\" .BR STAILQ_FOREACH_SAFE ()
253 .\" is NULL, else it treats
255 .\" as a previously found STAILQ element and begins the loop at
257 .\" instead of the first element in the STAILQ referenced by
265 .BR STAILQ_REMOVE_HEAD ()
266 removes the element at the head of the tail queue.
267 For optimum efficiency,
268 elements being removed from the head of the tail queue should
269 use this macro explicitly rather than the generic
273 .\" .BR STAILQ_REMOVE_AFTER ()
274 .\" removes the element after
276 .\" from the tail queue.
278 .\" .BR STAILQ_REMOVE (),
279 .\" this macro does not traverse the entire tail queue.
282 concatenates the tail queue headed by
284 onto the end of the one headed by
286 removing all entries from the former.
288 .\" .BR STAILQ_SWAP ()
289 .\" swaps the contents of
295 returns nonzero if the queue is empty,
296 and zero if the queue contains at least one entry.
301 return a pointer to the first or next
303 structure, respectively.
305 .BR STAILQ_HEAD_INITIALIZER ()
306 returns an initializer that can be assigned to the queue
309 Not in POSIX.1, POSIX.1-2001, or POSIX.1-2008.
311 (STAILQ macros first appeared in 4.4BSD).
313 .BR STAILQ_FOREACH ()
316 to be removed or freed within the loop,
317 as it would interfere with the traversal.
318 .BR STAILQ_FOREACH_SAFE (),
319 which is present on the BSDs but is not present in glibc,
320 fixes this limitation by allowing
322 to safely be removed from the list and freed from within the loop
323 without interfering with the traversal.
325 Some BSDs provide SIMPLEQ instead of STAILQ.
326 They are identical, but for historical reasons
327 they were named differently on different BSDs.
328 STAILQ originated on FreeBSD, and SIMPLEQ originated on NetBSD.
329 For compatibility reasons, some systems provide both sets of macros.
330 Glibc provides both STAILQ and SIMPLEQ,
331 which are identical except for a missing SIMPLEQ equivalent to
332 .BR STAILQ_CONCAT ().
338 #include <sys/queue.h>
342 STAILQ_ENTRY(entry) entries; /* Singly linked tail queue */
345 STAILQ_HEAD(stailhead, entry);
350 struct entry *n1, *n2, *n3, *np;
351 struct stailhead head; /* Singly linked tail queue
354 STAILQ_INIT(&head); /* Initialize the queue */
356 n1 = malloc(sizeof(struct entry)); /* Insert at the head */
357 STAILQ_INSERT_HEAD(&head, n1, entries);
359 n1 = malloc(sizeof(struct entry)); /* Insert at the tail */
360 STAILQ_INSERT_TAIL(&head, n1, entries);
362 n2 = malloc(sizeof(struct entry)); /* Insert after */
363 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
365 STAILQ_REMOVE(&head, n2, entry, entries); /* Deletion */
368 n3 = STAILQ_FIRST(&head);
369 STAILQ_REMOVE_HEAD(&head, entries); /* Deletion from the head */
372 n1 = STAILQ_FIRST(&head);
374 for (int i = 1; i < 5; i++) {
375 n1 = malloc(sizeof(struct entry));
376 STAILQ_INSERT_HEAD(&head, n1, entries);
379 /* Forward traversal */
380 STAILQ_FOREACH(np, &head, entries)
381 printf("%i\en", np\->data);
383 n1 = STAILQ_FIRST(&head);
385 n2 = STAILQ_NEXT(n1, entries);