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 LIST 3 2021-03-22 "GNU" "Linux Programmer's Manual"
40 .\"LIST_FOREACH_FROM_SAFE,
42 LIST_HEAD_INITIALIZER,
51 \- implementation of a doubly linked list
54 .B #include <sys/queue.h>
58 .B LIST_HEAD(HEADNAME, TYPE);
59 .BI "LIST_HEAD LIST_HEAD_INITIALIZER(LIST_HEAD " head );
60 .BI "void LIST_INIT(LIST_HEAD *" head );
62 .BI "int LIST_EMPTY(LIST_HEAD *" head );
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 );
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 );
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 );
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 );
84 .BI "void LIST_REMOVE(struct TYPE *" elm ", LIST_ENTRY " NAME );
86 .\" .BI "void LIST_SWAP(LIST_HEAD *" head1 ", LIST_HEAD *" head2 ,
87 .\" .BI " struct TYPE, LIST_ENTRY " NAME );
90 These macros define and operate on doubly linked lists.
92 In the macro definitions,
94 is the name of a user-defined structure,
95 that must contain a field of type
101 is the name of a user-defined structure
102 that must be declared using the macro
105 A list is headed by a structure defined by the
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.
117 structure is declared as follows:
121 LIST_HEAD(HEADNAME, TYPE) head;
127 is the structure to be defined, and
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:
134 struct HEADNAME *headp;
142 are user selectable.)
145 declares a structure that connects the elements in the list.
147 .BR LIST_HEAD_INITIALIZER ()
148 evaluates to an initializer for the list
152 initializes the list referenced by
156 evaluates to true if there are no elements in the list.
158 .BR LIST_INSERT_HEAD ()
159 inserts the new element
161 at the head of the list.
163 .BR LIST_INSERT_BEFORE ()
164 inserts the new element
169 .BR LIST_INSERT_AFTER ()
170 inserts the new element
176 returns the first element in the list, or NULL if the list is empty.
179 .\" returns the previous element in the list, or NULL if this is the first.
182 .\" must contain element
186 returns the next element in the list, or NULL if this is the last.
189 traverses the list referenced by
191 in the forward direction,
192 assigning each element in turn to
195 .\" .BR LIST_FOREACH_FROM ()
196 .\" behaves identically to
197 .\" .BR LIST_FOREACH ()
200 .\" is NULL, else it treats
202 .\" as a previously found LIST element and begins the loop at
204 .\" instead of the first element in the LIST referenced by
207 .\" .BR LIST_FOREACH_SAFE ()
208 .\" traverses the list referenced by
210 .\" in the forward direction, assigning each element in turn to
213 .\" .BR LIST_FOREACH ()
214 .\" here it is permitted to both remove
216 .\" as well as free it from within the loop safely without interfering with the
219 .\" .BR LIST_FOREACH_FROM_SAFE ()
220 .\" behaves identically to
221 .\" .BR LIST_FOREACH_SAFE ()
224 .\" is NULL, else it treats
226 .\" as a previously found LIST element and begins the loop at
228 .\" instead of the first element in the LIST referenced by
235 .\" .SS Other features
237 .\" swaps the contents of
243 returns nonzero if the list is empty,
244 and zero if the list contains at least one entry.
249 return a pointer to the first or next
251 structure, respectively.
253 .BR LIST_HEAD_INITIALIZER ()
254 returns an initializer that can be assigned to the list
257 Not in POSIX.1, POSIX.1-2001, or POSIX.1-2008.
259 (LIST macros first appeared in 4.4BSD).
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
270 to safely be removed from the list and freed from within the loop
271 without interfering with the traversal.
277 #include <sys/queue.h>
281 LIST_ENTRY(entry) entries; /* List */
284 LIST_HEAD(listhead, entry);
289 struct entry *n1, *n2, *n3, *np;
290 struct listhead head; /* List head */
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)
308 LIST_REMOVE(n2, entries); /* Deletion */
310 /* Forward traversal */
311 LIST_FOREACH(np, &head, entries)
312 printf("%i\en", np\->data);
314 n1 = LIST_FIRST(&head);
316 n2 = LIST_NEXT(n1, entries);