]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man3/queue.3
This commit was generated by cvs2svn to compensate for changes in r78828,
[FreeBSD/FreeBSD.git] / share / man / man3 / queue.3
1 .\" Copyright (c) 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
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.
19 .\"
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
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)queue.3     8.2 (Berkeley) 1/24/94
33 .\" $FreeBSD$
34 .\"
35 .Dd January 24, 1994
36 .Dt QUEUE 3
37 .Os BSD 4
38 .Sh NAME
39 .Nm SLIST_EMPTY ,
40 .Nm SLIST_ENTRY ,
41 .Nm SLIST_FIRST ,
42 .Nm SLIST_FOREACH ,
43 .Nm SLIST_HEAD ,
44 .Nm SLIST_HEAD_INITIALIZER ,
45 .Nm SLIST_INIT ,
46 .Nm SLIST_INSERT_AFTER ,
47 .Nm SLIST_INSERT_HEAD ,
48 .Nm SLIST_NEXT ,
49 .Nm SLIST_REMOVE_HEAD ,
50 .Nm SLIST_REMOVE ,
51 .Nm STAILQ_EMPTY ,
52 .Nm STAILQ_ENTRY ,
53 .Nm STAILQ_FIRST ,
54 .Nm STAILQ_FOREACH ,
55 .Nm STAILQ_HEAD ,
56 .Nm STAILQ_HEAD_INITIALIZER ,
57 .Nm STAILQ_INIT ,
58 .Nm STAILQ_INSERT_AFTER ,
59 .Nm STAILQ_INSERT_HEAD ,
60 .Nm STAILQ_INSERT_TAIL ,
61 .Nm STAILQ_LAST ,
62 .Nm STAILQ_NEXT ,
63 .Nm STAILQ_REMOVE_HEAD ,
64 .Nm STAILQ_REMOVE ,
65 .Nm LIST_EMPTY ,
66 .Nm LIST_ENTRY ,
67 .Nm LIST_FIRST ,
68 .Nm LIST_FOREACH ,
69 .Nm LIST_HEAD ,
70 .Nm LIST_HEAD_INITIALIZER ,
71 .Nm LIST_INIT ,
72 .Nm LIST_INSERT_AFTER ,
73 .Nm LIST_INSERT_BEFORE ,
74 .Nm LIST_INSERT_HEAD ,
75 .Nm LIST_NEXT ,
76 .Nm LIST_REMOVE ,
77 .Nm TAILQ_EMPTY ,
78 .Nm TAILQ_ENTRY ,
79 .Nm TAILQ_FIRST ,
80 .Nm TAILQ_FOREACH ,
81 .Nm TAILQ_FOREACH_REVERSE ,
82 .Nm TAILQ_HEAD ,
83 .Nm TAILQ_HEAD_INITIALIZER ,
84 .Nm TAILQ_INIT ,
85 .Nm TAILQ_INSERT_AFTER ,
86 .Nm TAILQ_INSERT_BEFORE ,
87 .Nm TAILQ_INSERT_HEAD ,
88 .Nm TAILQ_INSERT_TAIL ,
89 .Nm TAILQ_LAST ,
90 .Nm TAILQ_NEXT ,
91 .Nm TAILQ_PREV ,
92 .Nm TAILQ_REMOVE
93 .Nd implementations of singly-linked lists, singly-linked tail queues,
94 lists and tail queues
95 .Sh SYNOPSIS
96 .Fd #include <sys/queue.h>
97 .\"
98 .Fn SLIST_EMPTY "SLIST_HEAD *head"
99 .Fn SLIST_ENTRY "TYPE"
100 .Fn SLIST_FIRST "SLIST_HEAD *head"
101 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
102 .Fn SLIST_HEAD "HEADNAME" "TYPE"
103 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
104 .Fn SLIST_INIT "SLIST_HEAD *head"
105 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
106 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
107 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
108 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
109 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
110 .\"
111 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
112 .Fn STAILQ_ENTRY "TYPE"
113 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
114 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
115 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
116 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
117 .Fn STAILQ_INIT "STAILQ_HEAD *head"
118 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
119 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
120 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
121 .Fn STAILQ_LAST "STAILQ_HEAD *head"
122 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
123 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
124 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
125 .\"
126 .Fn LIST_EMPTY "LIST_HEAD *head"
127 .Fn LIST_ENTRY "TYPE"
128 .Fn LIST_FIRST "LIST_HEAD *head"
129 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
130 .Fn LIST_HEAD "HEADNAME" "TYPE"
131 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
132 .Fn LIST_INIT "LIST_HEAD *head"
133 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
134 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
135 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
136 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
137 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
138 .\"
139 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
140 .Fn TAILQ_ENTRY "TYPE"
141 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
142 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
143 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
144 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
145 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
146 .Fn TAILQ_INIT "TAILQ_HEAD *head"
147 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
148 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
149 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
150 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
151 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
152 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
153 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
154 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
155 .\"
156 .Sh DESCRIPTION
157 These macros define and operate on four types of data structures:
158 singly-linked lists, singly-linked tail queues, lists, and tail queues.
159 All four structures support the following functionality:
160 .Bl -enum -compact -offset indent
161 .It
162 Insertion of a new entry at the head of the list.
163 .It
164 Insertion of a new entry after any element in the list.
165 .It
166 O(1) removal of an entry from the head of the list.
167 .It
168 O(n) removal of any entry in the list.
169 .It
170 Forward traversal through the list.
171 .El
172 .Pp
173 Singly-linked lists are the simplest of the five data structures
174 and support only the above functionality.
175 Singly-linked lists are ideal for applications with large datasets
176 and few or no removals,
177 or for implementing a LIFO queue.
178 .Pp
179 Singly-linked tail queues add the following functionality:
180 .Bl -enum -compact -offset indent
181 .It
182 Entries can be added at the end of a list.
183 .El
184 However:
185 .Bl -enum -compact -offset indent
186 .It
187 All list insertions must specify the head of the list.
188 .It
189 Each head entry requires two pointers rather than one.
190 .It
191 Code size is about 15% greater and operations run about 20% slower
192 than singly-linked lists.
193 .El
194 .Pp
195 Singly-linked tailqs are ideal for applications with large datasets and
196 few or no removals,
197 or for implementing a FIFO queue.
198 .Pp
199 All doubly linked types of data structures (lists and tail queues)
200 additionally allow:
201 .Bl -enum -compact -offset indent
202 .It
203 Insertion of a new entry before any element in the list.
204 .It
205 O(1) removal of any entry in the list.
206 .El
207 However:
208 .Bl -enum -compact -offset indent
209 .It
210 Each elements requires two pointers rather than one.
211 .It
212 Code size and execution time of operations (except for removal) is about
213 twice that of the singly-linked data-structures.
214 .El
215 .Pp
216 Linked lists are the simplest of the doubly linked data structures and support
217 only the above functionality over singly-linked lists.
218 .Pp
219 Tail queues add the following functionality:
220 .Bl -enum -compact -offset indent
221 .It
222 Entries can be added at the end of a list.
223 .It
224 They may be traversed backwards, from tail to head.
225 .El
226 However:
227 .Bl -enum -compact -offset indent
228 .It
229 All list insertions and removals must specify the head of the list.
230 .It
231 Each head entry requires two pointers rather than one.
232 .It
233 Code size is about 15% greater and operations run about 20% slower
234 than singly-linked lists.
235 .El
236 .Pp
237 In the macro definitions,
238 .Fa TYPE
239 is the name of a user defined structure,
240 that must contain a field of type
241 .Li SLIST_ENTRY ,
242 .Li STAILQ_ENTRY ,
243 .Li LIST_ENTRY ,
244 or
245 .Li TAILQ_ENTRY ,
246 named
247 .Fa NAME .
248 The argument
249 .Fa HEADNAME
250 is the name of a user defined structure that must be declared
251 using the macros
252 .Li SLIST_HEAD ,
253 .Li STAILQ_HEAD ,
254 .Li LIST_HEAD ,
255 or
256 .Li TAILQ_HEAD .
257 See the examples below for further explanation of how these
258 macros are used.
259 .Sh SINGLY-LINKED LISTS
260 A singly-linked list is headed by a structure defined by the
261 .Nm SLIST_HEAD
262 macro.
263 This structure contains a single pointer to the first element
264 on the list.
265 The elements are singly linked for minimum space and pointer manipulation
266 overhead at the expense of O(n) removal for arbitrary elements.
267 New elements can be added to the list after an existing element or
268 at the head of the list.
269 An
270 .Fa SLIST_HEAD
271 structure is declared as follows:
272 .Bd -literal -offset indent
273 SLIST_HEAD(HEADNAME, TYPE) head;
274 .Ed
275 .Pp
276 where
277 .Fa HEADNAME
278 is the name of the structure to be defined, and
279 .Fa TYPE
280 is the type of the elements to be linked into the list.
281 A pointer to the head of the list can later be declared as:
282 .Bd -literal -offset indent
283 struct HEADNAME *headp;
284 .Ed
285 .Pp
286 (The names
287 .Li head
288 and
289 .Li headp
290 are user selectable.)
291 .Pp
292 The macro
293 .Nm SLIST_HEAD_INITIALIZER
294 evaluates to an initializer for the list
295 .Fa head .
296 .Pp
297 The macro
298 .Nm SLIST_EMPTY
299 evaluates to true if there are no elements in the list.
300 .Pp
301 The macro
302 .Nm SLIST_ENTRY
303 declares a structure that connects the elements in
304 the list.
305 .Pp
306 The macro
307 .Nm SLIST_FIRST
308 returns the first element in the list or NULL if the list is empty.
309 .Pp
310 The macro
311 .Nm SLIST_FOREACH
312 traverses the list referenced by
313 .Fa head
314 in the forward direction, assigning each element in
315 turn to
316 .Fa var .
317 .Pp
318 The macro
319 .Nm SLIST_INIT
320 initializes the list referenced by
321 .Fa head .
322 .Pp
323 The macro
324 .Nm SLIST_INSERT_HEAD
325 inserts the new element
326 .Fa elm
327 at the head of the list.
328 .Pp
329 The macro
330 .Nm SLIST_INSERT_AFTER
331 inserts the new element
332 .Fa elm
333 after the element
334 .Fa listelm .
335 .Pp
336 The macro
337 .Nm SLIST_NEXT
338 returns the next element in the list.
339 .Pp
340 The macro
341 .Nm SLIST_REMOVE_HEAD
342 removes the element
343 .Fa elm
344 from the head of the list.
345 For optimum efficiency,
346 elements being removed from the head of the list should explicitly use
347 this macro instead of the generic 
348 .Fa SLIST_REMOVE
349 macro.
350 .Pp
351 The macro
352 .Nm SLIST_REMOVE
353 removes the element
354 .Fa elm
355 from the list.
356 .Sh SINGLY-LINKED LIST EXAMPLE
357 .Bd -literal
358 SLIST_HEAD(slisthead, entry) head =
359     SLIST_HEAD_INITIALIZER(head);
360 struct slisthead *headp;                /* Singly-linked List head. */
361 struct entry {
362         ...
363         SLIST_ENTRY(entry) entries;     /* Singly-linked List. */
364         ...
365 } *n1, *n2, *n3, *np;
366
367 SLIST_INIT(&head);                      /* Initialize the list. */
368
369 n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
370 SLIST_INSERT_HEAD(&head, n1, entries);
371
372 n2 = malloc(sizeof(struct entry));      /* Insert after. */
373 SLIST_INSERT_AFTER(n1, n2, entries);
374
375 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
376 free(n2);
377
378 n3 = SLIST_FIRST(&head);
379 SLIST_REMOVE_HEAD(&head, entries);      /* Deletion from the head. */
380 free(n3);
381                                         /* Forward traversal. */
382 SLIST_FOREACH(np, &head, entries)
383         np-> ...
384
385 while (!SLIST_EMPTY(&head)) {           /* List Deletion. */
386         n1 = SLIST_FIRST(&head);
387         SLIST_REMOVE_HEAD(&head, entries);
388         free(n1);
389 }
390 .Ed
391 .Sh SINGLY-LINKED TAIL QUEUES
392 A singly-linked tail queue is headed by a structure defined by the
393 .Nm STAILQ_HEAD
394 macro.
395 This structure contains a pair of pointers,
396 one to the first element in the tail queue and the other to
397 the last element in the tail queue.
398 The elements are singly linked for minimum space and pointer
399 manipulation overhead at the expense of O(n) removal for arbitrary
400 elements.
401 New elements can be added to the tail queue after an existing element,
402 at the head of the tail queue, or at the end of the tail queue.
403 A
404 .Fa STAILQ_HEAD
405 structure is declared as follows:
406 .Bd -literal -offset indent
407 STAILQ_HEAD(HEADNAME, TYPE) head;
408 .Ed
409 .Pp
410 where
411 .Li HEADNAME
412 is the name of the structure to be defined, and
413 .Li TYPE
414 is the type of the elements to be linked into the tail queue.
415 A pointer to the head of the tail queue can later be declared as:
416 .Bd -literal -offset indent
417 struct HEADNAME *headp;
418 .Ed
419 .Pp
420 (The names
421 .Li head
422 and
423 .Li headp
424 are user selectable.)
425 .Pp
426 The macro
427 .Nm STAILQ_HEAD_INITIALIZER
428 evaluates to an initializer for the tail queue
429 .Fa head .
430 .Pp
431 The macro
432 .Nm STAILQ_EMPTY
433 evaluates to true if there are no items on the tail queue.
434 .Pp
435 The macro
436 .Nm STAILQ_ENTRY
437 declares a structure that connects the elements in
438 the tail queue.
439 .Pp
440 The macro
441 .Nm STAILQ_FIRST
442 returns the first item on the tail queue or NULL if the tail queue
443 is empty.
444 .Pp
445 The macro
446 .Nm STAILQ_FOREACH
447 traverses the tail queue referenced by
448 .Fa head
449 in the forward direction, assigning each element
450 in turn to
451 .Fa var .
452 .Pp
453 The macro
454 .Nm STAILQ_INIT
455 initializes the tail queue referenced by
456 .Fa head .
457 .Pp
458 The macro
459 .Nm STAILQ_INSERT_HEAD
460 inserts the new element
461 .Fa elm
462 at the head of the tail queue.
463 .Pp
464 The macro
465 .Nm STAILQ_INSERT_TAIL
466 inserts the new element
467 .Fa elm
468 at the end of the tail queue.
469 .Pp
470 The macro
471 .Nm STAILQ_INSERT_AFTER
472 inserts the new element
473 .Fa elm
474 after the element
475 .Fa listelm .
476 .Pp
477 The macro
478 .Nm STAILQ_LAST
479 returns the last item on the tail queue.
480 If the tail queue is empty the return value is undefined.
481 .Pp
482 The macro
483 .Nm STAILQ_NEXT
484 returns the next item on the tail queue, or NULL this item is the last.
485 .Pp
486 The macro
487 .Nm STAILQ_REMOVE_HEAD
488 removes the element at the head of the tail queue.
489 For optimum efficiency,
490 elements being removed from the head of the tail queue should
491 use this macro explicitly rather than the generic 
492 .Fa STAILQ_REMOVE
493 macro.
494 .Pp
495 The macro
496 .Nm STAILQ_REMOVE
497 removes the element
498 .Fa elm
499 from the tail queue.
500 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
501 .Bd -literal
502 STAILQ_HEAD(stailhead, entry) head =
503     STAILQ_HEAD_INITIALIZER(head);
504 struct stailhead *headp;                /* Singly-linked tail queue head. */
505 struct entry {
506         ...
507         STAILQ_ENTRY(entry) entries;    /* Tail queue. */
508         ...
509 } *n1, *n2, *n3, *np;
510
511 STAILQ_INIT(&head);                     /* Initialize the queue. */
512
513 n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
514 STAILQ_INSERT_HEAD(&head, n1, entries);
515
516 n1 = malloc(sizeof(struct entry));      /* Insert at the tail. */
517 STAILQ_INSERT_TAIL(&head, n1, entries);
518
519 n2 = malloc(sizeof(struct entry));      /* Insert after. */
520 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
521                                         /* Deletion. */
522 STAILQ_REMOVE(&head, n2, entry, entries);
523 free(n2);
524                                         /* Deletion from the head. */
525 n3 = STAILQ_FIRST(&head);
526 STAILQ_REMOVE_HEAD(&head, entries);
527 free(n3);
528                                         /* Forward traversal. */
529 STAILQ_FOREACH(np, &head, entries)
530         np-> ...
531                                         /* TailQ Deletion. */
532 while (!STAILQ_EMPTY(&head)) {
533         n1 = STAILQ_FIRST(&head);
534         STAILQ_REMOVE_HEAD(&head, entries);
535         free(n1);
536 }
537                                         /* Faster TailQ Deletion. */
538 n1 = STAILQ_FIRST(&head);
539 while (n1 != NULL) {
540         n2 = STAILQ_NEXT(n1, entries);
541         free(n1);
542         n1 = n2;
543 }
544 STAILQ_INIT(&head);
545 .Ed
546 .Sh LISTS
547 A list is headed by a structure defined by the
548 .Nm LIST_HEAD
549 macro.
550 This structure contains a single pointer to the first element
551 on the list.
552 The elements are doubly linked so that an arbitrary element can be
553 removed without traversing the list.
554 New elements can be added to the list after an existing element,
555 before an existing element, or at the head of the list.
556 A
557 .Fa LIST_HEAD
558 structure is declared as follows:
559 .Bd -literal -offset indent
560 LIST_HEAD(HEADNAME, TYPE) head;
561 .Ed
562 .Pp
563 where
564 .Fa HEADNAME
565 is the name of the structure to be defined, and
566 .Fa TYPE
567 is the type of the elements to be linked into the list.
568 A pointer to the head of the list can later be declared as:
569 .Bd -literal -offset indent
570 struct HEADNAME *headp;
571 .Ed
572 .Pp
573 (The names
574 .Li head
575 and
576 .Li headp
577 are user selectable.)
578 .Pp
579 The macro
580 .Nm LIST_HEAD_INITIALIZER
581 evaluates to an initializer for the list
582 .Fa head .
583 .Pp
584 The macro
585 .Nm LIST_EMPTY
586 evaluates to true if their are no elements in the list.
587 .Pp
588 The macro
589 .Nm LIST_ENTRY
590 declares a structure that connects the elements in
591 the list.
592 .Pp
593 The macro
594 .Nm LIST_FIRST
595 returns the first element in the list or NULL if the list
596 is empty.
597 .Pp
598 The macro
599 .Nm LIST_FOREACH
600 traverses the list referenced by
601 .Fa head
602 in the forward direction, assigning each element in turn to
603 .Fa var .
604 .Pp
605 The macro
606 .Nm LIST_INIT
607 initializes the list referenced by
608 .Fa head .
609 .Pp
610 The macro
611 .Nm LIST_INSERT_HEAD
612 inserts the new element
613 .Fa elm
614 at the head of the list.
615 .Pp
616 The macro
617 .Nm LIST_INSERT_AFTER
618 inserts the new element
619 .Fa elm
620 after the element
621 .Fa listelm .
622 .Pp
623 The macro
624 .Nm LIST_INSERT_BEFORE
625 inserts the new element
626 .Fa elm
627 before the element
628 .Fa listelm .
629 .Pp
630 The macro
631 .Nm LIST_NEXT
632 returns the next element in the list, or NULL if this is the last.
633 .Pp
634 The macro
635 .Nm LIST_REMOVE
636 removes the element
637 .Fa elm
638 from the list.
639 .Sh LIST EXAMPLE
640 .Bd -literal
641 LIST_HEAD(listhead, entry) head =
642     LIST_HEAD_INITIALIZER(head);
643 struct listhead *headp;                 /* List head. */
644 struct entry {
645         ...
646         LIST_ENTRY(entry) entries;      /* List. */
647         ...
648 } *n1, *n2, *n3, *np;
649
650 LIST_INIT(&head);                       /* Initialize the list. */
651
652 n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
653 LIST_INSERT_HEAD(&head, n1, entries);
654
655 n2 = malloc(sizeof(struct entry));      /* Insert after. */
656 LIST_INSERT_AFTER(n1, n2, entries);
657
658 n3 = malloc(sizeof(struct entry));      /* Insert before. */
659 LIST_INSERT_BEFORE(n2, n3, entries);
660
661 LIST_REMOVE(n2, entries);               /* Deletion. */
662 free(n2);
663                                         /* Forward traversal. */
664 LIST_FOREACH(np, &head, entries)
665         np-> ...
666
667 while (!LIST_EMPTY(&head)) {            /* List Deletion. */
668         n1 = LIST_FIRST(&head);
669         LIST_REMOVE(n1, entries);
670         free(n1);
671 }
672
673 n1 = LIST_FIRST(&head);                 /* Faster List Deletion. */
674 while (n1 != NULL) {
675         n2 = LIST_NEXT(n1, entries);
676         free(n1);
677         n1 = n2;
678 }
679 LIST_INIT(&head);
680 .Ed
681 .Sh TAIL QUEUES
682 A tail queue is headed by a structure defined by the
683 .Nm TAILQ_HEAD
684 macro.
685 This structure contains a pair of pointers,
686 one to the first element in the tail queue and the other to
687 the last element in the tail queue.
688 The elements are doubly linked so that an arbitrary element can be
689 removed without traversing the tail queue.
690 New elements can be added to the tail queue after an existing element,
691 before an existing element, at the head of the tail queue,
692 or at the end of the tail queue.
693 A
694 .Fa TAILQ_HEAD
695 structure is declared as follows:
696 .Bd -literal -offset indent
697 TAILQ_HEAD(HEADNAME, TYPE) head;
698 .Ed
699 .Pp
700 where
701 .Li HEADNAME
702 is the name of the structure to be defined, and
703 .Li TYPE
704 is the type of the elements to be linked into the tail queue.
705 A pointer to the head of the tail queue can later be declared as:
706 .Bd -literal -offset indent
707 struct HEADNAME *headp;
708 .Ed
709 .Pp
710 (The names
711 .Li head
712 and
713 .Li headp
714 are user selectable.)
715 .Pp
716 The macro
717 .Nm TAILQ_HEAD_INITIALIZER
718 evaluates to an initializer for the tail queue
719 .Fa head .
720 .Pp
721 The macro
722 .Nm TAILQ_EMPTY
723 evaluates to true if there are no items on the tail queue.
724 .Pp
725 The macro
726 .Nm TAILQ_ENTRY
727 declares a structure that connects the elements in
728 the tail queue.
729 .Pp
730 The macro
731 .Nm TAILQ_FIRST
732 returns the first item on the tail queue or NULL if the tail queue
733 is empty.
734 .Pp
735 The macro
736 .Nm TAILQ_FOREACH
737 traverses the tail queue referenced by
738 .Fa head
739 in the forward direction, assigning each element in turn to
740 .Fa var .
741 .Pp
742 The macro
743 .Nm TAILQ_FOREACH_REVERSE
744 traverses the tail queue referenced by
745 .Fa head
746 in the reverse direction, assigning each element in turn to
747 .Fa var .
748 .Pp
749 The macro
750 .Nm TAILQ_INIT
751 initializes the tail queue referenced by
752 .Fa head .
753 .Pp
754 The macro
755 .Nm TAILQ_INSERT_HEAD
756 inserts the new element
757 .Fa elm
758 at the head of the tail queue.
759 .Pp
760 The macro
761 .Nm TAILQ_INSERT_TAIL
762 inserts the new element
763 .Fa elm
764 at the end of the tail queue.
765 .Pp
766 The macro
767 .Nm TAILQ_INSERT_AFTER
768 inserts the new element
769 .Fa elm
770 after the element
771 .Fa listelm .
772 .Pp
773 The macro
774 .Nm TAILQ_INSERT_BEFORE
775 inserts the new element
776 .Fa elm
777 before the element
778 .Fa listelm .
779 .Pp
780 The macro
781 .Nm TAILQ_LAST
782 returns the last item on the tail queue.
783 If the tail queue is empty the return value is undefined.
784 .Pp
785 The macro
786 .Nm TAILQ_NEXT
787 returns the next item on the tail queue, or NULL if this item is the last.
788 .Pp
789 The macro
790 .Nm TAILQ_PREV
791 returns the previous item on the tail queue, or NULL if this item
792 is the first.
793 .Pp
794 The macro
795 .Nm TAILQ_REMOVE
796 removes the element
797 .Fa elm
798 from the tail queue.
799 .Sh TAIL QUEUE EXAMPLE
800 .Bd -literal
801 TAILQ_HEAD(tailhead, entry) head =
802     TAILQ_HEAD_INITIALIZER(head);
803 struct tailhead *headp;                 /* Tail queue head. */
804 struct entry {
805         ...
806         TAILQ_ENTRY(entry) entries;     /* Tail queue. */
807         ...
808 } *n1, *n2, *n3, *np;
809
810 TAILQ_INIT(&head);                      /* Initialize the queue. */
811
812 n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
813 TAILQ_INSERT_HEAD(&head, n1, entries);
814
815 n1 = malloc(sizeof(struct entry));      /* Insert at the tail. */
816 TAILQ_INSERT_TAIL(&head, n1, entries);
817
818 n2 = malloc(sizeof(struct entry));      /* Insert after. */
819 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
820
821 n3 = malloc(sizeof(struct entry));      /* Insert before. */
822 TAILQ_INSERT_BEFORE(n2, n3, entries);
823
824 TAILQ_REMOVE(&head, n2, entries);       /* Deletion. */
825 free(n2);
826                                         /* Forward traversal. */
827 TAILQ_FOREACH(np, &head, entries)
828         np-> ...
829                                         /* Reverse traversal. */
830 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
831         np-> ...
832                                         /* TailQ Deletion. */
833 while (!TAILQ_EMPTY(&head)) {
834         n1 = TAILQ_FIRST(&head);
835         TAILQ_REMOVE(&head, n1, entries);
836         free(n1);
837 }
838                                         /* Faster TailQ Deletion. */
839 n1 = TAILQ_FIRST(&head);
840 while (n1 != NULL) {
841         n2 = TAILQ_NEXT(n1, entries);
842         free(n1);
843         n1 = n2;
844 }
845 TAILQ_INIT(&head);
846 .Ed
847 .Sh HISTORY
848 The
849 .Nm queue
850 functions first appeared in
851 .Bx 4.4 .