---
name: "arb"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/ARB16_ENTRY"
---

# arb(3)

array-based red-black trees

Part of [ARB16\_ENTRY(3)](/ARB16_ENTRY/3.md).

## NAME

**ARB\_PROTOTYPE**, **ARB\_PROTOTYPE\_STATIC**, **ARB\_PROTOTYPE\_INSERT**, **ARB\_PROTOTYPE\_INSERT\_COLOR**, **ARB\_PROTOTYPE\_REMOVE**, **ARB\_PROTOTYPE\_REMOVE\_COLOR**, **ARB\_PROTOTYPE\_FIND**, **ARB\_PROTOTYPE\_NFIND**, **ARB\_PROTOTYPE\_NEXT**, **ARB\_PROTOTYPE\_PREV**, **ARB\_PROTOTYPE\_MINMAX**, **ARB\_PROTOTYPE\_REINSERT**, **ARB\_GENERATE**, **ARB\_GENERATE\_STATIC**, **ARB\_GENERATE\_INSERT**, **ARB\_GENERATE\_INSERT\_COLOR**, **ARB\_GENERATE\_REMOVE**, **ARB\_GENERATE\_REMOVE\_COLOR**, **ARB\_GENERATE\_FIND**, **ARB\_GENERATE\_NFIND**, **ARB\_GENERATE\_NEXT**, **ARB\_GENERATE\_PREV**, **ARB\_GENERATE\_MINMAX**, **ARB\_GENERATE\_REINSERT**, **ARB8\_ENTRY**, **ARB16\_ENTRY**, **ARB32\_ENTRY**, **ARB8\_HEAD**, **ARB16\_HEAD**, **ARB32\_HEAD**, **ARB\_ALLOCSIZE**, **ARB\_INITIALIZER**, **ARB\_ROOT**, **ARB\_EMPTY**, **ARB\_FULL**, **ARB\_CURNODES**, **ARB\_MAXNODES**, **ARB\_NEXT**, **ARB\_PREV**, **ARB\_MIN**, **ARB\_MAX**, **ARB\_FIND**, **ARB\_NFIND**, **ARB\_LEFT**, **ARB\_LEFTIDX**, **ARB\_RIGHT**, **ARB\_RIGHTIDX**, **ARB\_PARENT**, **ARB\_PARENTIDX**, **ARB\_GETFREE**, **ARB\_FREEIDX**, **ARB\_FOREACH**, **ARB\_FOREACH\_FROM**, **ARB\_FOREACH\_SAFE**, **ARB\_FOREACH\_REVERSE**, **ARB\_FOREACH\_REVERSE\_FROM**, **ARB\_FOREACH\_REVERSE\_SAFE**, **ARB\_INIT**, **ARB\_INSERT**, **ARB\_REMOVE**, **ARB\_REINSERT**, **ARB\_RESET\_TREE** – array-based red-black trees

## SYNOPSIS

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

**ARB\_PROTOTYPE**(*NAME*, *TYPE*, *FIELD*, *CMP*);

**ARB\_PROTOTYPE\_STATIC**(*NAME*, *TYPE*, *FIELD*, *CMP*);

**ARB\_PROTOTYPE\_INSERT**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_INSERT\_COLOR**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_REMOVE**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_REMOVE\_COLOR**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_FIND**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_NFIND**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_NEXT**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_PREV**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_MINMAX**(*NAME*, *TYPE*, *ATTR*);

**ARB\_PROTOTYPE\_REINSERT**(*NAME*, *TYPE*, *ATTR*);

**ARB\_GENERATE**(*NAME*, *TYPE*, *FIELD*, *CMP*);

**ARB\_GENERATE\_STATIC**(*NAME*, *TYPE*, *FIELD*, *CMP*);

**ARB\_GENERATE\_INSERT**(*NAME*, *TYPE*, *FIELD*, *CMP*, *ATTR*);

**ARB\_GENERATE\_INSERT\_COLOR**(*NAME*, *TYPE*, *FIELD*, *ATTR*);

**ARB\_GENERATE\_REMOVE**(*NAME*, *TYPE*, *FIELD*, *ATTR*);

**ARB\_GENERATE\_REMOVE\_COLOR**(*NAME*, *TYPE*, *FIELD*, *ATTR*);

**ARB\_GENERATE\_FIND**(*NAME*, *TYPE*, *FIELD*, *CMP*, *ATTR*);

