1 Below follows the manpage for tor_queue.h, as included with OpenBSD's
2 sys/queue.h. License follows at the end of the file.
4 ======================================================================
5 QUEUE(3) OpenBSD Programmer's Manual QUEUE(3)
8 SLIST_ENTRY, SLIST_HEAD, SLIST_HEAD_INITIALIZER, SLIST_FIRST, SLIST_NEXT,
9 SLIST_END, SLIST_EMPTY, SLIST_FOREACH, SLIST_FOREACH_SAFE, SLIST_INIT,
10 SLIST_INSERT_AFTER, SLIST_INSERT_HEAD, SLIST_REMOVE_AFTER,
11 SLIST_REMOVE_HEAD, SLIST_REMOVE, LIST_ENTRY, LIST_HEAD,
12 LIST_HEAD_INITIALIZER, LIST_FIRST, LIST_NEXT, LIST_END, LIST_EMPTY,
13 LIST_FOREACH, LIST_FOREACH_SAFE, LIST_INIT, LIST_INSERT_AFTER,
14 LIST_INSERT_BEFORE, LIST_INSERT_HEAD, LIST_REMOVE, LIST_REPLACE,
15 SIMPLEQ_ENTRY, SIMPLEQ_HEAD, SIMPLEQ_HEAD_INITIALIZER, SIMPLEQ_FIRST,
16 SIMPLEQ_NEXT, SIMPLEQ_END, SIMPLEQ_EMPTY, SIMPLEQ_FOREACH,
17 SIMPLEQ_FOREACH_SAFE, SIMPLEQ_INIT, SIMPLEQ_INSERT_AFTER,
18 SIMPLEQ_INSERT_HEAD, SIMPLEQ_INSERT_TAIL, SIMPLEQ_REMOVE_AFTER,
19 SIMPLEQ_REMOVE_HEAD, TAILQ_ENTRY, TAILQ_HEAD, TAILQ_HEAD_INITIALIZER,
20 TAILQ_FIRST, TAILQ_NEXT, TAILQ_END, TAILQ_LAST, TAILQ_PREV, TAILQ_EMPTY,
21 TAILQ_FOREACH, TAILQ_FOREACH_SAFE, TAILQ_FOREACH_REVERSE,
22 TAILQ_FOREACH_REVERSE_SAFE, TAILQ_INIT, TAILQ_INSERT_AFTER,
23 TAILQ_INSERT_BEFORE, TAILQ_INSERT_HEAD, TAILQ_INSERT_TAIL, TAILQ_REMOVE,
24 TAILQ_REPLACE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_HEAD_INITIALIZER,
25 CIRCLEQ_FIRST, CIRCLEQ_LAST, CIRCLEQ_END, CIRCLEQ_NEXT, CIRCLEQ_PREV,
26 CIRCLEQ_EMPTY, CIRCLEQ_FOREACH, CIRCLEQ_FOREACH_SAFE,
27 CIRCLEQ_FOREACH_REVERSE_SAFE, CIRCLEQ_INIT, CIRCLEQ_INSERT_AFTER,
28 CIRCLEQ_INSERT_BEFORE, CIRCLEQ_INSERT_HEAD, CIRCLEQ_INSERT_TAIL,
29 CIRCLEQ_REMOVE, CIRCLEQ_REPLACE - implementations of singly-linked lists,
30 doubly-linked lists, simple queues, tail queues, and circular queues
33 #include <sys/queue.h>
37 SLIST_HEAD(HEADNAME, TYPE);
39 SLIST_HEAD_INITIALIZER(SLIST_HEAD head);
42 SLIST_FIRST(SLIST_HEAD *head);
45 SLIST_NEXT(struct TYPE *listelm, SLIST_ENTRY NAME);
48 SLIST_END(SLIST_HEAD *head);
51 SLIST_EMPTY(SLIST_HEAD *head);
53 SLIST_FOREACH(VARNAME, SLIST_HEAD *head, SLIST_ENTRY NAME);
55 SLIST_FOREACH_SAFE(VARNAME, SLIST_HEAD *head, SLIST_ENTRY
59 SLIST_INIT(SLIST_HEAD *head);
62 SLIST_INSERT_AFTER(struct TYPE *listelm, struct TYPE *elm, SLIST_ENTRY
66 SLIST_INSERT_HEAD(SLIST_HEAD *head, struct TYPE *elm, SLIST_ENTRY NAME);
69 SLIST_REMOVE_AFTER(struct TYPE *elm, SLIST_ENTRY NAME);
72 SLIST_REMOVE_HEAD(SLIST_HEAD *head, SLIST_ENTRY NAME);
75 SLIST_REMOVE(SLIST_HEAD *head, struct TYPE *elm, TYPE, SLIST_ENTRY NAME);
79 LIST_HEAD(HEADNAME, TYPE);
81 LIST_HEAD_INITIALIZER(LIST_HEAD head);
84 LIST_FIRST(LIST_HEAD *head);
87 LIST_NEXT(struct TYPE *listelm, LIST_ENTRY NAME);
90 LIST_END(LIST_HEAD *head);
93 LIST_EMPTY(LIST_HEAD *head);
95 LIST_FOREACH(VARNAME, LIST_HEAD *head, LIST_ENTRY NAME);
97 LIST_FOREACH_SAFE(VARNAME, LIST_HEAD *head, LIST_ENTRY
101 LIST_INIT(LIST_HEAD *head);
104 LIST_INSERT_AFTER(struct TYPE *listelm, struct TYPE *elm, LIST_ENTRY
108 LIST_INSERT_BEFORE(struct TYPE *listelm, struct TYPE *elm, LIST_ENTRY
112 LIST_INSERT_HEAD(LIST_HEAD *head, struct TYPE *elm, LIST_ENTRY NAME);
115 LIST_REMOVE(struct TYPE *elm, LIST_ENTRY NAME);
118 LIST_REPLACE(struct TYPE *elm, struct TYPE *elm2, LIST_ENTRY NAME);
122 SIMPLEQ_HEAD(HEADNAME, TYPE);
124 SIMPLEQ_HEAD_INITIALIZER(SIMPLEQ_HEAD head);
127 SIMPLEQ_FIRST(SIMPLEQ_HEAD *head);
130 SIMPLEQ_NEXT(struct TYPE *listelm, SIMPLEQ_ENTRY NAME);
133 SIMPLEQ_END(SIMPLEQ_HEAD *head);
136 SIMPLEQ_EMPTY(SIMPLEQ_HEAD *head);
138 SIMPLEQ_FOREACH(VARNAME, SIMPLEQ_HEAD *head, SIMPLEQ_ENTRY NAME);
140 SIMPLEQ_FOREACH_SAFE(VARNAME, SIMPLEQ_HEAD *head, SIMPLEQ_ENTRY
144 SIMPLEQ_INIT(SIMPLEQ_HEAD *head);
147 SIMPLEQ_INSERT_AFTER(SIMPLEQ_HEAD *head, struct TYPE *listelm, struct
148 TYPE *elm, SIMPLEQ_ENTRY NAME);
151 SIMPLEQ_INSERT_HEAD(SIMPLEQ_HEAD *head, struct TYPE *elm, SIMPLEQ_ENTRY
155 SIMPLEQ_INSERT_TAIL(SIMPLEQ_HEAD *head, struct TYPE *elm, SIMPLEQ_ENTRY
159 SIMPLEQ_REMOVE_AFTER(SIMPLEQ_HEAD *head, struct TYPE *elm, SIMPLEQ_ENTRY
163 SIMPLEQ_REMOVE_HEAD(SIMPLEQ_HEAD *head, SIMPLEQ_ENTRY NAME);
167 TAILQ_HEAD(HEADNAME, TYPE);
169 TAILQ_HEAD_INITIALIZER(TAILQ_HEAD head);
172 TAILQ_FIRST(TAILQ_HEAD *head);
175 TAILQ_NEXT(struct TYPE *listelm, TAILQ_ENTRY NAME);
178 TAILQ_END(TAILQ_HEAD *head);
181 TAILQ_LAST(TAILQ_HEAD *head, HEADNAME NAME);
184 TAILQ_PREV(struct TYPE *listelm, HEADNAME NAME, TAILQ_ENTRY NAME);
187 TAILQ_EMPTY(TAILQ_HEAD *head);
189 TAILQ_FOREACH(VARNAME, TAILQ_HEAD *head, TAILQ_ENTRY NAME);
191 TAILQ_FOREACH_SAFE(VARNAME, TAILQ_HEAD *head, TAILQ_ENTRY
194 TAILQ_FOREACH_REVERSE(VARNAME, TAILQ_HEAD *head, HEADNAME, TAILQ_ENTRY
197 TAILQ_FOREACH_REVERSE_SAFE(VARNAME, TAILQ_HEAD
198 *head, HEADNAME, TAILQ_ENTRY NAME, TEMP_VARNAME);
201 TAILQ_INIT(TAILQ_HEAD *head);
204 TAILQ_INSERT_AFTER(TAILQ_HEAD *head, struct TYPE *listelm, struct TYPE
205 *elm, TAILQ_ENTRY NAME);
208 TAILQ_INSERT_BEFORE(struct TYPE *listelm, struct TYPE *elm, TAILQ_ENTRY
212 TAILQ_INSERT_HEAD(TAILQ_HEAD *head, struct TYPE *elm, TAILQ_ENTRY NAME);
215 TAILQ_INSERT_TAIL(TAILQ_HEAD *head, struct TYPE *elm, TAILQ_ENTRY NAME);
218 TAILQ_REMOVE(TAILQ_HEAD *head, struct TYPE *elm, TAILQ_ENTRY NAME);
221 TAILQ_REPLACE(TAILQ_HEAD *head, struct TYPE *elm, struct TYPE
222 *elm2, TAILQ_ENTRY NAME);
226 CIRCLEQ_HEAD(HEADNAME, TYPE);
228 CIRCLEQ_HEAD_INITIALIZER(CIRCLEQ_HEAD head);
231 CIRCLEQ_FIRST(CIRCLEQ_HEAD *head);
234 CIRCLEQ_LAST(CIRCLEQ_HEAD *head);
237 CIRCLEQ_END(CIRCLEQ_HEAD *head);
240 CIRCLEQ_NEXT(struct TYPE *listelm, CIRCLEQ_ENTRY NAME);
243 CIRCLEQ_PREV(struct TYPE *listelm, CIRCLEQ_ENTRY NAME);
246 CIRCLEQ_EMPTY(CIRCLEQ_HEAD *head);
248 CIRCLEQ_FOREACH(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY NAME);
250 CIRCLEQ_FOREACH_SAFE(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY
253 CIRCLEQ_FOREACH_REVERSE(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY NAME);
255 CIRCLEQ_FOREACH_REVERSE_SAFE(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY
259 CIRCLEQ_INIT(CIRCLEQ_HEAD *head);
262 CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *head, struct TYPE *listelm, struct
263 TYPE *elm, CIRCLEQ_ENTRY NAME);
266 CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *head, struct TYPE *listelm, struct
267 TYPE *elm, CIRCLEQ_ENTRY NAME);
270 CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *head, struct TYPE *elm, CIRCLEQ_ENTRY
274 CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *head, struct TYPE *elm, CIRCLEQ_ENTRY
278 CIRCLEQ_REMOVE(CIRCLEQ_HEAD *head, struct TYPE *elm, CIRCLEQ_ENTRY NAME);
281 CIRCLEQ_REPLACE(CIRCLEQ_HEAD *head, struct TYPE *elm, struct TYPE
282 *elm2, CIRCLEQ_ENTRY NAME);
285 These macros define and operate on five types of data structures: singly-
286 linked lists, simple queues, lists, tail queues, and circular queues.
287 All five structures support the following functionality:
289 1. Insertion of a new entry at the head of the list.
290 2. Insertion of a new entry after any element in the list.
291 3. Removal of an entry from the head of the list.
292 4. Forward traversal through the list.
294 Singly-linked lists are the simplest of the five data structures and
295 support only the above functionality. Singly-linked lists are ideal for
296 applications with large datasets and few or no removals, or for
297 implementing a LIFO queue.
299 Simple queues add the following functionality:
301 1. Entries can be added at the end of a list.
305 1. All list insertions must specify the head of the list.
306 2. Each head entry requires two pointers rather than one.
307 3. Code size is about 15% greater and operations run about 20%
308 slower than singly-linked lists.
310 Simple queues are ideal for applications with large datasets and few or
311 no removals, or for implementing a FIFO queue.
313 All doubly linked types of data structures (lists, tail queues, and
314 circle queues) additionally allow:
316 1. Insertion of a new entry before any element in the list.
317 2. Removal of any entry in the list.
321 1. Each element requires two pointers rather than one.
322 2. Code size and execution time of operations (except for
323 removal) is about twice that of the singly-linked data-
326 Lists are the simplest of the doubly linked data structures and support
327 only the above functionality over singly-linked lists.
329 Tail queues add the following functionality:
331 1. Entries can be added at the end of a list.
332 2. They may be traversed backwards, at a cost.
336 1. All list insertions and removals must specify the head of the
338 2. Each head entry requires two pointers rather than one.
339 3. Code size is about 15% greater and operations run about 20%
340 slower than singly-linked lists.
342 Circular queues add the following functionality:
344 1. Entries can be added at the end of a list.
345 2. They may be traversed backwards, from tail to head.
349 1. All list insertions and removals must specify the head of the
351 2. Each head entry requires two pointers rather than one.
352 3. The termination condition for traversal is more complex.
353 4. Code size is about 40% greater and operations run about 45%
356 In the macro definitions, TYPE is the name tag of a user defined
357 structure that must contain a field of type SLIST_ENTRY, LIST_ENTRY,
358 SIMPLEQ_ENTRY, TAILQ_ENTRY, or CIRCLEQ_ENTRY, named NAME. The argument
359 HEADNAME is the name tag of a user defined structure that must be
360 declared using the macros SLIST_HEAD(), LIST_HEAD(), SIMPLEQ_HEAD(),
361 TAILQ_HEAD(), or CIRCLEQ_HEAD(). See the examples below for further
362 explanation of how these macros are used.
365 A singly-linked list is headed by a structure defined by the SLIST_HEAD()
366 macro. This structure contains a single pointer to the first element on
367 the list. The elements are singly linked for minimum space and pointer
368 manipulation overhead at the expense of O(n) removal for arbitrary
369 elements. New elements can be added to the list after an existing
370 element or at the head of the list. A SLIST_HEAD structure is declared
373 SLIST_HEAD(HEADNAME, TYPE) head;
375 where HEADNAME is the name of the structure to be defined, and struct
376 TYPE is the type of the elements to be linked into the list. A pointer
377 to the head of the list can later be declared as:
379 struct HEADNAME *headp;
381 (The names head and headp are user selectable.)
383 The HEADNAME facility is often not used, leading to the following bizarre
386 SLIST_HEAD(, TYPE) head, *headp;
388 The SLIST_ENTRY() macro declares a structure that connects the elements
391 The SLIST_INIT() macro initializes the list referenced by head.
393 The list can also be initialized statically by using the
394 SLIST_HEAD_INITIALIZER() macro like this:
396 SLIST_HEAD(HEADNAME, TYPE) head = SLIST_HEAD_INITIALIZER(head);
398 The SLIST_INSERT_HEAD() macro inserts the new element elm at the head of
401 The SLIST_INSERT_AFTER() macro inserts the new element elm after the
404 The SLIST_REMOVE_HEAD() macro removes the first element of the list
407 The SLIST_REMOVE_AFTER() macro removes the list element immediately
410 The SLIST_REMOVE() macro removes the element elm of the list pointed by
413 The SLIST_FIRST() and SLIST_NEXT() macros can be used to traverse the
416 for (np = SLIST_FIRST(&head); np != NULL; np = SLIST_NEXT(np, NAME))
418 Or, for simplicity, one can use the SLIST_FOREACH() macro:
420 SLIST_FOREACH(np, head, NAME)
422 The macro SLIST_FOREACH_SAFE() traverses the list referenced by head in a
423 forward direction, assigning each element in turn to var. However,
424 unlike SLIST_FOREACH() it is permitted to remove var as well as free it
425 from within the loop safely without interfering with the traversal.
427 The SLIST_EMPTY() macro should be used to check whether a simple list is
430 SINGLY-LINKED LIST EXAMPLE
431 SLIST_HEAD(listhead, entry) head;
434 SLIST_ENTRY(entry) entries; /* Simple list. */
438 SLIST_INIT(&head); /* Initialize simple list. */
440 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
441 SLIST_INSERT_HEAD(&head, n1, entries);
443 n2 = malloc(sizeof(struct entry)); /* Insert after. */
444 SLIST_INSERT_AFTER(n1, n2, entries);
446 SLIST_FOREACH(np, &head, entries) /* Forward traversal. */
449 while (!SLIST_EMPTY(&head)) { /* Delete. */
450 n1 = SLIST_FIRST(&head);
451 SLIST_REMOVE_HEAD(&head, entries);
457 A list is headed by a structure defined by the LIST_HEAD() macro. This
458 structure contains a single pointer to the first element on the list.
459 The elements are doubly linked so that an arbitrary element can be
460 removed without traversing the list. New elements can be added to the
461 list after an existing element, before an existing element, or at the
462 head of the list. A LIST_HEAD structure is declared as follows:
464 LIST_HEAD(HEADNAME, TYPE) head;
466 where HEADNAME is the name of the structure to be defined, and struct
467 TYPE is the type of the elements to be linked into the list. A pointer
468 to the head of the list can later be declared as:
470 struct HEADNAME *headp;
472 (The names head and headp are user selectable.)
474 The HEADNAME facility is often not used, leading to the following bizarre
477 LIST_HEAD(, TYPE) head, *headp;
479 The LIST_ENTRY() macro declares a structure that connects the elements in
482 The LIST_INIT() macro initializes the list referenced by head.
484 The list can also be initialized statically by using the
485 LIST_HEAD_INITIALIZER() macro like this:
487 LIST_HEAD(HEADNAME, TYPE) head = LIST_HEAD_INITIALIZER(head);
489 The LIST_INSERT_HEAD() macro inserts the new element elm at the head of
492 The LIST_INSERT_AFTER() macro inserts the new element elm after the
495 The LIST_INSERT_BEFORE() macro inserts the new element elm before the
498 The LIST_REMOVE() macro removes the element elm from the list.
500 The LIST_REPLACE() macro replaces the list element elm with the new
503 The LIST_FIRST() and LIST_NEXT() macros can be used to traverse the list:
505 for (np = LIST_FIRST(&head); np != NULL; np = LIST_NEXT(np, NAME))
507 Or, for simplicity, one can use the LIST_FOREACH() macro:
509 LIST_FOREACH(np, head, NAME)
511 The macro LIST_FOREACH_SAFE() traverses the list referenced by head in a
512 forward direction, assigning each element in turn to var. However,
513 unlike LIST_FOREACH() it is permitted to remove var as well as free it
514 from within the loop safely without interfering with the traversal.
516 The LIST_EMPTY() macro should be used to check whether a list is empty.
519 LIST_HEAD(listhead, entry) head;
522 LIST_ENTRY(entry) entries; /* List. */
526 LIST_INIT(&head); /* Initialize list. */
528 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
529 LIST_INSERT_HEAD(&head, n1, entries);
531 n2 = malloc(sizeof(struct entry)); /* Insert after. */
532 LIST_INSERT_AFTER(n1, n2, entries);
534 n2 = malloc(sizeof(struct entry)); /* Insert before. */
535 LIST_INSERT_BEFORE(n1, n2, entries);
536 /* Forward traversal. */
537 LIST_FOREACH(np, &head, entries)
540 while (!LIST_EMPTY(&head)) /* Delete. */
541 n1 = LIST_FIRST(&head);
542 LIST_REMOVE(n1, entries);
547 A simple queue is headed by a structure defined by the SIMPLEQ_HEAD()
548 macro. This structure contains a pair of pointers, one to the first
549 element in the simple queue and the other to the last element in the
550 simple queue. The elements are singly linked. New elements can be added
551 to the queue after an existing element, at the head of the queue or at
552 the tail of the queue. A SIMPLEQ_HEAD structure is declared as follows:
554 SIMPLEQ_HEAD(HEADNAME, TYPE) head;
556 where HEADNAME is the name of the structure to be defined, and struct
557 TYPE is the type of the elements to be linked into the queue. A pointer
558 to the head of the queue can later be declared as:
560 struct HEADNAME *headp;
562 (The names head and headp are user selectable.)
564 The SIMPLEQ_ENTRY() macro declares a structure that connects the elements
567 The SIMPLEQ_INIT() macro initializes the queue referenced by head.
569 The queue can also be initialized statically by using the
570 SIMPLEQ_HEAD_INITIALIZER() macro like this:
572 SIMPLEQ_HEAD(HEADNAME, TYPE) head = SIMPLEQ_HEAD_INITIALIZER(head);
574 The SIMPLEQ_INSERT_AFTER() macro inserts the new element elm after the
577 The SIMPLEQ_INSERT_HEAD() macro inserts the new element elm at the head
580 The SIMPLEQ_INSERT_TAIL() macro inserts the new element elm at the end of
583 The SIMPLEQ_REMOVE_AFTER() macro removes the queue element immediately
586 The SIMPLEQ_REMOVE_HEAD() macro removes the first element from the queue.
588 The SIMPLEQ_FIRST() and SIMPLEQ_NEXT() macros can be used to traverse the
589 queue. The SIMPLEQ_FOREACH() is used for queue traversal:
591 SIMPLEQ_FOREACH(np, head, NAME)
593 The macro SIMPLEQ_FOREACH_SAFE() traverses the queue referenced by head
594 in a forward direction, assigning each element in turn to var. However,
595 unlike SIMPLEQ_FOREACH() it is permitted to remove var as well as free it
596 from within the loop safely without interfering with the traversal.
598 The SIMPLEQ_EMPTY() macro should be used to check whether a list is
602 SIMPLEQ_HEAD(listhead, entry) head = SIMPLEQ_HEAD_INITIALIZER(head);
605 SIMPLEQ_ENTRY(entry) entries; /* Simple queue. */
609 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
610 SIMPLEQ_INSERT_HEAD(&head, n1, entries);
612 n2 = malloc(sizeof(struct entry)); /* Insert after. */
613 SIMPLEQ_INSERT_AFTER(&head, n1, n2, entries);
615 n2 = malloc(sizeof(struct entry)); /* Insert at the tail. */
616 SIMPLEQ_INSERT_TAIL(&head, n2, entries);
617 /* Forward traversal. */
618 SIMPLEQ_FOREACH(np, &head, entries)
621 while (!SIMPLEQ_EMPTY(&head)) {
622 n1 = SIMPLEQ_FIRST(&head);
623 SIMPLEQ_REMOVE_HEAD(&head, entries);
628 A tail queue is headed by a structure defined by the TAILQ_HEAD() macro.
629 This structure contains a pair of pointers, one to the first element in
630 the tail queue and the other to the last element in the tail queue. The
631 elements are doubly linked so that an arbitrary element can be removed
632 without traversing the tail queue. New elements can be added to the
633 queue after an existing element, before an existing element, at the head
634 of the queue, or at the end of the queue. A TAILQ_HEAD structure is
637 TAILQ_HEAD(HEADNAME, TYPE) head;
639 where HEADNAME is the name of the structure to be defined, and struct
640 TYPE is the type of the elements to be linked into the tail queue. A
641 pointer to the head of the tail queue can later be declared as:
643 struct HEADNAME *headp;
645 (The names head and headp are user selectable.)
647 The TAILQ_ENTRY() macro declares a structure that connects the elements
650 The TAILQ_INIT() macro initializes the tail queue referenced by head.
652 The tail queue can also be initialized statically by using the
653 TAILQ_HEAD_INITIALIZER() macro.
655 The TAILQ_INSERT_HEAD() macro inserts the new element elm at the head of
658 The TAILQ_INSERT_TAIL() macro inserts the new element elm at the end of
661 The TAILQ_INSERT_AFTER() macro inserts the new element elm after the
664 The TAILQ_INSERT_BEFORE() macro inserts the new element elm before the
667 The TAILQ_REMOVE() macro removes the element elm from the tail queue.
669 The TAILQ_REPLACE() macro replaces the list element elm with the new
672 TAILQ_FOREACH() and TAILQ_FOREACH_REVERSE() are used for traversing a
673 tail queue. TAILQ_FOREACH() starts at the first element and proceeds
674 towards the last. TAILQ_FOREACH_REVERSE() starts at the last element and
675 proceeds towards the first.
677 TAILQ_FOREACH(np, &head, NAME)
678 TAILQ_FOREACH_REVERSE(np, &head, HEADNAME, NAME)
680 The macros TAILQ_FOREACH_SAFE() and TAILQ_FOREACH_REVERSE_SAFE() traverse
681 the list referenced by head in a forward or reverse direction
682 respectively, assigning each element in turn to var. However, unlike
683 their unsafe counterparts, they permit both the removal of var as well as
684 freeing it from within the loop safely without interfering with the
687 The TAILQ_FIRST(), TAILQ_NEXT(), TAILQ_LAST() and TAILQ_PREV() macros can
688 be used to manually traverse a tail queue or an arbitrary part of one.
690 The TAILQ_EMPTY() macro should be used to check whether a tail queue is
694 TAILQ_HEAD(tailhead, entry) head;
697 TAILQ_ENTRY(entry) entries; /* Tail queue. */
701 TAILQ_INIT(&head); /* Initialize queue. */
703 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
704 TAILQ_INSERT_HEAD(&head, n1, entries);
706 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
707 TAILQ_INSERT_TAIL(&head, n1, entries);
709 n2 = malloc(sizeof(struct entry)); /* Insert after. */
710 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
712 n2 = malloc(sizeof(struct entry)); /* Insert before. */
713 TAILQ_INSERT_BEFORE(n1, n2, entries);
714 /* Forward traversal. */
715 TAILQ_FOREACH(np, &head, entries)
717 /* Manual forward traversal. */
718 for (np = n2; np != NULL; np = TAILQ_NEXT(np, entries))
721 while ((np = TAILQ_FIRST(&head))) {
722 TAILQ_REMOVE(&head, np, entries);
728 A circular queue is headed by a structure defined by the CIRCLEQ_HEAD()
729 macro. This structure contains a pair of pointers, one to the first
730 element in the circular queue and the other to the last element in the
731 circular queue. The elements are doubly linked so that an arbitrary
732 element can be removed without traversing the queue. New elements can be
733 added to the queue after an existing element, before an existing element,
734 at the head of the queue, or at the end of the queue. A CIRCLEQ_HEAD
735 structure is declared as follows:
737 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
739 where HEADNAME is the name of the structure to be defined, and struct
740 TYPE is the type of the elements to be linked into the circular queue. A
741 pointer to the head of the circular queue can later be declared as:
743 struct HEADNAME *headp;
745 (The names head and headp are user selectable.)
747 The CIRCLEQ_ENTRY() macro declares a structure that connects the elements
748 in the circular queue.
750 The CIRCLEQ_INIT() macro initializes the circular queue referenced by
753 The circular queue can also be initialized statically by using the
754 CIRCLEQ_HEAD_INITIALIZER() macro.
756 The CIRCLEQ_INSERT_HEAD() macro inserts the new element elm at the head
757 of the circular queue.
759 The CIRCLEQ_INSERT_TAIL() macro inserts the new element elm at the end of
762 The CIRCLEQ_INSERT_AFTER() macro inserts the new element elm after the
765 The CIRCLEQ_INSERT_BEFORE() macro inserts the new element elm before the
768 The CIRCLEQ_REMOVE() macro removes the element elm from the circular
771 The CIRCLEQ_REPLACE() macro replaces the list element elm with the new
774 The CIRCLEQ_FIRST(), CIRCLEQ_LAST(), CIRCLEQ_END(), CIRCLEQ_NEXT() and
775 CIRCLEQ_PREV() macros can be used to traverse a circular queue. The
776 CIRCLEQ_FOREACH() is used for circular queue forward traversal:
778 CIRCLEQ_FOREACH(np, head, NAME)
780 The CIRCLEQ_FOREACH_REVERSE() macro acts like CIRCLEQ_FOREACH() but
781 traverses the circular queue backwards.
783 The macros CIRCLEQ_FOREACH_SAFE() and CIRCLEQ_FOREACH_REVERSE_SAFE()
784 traverse the list referenced by head in a forward or reverse direction
785 respectively, assigning each element in turn to var. However, unlike
786 their unsafe counterparts, they permit both the removal of var as well as
787 freeing it from within the loop safely without interfering with the
790 The CIRCLEQ_EMPTY() macro should be used to check whether a circular
793 CIRCULAR QUEUE EXAMPLE
794 CIRCLEQ_HEAD(circleq, entry) head;
797 CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
801 CIRCLEQ_INIT(&head); /* Initialize circular queue. */
803 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
804 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
806 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
807 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
809 n2 = malloc(sizeof(struct entry)); /* Insert after. */
810 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
812 n2 = malloc(sizeof(struct entry)); /* Insert before. */
813 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
814 /* Forward traversal. */
815 CIRCLEQ_FOREACH(np, &head, entries)
817 /* Reverse traversal. */
818 CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
821 while (!CIRCLEQ_EMPTY(&head)) {
822 n1 = CIRCLEQ_FIRST(&head);
823 CIRCLEQ_REMOVE(&head, n1, entries);
828 It is an error to assume the next and previous fields are preserved after
829 an element has been removed from a list or queue. Using any macro
830 (except the various forms of insertion) on an element removed from a list
831 or queue is incorrect. An example of erroneous usage is removing the
834 The SLIST_END(), LIST_END(), SIMPLEQ_END() and TAILQ_END() macros are
835 provided for symmetry with CIRCLEQ_END(). They expand to NULL and don't
836 serve any useful purpose.
838 Trying to free a list in the following way is a common error:
840 LIST_FOREACH(var, head, entry)
844 Since var is free'd, the FOREACH macros refer to a pointer that may have
845 been reallocated already. A similar situation occurs when the current
846 element is deleted from the list. In cases like these the data
847 structure's FOREACH_SAFE macros should be used instead.
850 The queue functions first appeared in 4.4BSD.
852 OpenBSD 5.0 April 11, 2012 OpenBSD 5.0
853 ======================================================================
854 .\" $OpenBSD: queue.3,v 1.56 2012/04/11 13:29:14 naddy Exp $
855 .\" $NetBSD: queue.3,v 1.4 1995/07/03 00:25:36 mycroft Exp $
857 .\" Copyright (c) 1993 The Regents of the University of California.
858 .\" All rights reserved.
860 .\" Redistribution and use in source and binary forms, with or without
861 .\" modification, are permitted provided that the following conditions
863 .\" 1. Redistributions of source code must retain the above copyright
864 .\" notice, this list of conditions and the following disclaimer.
865 .\" 2. Redistributions in binary form must reproduce the above copyright
866 .\" notice, this list of conditions and the following disclaimer in the
867 .\" documentation and/or other materials provided with the distribution.
868 .\" 3. Neither the name of the University nor the names of its contributors
869 .\" may be used to endorse or promote products derived from this software
870 .\" without specific prior written permission.
872 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
873 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
874 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
875 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
876 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
877 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
878 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
879 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
880 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
881 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF