QUEUE(3)
πΈ QUEUE(3) β Library Functions Manual πΈ
okay so bestie, buckle up π this is QUEUE(3) from the Library Functions Manual, and it's literally the mothership of every linked-list macro you're ever gonna meet. She's long. She's iconic. She's giving "I contain multitudes." π
β¨ NAME β¨
presentingβ¦ the entire cast list π¬ (yes, all of them, no skips, we don't do that here):
SLIST_CLASS_ENTRY,SLIST_CLASS_HEAD,SLIST_CONCAT,SLIST_EMPTY,SLIST_EMPTY_ATOMIC,SLIST_ENTRY,SLIST_FIRST,SLIST_FOREACH,SLIST_FOREACH_FROM,SLIST_FOREACH_FROM_SAFE,SLIST_FOREACH_SAFE,SLIST_HEAD,SLIST_HEAD_INITIALIZER,SLIST_INIT,SLIST_INSERT_AFTER,SLIST_INSERT_HEAD,SLIST_NEXT,SLIST_REMOVE,SLIST_REMOVE_AFTER,SLIST_REMOVE_HEAD,SLIST_SPLIT_AFTER,SLIST_SWAPSTAILQ_CLASS_ENTRY,STAILQ_CLASS_HEAD,STAILQ_CONCAT,STAILQ_EMPTY,STAILQ_EMPTY_ATOMIC,STAILQ_ENTRY,STAILQ_FIRST,STAILQ_FOREACH,STAILQ_FOREACH_FROM,STAILQ_FOREACH_FROM_SAFE,STAILQ_FOREACH_SAFE,STAILQ_HEAD,STAILQ_HEAD_INITIALIZER,STAILQ_INIT,STAILQ_INSERT_AFTER,STAILQ_INSERT_HEAD,STAILQ_INSERT_TAIL,STAILQ_LAST,STAILQ_NEXT,STAILQ_REMOVE,STAILQ_REMOVE_AFTER,STAILQ_REMOVE_HEAD,STAILQ_REVERSE,STAILQ_SPLIT_AFTER,STAILQ_SWAPLIST_CLASS_ENTRY,LIST_CLASS_HEAD,LIST_CONCAT,LIST_EMPTY,LIST_EMPTY_ATOMIC,LIST_ENTRY,LIST_FIRST,LIST_FOREACH,LIST_FOREACH_FROM,LIST_FOREACH_FROM_SAFE,LIST_FOREACH_SAFE,LIST_HEAD,LIST_HEAD_INITIALIZER,LIST_INIT,LIST_INSERT_AFTER,LIST_INSERT_BEFORE,LIST_INSERT_HEAD,LIST_NEXT,LIST_PREV,LIST_REMOVE,LIST_REPLACE,LIST_SPLIT_AFTER,LIST_SWAPTAILQ_CLASS_ENTRY,TAILQ_CLASS_HEAD,TAILQ_CONCAT,TAILQ_EMPTY,TAILQ_EMPTY_ATOMIC,TAILQ_ENTRY,TAILQ_FIRST,TAILQ_FOREACH,TAILQ_FOREACH_FROM,TAILQ_FOREACH_FROM_SAFE,TAILQ_FOREACH_REVERSE,TAILQ_FOREACH_REVERSE_FROM,TAILQ_FOREACH_REVERSE_FROM_SAFE,TAILQ_FOREACH_REVERSE_SAFE,TAILQ_FOREACH_SAFE,TAILQ_HEAD,TAILQ_HEAD_INITIALIZER,TAILQ_INIT,TAILQ_INSERT_AFTER,TAILQ_INSERT_BEFORE,TAILQ_INSERT_HEAD,TAILQ_INSERT_TAIL,TAILQ_LAST,TAILQ_NEXT,TAILQ_PREV,TAILQ_REMOVE,TAILQ_REPLACE,TAILQ_SPLIT_AFTER,TAILQ_SWAP
and what are all these, you ask? they're implementations of singly-linked lists, singly-linked tail queues, lists and tail queues π¦
so basically four flavours of "things pointing at other things," and every single one of them is about to be your new bestie. iykyk π
β¨ SYNOPSIS β¨
first things first, you gotta let the machine know:
#include <sys/queue.h>
that's the whole vibe check right there. sys/queue.h or you're not invited π
π©· the SLIST girlies (singly-linked lists)
SLIST_CLASS_ENTRY(CLASSTYPE);
SLIST_CLASS_HEAD(HEADNAME, CLASSTYPE);
SLIST_CONCAT(SLIST_HEAD *head1, SLIST_HEAD *head2, TYPE,
SLIST_ENTRY NAME);
SLIST_EMPTY(SLIST_HEAD *head);
SLIST_EMPTY_ATOMIC(SLIST_HEAD *head);
SLIST_ENTRY(TYPE);
SLIST_FIRST(SLIST_HEAD *head);
SLIST_FOREACH(TYPE *var, SLIST_HEAD *head, SLIST_ENTRY NAME);
SLIST_FOREACH_FROM(TYPE *var, SLIST_HEAD *head, SLIST_ENTRY NAME);
SLIST_FOREACH_FROM_SAFE(TYPE *var, SLIST_HEAD *head, SLIST_ENTRY NAME,
TYPE *temp_var);
SLIST_FOREACH_SAFE(TYPE *var, SLIST_HEAD *head, SLIST_ENTRY NAME,
TYPE *temp_var);
SLIST_HEAD(HEADNAME, TYPE);
SLIST_HEAD_INITIALIZER(SLIST_HEAD head);
SLIST_INIT(SLIST_HEAD *head);
SLIST_INSERT_AFTER(TYPE *listelm, TYPE *elm, SLIST_ENTRY NAME);
SLIST_INSERT_HEAD(SLIST_HEAD *head, TYPE *elm, SLIST_ENTRY NAME);
SLIST_NEXT(TYPE *elm, SLIST_ENTRY NAME);
SLIST_REMOVE(SLIST_HEAD *head, TYPE *elm, TYPE, SLIST_ENTRY NAME);
SLIST_REMOVE_AFTER(TYPE *elm, SLIST_ENTRY NAME);
SLIST_REMOVE_HEAD(SLIST_HEAD *head, SLIST_ENTRY NAME);
SLIST_SPLIT_AFTER(SLIST_HEAD *head, TYPE *elm, SLIST_HEAD *rest,
SLIST_ENTRY NAME);
SLIST_SWAP(SLIST_HEAD *head1, SLIST_HEAD *head2, TYPE);
look at her go π«Ά twenty-two macros and not a single one of them is filler.
π the STAILQ girlies (singly-linked tail queues)
STAILQ_CLASS_ENTRY(CLASSTYPE);
STAILQ_CLASS_HEAD(HEADNAME, CLASSTYPE);
STAILQ_CONCAT(STAILQ_HEAD *head1, STAILQ_HEAD *head2);
STAILQ_EMPTY(STAILQ_HEAD *head);
STAILQ_EMPTY_ATOMIC(STAILQ_HEAD *head);
STAILQ_ENTRY(TYPE);
STAILQ_FIRST(STAILQ_HEAD *head);
STAILQ_FOREACH(TYPE *var, STAILQ_HEAD *head, STAILQ_ENTRY NAME);
STAILQ_FOREACH_FROM(TYPE *var, STAILQ_HEAD *head, STAILQ_ENTRY NAME);
STAILQ_FOREACH_FROM_SAFE(TYPE *var, STAILQ_HEAD *head,
STAILQ_ENTRY NAME, TYPE *temp_var);
STAILQ_FOREACH_SAFE(TYPE *var, STAILQ_HEAD *head, STAILQ_ENTRY NAME,
TYPE *temp_var);
STAILQ_HEAD(HEADNAME, TYPE);
STAILQ_HEAD_INITIALIZER(STAILQ_HEAD head);
STAILQ_INIT(STAILQ_HEAD *head);
STAILQ_INSERT_AFTER(STAILQ_HEAD *head, TYPE *listelm, TYPE *elm,
STAILQ_ENTRY NAME);
STAILQ_INSERT_HEAD(STAILQ_HEAD *head, TYPE *elm, STAILQ_ENTRY NAME);
STAILQ_INSERT_TAIL(STAILQ_HEAD *head, TYPE *elm, STAILQ_ENTRY NAME);
STAILQ_LAST(STAILQ_HEAD *head, TYPE *elm, STAILQ_ENTRY NAME);
STAILQ_NEXT(TYPE *elm, STAILQ_ENTRY NAME);
STAILQ_REMOVE(STAILQ_HEAD *head, TYPE *elm, TYPE, STAILQ_ENTRY NAME);
STAILQ_REMOVE_AFTER(STAILQ_HEAD *head, TYPE *elm, STAILQ_ENTRY NAME);
STAILQ_REMOVE_HEAD(STAILQ_HEAD *head, STAILQ_ENTRY NAME);
STAILQ_REVERSE(STAILQ_HEAD *head, TYPE, STAILQ_ENTRY NAME);
STAILQ_SPLIT_AFTER(STAILQ_HEAD *head, TYPE *elm, STAILQ_HEAD *rest,
STAILQ_ENTRY NAME);
STAILQ_SWAP(STAILQ_HEAD *head1, STAILQ_HEAD *head2, TYPE);
note how there's a _TAIL insert and a LAST in this family β she's the SLIST who went to the gym and got a tail pointer, and honestly? ate. π₯
π©΅ the LIST girlies
LIST_CLASS_ENTRY(CLASSTYPE);
LIST_CLASS_HEAD(HEADNAME, CLASSTYPE);
LIST_CONCAT(LIST_HEAD *head1, LIST_HEAD *head2, TYPE, LIST_ENTRY NAME);
LIST_EMPTY(LIST_HEAD *head);
LIST_EMPTY_ATOMIC(LIST_HEAD *head);
LIST_ENTRY(TYPE);
LIST_FIRST(LIST_HEAD *head);
LIST_FOREACH(TYPE *var, LIST_HEAD *head, LIST_ENTRY NAME);
LIST_FOREACH_FROM(TYPE *var, LIST_HEAD *head, LIST_ENTRY NAME);
LIST_FOREACH_FROM_SAFE(TYPE *var, LIST_HEAD *head, LIST_ENTRY NAME,
TYPE *temp_var);
LIST_FOREACH_SAFE(TYPE *var, LIST_HEAD *head, LIST_ENTRY NAME,
TYPE *temp_var);
LIST_HEAD(HEADNAME, TYPE);
LIST_HEAD_INITIALIZER(LIST_HEAD head);
LIST_INIT(LIST_HEAD *head);
LIST_INSERT_AFTER(TYPE *listelm, TYPE *elm, LIST_ENTRY NAME);
LIST_INSERT_BEFORE(TYPE *listelm, TYPE *elm, LIST_ENTRY NAME);
LIST_INSERT_HEAD(LIST_HEAD *head, TYPE *elm, LIST_ENTRY NAME);
LIST_NEXT(TYPE *elm, LIST_ENTRY NAME);
LIST_PREV(TYPE *elm, LIST_HEAD *head, TYPE, LIST_ENTRY NAME);
LIST_REMOVE(TYPE *elm, LIST_ENTRY NAME);
LIST_REPLACE(TYPE *elm, TYPE *new, LIST_ENTRY NAME);
LIST_SPLIT_AFTER(LIST_HEAD *head, TYPE *elm, LIST_HEAD *rest,
LIST_ENTRY NAME);
LIST_SWAP(LIST_HEAD *head1, LIST_HEAD *head2, TYPE, LIST_ENTRY NAME);
she's got PREVβ¦ she's got REPLACEβ¦ she's got main character energy and she knows it π
π the TAILQ girlies (tail queues, the final boss)
TAILQ_CLASS_ENTRY(CLASSTYPE);
TAILQ_CLASS_HEAD(HEADNAME, CLASSTYPE);
TAILQ_CONCAT(TAILQ_HEAD *head1, TAILQ_HEAD *head2, TAILQ_ENTRY NAME);
TAILQ_EMPTY(TAILQ_HEAD *head);
TAILQ_EMPTY_ATOMIC(TAILQ_HEAD *head);
TAILQ_ENTRY(TYPE);
TAILQ_FIRST(TAILQ_HEAD *head);
TAILQ_FOREACH(TYPE *var, TAILQ_HEAD *head, TAILQ_ENTRY NAME);
TAILQ_FOREACH_FROM(TYPE *var, TAILQ_HEAD *head, TAILQ_ENTRY NAME);
TAILQ_FOREACH_FROM_SAFE(TYPE *var, TAILQ_HEAD *head, TAILQ_ENTRY NAME,
TYPE *temp_var);
TAILQ_FOREACH_REVERSE(TYPE *var, TAILQ_HEAD *head, HEADNAME,
TAILQ_ENTRY NAME);
TAILQ_FOREACH_REVERSE_FROM(TYPE *var, TAILQ_HEAD *head, HEADNAME,
TAILQ_ENTRY NAME);
TAILQ_FOREACH_REVERSE_FROM_SAFE(TYPE *var, TAILQ_HEAD *head, HEADNAME,
TAILQ_ENTRY NAME, TYPE *temp_var);
TAILQ_FOREACH_REVERSE_SAFE(TYPE *var, TAILQ_HEAD *head, HEADNAME,
TAILQ_ENTRY NAME, TYPE *temp_var);
TAILQ_FOREACH_SAFE(TYPE *var, TAILQ_HEAD *head, TAILQ_ENTRY NAME,
TYPE *temp_var);
TAILQ_HEAD(HEADNAME, TYPE);
TAILQ_HEAD_INITIALIZER(TAILQ_HEAD head);
TAILQ_INIT(TAILQ_HEAD *head);
TAILQ_INSERT_AFTER(TAILQ_HEAD *head, TYPE *listelm, TYPE *elm,
TAILQ_ENTRY NAME);
TAILQ_INSERT_BEFORE(TYPE *listelm, TYPE *elm, TAILQ_ENTRY NAME);
TAILQ_INSERT_HEAD(TAILQ_HEAD *head, TYPE *elm, TAILQ_ENTRY NAME);
TAILQ_INSERT_TAIL(TAILQ_HEAD *head, TYPE *elm, TAILQ_ENTRY NAME);
TAILQ_LAST(TAILQ_HEAD *head, HEADNAME);
TAILQ_NEXT(TYPE *elm, TAILQ_ENTRY NAME);
TAILQ_PREV(TYPE *elm, HEADNAME, TAILQ_ENTRY NAME);
TAILQ_REMOVE(TAILQ_HEAD *head, TYPE *elm, TAILQ_ENTRY NAME);
TAILQ_REPLACE(TAILQ_HEAD *head, TYPE *elm, TYPE *new, TAILQ_ENTRY NAME);
TAILQ_SPLIT_AFTER(TAILQ_HEAD *head, TYPE *elm, TAILQ_HEAD *rest,
TAILQ_ENTRY NAME);
TAILQ_SWAP(TAILQ_HEAD *head1, TAILQ_HEAD *head2, TYPE,
TAILQ_ENTRY NAME);
okay look at all those REVERSE variants π she walks backwards AND forwards, she's flexible like that. no cap. π
heads up bestie π«Ά every one of those macros above is spelled exactly like that β theNAMEargument, theTYPEargument, theHEADNAME, all of it. type them like you mean it, because a typo in a macro name is a bad time. π
and that's just the name-drop and the guest list, babes β we haven't even gotten to the description yet. that's coming up next, and it's a lot. π
DESCRIPTION π
okay so these macros define and operate on four types of data structures, and they work in both C and C++ source code β yes ma'am, bilingual icon behavior:
- Lists
- Singly-linked lists
- Singly-linked tail queues
- Tail queues
All four structures support the following functionality, no cap:
- Insertion of a new entry at the head of the list.
- Insertion of a new entry after any element in the list.
- O(1) removal of an entry from the head of the list.
- Forward traversal through the list.
- Splitting a list in two after any element in the list.
- Swapping the contents of two lists.
Singly-linked lists are the simplest of the four data structures and support only the above functionality. They are ideal for applications with large datasets and few or no removals, or for implementing a LIFO queue. And lowkey, singly-linked lists add the following functionality:
- O(n) removal of any entry in the list.
- O(n) concatenation of two lists.
Singly-linked tail queues add the following functionality:
- Entries can be added at the end of a list.
- O(n) removal of any entry in the list.
- They may be concatenated.
However, bestie, watch out:
- All list insertions must specify the head of the list.
- Each head entry requires two pointers rather than one.
- Code size is about 15% greater and operations run about 20% slower than singly-linked lists.
Singly-linked tail queues are ideal for applications with large datasets and few or no removals, or for implementing a FIFO queue. π
All doubly linked types of data structures (lists and tail queues) additionally allow:
- Insertion of a new entry before any element in the list.
- O(1) removal of any entry in the list.
However:
- Each element requires two pointers rather than one.
- Code size and execution time of operations (except for removal) is about twice that of the singly-linked data structures.
Linked lists are the simplest of the doubly linked data structures. They add the following functionality over the above:
- O(n) concatenation of two lists.
- They may be traversed backwards.
However:
- To traverse backwards, an entry to begin the traversal and the list in which it is contained must be specified.
Tail queues add the following functionality:
- Entries can be added at the end of a list.
- They may be traversed backwards, from tail to head.
- They may be concatenated.
However:
- All list insertions and removals must specify the head of the list.
- Each head entry requires two pointers rather than one.
- Code size is about 15% greater and operations run about 20% slower than singly-linked lists.
In the macro definitions, TYPE is the name of a user defined structure. The structure must contain a field called NAME which is of type SLIST_ENTRY, STAILQ_ENTRY, LIST_ENTRY, or TAILQ_ENTRY. In the macro definitions, CLASSTYPE is the name of a user defined class. The class must contain a field called NAME which is of type SLIST_CLASS_ENTRY, STAILQ_CLASS_ENTRY, LIST_CLASS_ENTRY, or TAILQ_CLASS_ENTRY. The argument HEADNAME is the name of a user defined structure that must be declared using the macros SLIST_HEAD, SLIST_CLASS_HEAD, STAILQ_HEAD, STAILQ_CLASS_HEAD, LIST_HEAD, LIST_CLASS_HEAD, TAILQ_HEAD, or TAILQ_CLASS_HEAD. See the examples below for further explanation of how these macros are used. π«Ά
SINGLY-LINKED LISTS π¦
A singly-linked list is headed by a structure defined by the SLIST_HEAD macro. This structure contains a single pointer to the first element on the list. The elements are singly linked for minimum space and pointer manipulation overhead at the expense of O(n) removal for arbitrary elements β yes, it's giving "cheap and cheerful but removal is a whole journey." New elements can be added to the list after an existing element or at the head of the list. An SLIST_HEAD structure is declared as follows:
SLIST_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 list. A pointer to the head of the list can later be declared as:
struct HEADNAME *headp;
(The names head and headp are user selectable.)
The macro SLIST_HEAD_INITIALIZER evaluates to an initializer for the list head. β¨
The macro SLIST_CONCAT concatenates the list headed by head2 onto the end of the one headed by head1, removing all entries from the former. Use of this macro should be avoided as it traverses the entirety of the head1 list. A singly-linked tail queue should be used if this macro is needed in high-usage code paths or to operate on long lists. π (ngl, that traversal is not the vibe.)
The macro SLIST_EMPTY evaluates to true if there are no elements in the list. The SLIST_EMPTY_ATOMIC variant has the same behavior, but can be safely used in contexts where it is possible that a different thread is concurrently updating the list. π§
The macro SLIST_ENTRY declares a structure that connects the elements in the list.
The macro SLIST_FIRST returns the first element in the list or NULL if the list is empty.
The macro SLIST_FOREACH traverses the list referenced by head in the forward direction, assigning each element in turn to var.
The macro SLIST_FOREACH_FROM behaves identically to SLIST_FOREACH when var is NULL, else it treats var as a previously found SLIST element and begins the loop at var instead of the first element in the SLIST referenced by head.
The macro SLIST_FOREACH_SAFE traverses the list referenced by head in the forward direction, assigning each element in turn to var. However, unlike SLIST_FOREACH() here it is permitted to both remove var as well as free it from within the loop safely without interfering with the traversal. π₯ (the safe one ate, fr fr.)
The macro SLIST_FOREACH_FROM_SAFE behaves identically to SLIST_FOREACH_SAFE when var is NULL, else it treats var as a previously found SLIST element and begins the loop at var instead of the first element in the SLIST referenced by head.
The macro SLIST_INIT initializes the list referenced by head.
The macro SLIST_INSERT_HEAD inserts the new element elm at the head of the list.
The macro SLIST_INSERT_AFTER inserts the new element elm after the element listelm.
The macro SLIST_NEXT returns the next element in the list.
The macro SLIST_REMOVE_AFTER removes the element after elm from the list. Unlike SLIST_REMOVE, this macro does not traverse the entire list. (iconic efficiency behavior.)
The macro SLIST_REMOVE_HEAD removes the element elm from the head of the list. For optimum efficiency, elements being removed from the head of the list should explicitly use this macro instead of the generic SLIST_REMOVE macro.
The macro SLIST_REMOVE removes the element elm from the list. Use of this macro should be avoided as it traverses the entire list. A doubly-linked list should be used if this macro is needed in high-usage code paths or to operate on long lists. π (not the O(n) walk of shame.)
The macro SLIST_SPLIT_AFTER splits the list referenced by head, making rest reference the list formed by elements after elm in head.
The macro SLIST_SWAP swaps the contents of head1 and head2. π
SINGLY-LINKED LIST EXAMPLE π
okay bestie, here's the whole slay in code form β don't touch the formatting, it's literal:
SLIST_HEAD(slisthead, entry) head =
SLIST_HEAD_INITIALIZER(head);
struct slisthead *headp; /* Singly-linked List head. */
struct entry {
...
SLIST_ENTRY(entry) entries; /* Singly-linked List. */
...
} *n1, *n2, *n3, *np;
SLIST_INIT(&head); /* Initialize the list. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
SLIST_INSERT_HEAD(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
SLIST_INSERT_AFTER(n1, n2, entries);
SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
free(n2);
n3 = SLIST_FIRST(&head);
SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
free(n3);
/* Forward traversal. */
SLIST_FOREACH(np, &head, entries)
np-> ...
/* Safe forward traversal. */
SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
np->do_stuff();
...
SLIST_REMOVE(&head, np, entry, entries);
free(np);
}
while (!SLIST_EMPTY(&head)) { /* List Deletion. */
n1 = SLIST_FIRST(&head);
SLIST_REMOVE_HEAD(&head, entries);
free(n1);
}
That's the SLIST moment β keep it safe, use the right macros, and don't let SLIST_REMOVE catch you in a long-list situation. More queue lore is coming in the next part, so stay tuned, bestie π
πΈ SINGLY-LINKED TAIL QUEUES πΈ
okay so bestie, a singly-linked tail queue is headed by a structure defined by the STAILQ_HEAD macro. That 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 singly linked for minimum space and pointer manipulation overhead at the expense of O(n) removal for arbitrary elements. New elements can be added to the tail queue after an existing element, at the head of the tail queue, or at the end of the tail queue. A STAILQ_HEAD structure is declared as follows:
STAILQ_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.)
STAILQ_HEAD_INITIALIZERβ evaluates to an initializer for the tail queue head.STAILQ_CONCATβ concatenates the tail queue headed byhead2onto the end of the one headed byhead1removing all entries from the former.STAILQ_EMPTYβ evaluates to true if there are no items on the tail queue. TheSTAILQ_EMPTY_ATOMICvariant has the same behavior, but can be safely used in contexts where it is possible that a different thread is concurrently updating the queue.STAILQ_ENTRYβ declares a structure that connects the elements in the tail queue.STAILQ_FIRSTβ returns the first item on the tail queue orNULLif the tail queue is empty.STAILQ_FOREACHβ traverses the tail queue referenced byheadin the forward direction, assigning each element in turn tovar.STAILQ_FOREACH_FROMβ behaves identically toSTAILQ_FOREACHwhenvarisNULL, else it treatsvaras a previously foundSTAILQelement and begins the loop atvarinstead of the first element in theSTAILQreferenced byhead.STAILQ_FOREACH_SAFEβ traverses the tail queue referenced byheadin the forward direction, assigning each element in turn tovar. However, unlikeSTAILQ_FOREACH()here it is permitted to both removevaras well as free it from within the loop safely without interfering with the traversal.STAILQ_FOREACH_FROM_SAFEβ behaves identically toSTAILQ_FOREACH_SAFEwhenvarisNULL, else it treatsvaras a previously foundSTAILQelement and begins the loop atvarinstead of the first element in theSTAILQreferenced byhead.STAILQ_INITβ initializes the tail queue referenced byhead.STAILQ_INSERT_HEADβ inserts the new elementelmat the head of the tail queue.STAILQ_INSERT_TAILβ inserts the new elementelmat the end of the tail queue.STAILQ_INSERT_AFTERβ inserts the new elementelmafter the elementlistelm.STAILQ_LASTβ returns the last item on the tail queue. If the tail queue is empty the return value isNULL.STAILQ_NEXTβ returns the next item on the tail queue, orNULLif this item is the last.STAILQ_REMOVE_AFTERβ removes the element afterelmfrom the tail queue. UnlikeSTAILQ_REMOVE, this macro does not traverse the entire tail queue.STAILQ_REMOVE_HEADβ removes the element at the head of the tail queue. For optimum efficiency, elements being removed from the head of the tail queue should use this macro explicitly rather than the genericSTAILQ_REMOVEmacro.STAILQ_REMOVEβ removes the elementelmfrom the tail queue.
π¨ bestie, real talk: use of STAILQ_REMOVE should be avoided as it traverses the entire list. A doubly-linked tail queue should be used if this macro is needed in high-usage code paths or to operate on long tail queues.
STAILQ_REVERSEβ reverses the queue in place.STAILQ_SPLIT_AFTERβ splits the tail queue referenced byhead, makingrestreference the tail queue formed by elements afterelminhead.STAILQ_SWAPβ swaps the contents ofhead1andhead2.
π¦ SINGLY-LINKED TAIL QUEUE EXAMPLE π¦
no cap, here's the whole example exactly as the page gives it β code stays code, always:
STAILQ_HEAD(stailhead, entry) head =
STAILQ_HEAD_INITIALIZER(head);
struct stailhead *headp; /* Singly-linked tail queue head. */
struct entry {
...
STAILQ_ENTRY(entry) entries; /* Tail queue. */
...
} *n1, *n2, *n3, *np;
STAILQ_INIT(&head); /* Initialize the queue. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
STAILQ_INSERT_HEAD(&head, n1, entries);
n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
STAILQ_INSERT_TAIL(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
STAILQ_INSERT_AFTER(&head, n1, n2, entries);
/* Deletion. */
STAILQ_REMOVE(&head, n2, entry, entries);
free(n2);
/* Deletion from the head. */
n3 = STAILQ_FIRST(&head);
STAILQ_REMOVE_HEAD(&head, entries);
free(n3);
/* Forward traversal. */
STAILQ_FOREACH(np, &head, entries)
np-> ...
/* Safe forward traversal. */
STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
np->do_stuff();
...
STAILQ_REMOVE(&head, np, entry, entries);
free(np);
}
/* TailQ Deletion. */
while (!STAILQ_EMPTY(&head)) {
n1 = STAILQ_FIRST(&head);
STAILQ_REMOVE_HEAD(&head, entries);
free(n1);
}
/* Faster TailQ Deletion. */
n1 = STAILQ_FIRST(&head);
while (n1 != NULL) {
n2 = STAILQ_NEXT(n1, entries);
free(n1);
n1 = n2;
}
STAILQ_INIT(&head);
π LISTS π
okay next up, bestie: a list is headed by a structure defined by the LIST_HEAD macro. This structure contains a single pointer to the first element on the list. The elements are doubly linked so that an arbitrary element can be removed without traversing the list. New elements can be added to the list after an existing element, before an existing element, or at the head of the list. A LIST_HEAD structure is declared as follows:
LIST_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 list. A pointer to the head of the list can later be declared as:
struct HEADNAME *headp;
(The names head and headp are user selectable.)
LIST_HEAD_INITIALIZERβ evaluates to an initializer for the list head.LIST_CONCATβ concatenates the list headed byhead2onto the end of the one headed byhead1removing all entries from the former.
β οΈ bestie, use ofLIST_CONCATshould be avoided as it traverses the entirety of thehead1list. A tail queue should be used if this macro is needed in high-usage code paths or to operate on long lists.
LIST_EMPTYβ evaluates to true if there are no elements in the list. TheLIST_EMPTY_ATOMICvariant has the same behavior, but can be safely used in contexts where it is possible that a different thread is concurrently updating the list.LIST_ENTRYβ declares a structure that connects the elements in the list.LIST_FIRSTβ returns the first element in the list orNULLif the list is empty.LIST_FOREACHβ traverses the list referenced byheadin the forward direction, assigning each element in turn tovar.LIST_FOREACH_FROMβ behaves identically toLIST_FOREACHwhenvarisNULL, else it treatsvaras a previously foundLISTelement and begins the loop atvarinstead of the first element in theLISTreferenced byhead.LIST_FOREACH_SAFEβ traverses the list referenced byheadin the forward direction, assigning each element in turn tovar. However, unlikeLIST_FOREACH()here it is permitted to both removevaras well as free it from within the loop safely without interfering with the traversal.LIST_FOREACH_FROM_SAFEβ behaves identically toLIST_FOREACH_SAFEwhenvarisNULL, else it treatsvaras a previously foundLISTelement and begins the loop atvarinstead of the first element in theLISTreferenced byhead.LIST_INITβ initializes the list referenced byhead.LIST_INSERT_HEADβ inserts the new elementelmat the head of the list.LIST_INSERT_AFTERβ inserts the new elementelmafter the elementlistelm.LIST_INSERT_BEFOREβ inserts the new elementelmbefore the elementlistelm.LIST_NEXTβ returns the next element in the list, orNULLif this is the last.LIST_PREVβ returns the previous element in the list, orNULLif this is the first. List head must contain elementelm.LIST_REMOVEβ removes the elementelmfrom the list.LIST_REPLACE()β replaces the elementelmwithnewin the list. The elementnewmust not already be on a list.LIST_SPLIT_AFTERβ splits the list referenced byhead, makingrestreference the list formed by elements afterelminhead.LIST_SWAPβ swaps the contents ofhead1andhead2.
π LIST EXAMPLE π
and here's the list example, copied exactly, because we respect the code in this group chat:
LIST_HEAD(listhead, entry) head =
LIST_HEAD_INITIALIZER(head);
struct listhead *headp; /* List head. */
struct entry {
...
LIST_ENTRY(entry) entries; /* List. */
...
} *n1, *n2, *n3, *np, *np_temp;
LIST_INIT(&head); /* Initialize the list. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
LIST_INSERT_HEAD(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
LIST_INSERT_AFTER(n1, n2, entries);
n3 = malloc(sizeof(struct entry)); /* Insert before. */
LIST_INSERT_BEFORE(n2, n3, entries);
LIST_REMOVE(n2, entries); /* Deletion. */
free(n2);
/* Forward traversal. */
LIST_FOREACH(np, &head, entries)
np-> ...
/* Safe forward traversal. */
LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
np->do_stuff();
...
LIST_REMOVE(np, entries);
free(np);
}
while (!LIST_EMPTY(&head)) { /* List Deletion. */
n1 = LIST_FIRST(&head);
LIST_REMOVE(n1, entries);
free(n1);
}
n1 = LIST_FIRST(&head); /* Faster List Deletion. */
while (n1 != NULL) {
n2 = LIST_NEXT(n1, entries);
free(n1);
n1 = n2;
}
LIST_INIT(&head);
π« TAIL QUEUES π«
Okay bestie, a tail queue is headed by a structure defined by the TAILQ_HEAD macro. That 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 like this:
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;
(And yes, the names head and headp are user selectable. pick your own faves β¨)
TAILQ_HEAD_INITIALIZERβ evaluates to an initializer for the tail queue head.TAILQ_CONCATβ concatenates the tail queue headed byhead2onto the end of the one headed byhead1, removing all entries from the former. πTAILQ_EMPTYβ evaluates to true if there are no items on the tail queue.TAILQ_EMPTY_ATOMICβ same behavior asTAILQ_EMPTY, but can be safely used in contexts where it is possible that a different thread is concurrently updating the queue.TAILQ_ENTRYβ declares a structure that connects the elements in the tail queue.TAILQ_FIRSTβ returns the first item on the tail queue, orNULLif the tail queue is empty.TAILQ_FOREACHβ traverses the tail queue referenced byheadin the forward direction, assigning each element in turn tovar.varis set toNULLif the loop completes normally, or if there were no elements.TAILQ_FOREACH_FROMβ behaves identically toTAILQ_FOREACHwhenvarisNULL; else it treatsvaras a previously found TAILQ element and begins the loop atvarinstead of the first element in the TAILQ referenced byhead.TAILQ_FOREACH_REVERSEβ traverses the tail queue referenced byheadin the reverse direction, assigning each element in turn tovar.TAILQ_FOREACH_REVERSE_FROMβ behaves identically toTAILQ_FOREACH_REVERSEwhenvarisNULL; else it treatsvaras a previously found TAILQ element and begins the reverse loop atvarinstead of the last element in the TAILQ referenced byhead.TAILQ_FOREACH_SAFEandTAILQ_FOREACH_REVERSE_SAFEβ traverse the list referenced byheadin the forward or reverse direction respectively, assigning each element in turn tovar. However, unlike their unsafe counterpartsTAILQ_FOREACHandTAILQ_FOREACH_REVERSE, they permit you to both removevaras well as free it from within the loop safely without interfering with the traversal. iconic behavior tbh π«ΆTAILQ_FOREACH_FROM_SAFEβ behaves identically toTAILQ_FOREACH_SAFEwhenvarisNULL; else it treatsvaras a previously found TAILQ element and begins the loop atvarinstead of the first element in the TAILQ referenced byhead.TAILQ_FOREACH_REVERSE_FROM_SAFEβ behaves identically toTAILQ_FOREACH_REVERSE_SAFEwhenvarisNULL; else it treatsvaras a previously found TAILQ element and begins the reverse loop atvarinstead of the last element in the TAILQ referenced byhead.TAILQ_INITβ initializes the tail queue referenced byhead.TAILQ_INSERT_HEADβ inserts the new elementelmat the head of the tail queue.TAILQ_INSERT_TAILβ inserts the new elementelmat the end of the tail queue.TAILQ_INSERT_AFTERβ inserts the new elementelmafter the elementlistelm.TAILQ_INSERT_BEFOREβ inserts the new elementelmbefore the elementlistelm.TAILQ_LASTβ returns the last item on the tail queue. If the tail queue is empty the return value isNULL.TAILQ_NEXTβ returns the next item on the tail queue, orNULLif this item is the last.TAILQ_PREVβ returns the previous item on the tail queue, orNULLif this item is the first.TAILQ_REMOVEβ removes the elementelmfrom the tail queue.TAILQ_REPLACE()β replaces the elementelmwithnewin the tail queue. The elementnewmust not already be on a list. no double-dipping bestie π«TAILQ_SPLIT_AFTERβ splits the tail queue referenced byhead, makingrestreference the tail queue formed by elements afterelminhead.TAILQ_SWAPβ swaps the contents ofhead1andhead2.
π 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, *n4, *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);
n4 = malloc(sizeof(struct entry)); /* Replacement. */
TAILQ_REPLACE(&head, n3, n4, entries);
free(n3);
/* 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);
π©Ί DIAGNOSTICS π©Ί
queue(3) provides several diagnostic and debugging facilities. β¨
Check code that performs basic integrity and API conformance checks is automatically inserted when using queue macros in the kernel if compiling it with INVARIANTS. One can request insertion or elision of check code by respectively defining one of the macros QUEUE_MACRO_DEBUG_ASSERTIONS or QUEUE_MACRO_NO_DEBUG_ASSERTIONS before first inclusion of <sys/queue.h>. When check code encounters an anomaly, it panics the kernel or aborts the program. To this end, in the kernel or in _STANDALONE builds, it by default calls panic(), while in userland builds it prints the diagnostic message on stderr and then calls abort(). These behaviors can be overridden by defining a custom QMD_PANIC() macro before first inclusion of <sys/queue.h>. The diagnostic messages automatically include the source file, line and function where the failing check occurred. This behavior can be overridden by defining a custom QMD_ASSERT() macro before first inclusion of <sys/queue.h>.
The SLIST_REMOVE_PREVPTR() macro is available to aid debugging:
SLIST_REMOVE_PREVPTR(TYPE **prev, TYPE *elm, SLIST_ENTRY NAME)
Removes element elm, which must directly follow the element whose &SLIST_NEXT() is prev, from the list. This macro may insert, under conditions detailed above, check code that validates that elm indeed follows prev in the list (through the QMD_SLIST_CHECK_PREVPTR() macro).
When debugging, it can be useful to trace queue changes. To enable tracing, define the macro QUEUE_MACRO_DEBUG_TRACE. Note that, at the moment, only macros for regular tail queues have been instrumented. π
It can also be useful to trash pointers that have been unlinked from a queue, to detect use after removal. To enable pointer trashing, define the macro QUEUE_MACRO_DEBUG_TRASH at compile time. Note that, at the moment, only a limited number of macros have been instrumented. The macro QMD_IS_TRASHED(void *ptr) returns true if ptr has been trashed by the QUEUE_MACRO_DEBUG_TRASH option.
π SEE ALSO π
πΈ HISTORY πΈ
The queue functions first appeared in 4.4BSD.
Footer vibe check: this page is serving Debian, dated April 28, 2025, and it's QUEUE(3) β bye bestie, go slay those linked structures πβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms