RBT_INIT(9)
π RBT_INIT(9) β Kernel Developer's Manual π
okay bestie, so this is the page for the red-black tree kernel API, section 9 of the Kernel Developer's Manual, and it's lowkey iconic. sit down, grab a strawberry drink π, and let's get into it.
β¨ NAME β¨
RBT_HEAD, RBT_ENTRY, RBT_PROTOTYPE, RBT_GENERATE, RBT_INITIALIZER, RBT_INIT, RBT_INSERT, RBT_REMOVE, RBT_FIND, RBT_NFIND, RBT_EMPTY, RBT_ROOT, RBT_MIN, RBT_MAX, RBT_NEXT, RBT_PREV, RBT_LEFT, RBT_RIGHT, RBT_PARENT, RBT_SET_LEFT, RBT_SET_RIGHT, RBT_SET_PARENT, RBT_FOREACH, RBT_FOREACH_SAFE, RBT_FOREACH_REVERSE, RBT_FOREACH_REVERSE_SAFE, RBT_POISON, RBT_CHECK β Kernel red-black trees πΈ
like, that's the whole cast. main characters only.
π SYNOPSIS π
#include <sys/tree.h>
RBT_HEAD(NAME, TYPE);
RBT_ENTRY(TYPE);
RBT_PROTOTYPE(NAME, TYPE, ENTRY,
int (*compare)(const struct TYPE *, const struct TYPE *));
RBT_GENERATE(NAME, TYPE, ENTRY,
int (*compare)(const struct TYPE *, const struct TYPE *));
struct NAME
RBT_INITIALIZER(struct NAME *self);
void
RBT_INIT(NAME, struct NAME *rbt);
struct TYPE *
RBT_INSERT(NAME, struct NAME *rbt, struct TYPE *elm);
struct TYPE *
RBT_REMOVE(NAME, struct NAME *rbt, struct TYPE *elm);
struct TYPE *
RBT_FIND(NAME, struct NAME *rbt, const struct TYPE *key);
struct TYPE *
RBT_NFIND(NAME, struct NAME *rbt, const struct TYPE *key);
int
RBT_EMPTY(NAME, struct NAME *rbt);
struct TYPE *
RBT_ROOT(NAME, struct NAME *rbt);
struct TYPE *
RBT_MIN(NAME, struct NAME *rbt);
struct TYPE *
RBT_MAX(NAME, struct NAME *rbt);
struct TYPE *
RBT_NEXT(NAME, struct TYPE *elm);
struct TYPE *
RBT_PREV(NAME, struct TYPE *elm);
struct TYPE *
RBT_LEFT(NAME, struct TYPE *elm);
struct TYPE *
RBT_RIGHT(NAME, struct TYPE *elm);
struct TYPE *
RBT_PARENT(NAME, struct TYPE *elm);
void
RBT_SET_LEFT(NAME, struct TYPE *elm, struct TYPE *left);
void
RBT_SET_RIGHT(NAME, struct TYPE *elm, struct TYPE *right);
void
RBT_SET_PARENT(NAME, struct TYPE *elm, struct TYPE *parent);
RBT_FOREACH(VARNAME, NAME, struct NAME *rbt);
RBT_FOREACH_REVERSE(VARNAME, NAME, struct NAME *rbt);
RBT_FOREACH_SAFE(VARNAME, NAME, struct NAME *rbt, NEXTVARNAME);
RBT_FOREACH_REVERSE_SAFE(VARNAME, NAME, struct NAME *rbt, NEXTVARNAME);
void
RBT_POISON(NAME, struct TYPE *elm, unsigned long poison);
int
RBT_CHECK(NAME, struct TYPE *elm, unsigned long poison);
copy those exactly, bestie. character for character. no improvising π
π¦ DESCRIPTION π¦
so the red-black tree API gives you data structures and operations for storing structures in red-black trees.
a red-black tree is a binary search tree, except every node also carries a color as an extra attribute. it's giving extra. and it fulfills a whole set of conditions:
- every search path from the root to a leaf consists of the same number of black nodes,
- each red node (except for the root) has a black parent,
- each leaf node is black.
every single operation on a red-black tree is bounded as O(lg n). the maximum height of a red-black tree is 2lg (n+1). gorgeous. efficient. no notes. β¨
this API is implemented as a set of functions that operate on generic pointers β yes, the void-pointer life β but users of the API generate wrappers and macros that provide type safety when calling the functions. so it's generic under the hood but type-safe at the surface. the way that works? obsessed.
in the macro definitions:
- TYPE is the name of a structure that will be stored in a red-black tree. the TYPE structure must contain an
RBT_ENTRY()field that lets the element be connected to a tree. no entry field, no tree. periodt. - the argument NAME is the name of a red-black tree type that can store a particular TYPE element.
π Creating a Red-Black Tree Type π
the RBT_HEAD() macro creates a red-black tree type to store TYPE structures as elements in the tree. the argument NAME must uniquely identify every type of tree that is defined. so likeβ¦ don't reuse names, bestie, that's not the vibe π
RBT_PROTOTYPE() produces the wrappers for a red-black tree type identified by NAME to operate on elements of type TYPE. ENTRY specifies which field in the TYPE structure is used to connect elements to NAME red-black trees. elements in the red-black tree are ordered according to the result of comparing them with the compare function, and the rules are:
- if the first argument to compare is to be ordered lower than the second, the function returns a value smaller than zero.
- if the first argument is to be ordered higher than the second, the function returns a value greater than zero.
- if they are equal, the function returns zero.
that's the whole contract, iykyk π
RBT_GENERATE() produces the internal data structures used by the red-black tree type identified by NAME to operate on elements of type TYPE. the ENTRY and compare arguments are the same as the RBT_PROTOTYPE() arguments of the same names. same names, same energy. π«Ά
π§ Initialising a Red-Black Tree π§
RBT_INIT()initialises the red-black tree rbt of type NAME to an empty state. fresh start, clean slate, brand new era β¨RBT_INITIALIZER()can be used to initialise a declaration of the red-black tree self of type NAME to an empty state.
π₯ Red-Black Tree Operations π₯
RBT_INSERT()β inserts the element elm into the red-black tree rbt of type NAME. upon success,NULLis returned. if a matching element already exists in the tree, the insertion is aborted and a pointer to the existing element is returned. so it's not an error, it's a "we already have one at home" moment πRBT_REMOVE()β removes the element elm from the red-black tree rbt of type NAME. elm must exist in the tree rbt before it is removed. key detail, don't skip it πRBT_FIND()β performs a binary search for an exact match of key in the red-black tree rbt of type NAME.RBT_NFIND()β performs a binary search for the first node that is greater than or equal to key in the red-black tree rbt of type NAME.RBT_EMPTY()β returns if the red-black tree rbt of type NAME is empty.RBT_ROOT()β returns the root element in the red-black tree rbt of type NAME. main character energy πRBT_MIN()β returns the lowest ordered element in the red-black tree rbt of type NAME.RBT_MAX()β returns the highest ordered element in the red-black tree rbt of type NAME.
π Red-Black Tree Element Operations π
RBT_NEXT()β returns a pointer to the next ordered element after elm in a red-black tree of type NAME.RBT_PREV()β returns a pointer to the previous ordered element before elm in a red-black tree of type NAME.RBT_LEFT()β returns a pointer to the left child element of elm in a red-black tree of type NAME.RBT_RIGHT()β returns a pointer to the right child element of elm in a red-black tree of type NAME.RBT_PARENT()β returns a pointer to the parent element of elm in a red-black tree of type NAME.RBT_SET_LEFT()β sets the left child pointer of element elm to left in a red-black tree of type NAME.RBT_SET_RIGHT()β sets the right child pointer of element elm to right in a red-black tree of type NAME.RBT_SET_PARENT()β sets the parent pointer of element elm to parent in a red-black tree of type NAME.
π Red-Black Tree Iterators π
the RBT_FOREACH() macro iterates over the red-black tree rbt of type NAME from the lowest ordered element to the highest ordered element, setting VARNAME to each element in turn. going up β¬οΈ
the RBT_FOREACH_REVERSE() macro iterates over the red-black tree rbt of type NAME from the highest ordered element to the lowest ordered element, setting VARNAME to each element in turn. going down β¬οΈ
the RBT_FOREACH_SAFE() macro iterates over the red-black tree rbt of type NAME from the lowest ordered element to the highest ordered element, setting VARNAME to each element in turn. and here's the slay part: VARNAME may be removed from the tree during iteration because a reference to the next element is held in NEXTVARNAME. safe deletion while looping, we love to see it π«Ά
the RBT_FOREACH_REVERSE_SAFE() macro iterates over the red-black tree rbt of type NAME from the highest ordered element to the lowest ordered element, setting VARNAME to each element in turn. VARNAME may be removed from the tree during iteration because a reference to the next element is held in NEXTVARNAME. same safety, opposite direction, equally iconic π
π Red-Black Tree Element Poisoning π
RBT_POISON()β used to poison the pointers in theRBT_ENTRYstructure inside elm which has been removed from a red-black tree of type NAME, with the poison value.RBT_CHECK()β used to verify that the pointers in theRBT_ENTRYstructure inside elm are set to the poison value.
so you yeet an element, you poison its pointers, and later you check they're still poisoned. it's giving toxic ex energy and honestly? valid π
β CONTEXT β
these can be called during autoconf, from process context, or from interrupt context:
RBT_INIT(), RBT_INSERT(), RBT_REMOVE(), RBT_FIND(), RBT_NFIND(), RBT_EMPTY(), RBT_ROOT(), RBT_MIN(), RBT_MAX(), RBT_NEXT(), RBT_PREV(), RBT_LEFT(), RBT_RIGHT(), RBT_PARENT(), RBT_SET_LEFT(), RBT_SET_RIGHT(), RBT_SET_PARENT(), RBT_FOREACH(), RBT_FOREACH_REVERSE(), RBT_FOREACH_SAFE(), RBT_FOREACH_SAFE_REVERSE(), RBT_POISON(), and RBT_CHECK().
(yes, the list here says RBT_FOREACH_SAFE_REVERSE() β we're just reporting what the page says, bestie, we don't make the rules π)
β οΈ the locking is on YOU. it is up to the caller to provide appropriate locking around calls to these functions to prevent concurrent access to the relevant data structures. nobody's gonna do it for you. this is not a drill, fr fr.
πΈ RETURN VALUES πΈ
RBT_INSERT()β returnsNULLon successful insertion of elm into the tree; otherwise it returns a reference to an existing element with the same key.RBT_FIND()β returns a reference to an element that compares as equal to key, orNULLif no such element could be found.RBT_NFIND()β returns a reference to the nearest element that compares as equal or greater to key, orNULLif no such element could be found.RBT_EMPTY()β returns non-zero if the red-black tree rbt is empty, otherwise0.RBT_ROOT()β returns a reference to the root node in the red-black tree rbt, orNULLif it is empty.RBT_MIN()β returns a reference to the lowest ordered element in the red-black tree rbt, orNULLif it is empty.RBT_MAX()β returns a reference to the lowest ordered element in the red-black tree rbt, orNULLif it is empty. (okay yes, the page literally says lowest forRBT_MAX. we are simply the messenger. don't shoot π«Ά)RBT_NEXT()β returns a reference to the next ordered element in the red-black tree after elm, orNULLif it is the greatest element in the tree.RBT_PREV()β returns a reference to the previous ordered element in the red-black tree before elm, orNULLif it is the lowest element in the tree.RBT_LEFT()β returns a reference to the left child element of elm, orNULLif it is a leaf in the tree.RBT_RIGHT()β returns a reference to the right child element of elm, orNULLif it is a leaf in the tree.RBT_PARENT()β returns a reference to the parent element of elm, orNULLif it is the root of the tree.RBT_CHECK()β returns non-zero if theRBT_ENTRYin the red-black tree element contains the poison value, otherwise0.
π SEE ALSO π
the cousin. the lore. go read it too π
π HISTORY π
the red-black tree kernel API first appeared in OpenBSD 6.1. a cultural reset. β¨
and that's the page, bestie!! π¦ this one shipped on the Debian build of the manual, dated June 8, 2017 β so if you're citing it, that's the source and date to reference π« go forth and balance your trees, you gorgeous nerd ππ
π¦ where this page comes from
- nixpkgs unstable: cvs-export
- Copyright 2002 Niels Provos <provos@citi.umich.edu>
- Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms