---
name: "LIST_CLASS_ENTRY"
section: "3bsd"
platform: "linux"
package: "libbsd"
version: "0.12.2-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/queue/3bsd"
---

# LIST\_CLASS\_ENTRY(3bsd)

implementations of singly-linked lists, singly-linked tail queues, lists and tail queues

Part of [queue(3bsd)](/queue/3bsd.md).

## NAME

**SLIST\_CLASS\_ENTRY**, **SLIST\_CLASS\_HEAD**, **SLIST\_CONCAT**, **SLIST\_EMPTY**, **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\_SWAP**, **STAILQ\_CLASS\_ENTRY**, **STAILQ\_CLASS\_HEAD**, **STAILQ\_CONCAT**, **STAILQ\_EMPTY**, **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\_SWAP**, **LIST\_CLASS\_ENTRY**, **LIST\_CLASS\_HEAD**, **LIST\_CONCAT**, **LIST\_EMPTY**, **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\_SWAP**, **TAILQ\_CLASS\_ENTRY**, **TAILQ\_CLASS\_HEAD**, **TAILQ\_CONCAT**, **TAILQ\_EMPTY**, **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\_SWAP** – implementations of singly-linked lists, singly-linked tail queues, lists and tail queues

## LIBRARY

library “libbsd”

## SYNOPSIS

**#include \<sys/queue.h\>**

(See [libbsd(7)](/libbsd/7.md) for include usage.)

**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\_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\_SWAP**(*SLIST\_HEAD \*head1*, *SLIST\_HEAD \*head2*, *TYPE*);

**STAILQ\_CLASS\_ENTRY**(*CLASSTYPE*);

**STAILQ\_CLASS\_HEAD**(*HEADNAME*, *CLASSTYPE*);

**STAILQ\_CONCAT**(*STAILQ\_HEAD \*head1*, *STAILQ\_HEAD \*head2*);

**STAILQ\_EMPTY**(*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\_SWAP**(*STAILQ\_HEAD \*head1*, *STAILQ\_HEAD \*head2*, *TYPE*);

**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\_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\_SWAP**(*LIST\_HEAD \*head1*, *LIST\_HEAD \*head2*, *TYPE*, *LIST\_ENTRY NAME*);

**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\_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\_SWAP**(*TAILQ\_HEAD \*head1*, *TAILQ\_HEAD \*head2*, *TYPE*, *TAILQ\_ENTRY NAME*);

## DESCRIPTION

These macros define and operate on four types of data structures which can be used in both C and C++ source code:

1. Lists

2. Singly-linked lists

3. Singly-linked tail queues

4. Tail queues

All four structures support the following functionality:

1. Insertion of a new entry at the head of the list.

2. Insertion of a new entry after any element in the list.

3. O(1) removal of an entry from the head of the list.

4. Forward traversal through the list.

5. Swapping the contents of two lists.

Singly-linked lists are the simplest of the four data structures and support only the above functionality. Singly-linked lists are ideal for applications with large datasets and few or no removals, or for implementing a LIFO queue. Singly-linked lists add the following functionality:

1. O(n) removal of any entry in the list.

2. O(n) concatenation of two lists.

Singly-linked tail queues add the following functionality:

1. Entries can be added at the end of a list.

2. O(n) removal of any entry in the list.

3. They may be concatenated.

However:

1. All list insertions must specify the head of the list.

2. Each head entry requires two pointers rather than one.

3. 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:

1. Insertion of a new entry before any element in the list.

2. O(1) removal of any entry in the list.

However:

1. Each element requires two pointers rather than one.

2. 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:

1. O(n) concatenation of two lists.

2. They may be traversed backwards.

However:

1. 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:

1. Entries can be added at the end of a list.

2. They may be traversed backwards, from tail to head.

3. They may be concatenated.

However:

1. All list insertions and removals must specify the head of the list.

2. Each head entry requires two pointers rather than one.

3. 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. 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.

The macro **SLIST\_EMPTY** evaluates to true if there are no elements in 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 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.

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.

The macro **SLIST\_SWAP** swaps the contents of *head1* and *head2*.

## SINGLY-LINKED LIST EXAMPLE

```
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);
}
```

## SINGLY-LINKED TAIL QUEUES

A singly-linked tail queue is headed by a structure defined by the **STAILQ\_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 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.)

The macro **STAILQ\_HEAD\_INITIALIZER** evaluates to an initializer for the tail queue *head*.

The macro **STAILQ\_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 **STAILQ\_EMPTY** evaluates to true if there are no items on the tail queue.

The macro **STAILQ\_ENTRY** declares a structure that connects the elements in the tail queue.

The macro **STAILQ\_FIRST** returns the first item on the tail queue or NULL if the tail queue is empty.

The macro **STAILQ\_FOREACH** traverses the tail queue referenced by *head* in the forward direction, assigning each element in turn to *var*.

The macro **STAILQ\_FOREACH\_FROM** behaves identically to **STAILQ\_FOREACH** when *var* is NULL, else it treats *var* as a previously found STAILQ element and begins the loop at *var* instead of the first element in the STAILQ referenced by *head*.

The macro **STAILQ\_FOREACH\_SAFE** traverses the tail queue referenced by *head* in the forward direction, assigning each element in turn to *var*. However, unlike **STAILQ\_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 macro **STAILQ\_FOREACH\_FROM\_SAFE** behaves identically to **STAILQ\_FOREACH\_SAFE** when *var* is NULL, else it treats *var* as a previously found STAILQ element and begins the loop at *var* instead of the first element in the STAILQ referenced by *head*.

