poll.2: Remove <signal.h>
[man-pages.git] / man3 / list.3
blob04bb4166815f8a9796a0790e124199d118d94be7
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 LIST 3 2021-03-22 "GNU" "Linux Programmer's Manual"
33 .SH NAME
34 LIST_EMPTY,
35 LIST_ENTRY,
36 LIST_FIRST,
37 LIST_FOREACH,
38 .\"LIST_FOREACH_FROM,
39 .\"LIST_FOREACH_SAFE,
40 .\"LIST_FOREACH_FROM_SAFE,
41 LIST_HEAD,
42 LIST_HEAD_INITIALIZER,
43 LIST_INIT,
44 LIST_INSERT_AFTER,
45 LIST_INSERT_BEFORE,
46 LIST_INSERT_HEAD,
47 LIST_NEXT,
48 .\"LIST_PREV,
49 LIST_REMOVE
50 .\"LIST_SWAP
51 \- implementation of a doubly linked list
52 .SH SYNOPSIS
53 .nf
54 .B #include <sys/queue.h>
55 .PP
56 .B LIST_ENTRY(TYPE);
57 .PP
58 .B LIST_HEAD(HEADNAME, TYPE);
59 .BI "LIST_HEAD LIST_HEAD_INITIALIZER(LIST_HEAD " head );
60 .BI "void LIST_INIT(LIST_HEAD *" head );
61 .PP
62 .BI "int LIST_EMPTY(LIST_HEAD *" head );
63 .PP
64 .BI "void LIST_INSERT_HEAD(LIST_HEAD *" head ,
65 .BI "                        struct TYPE *" elm ", LIST_ENTRY " NAME );
66 .BI "void LIST_INSERT_BEFORE(struct TYPE *" listelm ,
67 .BI "                        struct TYPE *" elm ", LIST_ENTRY " NAME );
68 .BI "void LIST_INSERT_AFTER(struct TYPE *" listelm ,
69 .BI "                        struct TYPE *" elm ", LIST_ENTRY " NAME );
70 .PP
71 .BI "struct TYPE *LIST_FIRST(LIST_HEAD *" head );
72 .\" .BI "struct TYPE *LIST_PREV(struct TYPE *" elm ", LIST_HEAD *" head ,
73 .\" .BI "                        struct TYPE, LIST_ENTRY " NAME );
74 .BI "struct TYPE *LIST_NEXT(struct TYPE *" elm ", LIST_ENTRY " NAME );
75 .PP
76 .BI "LIST_FOREACH(struct TYPE *" var ", LIST_HEAD *" head ", LIST_ENTRY " NAME );
77 .\" .BI "LIST_FOREACH_FROM(struct TYPE *" var ", LIST_HEAD *" head ", LIST_ENTRY " NAME );
78 .\" .PP
79 .\" .BI "LIST_FOREACH_SAFE(struct TYPE *" var ", LIST_HEAD *" head ,
80 .\" .BI "                        LIST_ENTRY " NAME ", struct TYPE *" temp_var );
81 .\" .BI "LIST_FOREACH_FROM_SAFE(struct TYPE *" var ", LIST_HEAD *" head ,
82 .\" .BI "                        LIST_ENTRY " NAME ", struct TYPE *" temp_var );
83 .PP
84 .BI "void LIST_REMOVE(struct TYPE *" elm ", LIST_ENTRY " NAME );
85 .\" .PP
86 .\" .BI "void LIST_SWAP(LIST_HEAD *" head1 ", LIST_HEAD *" head2 ,
87 .\" .BI "                        struct TYPE, LIST_ENTRY " NAME );
88 .fi
89 .SH DESCRIPTION
90 These macros define and operate on doubly linked lists.
91 .PP
92 In the macro definitions,
93 .I TYPE
94 is the name of a user-defined structure,
95 that must contain a field of type
96 .IR LIST_ENTRY ,
97 named
98 .IR NAME .
99 The argument
100 .IR HEADNAME
101 is the name of a user-defined structure
102 that must be declared using the macro
103 .BR LIST_HEAD ().
104 .SS Creation
105 A list is headed by a structure defined by the
106 .BR LIST_HEAD ()
107 macro.
108 This structure contains a single pointer to the first element on the list.
109 The elements are doubly linked
110 so that an arbitrary element can be removed without traversing the list.
111 New elements can be added to the list
112 after an existing element,
113 before an existing element,
114 or at the head of the list.
116 .I LIST_HEAD
117 structure is declared as follows:
119 .in +4
121 LIST_HEAD(HEADNAME, TYPE) head;
125 where
126 .I struct HEADNAME
127 is the structure to be defined, and
128 .I struct TYPE
129 is the type of the elements to be linked into the list.
130 A pointer to the head of the list can later be declared as:
132 .in +4
134 struct HEADNAME *headp;
138 (The names
139 .I head
141 .I headp
142 are user selectable.)
144 .BR LIST_ENTRY ()
145 declares a structure that connects the elements in the list.
147 .BR LIST_HEAD_INITIALIZER ()
148 evaluates to an initializer for the list
149 .IR head .
151 .BR LIST_INIT ()
152 initializes the list referenced by
153 .IR head .
155 .BR LIST_EMPTY ()
156 evaluates to true if there are no elements in the list.
157 .SS Insertion
158 .BR LIST_INSERT_HEAD ()
159 inserts the new element
160 .I elm
161 at the head of the list.
163 .BR LIST_INSERT_BEFORE ()
164 inserts the new element
165 .I elm
166 before the element
167 .IR listelm .
169 .BR LIST_INSERT_AFTER ()
170 inserts the new element
171 .I elm
172 after the element
173 .IR listelm .
174 .SS Traversal
175 .BR LIST_FIRST ()
176 returns the first element in the list, or NULL if the list is empty.
177 .\" .PP
178 .\" .BR LIST_PREV ()
179 .\" returns the previous element in the list, or NULL if this is the first.
180 .\" List
181 .\" .I head
182 .\" must contain element
183 .\" .IR elm .
185 .BR LIST_NEXT ()
186 returns the next element in the list, or NULL if this is the last.
188 .BR LIST_FOREACH ()
189 traverses the list referenced by
190 .I head
191 in the forward direction,
192 assigning each element in turn to
193 .IR var .
194 .\" .PP
195 .\" .BR LIST_FOREACH_FROM ()
196 .\" behaves identically to
197 .\" .BR LIST_FOREACH ()
198 .\" when
199 .\" .I var
200 .\" is NULL, else it treats
201 .\" .I var
202 .\" as a previously found LIST element and begins the loop at
203 .\" .I var
204 .\" instead of the first element in the LIST referenced by
205 .\" .IR head .
206 .\" .PP
207 .\" .BR LIST_FOREACH_SAFE ()
208 .\" traverses the list referenced by
209 .\" .I head
210 .\" in the forward direction, assigning each element in turn to
211 .\" .IR var .
212 .\" However, unlike
213 .\" .BR LIST_FOREACH ()
214 .\" here it is permitted to both remove
215 .\" .I var
216 .\" as well as free it from within the loop safely without interfering with the
217 .\" traversal.
218 .\" .PP
219 .\" .BR LIST_FOREACH_FROM_SAFE ()
220 .\" behaves identically to
221 .\" .BR LIST_FOREACH_SAFE ()
222 .\" when
223 .\" .I var
224 .\" is NULL, else it treats
225 .\" .I var
226 .\" as a previously found LIST element and begins the loop at
227 .\" .I var
228 .\" instead of the first element in the LIST referenced by
229 .\" .IR head .
230 .SS Removal
231 .BR LIST_REMOVE ()
232 removes the element
233 .I elm
234 from the list.
235 .\" .SS Other features
236 .\" .BR LIST_SWAP ()
237 .\" swaps the contents of
238 .\" .I head1
239 .\" and
240 .\" .IR head2 .
241 .SH RETURN VALUE
242 .BR LIST_EMPTY ()
243 returns nonzero if the list is empty,
244 and zero if the list contains at least one entry.
246 .BR LIST_FIRST (),
248 .BR LIST_NEXT ()
249 return a pointer to the first or next
250 .I TYPE
251 structure, respectively.
253 .BR LIST_HEAD_INITIALIZER ()
254 returns an initializer that can be assigned to the list
255 .IR head .
256 .SH CONFORMING TO
257 Not in POSIX.1, POSIX.1-2001, or POSIX.1-2008.
258 Present on the BSDs
259 (LIST macros first appeared in 4.4BSD).
260 .SH BUGS
261 .BR LIST_FOREACH ()
262 doesn't allow
263 .I var
264 to be removed or freed within the loop,
265 as it would interfere with the traversal.
266 .BR LIST_FOREACH_SAFE (),
267 which is present on the BSDs but is not present in glibc,
268 fixes this limitation by allowing
269 .I var
270 to safely be removed from the list and freed from within the loop
271 without interfering with the traversal.
272 .SH EXAMPLES
274 #include <stddef.h>
275 #include <stdio.h>
276 #include <stdlib.h>
277 #include <sys/queue.h>
279 struct entry {
280     int data;
281     LIST_ENTRY(entry) entries;              /* List */
284 LIST_HEAD(listhead, entry);
287 main(void)
289     struct entry *n1, *n2, *n3, *np;
290     struct listhead head;                   /* List head */
291     int i;
293     LIST_INIT(&head);                       /* Initialize the list */
295     n1 = malloc(sizeof(struct entry));      /* Insert at the head */
296     LIST_INSERT_HEAD(&head, n1, entries);
298     n2 = malloc(sizeof(struct entry));      /* Insert after */
299     LIST_INSERT_AFTER(n1, n2, entries);
301     n3 = malloc(sizeof(struct entry));      /* Insert before */
302     LIST_INSERT_BEFORE(n2, n3, entries);
304     i = 0;                                  /* Forward traversal */
305     LIST_FOREACH(np, &head, entries)
306         np\->data = i++;
308     LIST_REMOVE(n2, entries);               /* Deletion */
309     free(n2);
310                                             /* Forward traversal */
311     LIST_FOREACH(np, &head, entries)
312         printf("%i\en", np\->data);
313                                             /* List deletion */
314     n1 = LIST_FIRST(&head);
315     while (n1 != NULL) {
316         n2 = LIST_NEXT(n1, entries);
317         free(n1);
318         n1 = n2;
319     }
320     LIST_INIT(&head);
322     exit(EXIT_SUCCESS);
325 .SH SEE ALSO
326 .BR insque (3),
327 .BR queue (7)