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 STAILQ 3 (date) "Linux man-pages (unreleased)"
15 .\"SIMPLEQ_FOREACH_FROM,
16 .\"SIMPLEQ_FOREACH_FROM_SAFE,
17 .\"SIMPLEQ_FOREACH_SAFE,
19 SIMPLEQ_HEAD_INITIALIZER,
27 .\"SIMPLEQ_REMOVE_AFTER,
35 .\"STAILQ_FOREACH_FROM,
36 .\"STAILQ_FOREACH_FROM_SAFE,
37 .\"STAILQ_FOREACH_SAFE,
39 STAILQ_HEAD_INITIALIZER,
47 .\"STAILQ_REMOVE_AFTER,
50 \- implementation of a singly linked tail queue
53 .RI ( libc ", " \-lc )
56 .B #include <sys/queue.h>
58 .B STAILQ_ENTRY(TYPE);
60 .B STAILQ_HEAD(HEADNAME, TYPE);
61 .BI "STAILQ_HEAD STAILQ_HEAD_INITIALIZER(STAILQ_HEAD " head );
62 .BI "void STAILQ_INIT(STAILQ_HEAD *" head );
64 .BI "int STAILQ_EMPTY(STAILQ_HEAD *" head );
66 .BI "void STAILQ_INSERT_HEAD(STAILQ_HEAD *" head ,
67 .BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME );
68 .BI "void STAILQ_INSERT_TAIL(STAILQ_HEAD *" head ,
69 .BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME );
70 .BI "void STAILQ_INSERT_AFTER(STAILQ_HEAD *" head ", struct TYPE *" listelm ,
71 .BI " struct TYPE *" elm ", STAILQ_ENTRY " NAME );
73 .BI "struct TYPE *STAILQ_FIRST(STAILQ_HEAD *" head );
74 .\" .BI "struct TYPE *STAILQ_LAST(STAILQ_HEAD *" head ", struct TYPE *" elm ,
75 .\" .BI " STAILQ_ENTRY " NAME );
76 .BI "struct TYPE *STAILQ_NEXT(struct TYPE *" elm ", STAILQ_ENTRY " NAME );
78 .BI "STAILQ_FOREACH(struct TYPE *" var ", STAILQ_HEAD *" head ", STAILQ_ENTRY " NAME );
79 .\" .BI "STAILQ_FOREACH_FROM(struct TYPE *" var ", STAILQ_HEAD *" head ,
80 .\" .BI " STAILQ_ENTRY " NAME );
82 .\" .BI "STAILQ_FOREACH_SAFE(struct TYPE *" var ", STAILQ_HEAD *" head ,
83 .\" .BI " STAILQ_ENTRY " NAME ", struct TYPE *" temp_var );
84 .\" .BI "STAILQ_FOREACH_FROM_SAFE(struct TYPE *" var ", STAILQ_HEAD *" head ,
85 .\" .BI " STAILQ_ENTRY " NAME ", struct TYPE *" temp_var );
87 .BI "void STAILQ_REMOVE(STAILQ_HEAD *" head ", struct TYPE *" elm ", TYPE,"
88 .BI " STAILQ_ENTRY " NAME );
89 .BI "void STAILQ_REMOVE_HEAD(STAILQ_HEAD *" head ,
90 .BI " STAILQ_ENTRY " NAME );
91 .\" .BI "void STAILQ_REMOVE_AFTER(STAILQ_HEAD *" head ", struct TYPE *" elm ,
92 .\" .BI " STAILQ_ENTRY " NAME );
94 .BI "void STAILQ_CONCAT(STAILQ_HEAD *" head1 ", STAILQ_HEAD *" head2 );
95 .\" .BI "void STAILQ_SWAP(STAILQ_HEAD *" head1 ", STAILQ_HEAD *" head2 ,
96 .\" .BI " STAILQ_ENTRY " NAME );
99 Identical macros prefixed with SIMPLEQ instead of STAILQ exist; see NOTES.
101 These macros define and operate on singly linked tail queues.
103 In the macro definitions,
105 is the name of a user-defined structure,
106 that must contain a field of type
112 is the name of a user-defined structure that must be declared
116 A singly linked tail queue is headed by a structure defined by the
119 This structure contains a pair of pointers,
120 one to the first element in the tail queue and the other to
121 the last element in the tail queue.
122 The elements are singly linked for minimum space and pointer
123 manipulation overhead at the expense of O(n) removal for arbitrary elements.
124 New elements can be added to the tail queue after an existing element,
125 at the head of the tail queue, or at the end of the tail queue.
128 structure is declared as follows:
132 STAILQ_HEAD(HEADNAME, TYPE) head;
138 is the structure to be defined, and
140 is the type of the elements to be linked into the tail queue.
141 A pointer to the head of the tail queue can later be declared as:
145 struct HEADNAME *headp;
153 are user selectable.)
156 declares a structure that connects the elements in the tail queue.
158 .BR STAILQ_HEAD_INITIALIZER ()
159 evaluates to an initializer for the tail queue
163 initializes the tail queue referenced by
167 evaluates to true if there are no items on the tail queue.
169 .BR STAILQ_INSERT_HEAD ()
170 inserts the new element
172 at the head of the tail queue.
174 .BR STAILQ_INSERT_TAIL ()
175 inserts the new element
177 at the end of the tail queue.
179 .BR STAILQ_INSERT_AFTER ()
180 inserts the new element
186 returns the first item on the tail queue or NULL if the tail queue is empty.
188 .\" .BR STAILQ_LAST ()
189 .\" returns the last item on the tail queue.
190 .\" If the tail queue is empty the return value is NULL .
193 returns the next item on the tail queue, or NULL this item is the last.
195 .BR STAILQ_FOREACH ()
196 traverses the tail queue referenced by
198 in the forward direction,
199 assigning each element in turn to
202 .\" .BR STAILQ_FOREACH_FROM ()
203 .\" behaves identically to
204 .\" .BR STAILQ_FOREACH ()
207 .\" is NULL, else it treats
209 .\" as a previously found STAILQ element and begins the loop at
211 .\" instead of the first element in the STAILQ referenced by
214 .\" .BR STAILQ_FOREACH_SAFE ()
215 .\" traverses the tail queue referenced by
217 .\" in the forward direction, assigning each element
221 .\" .BR STAILQ_FOREACH ()
222 .\" here it is permitted to both remove
224 .\" as well as free it from within the loop safely without interfering with the
227 .\" .BR STAILQ_FOREACH_FROM_SAFE ()
228 .\" behaves identically to
229 .\" .BR STAILQ_FOREACH_SAFE ()
232 .\" is NULL, else it treats
234 .\" as a previously found STAILQ element and begins the loop at
236 .\" instead of the first element in the STAILQ referenced by
244 .BR STAILQ_REMOVE_HEAD ()
245 removes the element at the head of the tail queue.
246 For optimum efficiency,
247 elements being removed from the head of the tail queue should
248 use this macro explicitly rather than the generic
252 .\" .BR STAILQ_REMOVE_AFTER ()
253 .\" removes the element after
255 .\" from the tail queue.
257 .\" .BR STAILQ_REMOVE (),
258 .\" this macro does not traverse the entire tail queue.
261 concatenates the tail queue headed by
263 onto the end of the one headed by
265 removing all entries from the former.
267 .\" .BR STAILQ_SWAP ()
268 .\" swaps the contents of
274 returns nonzero if the queue is empty,
275 and zero if the queue contains at least one entry.
280 return a pointer to the first or next
282 structure, respectively.
284 .BR STAILQ_HEAD_INITIALIZER ()
285 returns an initializer that can be assigned to the queue
288 Some BSDs provide SIMPLEQ instead of STAILQ.
289 They are identical, but for historical reasons
290 they were named differently on different BSDs.
291 STAILQ originated on FreeBSD, and SIMPLEQ originated on NetBSD.
292 For compatibility reasons, some systems provide both sets of macros.
293 glibc provides both STAILQ and SIMPLEQ,
294 which are identical except for a missing SIMPLEQ equivalent to
295 .BR STAILQ_CONCAT ().
297 .BR STAILQ_FOREACH ()
300 to be removed or freed within the loop,
301 as it would interfere with the traversal.
302 .BR STAILQ_FOREACH_SAFE (),
303 which is present on the BSDs but is not present in glibc,
304 fixes this limitation by allowing
306 to safely be removed from the list and freed from within the loop
307 without interfering with the traversal.
313 .\" SRC BEGIN (stailq.c)
318 #include <sys/queue.h>
322 STAILQ_ENTRY(entry) entries; /* Singly linked tail queue */
325 STAILQ_HEAD(stailhead, entry);
330 struct entry *n1, *n2, *n3, *np;
331 struct stailhead head; /* Singly linked tail queue
334 STAILQ_INIT(&head); /* Initialize the queue */
336 n1 = malloc(sizeof(struct entry)); /* Insert at the head */
337 STAILQ_INSERT_HEAD(&head, n1, entries);
339 n1 = malloc(sizeof(struct entry)); /* Insert at the tail */
340 STAILQ_INSERT_TAIL(&head, n1, entries);
342 n2 = malloc(sizeof(struct entry)); /* Insert after */
343 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
345 STAILQ_REMOVE(&head, n2, entry, entries); /* Deletion */
348 n3 = STAILQ_FIRST(&head);
349 STAILQ_REMOVE_HEAD(&head, entries); /* Deletion from the head */
352 n1 = STAILQ_FIRST(&head);
354 for (unsigned int i = 1; i < 5; i++) {
355 n1 = malloc(sizeof(struct entry));
356 STAILQ_INSERT_HEAD(&head, n1, entries);
359 /* Forward traversal */
360 STAILQ_FOREACH(np, &head, entries)
361 printf("%i\en", np\->data);
363 n1 = STAILQ_FIRST(&head);
365 n2 = STAILQ_NEXT(n1, entries);