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 CIRCLEQ 3 2021-03-22 "GNU" "Linux Programmer's Manual"
38 CIRCLEQ_FOREACH_REVERSE,
40 CIRCLEQ_HEAD_INITIALIZER,
43 CIRCLEQ_INSERT_BEFORE,
52 \- implementation of a doubly linked circular queue
55 .B #include <sys/queue.h>
57 .B CIRCLEQ_ENTRY(TYPE);
59 .B CIRCLEQ_HEAD(HEADNAME, TYPE);
60 .BI "CIRCLEQ_HEAD CIRCLEQ_HEAD_INITIALIZER(CIRCLEQ_HEAD " head );
61 .BI "void CIRCLEQ_INIT(CIRCLEQ_HEAD *" head );
63 .BI "int CIRCLEQ_EMPTY(CIRCLEQ_HEAD *" head );
65 .BI "void CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *" head ,
66 .BI " struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
67 .BI "void CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *" head ,
68 .BI " struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
69 .BI "void CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *" head ", struct TYPE *" listelm ,
70 .BI " struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
71 .BI "void CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *" head ", struct TYPE *" listelm ,
72 .BI " struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
74 .BI "struct TYPE *CIRCLEQ_FIRST(CIRCLEQ_HEAD *" head );
75 .BI "struct TYPE *CIRCLEQ_LAST(CIRCLEQ_HEAD *" head );
76 .BI "struct TYPE *CIRCLEQ_PREV(struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
77 .BI "struct TYPE *CIRCLEQ_NEXT(struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
78 .BI "struct TYPE *CIRCLEQ_LOOP_PREV(CIRCLEQ_HEAD *" head ,
79 .BI " struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
80 .BI "struct TYPE *CIRCLEQ_LOOP_NEXT(CIRCLEQ_HEAD *" head ,
81 .BI " struct TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
83 .BI "CIRCLEQ_FOREACH(struct TYPE *" var ", CIRCLEQ_HEAD *" head ,
84 .BI " CIRCLEQ_ENTRY " NAME );
85 .BI "CIRCLEQ_FOREACH_REVERSE(struct TYPE *" var ", CIRCLEQ_HEAD *" head ,
86 .BI " CIRCLEQ_ENTRY " NAME );
88 .BI "void CIRCLEQ_REMOVE(CIRCLEQ_HEAD *" head ", struct TYPE *" elm ,
89 .BI " CIRCLEQ_ENTRY " NAME );
92 These macros define and operate on doubly linked circular queues.
94 In the macro definitions,
96 is the name of a user-defined structure,
97 that must contain a field of type
103 is the name of a user-defined structure
104 that must be declared using the macro
107 A circular queue is headed by a structure defined by the
110 This structure contains a pair of pointers,
111 one to the first element in the queue
112 and the other to the last element in the queue.
113 The elements are doubly linked
114 so that an arbitrary element can be removed without traversing the queue.
115 New elements can be added to the queue
116 after an existing element,
117 before an existing element,
118 at the head of the queue,
119 or at the end of the queue.
122 structure is declared as follows:
126 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
132 is the structure to be defined, and
134 is the type of the elements to be linked into the queue.
135 A pointer to the head of the queue can later be declared as:
139 struct HEADNAME *headp;
147 are user selectable.)
150 declares a structure that connects the elements in the queue.
152 .BR CIRCLEQ_HEAD_INITIALIZER ()
153 evaluates to an initializer for the queue
157 initializes the queue referenced by
161 evaluates to true if there are no items on the queue.
163 .BR CIRCLEQ_INSERT_HEAD ()
164 inserts the new element
166 at the head of the queue.
168 .BR CIRCLEQ_INSERT_TAIL ()
169 inserts the new element
171 at the end of the queue.
173 .BR CIRCLEQ_INSERT_BEFORE ()
174 inserts the new element
179 .BR CIRCLEQ_INSERT_AFTER ()
180 inserts the new element
186 returns the first item on the queue.
189 returns the last item on the queue.
192 returns the previous item on the queue, or
194 if this item is the first one.
197 returns the next item on the queue, or
199 if this item is the last one.
201 .BR CIRCLEQ_LOOP_PREV ()
202 returns the previous item on the queue.
205 is the first element on the queue, the last element is returned.
207 .BR CIRCLEQ_LOOP_NEXT ()
208 returns the next item on the queue.
211 is the last element on the queue, the first element is returned.
213 .BR CIRCLEQ_FOREACH ()
214 traverses the queue referenced by
216 in the forward direction, assigning each element in turn to
221 if the loop completes normally, or if there were no elements.
223 .BR CIRCLEQ_FOREACH_REVERSE ()
224 traverses the queue referenced by
226 in the reverse direction,
227 assigning each element in turn to
230 .BR CIRCLEQ_REMOVE ()
236 returns nonzero if the queue is empty,
237 and zero if the queue contains at least one entry.
239 .BR CIRCLEQ_FIRST (),
241 .BR CIRCLEQ_LOOP_PREV (),
243 .BR CIRCLEQ_LOOP_NEXT ()
244 return a pointer to the first, last, previous, or next
246 structure, respectively.
252 .BR CIRCLEQ_LOOP_* ()
254 except that if the argument is the first or last element, respectively,
258 .BR CIRCLEQ_HEAD_INITIALIZER ()
259 returns an initializer that can be assigned to the queue
262 Not in POSIX.1, POSIX.1-2001, or POSIX.1-2008.
264 (CIRCLEQ macros first appeared in 4.4BSD).
266 .BR CIRCLEQ_FOREACH ()
268 .BR CIRCLEQ_FOREACH_REVERSE ()
271 to be removed or freed within the loop,
272 as it would interfere with the traversal.
273 .BR CIRCLEQ_FOREACH_SAFE ()
275 .BR CIRCLEQ_FOREACH_REVERSE_SAFE (),
276 which are present on the BSDs but are not present in glibc,
277 fix this limitation by allowing
279 to safely be removed from the list and freed from within the loop
280 without interfering with the traversal.
286 #include <sys/queue.h>
290 CIRCLEQ_ENTRY(entry) entries; /* Queue */
293 CIRCLEQ_HEAD(circlehead, entry);
298 struct entry *n1, *n2, *n3, *np;
299 struct circlehead head; /* Queue head */
302 CIRCLEQ_INIT(&head); /* Initialize the queue */
304 n1 = malloc(sizeof(struct entry)); /* Insert at the head */
305 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
307 n1 = malloc(sizeof(struct entry)); /* Insert at the tail */
308 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
310 n2 = malloc(sizeof(struct entry)); /* Insert after */
311 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
313 n3 = malloc(sizeof(struct entry)); /* Insert before */
314 CIRCLEQ_INSERT_BEFORE(&head, n2, n3, entries);
316 CIRCLEQ_REMOVE(&head, n2, entries); /* Deletion */
318 /* Forward traversal */
320 CIRCLEQ_FOREACH(np, &head, entries)
322 /* Reverse traversal */
323 CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
324 printf("%i\en", np\->data);
326 n1 = CIRCLEQ_FIRST(&head);
327 while (n1 != (void *)&head) {
328 n2 = CIRCLEQ_NEXT(n1, entries);