TAIL QUEUES
A tail queue is headed by a structure defined by the TAILQ_HEAD macro. This structure contains a pair of pointers, one to the first element in the tail queue and the other to the last element in the tail queue. The elements are doubly linked so that an arbitrary element can be removed without traversing the tail queue. New elements can be added to the tail queue after an existing element, before an existing element, at the head of the tail queue, or at the end of the tail queue. A TAILQ_HEAD structure is declared as follows:
TAILQ_HEAD(HEADNAME, TYPE) head;
where HEADNAME is the name of the structure to be defined, and TYPE is the type of the elements to be linked into the tail queue. A pointer to the head of the tail queue can later be declared as:
struct HEADNAME *headp;
(The names head and headp are user selectable.)
The macro TAILQ_HEAD_INITIALIZER evaluates to an initializer for the tail queue head.
The macro TAILQ_CONCAT concatenates the tail queue headed by head2 onto the end of the one headed by head1 removing all entries from the former.
The macro TAILQ_EMPTY evaluates to true if there are no items on the tail queue.
The macro TAILQ_ENTRY declares a structure that connects the elements in the tail queue.
The macro TAILQ_FIRST returns the first item on the tail queue or NULL if the tail queue is empty.
The macro TAILQ_FOREACH traverses the tail queue referenced by head in the forward direction, assigning each element in turn to var. var is set to NULL if the loop completes normally, or if there were no elements.
The macro TAILQ_FOREACH_REVERSE traverses the tail queue referenced by head in the reverse direction, assigning each element in turn to var.
The macros TAILQ_FOREACH_SAFE and TAILQ_FOREACH_REVERSE_SAFE traverse the list referenced by head in the forward or reverse direction respectively, assigning each element in turn to var. However, unlike their unsafe counterparts, TAILQ_FOREACH and TAILQ_FOREACH_REVERSE permit to both remove var as well as free it from within the loop safely without interfering with the traversal.
The macro TAILQ_INIT initializes the tail queue referenced by head.
The macro TAILQ_INSERT_HEAD inserts the new element elm at the head of the tail queue.
The macro TAILQ_INSERT_TAIL inserts the new element elm at the end of the tail queue.
The macro TAILQ_INSERT_AFTER inserts the new element elm after the element listelm.
The macro TAILQ_INSERT_BEFORE inserts the new element elm before the element listelm.
The macro TAILQ_LAST returns the last item on the tail queue. If the tail queue is empty the return value is undefined.
The macro TAILQ_NEXT returns the next item on the tail queue, or NULL if this item is the last.
The macro TAILQ_PREV returns the previous item on the tail queue, or NULL if this item is the first.
The macro TAILQ_REMOVE removes the element elm from the tail queue.
TAIL QUEUE EXAMPLE
TAILQ_HEAD(tailhead, entry) head =
TAILQ_HEAD_INITIALIZER(head);
struct tailhead *headp; /* Tail queue head. */
struct entry {
...
TAILQ_ENTRY(entry) entries;/* Tail queue. */
...
} *n1, *n2, *n3, *np;
TAILQ_INIT(&head);/* Initialize the queue. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
TAILQ_INSERT_HEAD(&head, n1, entries);
n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
TAILQ_INSERT_TAIL(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
TAILQ_INSERT_AFTER(&head, n1, n2, entries);
n3 = malloc(sizeof(struct entry)); /* Insert before. */
TAILQ_INSERT_BEFORE(n2, n3, entries);
TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
free(n2);
/* Forward traversal. */
TAILQ_FOREACH(np, &head, entries)
np-> ...
/* Safe forward traversal. */
TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
np->do_stuff();
...
TAILQ_REMOVE(&head, np, entries);
free(np);
}
/* Reverse traversal. */
TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
np-> ...
/* TailQ Deletion. */
while (!TAILQ_EMPTY(&head)) {
n1 = TAILQ_FIRST(&head);
TAILQ_REMOVE(&head, n1, entries);
free(n1);
}
/* Faster TailQ Deletion. */
n1 = TAILQ_FIRST(&head);
while (n1 != NULL) {
n2 = TAILQ_NEXT(n1, entries);
free(n1);
n1 = n2;
}
TAILQ_INIT(&head);
HISTORY
The queue functions first appeared in BSD 4.4 .