35static const char*
const s_tag =
"ra8_keycache";
43typedef enum : uint32_t {
59typedef enum : uint8_t {
195 const uint8_t* p = (
const uint8_t*)key;
197 for (uint32_t i = 0U; i < key_bytes; ++i) {
270 if (m[f].prev != -1) {
272 }
else if (*head == f) {
277 if (m[f].next != -1) {
279 }
else if (*tail == f) {
377 for (uint32_t guard = 0U; guard < kc->
cfg.
cell_count; ++guard) {
417 for (uint32_t guard = 0U; guard < kc->
cfg.
cell_count; ++guard) {
422 if (m->
valid != 0U) {
459 for (uint32_t guard = 0U; guard < kc->
cfg.
cell_count; ++guard) {
760 (void)
memset(kc, 0,
sizeof(*kc));
769 for (uint32_t i = 0U; i < cfg->
cell_count; ++i) {
834 if (m->
valid != 0U) {
838 const uint8_t seg = m->
seg;
901 const uintptr_t base = (uintptr_t)kc->
cfg.
cell_mem;
902 const uintptr_t addr = (uintptr_t)data;
906 const uintptr_t off = addr - base;
914 const uint32_t idx = (uint32_t)(off / (uintptr_t)kc->
cfg.
cell_bytes);
924 uint32_t* out_misses,
925 uint32_t* out_evictions)
931 if (out_hits !=
nullptr) {
932 *out_hits = kc->
hits;
934 if (out_misses !=
nullptr) {
937 if (out_evictions !=
nullptr) {
static const char * s_tag
Logging / check tag.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
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.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_no_mem
Static buffer exhausted (no dynamic memory on this project).
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
static uint32_t internal_protected_cap(const ra8_keycache_cfg_t *cfg)
Resolve the SLRU protected-segment capacity, in cells.
static void internal_seed_cells(ra8_keycache_t *kc, const ra8_keycache_cfg_t *cfg)
Seed a validated cache: clear buckets and link every cell cold.
static void internal_access(ra8_keycache_t *kc, int32_t f)
Re-reference cell f on a hit under the configured policy.
static int32_t internal_pick_victim(const ra8_keycache_t *kc)
Select an evictable victim: probationary LRU first, then protected LRU.
static void * internal_user_ptr(const ra8_keycache_t *kc, uint32_t idx)
User-descriptor pointer for cell idx, or NULL when unused.
static uint8_t * internal_cell_ptr(const ra8_keycache_t *kc, uint32_t idx)
Cell payload pointer for cell idx.
ra8_err_t ra8_keycache_get(ra8_keycache_t *kc, const void *key, ra8_keycache_view_t *out_view)
Get (and pin) the cell for key, rendering it on a miss.
static bool internal_key_eq(const ra8_keycache_t *kc, const void *a, const void *b)
Byte-wise key equality over key_bytes.
static uint32_t internal_hash(const ra8_keycache_t *kc, const void *key)
Hash a key blob into a bucket index using the configured policy.
static void internal_hash_remove(ra8_keycache_t *kc, int32_t f)
Remove cell f from its hash bucket chain.
ra8_err_t ra8_keycache_stats(const ra8_keycache_t *kc, uint32_t *out_hits, uint32_t *out_misses, uint32_t *out_evictions)
Report the cache hit / miss / eviction counters.
static void internal_unlink(ra8_keycache_t *kc, int32_t f, int32_t *head, int32_t *tail)
Detach cell f from the recency list owned by head / tail.
static int32_t internal_hash_lookup(const ra8_keycache_t *kc, const void *key)
Find the valid cell holding key, or -1.
static uint32_t internal_fnv1a(const void *key, uint32_t key_bytes)
FNV-1a hash of a key blob (the built-in default hash).
static void internal_slru_access(ra8_keycache_t *kc, int32_t f)
SLRU re-reference: promote / refresh cell f on a hit.
ra8_err_t ra8_keycache_init(ra8_keycache_t *kc, const ra8_keycache_cfg_t *cfg)
Initialise a cache engine over caller-supplied storage.
static ra8_err_t internal_miss(ra8_keycache_t *kc, const void *key, ra8_keycache_view_t *out_view)
Handle a get miss: evict a victim, render the cell, insert + pin it.
ra8_keycache_const_t
Hashing constants and the SLRU split defaults / bounds.
@ k_keycache_percent_full
Percent denominator / max split.
@ k_keycache_protected_pct_def
Default SLRU protected share.
@ k_keycache_fnv_prime
FNV-1a 32-bit prime.
@ k_keycache_fnv_offset
FNV-1a 32-bit offset basis.
static void internal_hash_insert(ra8_keycache_t *kc, int32_t f)
Insert cell f into its hash bucket chain.
ra8_err_t ra8_keycache_put(ra8_keycache_t *kc, const uint8_t *data)
Release one pin on a cell previously returned by ra8_keycache_get.
static void internal_push_head(ra8_keycache_t *kc, int32_t f, int32_t *head, int32_t *tail)
Push cell f onto the MRU head of a recency list.
static ra8_err_t internal_validate_cfg_sizes(const ra8_keycache_cfg_t *cfg)
Validate that every config sizing field is non-zero.
static ra8_err_t internal_validate_cfg_policy(const ra8_keycache_cfg_t *cfg)
Validate the SLRU split knob when the SLRU policy is selected.
ra8_keycache_seg_t
SLRU segment tags stored in ra8_keycache_cell_t::seg.
@ k_keycache_seg_probation
Probationary segment (scan absorber).
@ k_keycache_seg_protected
Protected segment (hot working set).
static ra8_err_t internal_validate_cfg_ptrs(const ra8_keycache_cfg_t *cfg)
Validate that every required config pointer is non-NULL.
static uint8_t * internal_key_ptr(const ra8_keycache_t *kc, uint32_t idx)
Key-storage pointer for cell idx.
static int32_t internal_first_unpinned(const ra8_keycache_t *kc, int32_t tail)
Find the first unpinned cell walking from tail toward the MRU.
ra8_err_t ra8_keycache_prefetch(ra8_keycache_t *kc, const void *key)
Warm the cell for key into the cache without holding a pin.
The one reusable hash + pin + evict cache engine (#147, #345).
@ k_ra8_keycache_evict_slru
Segmented LRU / 2Q (scan-resistant).
Per-cell link metadata (one caller-owned array entry per cell).
int32_t next
Recency link toward LRU within the segment, or -1.
uint16_t pin_count
Outstanding pins (0 => evictable).
int32_t prev
Recency link toward MRU within the segment, or -1.
uint8_t valid
1 => this cell holds an entry.
uint8_t seg
SLRU segment tag (probationary / protected).
int32_t hash_next
Hash bucket chain link, or -1.
Caller-supplied storage + policy + renderer for ra8_keycache_init.
void * hash_ctx
Opaque context passed to hash.
uint32_t bucket_count
Number of hash buckets (>= 1).
uint32_t user_bytes
Bytes per user descriptor (may be 0).
uint8_t * cell_mem
cell_count * cell_bytes of cell storage.
uint8_t protected_pct
SLRU protected share 1..100; 0 => 75%.
uint32_t cell_bytes
Bytes per cell (the rendered payload).
uint8_t * key_mem
cell_count * key_bytes of key storage.
int32_t * buckets
bucket_count hash-bucket heads.
ra8_keycache_evict_t evict
Eviction policy (0 => LRU; SLRU opt-in).
uint32_t key_bytes
Bytes per key (>= 1).
ra8_keycache_cell_t * meta
cell_count link-metadata entries.
uint32_t cell_count
Number of cells.
void * render_ctx
Opaque context passed to render.
ra8_keycache_render_fn render
Render-on-miss callback.
uint8_t * user_mem
cell_count * user_bytes, or NULL if none.
ra8_keycache_hash_fn hash
Key hash; NULL selects built-in FNV-1a.
Cache engine state (caller-owned; treat as private).
uint32_t protected_count
Cells in the protected segment.
int32_t pt_tail
Protected LRU cell, or -1 (SLRU only).
uint32_t protected_cap
Protected-segment capacity (0 = LRU).
int32_t pt_head
Protected MRU cell, or -1 (SLRU only).
uint32_t hits
Get hits so far.
uint32_t misses
Get misses so far.
ra8_keycache_cfg_t cfg
Configuration (copied at init).
uint32_t evictions
Entries evicted so far.
int32_t pb_tail
Probationary LRU cell, or -1.
int32_t pb_head
Probationary MRU cell, or -1.
A pinned view of a cached cell returned by ra8_keycache_get.
uint8_t * data
Cell payload (cell_bytes wide).