GENERIC RADIX TREES SPARSE ARRAYS(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

✨ Generic radix trees/sparse arrays ✨

okay bestie, welcome to section 9 of the Kernel API Manual β€” this is the genradix page and honestly? it's giving "small but mighty" πŸ’…

🌸 DESCRIPTION 🌸

So a genradix is very simple and minimalistic, and it supports arbitrary size entries up to GENRADIX_NODE_SIZE. no cap, that's the whole vibe β€” tiny API, does one thing, slays.

You define a genradix with the type it's gonna store, like this πŸŽ€:

static GENRADIX(struct foo) foo_genradix;

and the main operations are, like, the whole squad:

  • genradix_init(radix) β€” initialize an empty genradix ✨
  • genradix_free(radix) β€” frees all memory owned by the genradix and reinitializes it (two-for-one, we love a multitasker) πŸ’–
  • genradix_ptr(radix, idx) β€” gets a pointer to the entry at idx, returning NULL if that entry does not exist πŸ‘€
  • genradix_ptr_alloc(radix, idx, gfp) β€” gets a pointer to an entry, allocating it if necessary πŸ› οΈ
  • genradix_for_each(radix, iter, p) β€” iterate over each entry in a genradix 🌈

Now the important deets, bestie, listen up 🫢: the radix tree allocates one page of entries at a time, so entries may exist that were never explicitly allocated β€” and those get initialized to all zeroes. free zeroes, no drama, iconic.

Internally a genradix is just a radix tree of pages, and indexing works in terms of byte offsets. The wrappers in this header file use sizeof on the type the radix contains to calculate a byte offset from the index β€” see __idx_to_offset. that's the whole magic trick, iykyk 🧚


πŸ”₯ SEE ALSO πŸ”₯

Kernel file ./include/linux/generic-radix-tree.h, plus the whole friend group: genradix_init(9), genradix_free(9), genradix_ptr(9), genradix_ptr_alloc(9), genradix_iter_init(9), genradix_iter_peek(9), genradix_iter_peek_prev(9), genradix_for_each(9), genradix_for_each_reverse(9), genradix_prealloc(9) ⭐


and that's the page, periodt πŸ’… mwah 🫢 this cutie came from ./include/linux, dated October 2026, and it goes by Generic rad...parse arrays(9) β€” yes the title is truncated like that, it's a whole aesthetic, don't question it πŸ’€πŸŽ€

πŸ“¦ where this page comes from