ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_stbtt_alloc.c
Go to the documentation of this file.
1
20
21#include "ra8_stbtt_alloc.h"
22
23#include <stdalign.h>
24#include <stddef.h>
25#include <stdint.h>
26
36
40
42static size_t s_offset = 0U;
43
45static size_t s_live = 0U;
46
48static size_t s_high_water = 0U;
49
50void* ra8_stbtt_malloc(size_t n)
51{
52 /* Reject requests too large to ever fit; this also prevents the
53 * alignment round-up below from overflowing. */
54 if (n > (size_t)k_ra8_stbtt_arena_bytes) {
55 return nullptr;
56 }
57 const size_t aligned = (n + (size_t)k_ra8_stbtt_align_mask) & ~(size_t)k_ra8_stbtt_align_mask;
58 if (aligned > (size_t)k_ra8_stbtt_arena_bytes - s_offset) {
59 return nullptr;
60 }
61 void* const block = &s_arena[s_offset];
62 s_offset += aligned;
63 s_live += 1U;
64 if (s_offset > s_high_water) {
66 }
67 return block;
68}
69
70void ra8_stbtt_free(void* p)
71{
72 if (p == nullptr) {
73 return;
74 }
75 if (s_live > 0U) {
76 s_live -= 1U;
77 }
78 if (s_live == 0U) {
79 s_offset = 0U;
80 }
81}
82
84{
85 return s_high_water;
86}
static uint8_t s_arena[k_c6_cam_arena_bytes]
Definition c6_cam_app.c:43
static size_t s_high_water
Peak s_offset observed since boot (diagnostic).
void * ra8_stbtt_malloc(size_t n)
Allocate n bytes of glyph scratch from the static arena.
static size_t s_live
Count of live (allocated, not yet freed) blocks.
static size_t s_offset
Current bump offset into s_arena, bytes.
void ra8_stbtt_free(void *p)
Release a block previously returned by ra8_stbtt_malloc().
ra8_stbtt_alloc_consts_t
Arena sizing knobs (no magic numbers).
@ k_ra8_stbtt_align
Allocation alignment, bytes.
@ k_ra8_stbtt_arena_bytes
3x the 32 KiB worst case.
@ k_ra8_stbtt_align_mask
k_ra8_stbtt_align - 1.
size_t ra8_stbtt_alloc_high_water(void)
Worst-case arena high-water mark observed since boot, in bytes.
Heap-free scratch allocator for stb_truetype (glyph rasterise).