**ARB\_GENERATE\_NFIND**(*NAME*, *TYPE*, *FIELD*, *CMP*, *ATTR*);

**ARB\_GENERATE\_NEXT**(*NAME*, *TYPE*, *FIELD*, *ATTR*);

**ARB\_GENERATE\_PREV**(*NAME*, *TYPE*, *FIELD*, *ATTR*);

**ARB\_GENERATE\_MINMAX**(*NAME*, *TYPE*, *FIELD*, *ATTR*);

**ARB\_GENERATE\_REINSERT**(*NAME*, *TYPE*, *FIELD*, *CMP*, *ATTR*);

**ARB\<8\|16\|32\>\_ENTRY**();

**ARB\<8\|16\|32\>\_HEAD**(*HEADNAME*, *TYPE*);

*size\_t*\

**ARB\_ALLOCSIZE**(*ARB\_HEAD \*head*, *int\<8\|16\|32\>\_t maxnodes*, *struct TYPE \*elm*);

**ARB\_INITIALIZER**(*ARB\_HEAD \*head*, *int\<8\|16\|32\>\_t maxnodes*);

*struct TYPE \**\

**ARB\_ROOT**(*ARB\_HEAD \*head*);

*bool*\

**ARB\_EMPTY**(*ARB\_HEAD \*head*);

*bool*\

**ARB\_FULL**(*ARB\_HEAD \*head*);

*int\<8\|16\|32\>\_t*\

**ARB\_CURNODES**(*ARB\_HEAD \*head*);

*int\<8\|16\|32\>\_t*\

**ARB\_MAXNODES**(*ARB\_HEAD \*head*);

*struct TYPE \**\

**ARB\_NEXT**(*NAME*, *ARB\_HEAD \*head*, *struct TYPE \*elm*);

*struct TYPE \**\

**ARB\_PREV**(*NAME*, *ARB\_HEAD \*head*, *struct TYPE \*elm*);

*struct TYPE \**\

**ARB\_MIN**(*NAME*, *ARB\_HEAD \*head*);

*struct TYPE \**\

**ARB\_MAX**(*NAME*, *ARB\_HEAD \*head*);

*struct TYPE \**\

**ARB\_FIND**(*NAME*, *ARB\_HEAD \*head*, *struct TYPE \*elm*);

*struct TYPE \**\

**ARB\_NFIND**(*NAME*, *ARB\_HEAD \*head*, *struct TYPE \*elm*);

*struct TYPE \**\

**ARB\_LEFT**(*struct TYPE \*elm*, *ARB\_ENTRY NAME*);

*int\<8\|16\|32\>\_t*\

**ARB\_LEFTIDX**(*struct TYPE \*elm*, *ARB\_ENTRY NAME*);

*struct TYPE \**\

**ARB\_RIGHT**(*struct TYPE \*elm*, *ARB\_ENTRY NAME*);

*int\<8\|16\|32\>\_t*\

**ARB\_RIGHTIDX**(*struct TYPE \*elm*, *ARB\_ENTRY NAME*);

*struct TYPE \**\

**ARB\_PARENT**(*struct TYPE \*elm*, *ARB\_ENTRY NAME*);

*int\<8\|16\|32\>\_t*\

**ARB\_PARENTIDX**(*struct TYPE \*elm*, *ARB\_ENTRY NAME*);

*struct TYPE \**\

**ARB\_GETFREE**(*ARB\_HEAD \*head*, *FIELD*);

*int\<8\|16\|32\>\_t*\

**ARB\_FREEIDX**(*ARB\_HEAD \*head*);

**ARB\_FOREACH**(*VARNAME*, *NAME*, *ARB\_HEAD \*head*);

**ARB\_FOREACH\_FROM**(*VARNAME*, *NAME*, *POS\_VARNAME*);

**ARB\_FOREACH\_SAFE**(*VARNAME*, *NAME*, *ARB\_HEAD \*head*, *TEMP\_VARNAME*);

**ARB\_FOREACH\_REVERSE**(*VARNAME*, *NAME*, *ARB\_HEAD \*head*);

**ARB\_FOREACH\_REVERSE\_FROM**(*VARNAME*, *NAME*, *POS\_VARNAME*);

**ARB\_FOREACH\_REVERSE\_SAFE**(*VARNAME*, *NAME*, *ARB\_HEAD \*head*, *TEMP\_VARNAME*);

*void*\

