ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mdl_export_workspace.c
Go to the documentation of this file.
1
14
15#include <string.h>
16
17#include "mdl_export.h"
18
19void mdl_export_workspace_init(mdl_export_workspace_t* ws, void* data, size_t cap)
20{
21 if (ws == nullptr) {
22 return;
23 }
24 ws->data = (uint8_t*)data;
25 ws->cap = (data == nullptr) ? 0U : cap;
26 ws->used = 0U;
27 ws->high_water = 0U;
28}
29
30void* mdl_export_workspace_take(mdl_export_workspace_t* ws, size_t bytes, size_t alignment)
31{
32 if ((ws == nullptr) || (ws->data == nullptr) || (bytes == 0U) || (alignment == 0U) ||
33 ((alignment & (alignment - 1U)) != 0U)) {
34 return nullptr;
35 }
36 const size_t mask = alignment - 1U;
37 uintptr_t base = 0U;
38 static_assert(sizeof(base) >= sizeof(ws->data), "uintptr_t must preserve object pointers");
39 (void)memcpy((void*)&base, (const void*)&ws->data, sizeof(ws->data));
40 if ((ws->used > (size_t)(UINTPTR_MAX - base)) ||
41 ((base + (uintptr_t)ws->used) > (UINTPTR_MAX - (uintptr_t)mask))) {
42 return nullptr;
43 }
44 const uintptr_t cursor = base + (uintptr_t)ws->used;
45 const uintptr_t aligned = (cursor + (uintptr_t)mask) & ~(uintptr_t)mask;
46 const size_t start = (size_t)(aligned - base);
47 if ((start > ws->cap) || (bytes > (ws->cap - start))) {
48 return nullptr;
49 }
50 ws->used = start + bytes;
51 if (ws->used > ws->high_water) {
52 ws->high_water = ws->used;
53 }
54 return &ws->data[start];
55}
Package a downloaded chapter folder into a reader-openable container.
struct mdl_export_workspace mdl_export_workspace_t
Caller-owned bounded arena for all exporter scratch state.
void * mdl_export_workspace_take(mdl_export_workspace_t *ws, size_t bytes, size_t alignment)
Reserve aligned bytes from an exporter arena.
void mdl_export_workspace_init(mdl_export_workspace_t *ws, void *data, size_t cap)
Bind an exporter arena without allocating memory.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
size_t high_water
Largest used value observed.
Definition mdl_export.h:221
size_t used
Current allocation high edge.
Definition mdl_export.h:220
size_t cap
Total arena capacity.
Definition mdl_export.h:219
uint8_t * data
Writable arena bytes.
Definition mdl_export.h:218