ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_vmem_stream.c
Go to the documentation of this file.
1
18
19#include "ra8_vmem_stream.h"
20
21#include <stddef.h>
22#include <stdint.h>
23#include <string.h>
24
25#include "ra8_check.h"
26#include "ra8_err.h"
27#include "ra8_vmem.h"
28
30static const char* const s_tag = "ra8_vmem_stream";
31
33ra8_vmem_stream_init(ra8_vmem_stream_t* st, ra8_vmem_t* vm, uint32_t object_id, uint64_t size)
34{
35 RA8_CHECK_NULL_PTR(st, s_tag, "st must not be nullptr");
36 RA8_CHECK_NULL_PTR(vm, s_tag, "vm must not be nullptr");
37 if (size == 0U) {
39 }
40 const uint32_t fb = vm->cfg.frame_bytes;
41 if (fb == 0U) {
43 }
44 st->vm = vm;
45 st->object_id = object_id;
46 st->frame_bytes = fb;
47 st->size = size;
48 return k_ra8_ok;
49}
50
51size_t ra8_vmem_stream_read(void* ctx, uint64_t offset, void* buf, size_t len)
52{
54 if (st == nullptr) {
55 return 0U;
56 }
57 if (buf == nullptr) {
58 return 0U;
59 }
60 if (st->frame_bytes == 0U) {
61 return 0U;
62 }
63 if (len == 0U) {
64 return 0U;
65 }
66 if (offset >= st->size) {
67 return 0U;
68 }
69
70 const uint64_t avail = st->size - offset;
71 const size_t want = ((uint64_t)len > avail) ? (size_t)avail : len;
72
73 uint8_t* const out = (uint8_t*)buf;
74 const uint32_t fb = st->frame_bytes;
75 size_t done = 0U;
76 uint64_t cur = offset;
77
78 /* Bounded loop (NASA P10 Rule 2): every pass copies `chunk >= 1` bytes and
79 * `done` rises monotonically toward the fixed `want`, so the loop runs at most
80 * `want` times; `cur` advances by the same amount, walking frame by frame. */
81 while (done < want) {
82 const uint64_t frame_base = cur - (cur % (uint64_t)fb);
83 const uint32_t in_frame = (uint32_t)(cur - frame_base);
84 const uint32_t frame_room = fb - in_frame;
85 const size_t remaining = want - done;
86 const size_t chunk = (remaining < (size_t)frame_room) ? remaining : (size_t)frame_room;
87
88 void* page = nullptr;
89 if (ra8_vmem_get(st->vm, st->object_id, frame_base, &page) != k_ra8_ok) {
90 break;
91 }
92 (void)memcpy(out + done, (const uint8_t*)page + in_frame, chunk);
93 if (ra8_vmem_put(st->vm, page) != k_ra8_ok) {
94 break; /* GCOVR_EXCL_LINE -- put fails only on a foreign page; pin came from the get above */
95 }
96 done += chunk;
97 cur += chunk;
98 }
99 return done;
100}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
Byte-range page cache with SLRU eviction (Layer 2, #147).
ra8_err_t ra8_vmem_put(ra8_vmem_t *vm, void *page)
Release one pin on a frame previously returned by ra8_vmem_get.
Definition ra8_vmem.c:179
ra8_err_t ra8_vmem_get(ra8_vmem_t *vm, uint32_t object_id, uint64_t offset, void **out_page)
Get (and pin) the frame holding object object_id at offset.
Definition ra8_vmem.c:162
ra8_err_t ra8_vmem_stream_init(ra8_vmem_stream_t *st, ra8_vmem_t *vm, uint32_t object_id, uint64_t size)
Bind a page-cached paged object to the byte-stream reader.
size_t ra8_vmem_stream_read(void *ctx, uint64_t offset, void *buf, size_t len)
Read len bytes at absolute offset through the page cache.
Read a page-cached object as a seekable byte stream (Layer 2 helper, #147/#151).
uint32_t frame_bytes
Bytes per frame (page size, e.g.
Definition ra8_vmem.h:153
Binds one page-cached object to the byte-stream read adapter.
uint32_t object_id
Paged object id registered in the cache's source.
ra8_vmem_t * vm
Initialised page cache (fixed pool = RAM budget).
uint64_t size
Object length in bytes.
uint32_t frame_bytes
Cache frame size, cached from vm->cfg at init.
Page-cache state (caller-owned; treat as private).
Definition ra8_vmem.h:179
ra8_vmem_cfg_t cfg
The configuration (copied at init).
Definition ra8_vmem.h:180