ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_c6link_arena.c
Go to the documentation of this file.
1
31
32#include <stddef.h>
33#include <stdint.h>
34
35#include "ra8_attributes.h"
36#include "ra8_c6link.h"
37#include "ra8_c6link_internal.h"
38
66
67RA8_PRIV void* priv_c6link_arena_alloc(void* ctx, size_t size)
68{
69 ra8_c6link_t* link = (ra8_c6link_t*)ctx;
70 if ((link == nullptr) || (link->arena == nullptr)) {
71 return nullptr;
72 }
73
74 /* `arena_used` never exceeds `arena_bytes`, so this cannot underflow, and
75 every later comparison stays inside the remaining space -- which is what
76 lets the rounding below be checked without an overflow test that no
77 reachable input could ever make true. */
78 const uint32_t avail = link->arena_bytes - link->arena_used;
79 if (size > (size_t)avail) {
80 return nullptr;
81 }
82 const uint32_t want = (uint32_t)size;
83 const uint32_t pad = (uint32_t)((0U - want) & (uint32_t)k_ra8_c6link_arena_mask);
84 if (pad > (avail - want)) {
85 return nullptr;
86 }
87
88 const uint32_t at = link->arena_used;
89 link->arena_used = at + want + pad;
90 link->arena_last = at + 1U;
91 return &link->arena[at];
92}
93
94RA8_PRIV void priv_c6link_arena_free(void* ctx, void* pointer)
95{
96 ra8_c6link_t* link = (ra8_c6link_t*)ctx;
97 if ((link == nullptr) || (pointer == nullptr) || (link->arena_last == 0U)) {
98 return;
99 }
100 const uint32_t at = link->arena_last - 1U;
101 if (pointer == &link->arena[at]) {
102 link->arena_used = at;
103 link->arena_last = 0U;
104 }
105}
106
108{
109 if (link == nullptr) {
110 return;
111 }
112 link->arena_used = 0U;
113 link->arena_last = 0U;
114}
115
116RA8_PRIV void priv_c6link_arena_bind(ProtobufCAllocator* out, ra8_c6link_t* link)
117{
118 if ((out == nullptr) || (link == nullptr)) {
119 return;
120 }
121 out->alloc = priv_c6link_arena_alloc;
122 out->free = priv_c6link_arena_free;
123 out->allocator_data = link;
124}
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.