ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_arena.c File Reference

Init-time bump-arena implementation (Layer 0, #147). More...

#include "ra8_arena.h"
#include <stdint.h>
#include "ra8_attributes.h"
#include "ra8_check.h"
#include "ra8_err.h"
Include dependency graph for ra8_arena.c:

Go to the source code of this file.

Functions

static bool internal_is_pow2 (uint32_t v)
 Test whether v is a power of two.
ra8_err_t ra8_arena_init (ra8_arena_t *arena, void *base, uint32_t size)
 Bind a bump arena over a caller-owned tier region.
ra8_err_t ra8_arena_carve (ra8_arena_t *arena, uint32_t bytes, uint32_t align, void **out_ptr)
 Carve an aligned sub-block from the arena (bump; no free).
ra8_err_t ra8_arena_remaining (const ra8_arena_t *arena, uint32_t *out_remaining)
 Report the bytes still available in the arena.

Variables

static const char *const s_tag = "ra8_arena"
 Module log tag.

Detailed Description

Init-time bump-arena implementation (Layer 0, #147).

Tag
[Ring 2 / Core] {World: NS}

Bump-only allocation with power-of-two alignment. Fit checks use uintptr_t subtraction against the region end so they never overflow.

Definition in file ra8_arena.c.

Function Documentation

◆ internal_is_pow2()

bool internal_is_pow2 ( uint32_t v)
static

Test whether v is a power of two.

A power of two has exactly one set bit, so v & (v - 1) is zero; zero is rejected by the leading v != 0 term.

Parameters
[in]vValue to test.
Returns
true if v is a non-zero power of two, else false.
Return values
truev is a non-zero power of two.
falsev is zero or has more than one set bit.
Precondition
None.
v fits in a uint32_t.
Postcondition
No state is modified.
The result depends only on v.
Note
Pure; thread-safe.
Since
0.1.0

Definition at line 48 of file ra8_arena.c.

References RA8_INTERNAL.

Referenced by ra8_arena_carve().

◆ ra8_arena_carve()

ra8_err_t ra8_arena_carve ( ra8_arena_t * arena,
uint32_t bytes,
uint32_t align,
void ** out_ptr )
nodiscard

Carve an aligned sub-block from the arena (bump; no free).

Parameters
[in]arenaInitialised arena.
[in]bytesBlock size to carve (> 0).
[in]alignRequired alignment in bytes (a power of two, >= 1).
[out]out_ptrReceives the aligned block pointer on success.
Returns
ra8_err_t Error code.
Return values
k_ra8_okBlock carved; *out_ptr set.
k_ra8_err_null_ptrarena or out_ptr was NULL.
k_ra8_err_invalid_sizebytes was zero.
k_ra8_err_invalid_argalign was zero or not a power of two.
k_ra8_err_no_memThe aligned block does not fit the remainder.
Precondition
arena was populated by ra8_arena_init.
align is a power of two.
Postcondition
On success *out_ptr is align-aligned with bytes of storage, and arena->used advanced past it.
On any non-ok return the arena is unchanged.
Note
Not thread-safe; init-time use only (there is no matching free).
Since
0.1.0

Definition at line 69 of file ra8_arena.c.

References ra8_arena_t::base, internal_is_pow2(), k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_no_mem, k_ra8_ok, RA8_CHECK_NULL_PTR, s_tag, ra8_arena_t::size, and ra8_arena_t::used.

Referenced by mem_run_arena().

◆ ra8_arena_init()

ra8_err_t ra8_arena_init ( ra8_arena_t * arena,
void * base,
uint32_t size )
nodiscard

Bind a bump arena over a caller-owned tier region.

Parameters
[out]arenaArena state to populate (zero-initialised by caller).
[in]baseFirst byte of the region (out-lives the arena).
[in]sizeRegion length in bytes.
Returns
ra8_err_t Error code.
Return values
k_ra8_okArena ready (empty).
k_ra8_err_null_ptrarena or base was NULL.
k_ra8_err_invalid_sizesize was zero.
Precondition
base addresses at least size writable bytes.
arena does not alias base.
Postcondition
On success arena->used == 0 and the whole region is available.
On any non-ok return arena is left unbound.
Note
Not thread-safe.
Since
0.1.0

Definition at line 56 of file ra8_arena.c.

References ra8_arena_t::base, k_ra8_err_invalid_size, k_ra8_ok, RA8_CHECK_NULL_PTR, s_tag, ra8_arena_t::size, and ra8_arena_t::used.

Referenced by mem_run_arena().

◆ ra8_arena_remaining()

ra8_err_t ra8_arena_remaining ( const ra8_arena_t * arena,
uint32_t * out_remaining )
nodiscard

Report the bytes still available in the arena.

Parameters
[in]arenaInitialised arena.
[out]out_remainingReceives size - used.
Returns
ra8_err_t Error code.
Return values
k_ra8_okRemaining bytes reported.
k_ra8_err_null_ptrarena or out_remaining was NULL.
Precondition
arena was populated by ra8_arena_init.
out_remaining is writable.
Postcondition
*out_remaining == arena->size - arena->used.
No arena state is mutated.
Note
Thread-safe with respect to a quiescent arena (pure read).
Since
0.1.0

Definition at line 94 of file ra8_arena.c.

References k_ra8_ok, RA8_CHECK_NULL_PTR, s_tag, ra8_arena_t::size, and ra8_arena_t::used.

Referenced by mem_run_arena().

Variable Documentation

◆ s_tag

const char* const s_tag = "ra8_arena"
static

Module log tag.

Definition at line 25 of file ra8_arena.c.