**ARB\_INIT**(*struct TYPE \*elm*, *FIELD*, *ARB\_HEAD \*head*, *int\<8\|16\|32\>\_t maxnodes*);

*struct TYPE \**\

**ARB\_INSERT**(*NAME*, *ARB\_HEAD \*head*, *struct TYPE \*elm*);

*struct TYPE \**\

**ARB\_REMOVE**(*NAME*, *ARB\_HEAD \*head*, *struct TYPE \*elm*);

*struct TYPE \**\

**ARB\_REINSERT**(*NAME*, *ARB\_HEAD \*head*, *struct TYPE \*elm*);

*void*\

**ARB\_RESET\_TREE**(*ARB\_HEAD \*head*, *NAME*, *int\<8\|16\|32\>\_t maxnodes*);

## DESCRIPTION

These macros define data structures for and array-based red-black trees. They use a single, continuous chunk of memory, and are useful e.g., when the tree needs to be transferred between userspace and kernel.

In the macro definitions, *TYPE* is the name tag of a user defined structure that must contain a field of type *ARB\_ENTRY*, named *ENTRYNAME*. The argument *HEADNAME* is the name tag of a user defined structure that must be declared using the **ARB\_HEAD**() macro. The argument *NAME* has to be a unique name prefix for every tree that is defined.

The function prototypes are declared with **ARB\_PROTOTYPE**(), or **ARB\_PROTOTYPE\_STATIC**(). The function bodies are generated with **ARB\_GENERATE**(), or **ARB\_GENERATE\_STATIC**(). See the examples below for further explanation of how these macros are used.

A red-black tree is a binary search tree with the node color as an extra attribute. It fulfills a set of conditions:

1. Every search path from the root to a leaf consists of the same number of black nodes.

2. Each red node (except for the root) has a black parent.

3. Each leaf node is black.

Every operation on a red-black tree is bounded as **O**(*lg n*). The maximum height of a red-black tree is **2lg**(*n + 1*).

**ARB\_\***() trees require entries to be allocated as an array, and uses array indices to link entries together. The maximum number of **ARB\_\***() tree entries is therefore constrained by the minimum of array size and choice of signed integer data type used to store array indices. Use **ARB\_ALLOCSIZE**() to compute the size of memory chunk to allocate.

A red-black tree is headed by a structure defined by the **ARB\_HEAD**() macro. A structure is declared with either of the following:

**ARB\<8\|16\|32\>\_HEAD**(*HEADNAME*, *TYPE*) *head*;

where *HEADNAME* is the name of the structure to be defined, and struct *TYPE* is the type of the elements to be inserted into the tree.

The **ARB\_HEAD**() variant includes a suffix denoting the signed integer data type size (in bits) used to store array indices. For example, **ARB\_HEAD8**() creates a red-black tree head structure with 8-bit signed array indices capable of indexing up to 128 entries.

The **ARB\_ENTRY**() macro declares a structure that allows elements to be connected in the tree. Similarly to the **ARB\<8\|16\|32\>\_HEAD**() macro, the **ARB\_ENTRY**() variant includes a suffix denoting the signed integer data type size (in bits) used to store array indices. Entries should use the same number of bits as the tree head structure they will be linked into.

In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the **ARB\_PROTOTYPE**() or **ARB\_PROTOTYPE\_STATIC**() macro, where *NAME* is a unique identifier for this particular tree. The *TYPE* argument is the type of the structure that is being managed by the tree. The *FIELD* argument is the name of the element defined by **ARB\_ENTRY**(). Individual prototypes can be declared with **ARB\_PROTOTYPE\_INSERT**(), **ARB\_PROTOTYPE\_INSERT\_COLOR**(), **ARB\_PROTOTYPE\_REMOVE**(), **ARB\_PROTOTYPE\_REMOVE\_COLOR**(), **ARB\_PROTOTYPE\_FIND**(), **ARB\_PROTOTYPE\_NFIND**(), **ARB\_PROTOTYPE\_NEXT**(), **ARB\_PROTOTYPE\_PREV**(), **ARB\_PROTOTYPE\_MINMAX**(), and **ARB\_PROTOTYPE\_REINSERT**() in case not all functions are required. The individual prototype macros expect *NAME*, *TYPE*, and *ATTR* arguments. The *ATTR* argument must be empty for global functions or *static* for static functions.

