MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
memoria_externa.h
Go to the documentation of this file.
1/* MMgr - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial OR LicenseRef-Educational
3 *
4 * Every use falls under AGPL-3.0-or-later unless you hold explicit permission, which is either a
5 * negotiated commercial licensing contract or an educator's license issued to you personally.
6 */
7/**
8 * @file memoria_externa.h
9 * @brief Placement between internal and external memory, and a two-buffer index.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @warning Everything below exists only when MMGR_ENABLE_EXTRAM holds.
14 */
15#ifndef MMGR_MEMORIA_EXTERNA_H
16#define MMGR_MEMORIA_EXTERNA_H
17
18/** @addtogroup mod_exter
19 * @{ */
20
21#include "mmgr.h"
22
23#if MMGR_ENABLE_EXTRAM
24
25EMBED_BEGIN_DECLS
26
27/**
28 * @brief Where a request should go.
29 *
30 * @note One byte on every compiler, through EMBED_ENUM_BYTE.
31 */
32EMBED_ENUM_BYTE(mmgr_place, PLACE_DRAM = 0, /**< Internal memory. */
33 PLACE_PSRAM = 1, /**< External memory. */
34 PLACE_FAIL = 2 /**< Neither will take it. */
35);
36
37/**
38 * @brief A pair of buffers, one under fill while the other is under drain.
39 *
40 * @note fill_index is the only member. The buffers it indexes live elsewhere.
41 * @note The two roles are one bit. The drain index is this one's complement. A swap is a flip and
42 * there is no second member to retain in step.
43 * @warning What holds fill_index to 0 or 1 is the set in mmgr_pingpong_init and the flip in
44 * mmgr_pingpong_swap. A pair that did not go through mmgr_pingpong_init carries whatever its
45 * storage contained, and no call here tests the value.
46 */
47typedef struct
48{
49 uint8_t fill_index; /**< Index of the buffer under fill, 0 or 1. */
50} PingPong;
51
52/**
53 * @brief Arguments for every exter call, where each call reads only the members it needs.
54 *
55 * @note The caller supplies free_dram and free_psram and they are as given.
56 * @note place reads the six figures. The pingpong entries read pingpong alone.
57 */
58typedef struct
59{
60 const size_t size; /**< Bytes to place. */
61 const embed_bool dma_required; /**< The bytes must be reachable by DMA. */
62 const size_t free_dram; /**< Bytes still free in internal memory. */
63 const size_t free_psram; /**< Bytes still free in external memory. */
64 const size_t psram_threshold; /**< Size at or above which external memory comes first. */
65 const size_t dram_reserve; /**< Internal bytes that must remain free afterwards. */
66 PingPong *const pingpong; /**< Pair the pingpong entries act on [BORROWS]. */
68
69/**
70 * @brief Type of the exter dispatch table.
71 *
72 * @note EMBED_TABLE_LAYOUT asserts the five members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
73 * nothing else.
74 * @note Every entry takes the same argument pack, as in locus_carcerum and memoria_anularis.
75 */
76typedef struct
77{
78 mmgr_place (*place)(const ExternaCfg *args); /**< Decides where a request goes. */
79 void (*pingpong_init)(const ExternaCfg *args); /**< Points the pair at buffer 0. */
80 uint8_t (*pingpong_fill)(const ExternaCfg *args); /**< Index under fill. */
81 uint8_t (*pingpong_drain)(const ExternaCfg *args); /**< Index under drain. */
82 uint8_t (*pingpong_swap)(const ExternaCfg *args); /**< Swaps the two roles. */
84EMBED_TABLE_LAYOUT(MemoriaExternaNs, place, pingpong_init, pingpong_fill, pingpong_drain, pingpong_swap);
85
86/**
87 * @brief Decides whether a request belongs in internal or external memory.
88 *
89 * @param[in] args Request size, the DMA requirement and both memory figures [BORROWS].
90 * @return PLACE_DRAM, PLACE_PSRAM, or PLACE_FAIL when neither will take it.
91 * @note A size of 0 gives PLACE_FAIL.
92 * @note A DMA request only ever gives PLACE_DRAM or PLACE_FAIL, never external memory.
93 * @note At or above psram_threshold external memory comes first, below it internal does.
94 * @warning The two tests are not the same shape. Internal placement must also leave dram_reserve
95 * free, where the external one is a size comparison alone.
96 */
97EMBED_HOST_DEVICE mmgr_place mmgr_extern_place(const ExternaCfg *args);
98
99/**
100 * @brief Points the pair at buffer 0.
101 *
102 * @param[in,out] args Pair to reset, as args->pingpong [BORROWS].
103 * @note The other three pingpong entries read a pair this call set.
104 * @warning args->pingpong must not be null. Nothing checks it and no assertion covers it, and this
105 * call writes through it at once.
106 */
107EMBED_HOST_DEVICE void mmgr_pingpong_init(const ExternaCfg *args);
108
109/**
110 * @brief Returns the index of the buffer under fill.
111 *
112 * @param[in] args Pair to read, as args->pingpong [BORROWS].
113 * @return 0 or 1.
114 * @note Does not modify args->pingpong.
115 * @warning args->pingpong must not be null. Nothing checks it and no assertion covers it.
116 * @warning Reports fill_index as it is. On a pair that has not been through mmgr_pingpong_init
117 * that is whatever its storage contained, and this call does not bound it to 0 or 1.
118 */
119EMBED_HOST_DEVICE uint8_t mmgr_pingpong_fill_index(const ExternaCfg *args);
120
121/**
122 * @brief Returns the index of the buffer under drain.
123 *
124 * @param[in] args Pair to read, as args->pingpong [BORROWS].
125 * @return The other index, 0 or 1.
126 * @note The answer is fill_index with its low bit flipped, and so the two indexes always disagree.
127 * @note Does not modify args->pingpong.
128 * @warning args->pingpong must not be null. Nothing checks it and no assertion covers it.
129 * @warning The answer is an index only while fill_index is 0 or 1. On a pair that has not been
130 * through mmgr_pingpong_init it is the flip of whatever its storage contained.
131 */
132EMBED_HOST_DEVICE uint8_t mmgr_pingpong_drain_index(const ExternaCfg *args);
133
134/**
135 * @brief Swaps which buffer takes the fill and which the drain.
136 *
137 * @param[in,out] args Pair to flip, as args->pingpong [BORROWS].
138 * @return The index now under fill, 0 or 1.
139 * @note Flips the low bit. A swap of a swap is where it started.
140 * @warning args->pingpong must not be null. Nothing checks it and no assertion covers it.
141 * @warning One caller at a time. The flip and the read that follows it are two steps on a plain byte,
142 * with no atomic and no lock. So two callers that swap the same pair can come away with
143 * the same index.
144 */
145EMBED_HOST_DEVICE uint8_t mmgr_pingpong_swap(const ExternaCfg *args);
146
147/**
148 * @brief Dispatch table instance named exter.
149 *
150 * @note pingpong_fill calls mmgr_pingpong_fill_index and pingpong_drain calls mmgr_pingpong_drain_index.
151 */
152EMBED_TABLE_STORAGE MemoriaExternaNs exter EMBED_UNUSED = {
153 .place = mmgr_extern_place,
154 .pingpong_init = mmgr_pingpong_init,
155 .pingpong_fill = mmgr_pingpong_fill_index,
156 .pingpong_drain = mmgr_pingpong_drain_index,
157 .pingpong_swap = mmgr_pingpong_swap,
158};
159
160EMBED_END_DECLS
161
162#endif
163
164/** @} */
165
166#endif
static const MemoriaExternaNs exter
Dispatch table instance named exter.
mmgr_place
Where a request should go.
EMBED_HOST_DEVICE mmgr_place mmgr_extern_place(const ExternaCfg *args)
Decides whether a request belongs in internal or external memory.
EMBED_HOST_DEVICE void mmgr_pingpong_init(const ExternaCfg *args)
Points the pair at buffer 0.
EMBED_HOST_DEVICE uint8_t mmgr_pingpong_fill_index(const ExternaCfg *args)
Returns the index of the buffer under fill.
EMBED_HOST_DEVICE uint8_t mmgr_pingpong_swap(const ExternaCfg *args)
Swaps which buffer takes the fill and which the drain.
EMBED_HOST_DEVICE uint8_t mmgr_pingpong_drain_index(const ExternaCfg *args)
Returns the index of the buffer under drain.
@ PLACE_FAIL
@ PLACE_PSRAM
@ PLACE_DRAM
The embedded_types headers every module builds on.
Arguments for every exter call, where each call reads only the members it needs.
const size_t size
const embed_bool dma_required
PingPong *const pingpong
const size_t psram_threshold
const size_t dram_reserve
const size_t free_psram
const size_t free_dram
Type of the exter dispatch table.
uint8_t(*) pingpong_fill(const ExternaCfg *args)
void(*) pingpong_init(const ExternaCfg *args)
uint8_t(*) pingpong_drain(const ExternaCfg *args)
mmgr_place(*) place(const ExternaCfg *args)
uint8_t(*) pingpong_swap(const ExternaCfg *args)
A pair of buffers, one under fill while the other is under drain.
uint8_t fill_index