2 .\" The Regents of the University of California. All rights reserved.
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
37 .\" 2007-12-08, mtk, Converted from mdoc to man macros
39 .TH QUEUE 3 2007-12-28 "Linux" "Linux Programmer's Manual"
41 LIST_ENTRY, LIST_HEAD, LIST_INIT, LIST_INSERT_AFTER,
42 LIST_INSERT_HEAD, LIST_REMOVE, TAILQ_ENTRY, TAILQ_HEAD,
43 TAILQ_INIT, TAILQ_INSERT_AFTER, TAILQ_INSERT_HEAD, TAILQ_INSERT_TAIL,
44 TAILQ_REMOVE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_INIT,
45 CIRCLEQ_INSERT_AFTER, CIRCLEQ_INSERT_BEFORE,
46 CIRCLEQ_INSERT_HEAD, CIRCLEQ_INSERT_TAIL,
47 CIRCLEQ_REMOVE \- implementations of lists, tail queues, and circular queues
50 .B #include <sys/queue.h>
52 .BI "LIST_ENTRY(" TYPE );
53 .BI "LIST_HEAD(" HEADNAME ", " TYPE );
54 .BI "LIST_INIT(LIST_HEAD *" head );
55 .BI "LIST_INSERT_AFTER(LIST_ENTRY *" listelm ", "
56 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
57 .BI "LIST_INSERT_HEAD(LIST_HEAD *" head ", "
58 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
59 .BI "LIST_REMOVE(TYPE *" elm ", LIST_ENTRY " NAME );
61 .BI "TAILQ_ENTRY(" TYPE );
62 .BI "TAILQ_HEAD("HEADNAME ", " TYPE );
63 .BI "TAILQ_INIT(TAILQ_HEAD *" head );
64 .BI "TAILQ_INSERT_AFTER(TAILQ_HEAD *" head ", TYPE *" listelm ", "
65 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
66 .BI "TAILQ_INSERT_HEAD(TAILQ_HEAD *" head ", "
67 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
68 .BI "TAILQ_INSERT_TAIL(TAILQ_HEAD *" head ", "
69 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
70 .BI "TAILQ_REMOVE(TAILQ_HEAD *" head ", TYPE *" elm ", TAILQ_ENTRY " NAME );
72 .BI CIRCLEQ_ENTRY( TYPE );
73 .BI "CIRCLEQ_HEAD(" HEADNAME ", " TYPE );
74 .BI "CIRCLEQ_INIT(CIRCLEQ_HEAD *" head );
75 .BI "CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
76 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
77 .BI "CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
78 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
79 .BI "CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *" head ", "
80 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
81 .BI "CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *" head ", "
82 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
83 .BI "CIRCLEQ_REMOVE(CIRCLEQ_HEAD *" head ", "
84 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
87 These macros define and operate on three types of data structures:
88 lists, tail queues, and circular queues.
89 All three structures support the following functionality:
94 Insertion of a new entry at the head of the list.
96 Insertion of a new entry after any element in the list.
98 Removal of any entry in the list.
100 Forward traversal through the list.
104 Lists are the simplest of the three data structures and support
105 only the above functionality.
107 Tail queues add the following functionality:
110 Entries can be added at the end of a list.
118 All list insertions and removals must specify the head of the list.
120 Each head entry requires two pointers rather than one.
122 Code size is about 15% greater and operations run about 20% slower
127 Circular queues add the following functionality:
132 Entries can be added at the end of a list.
134 Entries can be added before another entry.
136 They may be traversed backward, from tail to head.
145 All list insertions and removals must specify the head of the list.
147 Each head entry requires two pointers rather than one.
149 The termination condition for traversal is more complex.
151 Code size is about 40% greater and operations run about 45% slower
156 In the macro definitions,
158 is the name of a user-defined structure,
159 that must contain a field of type
163 .BR "CIRCLEQ_ENTRY" ,
168 is the name of a user-defined structure that must be declared
174 See the examples below for further explanation of how these
177 A list is headed by a structure defined by the
180 This structure contains a single pointer to the first element
182 The elements are doubly linked so that an arbitrary element can be
183 removed without traversing the list.
184 New elements can be added to the list after an existing element or
185 at the head of the list.
188 structure is declared as follows:
192 LIST_HEAD(HEADNAME, TYPE) head;
198 is the name of the structure to be defined, and
200 is the type of the elements to be linked into the list.
201 A pointer to the head of the list can later be declared as:
205 struct HEADNAME *headp;
213 are user selectable.)
217 declares a structure that connects the elements in
222 initializes the list referenced by
227 inserts the new element
229 at the head of the list.
233 inserts the new element
245 LIST_HEAD(listhead, entry) head;
246 struct listhead *headp; /* List head. */
249 LIST_ENTRY(entry) entries; /* List. */
253 LIST_INIT(&head); /* Initialize the list. */
255 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
256 LIST_INSERT_HEAD(&head, n1, entries);
258 n2 = malloc(sizeof(struct entry)); /* Insert after. */
259 LIST_INSERT_AFTER(n1, n2, entries);
260 /* Forward traversal. */
261 for (np = head.lh_first; np != NULL; np = np\->entries.le_next)
264 .\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=1506
265 while (head.lh_first != NULL) /* Delete. */
266 LIST_REMOVE(head.lh_first, entries);
269 A tail queue is headed by a structure defined by the
272 This structure contains a pair of pointers,
273 one to the first element in the tail queue and the other to
274 the last element in the tail queue.
275 The elements are doubly linked so that an arbitrary element can be
276 removed without traversing the tail queue.
277 New elements can be added to the tail queue after an existing element,
278 at the head of the tail queue, or at the end of the tail queue.
281 structure is declared as follows:
285 TAILQ_HEAD(HEADNAME, TYPE) head;
291 is the name of the structure to be defined, and
293 is the type of the elements to be linked into the tail queue.
294 A pointer to the head of the tail queue can later be declared as:
298 struct HEADNAME *headp;
306 are user selectable.)
310 declares a structure that connects the elements in
315 initializes the tail queue referenced by
320 inserts the new element
322 at the head of the tail queue.
326 inserts the new element
328 at the end of the tail queue.
331 .B TAILQ_INSERT_AFTER
332 inserts the new element
342 .SS Tail queue example
344 TAILQ_HEAD(tailhead, entry) head;
345 struct tailhead *headp; /* Tail queue head. */
348 TAILQ_ENTRY(entry) entries; /* Tail queue. */
352 TAILQ_INIT(&head); /* Initialize the queue. */
354 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
355 TAILQ_INSERT_HEAD(&head, n1, entries);
357 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
358 TAILQ_INSERT_TAIL(&head, n1, entries);
360 n2 = malloc(sizeof(struct entry)); /* Insert after. */
361 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
362 /* Forward traversal. */
363 for (np = head.tqh_first; np != NULL; np = np\->entries.tqe_next)
366 while (head.tqh_first != NULL)
367 TAILQ_REMOVE(&head, head.tqh_first, entries);
370 A circular queue is headed by a structure defined by the
373 This structure contains a pair of pointers,
374 one to the first element in the circular queue and the other to the
375 last element in the circular queue.
376 The elements are doubly linked so that an arbitrary element can be
377 removed without traversing the queue.
378 New elements can be added to the queue after an existing element,
379 before an existing element, at the head of the queue, or at the end
383 structure is declared as follows:
387 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
393 is the name of the structure to be defined, and
395 is the type of the elements to be linked into the circular queue.
396 A pointer to the head of the circular queue can later be declared as:
400 struct HEADNAME *headp;
408 are user selectable.)
412 declares a structure that connects the elements in
417 initializes the circular queue referenced by
421 .B CIRCLEQ_INSERT_HEAD
422 inserts the new element
424 at the head of the circular queue.
427 .B CIRCLEQ_INSERT_TAIL
428 inserts the new element
430 at the end of the circular queue.
433 .B CIRCLEQ_INSERT_AFTER
434 inserts the new element
440 .B CIRCLEQ_INSERT_BEFORE
441 inserts the new element
450 from the circular queue.
451 .SS Circular queue example
453 CIRCLEQ_HEAD(circleq, entry) head;
454 struct circleq *headp; /* Circular queue head. */
457 CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
461 CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
463 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
464 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
466 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
467 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
469 n2 = malloc(sizeof(struct entry)); /* Insert after. */
470 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
472 n2 = malloc(sizeof(struct entry)); /* Insert before. */
473 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
474 /* Forward traversal. */
475 for (np = head.cqh_first; np != (void *)&head;
476 np = np\->entries.cqe_next)
478 /* Reverse traversal. */
479 for (np = head.cqh_last; np != (void *)&head; np = np\->entries.cqe_prev)
482 while (head.cqh_first != (void *)&head)
483 CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
489 queue functions first appeared in