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