Skip to content

Memory Arenas

include/mem/arena.h View source View on GitHub

This subsystem allows the kernel to provide generic memory arenas that explicitly declare their capabilities and accept generic parameters and have extensible extra functions, for whatever purposes they desire.

Broadly, everything (or almost everything) is built with substantial room for extensibility, which allows for very different arenas that all funnel through this API, which exists to enforce safety and provide a unified substrate.

struct arena_params (3 fields) source

These are generally passed into the arena allocation creation as a generic way of giving parameters. Notably NOT arena_attributes, as these are merely the param words.

Also usable in arena_alloc_special in substitution of the size parameter, which is relevant to some allocators

§ void *ptr
§ struct alloc_params alloc_params
§ uint64_t params[4]
struct arena_attributes (2 fields) source
§ enum arena_flags flags
§ struct arena_params params

For the underlying arena, to describe whatever

struct arena_result (2 fields) source

The output of the extended arena functions

§ enum err err
§ struct arena_params params
enum arena_err (2 variants) source

Errors 128+ can be used by any provider

§ ARENA_ERR_BUDGET = ERR_DELTA_START
§ ARENA_ERR_ALLOC_INVALID
enum arena_flags : uint64_t (7 variants) source

arena_flags: 64 bit bitflags

These flags denote the subset of globally recognized arena capabilities

Upper 32 bits are available for whatever needs it in the arena allocator internally

┌─────────────────────────────────────────────────────────────┐
Bits │ 63--32 31..28 27..24 23..20 19..16 15..12 11..8 7..4 3..0 │
Use │ AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA │
└─────────────────────────────────────────────────────────────┘

A - Unused (available) * - Unused (unavailable)

§ ARENA_FLAG_TAGGED = 1 << 0

“tagging at the per-object granularity

§ ARENA_FLAG_TRACED = 1 << 1

“stack trace at the per-object granularity”

§ ARENA_FLAG_LOGGED = 1 << 2

Has a log site

§ ARENA_FLAG_LOCKED = 1 << 3

Use locks

§ ARENA_FLAG_REFCOUNTED = 1 << 4

A reference counter is used

§ ARENA_FLAG_RCU = 1 << 5

Protected by RCU and a defer callback

§ ARENA_FLAG_BUDGETED = 1 << 6

Have a budget

enum arena_strategy (9 variants) source

There are 31 default arena policies (1-31, 0 is omitted to avoid uninitialized issues and bugs), and 32 marks the point where anything at or greater than this is a strategy registered by other code, not by default

§ ARENA_STRATEGY_SIMPLE = 1

simple, carve out from valloc

§ ARENA_STRATEGY_BUMP

Bump allocator

§ ARENA_STRATEGY_FIXED

All allocations are the same size, managed at initialization time

§ ARENA_STRATEGY_FREELIST
§ ARENA_STRATEGY_SLAB
TODO

this should ideally reuse slab allocator code, which might mean a little porting will need to happen.

Hopefully not much, since this is ideally just a wrapper around objects

§ ARENA_STRATEGY_MINI_SLAB

DIFFERENT. Notably significantly lighter-weight than the full slab allocator

§ ARENA_STRATEGY_BUDDY
§ ARENA_STRATEGY_RESERVE

Reserve allocators are explicitly fed memory in, so they never perform an underlying allocation operation

§ ARENA_STRATEGY_MAX = 32
void arena_global_init(void)
source
extern struct err_facility arena_err_facility
source