The function bodies are generated with the **ARB\_GENERATE**() or **ARB\_GENERATE\_STATIC**() macro. These macros take the same arguments as the **ARB\_PROTOTYPE**() and **ARB\_PROTOTYPE\_STATIC**() macros, but should be used only once. As an alternative individual function bodies are generated with the **ARB\_GENERATE\_INSERT**(), **ARB\_GENERATE\_INSERT\_COLOR**(), **ARB\_GENERATE\_REMOVE**(), **ARB\_GENERATE\_REMOVE\_COLOR**(), **ARB\_GENERATE\_FIND**(), **ARB\_GENERATE\_NFIND**(), **ARB\_GENERATE\_NEXT**(), **ARB\_GENERATE\_PREV**(), **ARB\_GENERATE\_MINMAX**(), and **ARB\_GENERATE\_REINSERT**() macros.

Finally, the *CMP* argument is the name of a function used to compare tree nodes with each other. The function takes two arguments of type *struct TYPE \**. If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements.

The **ARB\_INIT**() macro initializes the tree referenced by *head*, with the array length of *maxnodes*.

The red-black tree can also be initialized statically by using the **ARB\_INITIALIZER**() macro:

**ARB\<8\|16\|32\>\_HEAD**(*HEADNAME*, *TYPE*) *head* = **ARB\_INITIALIZER**(*&head*, *maxnodes*);

The **ARB\_INSERT**() macro inserts the new element *elm* into the tree.

The **ARB\_REMOVE**() macro removes the element *elm* from the tree pointed by *head*.

The **ARB\_FIND**() and **ARB\_NFIND**() macros can be used to find a particular element in the tree.

```
struct TYPE find, *res;
find.key = 30;
res = ARB_FIND(NAME, head, &find);
```

The **ARB\_ROOT**(), **ARB\_MIN**(), **ARB\_MAX**(), **ARB\_NEXT**(), and **ARB\_PREV**() macros can be used to traverse the tree:

```
for (np = ARB_MIN(NAME, &head); np != NULL; np = ARB_NEXT(NAME, &head, np))
```

Or, for simplicity, one can use the **ARB\_FOREACH**() or **ARB\_FOREACH\_REVERSE**() macro:

**ARB\_FOREACH**(*np*, *NAME*, *head*)

The macros **ARB\_FOREACH\_SAFE**() and **ARB\_FOREACH\_REVERSE\_SAFE**() traverse the tree referenced by head in a forward or reverse direction respectively, assigning each element in turn to np. However, unlike their unsafe counterparts, they permit both the removal of np as well as freeing it from within the loop safely without interfering with the traversal.

Both **ARB\_FOREACH\_FROM**() and **ARB\_FOREACH\_REVERSE\_FROM**() may be used to continue an interrupted traversal in a forward or reverse direction respectively. The head pointer is not required. The pointer to the node from where to resume the traversal should be passed as their last argument, and will be overwritten to provide safe traversal.

The **ARB\_EMPTY**() macro should be used to check whether a red-black tree is empty.

Given that ARB trees have an intrinsic upper bound on the number of entries, some ARB-specific additional macros are defined. The **ARB\_FULL**() macro returns a boolean indicating whether the current number of tree entries equals the tree's maximum. The **ARB\_CURNODES**() and **ARB\_MAXNODES**() macros return the current and maximum number of entries respectively. The **ARB\_GETFREE**() macro returns a pointer to the next free entry in the array of entries, ready to be linked into the tree. The **ARB\_INSERT**() returns NULL if the element was inserted in the tree successfully, otherwise they return a pointer to the element with the colliding key.

Accordingly, **ARB\_REMOVE**() returns the pointer to the removed element otherwise they return NULL to indicate an error.

The **ARB\_REINSERT**() macro updates the position of the element *elm* in the tree. This must be called if a member of a **tree** is modified in a way that affects comparison, such as by modifying a node's key. This is a lower overhead alternative to removing the element and reinserting it again.

The **ARB\_RESET\_TREE**() macro discards the tree topology. It does not modify embedded object values or the free list.

## SEE ALSO

[queue(3)](/queue/3.md), [tree(3)](/tree/3.md)

## HISTORY

The **ARB** macros first appeared in FreeBSD 13.0.

## AUTHORS

The **ARB** macros were implemented by Lawrence Stewart ⟨*lstewart@FreeBSD.org*⟩, based on [tree(3)](/tree/3.md) macros written by Niels Provos.
