ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_webp_arena.c
Go to the documentation of this file.
1
20
21#include "ra8_webp_arena.h"
22
23#include <stddef.h>
24#include <stdint.h>
25#include <string.h>
26
31typedef enum : uint32_t {
35
37static ra8_webp_arena_t* s_arena = nullptr;
38
40{
41 s_arena = arena;
42 if (arena != nullptr) {
43 arena->offset = 0U;
44 arena->live = 0U;
45 }
46}
47
49{
50 s_arena = nullptr;
51}
52
53void* ra8_webp_arena_malloc(size_t n)
54{
55 ra8_webp_arena_t* const a = s_arena;
56 if (a == nullptr) {
57 return nullptr;
58 }
59 /* Reject requests too large to ever fit; this also prevents the alignment
60 * round-up below from overflowing. */
61 if (n > a->cap) {
62 return nullptr;
63 }
64 const size_t aligned = (n + (size_t)k_ra8_webp_align_mask) & ~(size_t)k_ra8_webp_align_mask;
65 if (aligned > (a->cap - a->offset)) {
66 return nullptr;
67 }
68 void* const block = &a->base[a->offset];
69 a->offset += aligned;
70 a->live += 1U;
71 return block;
72}
73
74void* ra8_webp_arena_calloc(size_t nmemb, size_t size)
75{
76 /* Guard the product against size_t overflow before allocating (libwebp
77 * checks this too, but the arena must be safe on its own). */
78 if ((size != 0U) && (nmemb > (SIZE_MAX / size))) {
79 return nullptr;
80 }
81 const size_t total = nmemb * size;
82 void* const block = ra8_webp_arena_malloc(total);
83 if (block == nullptr) {
84 return nullptr;
85 }
86 if (total > 0U) {
87 memset(block, 0, total);
88 }
89 return block;
90}
91
93{
94 ra8_webp_arena_t* const a = s_arena;
95 if ((p == nullptr) || (a == nullptr)) {
96 return;
97 }
98 if (a->live > 0U) {
99 a->live -= 1U;
100 }
101 if (a->live == 0U) {
102 a->offset = 0U;
103 }
104}
static uint8_t s_arena[k_c6_cam_arena_bytes]
Definition c6_cam_app.c:43
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
void ra8_webp_arena_unbind(void)
Unbind the active arena; subsequent allocation hooks fail with nullptr.
void * ra8_webp_arena_calloc(size_t nmemb, size_t size)
libwebp WebPSafeCalloc hook: zeroed nmemb * size bytes.
void ra8_webp_arena_free(void *p)
libwebp WebPSafeFree hook: release a block; auto-reset when none live.
void ra8_webp_arena_bind(ra8_webp_arena_t *arena)
Bind arena as the active scratch for subsequent decode allocations.
ra8_webp_arena_consts_t
Arena alignment knobs (no magic numbers).
@ k_ra8_webp_align
Allocation alignment, bytes.
@ k_ra8_webp_align_mask
k_ra8_webp_align - 1 (round-up mask).
void * ra8_webp_arena_malloc(size_t n)
libwebp WebPSafeMalloc hook: bump n bytes from the bound arena.
Heap-free scratch allocator hooks for the vendored libwebp decoder.
Caller-owned bump arena backing a single WebP decode.
size_t cap
Backing-store capacity in bytes.
uint8_t * base
First byte of caller-owned backing store (>= cap bytes).
uint32_t live
Count of outstanding (unfreed) allocations.
size_t offset
Bump cursor; next allocation starts here.