2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
35 .\" 2007-12-08, mtk, Converted from mdoc to man macros
37 .TH QUEUE 3 2007-12-08 "Linux" "Linux Programmer's Manual"
39 LIST_ENTRY, LIST_HEAD, LIST_INIT, LIST_INSERT_AFTER, \
40 LIST_INSERT_HEAD, LIST_REMOVE, TAILQ_ENTRY, TAILQ_HEAD, \
41 TAILQ_INIT, TAILQ_INSERT_AFTER, TAILQ_INSERT_HEAD, TAILQ_INSERT_TAIL, \
42 TAILQ_REMOVE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_INIT, \
43 CIRCLEQ_INSERT_AFTER, CIRCLEQ_INSERT_BEFORE, \
44 CIRCLEQ_INSERT_HEAD, CIRCLEQ_INSERT_TAIL, \
45 CIRCLEQ_REMOVE \- implementations of lists, tail queues, and circular queues
48 .B #include <sys/queue.h>
50 .BI "LIST_ENTRY(" TYPE );
51 .BI "LIST_HEAD(" HEADNAME ", " TYPE );
52 .BI "LIST_INIT(LIST_HEAD *" head );
53 .BI "LIST_INSERT_AFTER(LIST_ENTRY *" listelm ", "
54 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
55 .BI "LIST_INSERT_HEAD(LIST_HEAD *" head ", "
56 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
57 .BI "LIST_REMOVE(TYPE *" elm ", LIST_ENTRY " NAME );
59 .BI "TAILQ_ENTRY(" TYPE );
60 .BI "TAILQ_HEAD("HEADNAME ", " TYPE );
61 .BI "TAILQ_INIT(TAILQ_HEAD *" head );
62 .BI "TAILQ_INSERT_AFTER(TAILQ_HEAD *" head ", TYPE *" listelm ", "
63 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
64 .BI "TAILQ_INSERT_HEAD(TAILQ_HEAD *" head ", "
65 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
66 .BI "TAILQ_INSERT_TAIL(TAILQ_HEAD *" head ", "
67 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
68 .BI "TAILQ_REMOVE(TAILQ_HEAD *" head ", TYPE *" elm ", TAILQ_ENTRY " NAME );
70 .BI CIRCLEQ_ENTRY( TYPE );
71 .BI "CIRCLEQ_HEAD(" HEADNAME ", " TYPE );
72 .BI "CIRCLEQ_INIT(CIRCLEQ_HEAD *" head );
73 .BI "CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
74 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
75 .BI "CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
76 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
77 .BI "CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *" head ", "
78 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
79 .BI "CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *" head ", "
80 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
81 .BI "CIRCLEQ_REMOVE(CIRCLEQ_HEAD *" head ", "
82 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
85 These macros define and operate on three types of data structures:
86 lists, tail queues, and circular queues.
87 All three structures support the following functionality:
90 Insertion of a new entry at the head of the list.
92 Insertion of a new entry after any element in the list.
94 Removal of any entry in the list.
96 Forward traversal through the list.
99 Lists are the simplest of the three data structures and support
100 only the above functionality.
102 Tail queues add the following functionality:
105 Entries can be added at the end of a list.
111 All list insertions and removals must specify the head of the list.
113 Each head entry requires two pointers rather than one.
115 Code size is about 15% greater and operations run about 20% slower
119 Circular queues add the following functionality:
122 Entries can be added at the end of a list.
124 Entries can be added before another entry.
126 They may be traversed backwards, from tail to head.
132 All list insertions and removals must specify the head of the list.
134 Each head entry requires two pointers rather than one.
136 The termination condition for traversal is more complex.
138 Code size is about 40% greater and operations run about 45% slower
142 In the macro definitions,
144 is the name of a user defined structure,
145 that must contain a field of type
149 .IR "CIRCLEQ_ENTRY" ,
154 is the name of a user defined structure that must be declared
160 See the examples below for further explanation of how these
163 A list is headed by a structure defined by the
165 This structure contains a single pointer to the first element
167 The elements are doubly linked so that an arbitrary element can be
168 removed without traversing the list.
169 New elements can be added to the list after an existing element or
170 at the head of the list.
173 structure is declared as follows:
177 LIST_HEAD(HEADNAME, TYPE) head;
183 is the name of the structure to be defined, and
185 is the type of the elements to be linked into the list.
186 A pointer to the head of the list can later be declared as:
190 struct HEADNAME *headp;
198 are user selectable.)
202 declares a structure that connects the elements in
207 initializes the list referenced by
212 inserts the new element
214 at the head of the list.
218 inserts the new element
230 LIST_HEAD(listhead, entry) head;
231 struct listhead *headp; /* List head. */
234 LIST_ENTRY(entry) entries; /* List. */
238 LIST_INIT(&head); /* Initialize the list. */
240 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
241 LIST_INSERT_HEAD(&head, n1, entries);
243 n2 = malloc(sizeof(struct entry)); /* Insert after. */
244 LIST_INSERT_AFTER(n1, n2, entries);
245 /* Forward traversal. */
246 for (np = head.lh_first; np != NULL; np = np\->entries.le_next)
249 while (head.lh_first != NULL) /* Delete. */
250 LIST_REMOVE(head.lh_first, entries);
253 A tail queue is headed by a structure defined by the
255 This structure contains a pair of pointers,
256 one to the first element in the tail queue and the other to
257 the last element in the tail queue.
258 The elements are doubly linked so that an arbitrary element can be
259 removed without traversing the tail queue.
260 New elements can be added to the tail queue after an existing element,
261 at the head of the tail queue, or at the end of the tail queue.
264 structure is declared as follows:
268 TAILQ_HEAD(HEADNAME, TYPE) head;
274 is the name of the structure to be defined, and
276 is the type of the elements to be linked into the tail queue.
277 A pointer to the head of the tail queue can later be declared as:
281 struct HEADNAME *headp;
289 are user selectable.)
293 declares a structure that connects the elements in
298 initializes the tail queue referenced by
303 inserts the new element
305 at the head of the tail queue.
309 inserts the new element
311 at the end of the tail queue.
314 .B TAILQ_INSERT_AFTER
315 inserts the new element
325 .SS Tail Queue Example
327 TAILQ_HEAD(tailhead, entry) head;
328 struct tailhead *headp; /* Tail queue head. */
331 TAILQ_ENTRY(entry) entries; /* Tail queue. */
335 TAILQ_INIT(&head); /* Initialize the queue. */
337 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
338 TAILQ_INSERT_HEAD(&head, n1, entries);
340 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
341 TAILQ_INSERT_TAIL(&head, n1, entries);
343 n2 = malloc(sizeof(struct entry)); /* Insert after. */
344 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
345 /* Forward traversal. */
346 for (np = head.tqh_first; np != NULL; np = np->entries.tqe_next)
349 while (head.tqh_first != NULL)
350 TAILQ_REMOVE(&head, head.tqh_first, entries);
353 A circular queue is headed by a structure defined by the
355 This structure contains a pair of pointers,
356 one to the first element in the circular queue and the other to the
357 last element in the circular queue.
358 The elements are doubly linked so that an arbitrary element can be
359 removed without traversing the queue.
360 New elements can be added to the queue after an existing element,
361 before an existing element, at the head of the queue, or at the end
365 structure is declared as follows:
369 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
375 is the name of the structure to be defined, and
377 is the type of the elements to be linked into the circular queue.
378 A pointer to the head of the circular queue can later be declared as:
382 struct HEADNAME *headp;
390 are user selectable.)
394 declares a structure that connects the elements in
399 initializes the circular queue referenced by
403 .B CIRCLEQ_INSERT_HEAD
404 inserts the new element
406 at the head of the circular queue.
409 .B CIRCLEQ_INSERT_TAIL
410 inserts the new element
412 at the end of the circular queue.
415 .B CIRCLEQ_INSERT_AFTER
416 inserts the new element
422 .B CIRCLEQ_INSERT_BEFORE
423 inserts the new element
432 from the circular queue.
433 .SS Circular Queue Example
435 CIRCLEQ_HEAD(circleq, entry) head;
436 struct circleq *headp; /* Circular queue head. */
439 CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
443 CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
445 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
446 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
448 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
449 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
451 n2 = malloc(sizeof(struct entry)); /* Insert after. */
452 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
454 n2 = malloc(sizeof(struct entry)); /* Insert before. */
455 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
456 /* Forward traversal. */
457 for (np = head.cqh_first; np != (void *)&head;
458 np = np->entries.cqe_next)
460 /* Reverse traversal. */
461 for (np = head.cqh_last; np != (void *)&head; np = np->entries.cqe_prev)
464 while (head.cqh_first != (void *)&head)
465 CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
471 queue functions first appeared in