vdso.7: Minor tweak to Alejandro Colomar's patch
[man-pages.git] / man3 / stailq.3
blob085b7aeef786b39ae495335954a736b2e8362440
1 .\" Copyright (c) 1993
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>
4 .\"
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
8 .\" are met:
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.
17 .\"
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
28 .\" SUCH DAMAGE.
29 .\" %%%LICENSE_END
30 .\"
31 .\"
32 .TH STAILQ 3 2021-03-22 "GNU" "Linux Programmer's Manual"
33 .SH NAME
34 .\"SIMPLEQ_CONCAT,
35 SIMPLEQ_EMPTY,
36 SIMPLEQ_ENTRY,
37 SIMPLEQ_FIRST,
38 SIMPLEQ_FOREACH,
39 .\"SIMPLEQ_FOREACH_FROM,
40 .\"SIMPLEQ_FOREACH_FROM_SAFE,
41 .\"SIMPLEQ_FOREACH_SAFE,
42 SIMPLEQ_HEAD,
43 SIMPLEQ_HEAD_INITIALIZER,
44 SIMPLEQ_INIT,
45 SIMPLEQ_INSERT_AFTER,
46 SIMPLEQ_INSERT_HEAD,
47 SIMPLEQ_INSERT_TAIL,
48 .\"SIMPLEQ_LAST,
49 SIMPLEQ_NEXT,
50 SIMPLEQ_REMOVE,
51 .\"SIMPLEQ_REMOVE_AFTER,
52 SIMPLEQ_REMOVE_HEAD,
53 .\"SIMPLEQ_SWAP,
54 STAILQ_CONCAT,
55 STAILQ_EMPTY,
56 STAILQ_ENTRY,
57 STAILQ_FIRST,
58 STAILQ_FOREACH,
59 .\"STAILQ_FOREACH_FROM,
60 .\"STAILQ_FOREACH_FROM_SAFE,
61 .\"STAILQ_FOREACH_SAFE,
62 STAILQ_HEAD,
63 STAILQ_HEAD_INITIALIZER,
64 STAILQ_INIT,
65 STAILQ_INSERT_AFTER,
66 STAILQ_INSERT_HEAD,
67 STAILQ_INSERT_TAIL,
68 .\"STAILQ_LAST,
69 STAILQ_NEXT,
70 STAILQ_REMOVE,
71 .\"STAILQ_REMOVE_AFTER,
72 STAILQ_REMOVE_HEAD,
73 .\"STAILQ_SWAP
74 \- implementation of a singly linked tail queue
75 .SH SYNOPSIS
76 .nf
77 .B #include <sys/queue.h>
78 .PP
79 .B STAILQ_ENTRY(TYPE);
80 .PP
81 .B STAILQ_HEAD(HEADNAME, TYPE);
82 .BI "STAILQ_HEAD STAILQ_HEAD_INITIALIZER(STAILQ_HEAD " head );
83 .BI "void STAILQ_INIT(STAILQ_HEAD *" head );
84 .PP
85 .BI "int STAILQ_EMPTY(STAILQ_HEAD *" head );
86 .PP
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 );
93 .PP
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 );
98 .PP
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 );
102 .\" .PP
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 );
119 .IR Note :
120 Identical macros prefixed with SIMPLEQ instead of STAILQ exist; see NOTES.
121 .SH DESCRIPTION
122 These macros define and operate on singly linked tail queues.
124 In the macro definitions,
125 .I TYPE
126 is the name of a user-defined structure,
127 that must contain a field of type
128 .IR STAILQ_ENTRY ,
129 named
130 .IR NAME .
131 The argument
132 .I HEADNAME
133 is the name of a user-defined structure that must be declared
134 using the macro
135 .BR STAILQ_HEAD ().
136 .SS Creation
137 A singly linked tail queue is headed by a structure defined by the
138 .BR STAILQ_HEAD ()
139 macro.
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.
148 .I STAILQ_HEAD
149 structure is declared as follows:
151 .in +4
153 STAILQ_HEAD(HEADNAME, TYPE) head;
157 where
158 .I struct HEADNAME
159 is the structure to be defined, and
160 .I struct TYPE
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:
164 .in +4
166 struct HEADNAME *headp;
170 (The names
171 .I head
173 .I headp
174 are user selectable.)
176 .BR STAILQ_ENTRY ()
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
181 .IR head .
183 .BR STAILQ_INIT ()
184 initializes the tail queue referenced by
185 .IR head .
187 .BR STAILQ_EMPTY ()
188 evaluates to true if there are no items on the tail queue.
189 .SS Insertion
190 .BR STAILQ_INSERT_HEAD ()
191 inserts the new element
192 .I elm
193 at the head of the tail queue.
195 .BR STAILQ_INSERT_TAIL ()
196 inserts the new element
197 .I elm
198 at the end of the tail queue.
200 .BR STAILQ_INSERT_AFTER ()
201 inserts the new element
202 .I elm
203 after the element
204 .IR listelm .
205 .SS Traversal
206 .BR STAILQ_FIRST ()
207 returns the first item on the tail queue or NULL if the tail queue is empty.
208 .\" .PP
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 .
213 .BR STAILQ_NEXT ()
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
218 .I head
219 in the forward direction,
220 assigning each element in turn to
221 .IR var .
222 .\" .PP
223 .\" .BR STAILQ_FOREACH_FROM ()
224 .\" behaves identically to
225 .\" .BR STAILQ_FOREACH ()
226 .\" when
227 .\" .I var
228 .\" is NULL, else it treats
229 .\" .I var
230 .\" as a previously found STAILQ element and begins the loop at
231 .\" .I var
232 .\" instead of the first element in the STAILQ referenced by
233 .\" .IR head .
234 .\" .PP
235 .\" .BR STAILQ_FOREACH_SAFE ()
236 .\" traverses the tail queue referenced by
237 .\" .I head
238 .\" in the forward direction, assigning each element
239 .\" in turn to
240 .\" .IR var .
241 .\" However, unlike
242 .\" .BR STAILQ_FOREACH ()
243 .\" here it is permitted to both remove
244 .\" .I var
245 .\" as well as free it from within the loop safely without interfering with the
246 .\" traversal.
247 .\" .PP
248 .\" .BR STAILQ_FOREACH_FROM_SAFE ()
249 .\" behaves identically to
250 .\" .BR STAILQ_FOREACH_SAFE ()
251 .\" when
252 .\" .I var
253 .\" is NULL, else it treats
254 .\" .I var
255 .\" as a previously found STAILQ element and begins the loop at
256 .\" .I var
257 .\" instead of the first element in the STAILQ referenced by
258 .\" .IR head .
259 .SS Removal
260 .BR STAILQ_REMOVE ()
261 removes the element
262 .I elm
263 from the tail queue.
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
270 .BR STAILQ_REMOVE ()
271 macro.
272 .\" .PP
273 .\" .BR STAILQ_REMOVE_AFTER ()
274 .\" removes the element after
275 .\" .I elm
276 .\" from the tail queue.
277 .\" Unlike
278 .\" .BR STAILQ_REMOVE (),
279 .\" this macro does not traverse the entire tail queue.
280 .SS Other features
281 .BR STAILQ_CONCAT ()
282 concatenates the tail queue headed by
283 .I head2
284 onto the end of the one headed by
285 .I head1
286 removing all entries from the former.
287 .\" .PP
288 .\" .BR STAILQ_SWAP ()
289 .\" swaps the contents of
290 .\" .I head1
291 .\" and
292 .\" .IR head2 .
293 .SH RETURN VALUE
294 .BR STAILQ_EMPTY ()
295 returns nonzero if the queue is empty,
296 and zero if the queue contains at least one entry.
298 .BR STAILQ_FIRST (),
300 .BR STAILQ_NEXT ()
301 return a pointer to the first or next
302 .I TYPE
303 structure, respectively.
305 .BR STAILQ_HEAD_INITIALIZER ()
306 returns an initializer that can be assigned to the queue
307 .IR head .
308 .SH CONFORMING TO
309 Not in POSIX.1, POSIX.1-2001, or POSIX.1-2008.
310 Present on the BSDs
311 (STAILQ macros first appeared in 4.4BSD).
312 .SH BUGS
313 .BR STAILQ_FOREACH ()
314 doesn't allow
315 .I var
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
321 .I var
322 to safely be removed from the list and freed from within the loop
323 without interfering with the traversal.
324 .SH NOTES
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 ().
333 .SH EXAMPLES
335 #include <stddef.h>
336 #include <stdio.h>
337 #include <stdlib.h>
338 #include <sys/queue.h>
340 struct entry {
341     int data;
342     STAILQ_ENTRY(entry) entries;        /* Singly linked tail queue */
345 STAILQ_HEAD(stailhead, entry);
348 main(void)
350     struct entry *n1, *n2, *n3, *np;
351     struct stailhead head;                  /* Singly linked tail queue
352                                                head */
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 */
366     free(n2);
368     n3 = STAILQ_FIRST(&head);
369     STAILQ_REMOVE_HEAD(&head, entries);     /* Deletion from the head */
370     free(n3);
372     n1 = STAILQ_FIRST(&head);
373     n1\->data = 0;
374     for (int i = 1; i < 5; i++) {
375         n1 = malloc(sizeof(struct entry));
376         STAILQ_INSERT_HEAD(&head, n1, entries);
377         n1\->data = i;
378     }
379                                             /* Forward traversal */
380     STAILQ_FOREACH(np, &head, entries)
381         printf("%i\en", np\->data);
382                                             /* TailQ deletion */
383     n1 = STAILQ_FIRST(&head);
384     while (n1 != NULL) {
385         n2 = STAILQ_NEXT(n1, entries);
386         free(n1);
387         n1 = n2;
388     }
389     STAILQ_INIT(&head);
391     exit(EXIT_SUCCESS);
394 .SH SEE ALSO
395 .BR insque (3),
396 .BR queue (7)