The macro **STAILQ\_INIT** initializes the tail queue referenced by *head*.

The macro **STAILQ\_INSERT\_HEAD** inserts the new element *elm* at the head of the tail queue.

The macro **STAILQ\_INSERT\_TAIL** inserts the new element *elm* at the end of the tail queue.

The macro **STAILQ\_INSERT\_AFTER** inserts the new element *elm* after the element *listelm*.

The macro **STAILQ\_LAST** returns the last item on the tail queue. If the tail queue is empty the return value is NULL.

The macro **STAILQ\_NEXT** returns the next item on the tail queue, or NULL this item is the last.

The macro **STAILQ\_REMOVE\_AFTER** removes the element after *elm* from the tail queue. Unlike *STAILQ\_REMOVE*, this macro does not traverse the entire tail queue.

The macro **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 generic *STAILQ\_REMOVE* macro.

The macro **STAILQ\_REMOVE** removes the element *elm* from the tail queue. Use of this macro 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.

The macro **STAILQ\_SWAP** swaps the contents of *head1* and *head2*.

## SINGLY-LINKED TAIL QUEUE EXAMPLE

```
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

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.)

The macro **LIST\_HEAD\_INITIALIZER** evaluates to an initializer for the list *head*.

The macro **LIST\_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 tail queue should be used if this macro is needed in high-usage code paths or to operate on long lists.

The macro **LIST\_EMPTY** evaluates to true if there are no elements in the list.

The macro **LIST\_ENTRY** declares a structure that connects the elements in the list.

The macro **LIST\_FIRST** returns the first element in the list or NULL if the list is empty.

The macro **LIST\_FOREACH** traverses the list referenced by *head* in the forward direction, assigning each element in turn to *var*.

The macro **LIST\_FOREACH\_FROM** behaves identically to **LIST\_FOREACH** when *var* is NULL, else it treats *var* as a previously found LIST element and begins the loop at *var* instead of the first element in the LIST referenced by *head*.

The macro **LIST\_FOREACH\_SAFE** traverses the list referenced by *head* in the forward direction, assigning each element in turn to *var*. However, unlike **LIST\_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 macro **LIST\_FOREACH\_FROM\_SAFE** behaves identically to **LIST\_FOREACH\_SAFE** when *var* is NULL, else it treats *var* as a previously found LIST element and begins the loop at *var* instead of the first element in the LIST referenced by *head*.

The macro **LIST\_INIT** initializes the list referenced by *head*.

The macro **LIST\_INSERT\_HEAD** inserts the new element *elm* at the head of the list.

The macro **LIST\_INSERT\_AFTER** inserts the new element *elm* after the element *listelm*.

The macro **LIST\_INSERT\_BEFORE** inserts the new element *elm* before the element *listelm*.

The macro **LIST\_NEXT** returns the next element in the list, or NULL if this is the last.

The macro **LIST\_PREV** returns the previous element in the list, or NULL if this is the first. List *head* must contain element *elm*.

The macro **LIST\_REMOVE** removes the element *elm* from the list.

The macro **LIST\_SWAP** swaps the contents of *head1* and *head2*.

## LIST EXAMPLE

```
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

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\_FROM** behaves identically to **TAILQ\_FOREACH** when *var* is NULL, else it treats *var* as a previously found TAILQ element and begins the loop at *var* instead of the first element in the TAILQ referenced by *head*.

The macro **TAILQ\_FOREACH\_REVERSE** traverses the tail queue referenced by *head* in the reverse direction, assigning each element in turn to *var*.

The macro **TAILQ\_FOREACH\_REVERSE\_FROM** behaves identically to **TAILQ\_FOREACH\_REVERSE** when *var* is NULL, else it treats *var* as a previously found TAILQ element and begins the reverse loop at *var* instead of the last element in the TAILQ referenced by *head*.

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** make it possible to both remove *var* as well as free it from within the loop safely without interfering with the traversal.

The macro **TAILQ\_FOREACH\_FROM\_SAFE** behaves identically to **TAILQ\_FOREACH\_SAFE** when *var* is NULL, else it treats *var* as a previously found TAILQ element and begins the loop at *var* instead of the first element in the TAILQ referenced by *head*.

The macro **TAILQ\_FOREACH\_REVERSE\_FROM\_SAFE** behaves identically to **TAILQ\_FOREACH\_REVERSE\_SAFE** when *var* is NULL, else it treats *var* as a previously found TAILQ element and begins the reverse loop at *var* instead of the last element in the TAILQ referenced by *head*.

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 NULL.

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.

The macro **TAILQ\_SWAP** swaps the contents of *head1* and *head2*.

## 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);
```

## DIAGNOSTICS

When debugging **[queue(3)](/queue/3.md)**, it can be useful to trace queue changes. To enable tracing, define the macro *QUEUE\_MACRO\_DEBUG\_TRACE* at compile time.

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. The macro **QMD\_IS\_TRASHED**(*void \*ptr*) returns true if *ptr* has been trashed by the *QUEUE\_MACRO\_DEBUG\_TRASH* option.

## SEE ALSO

[tree(3bsd)](/tree/3bsd.md)

## HISTORY

The **queue** functions first appeared in 4.4BSD.
