19#if defined(ARDUINO) && !defined(NDEBUG)
20#include "freertos/FreeRTOS.h"
21#include "freertos/task.h"
51ScratchArenaCtx s_scratch_arena;
54constexpr size_t SCRATCH_DEFAULT_ALIGN =
sizeof(
void *) > 8 ?
sizeof(
void *) : 8;
58inline int cur_worker()
70inline void assert_single_owner(
int w)
72#if defined(ARDUINO) && !defined(NDEBUG)
74 TaskHandle_t cur = xTaskGetCurrentTaskHandle();
75 if (s_owner[w] ==
nullptr)
78 assert(s_owner[w] == cur &&
"scratch arena borrowed from a foreign task");
88 assert_single_owner(w);
91 align = SCRATCH_DEFAULT_ALIGN;
92 assert((align & (align - 1)) == 0 &&
"scratch alignment must be a power of two");
95 size_t base = (s_scratch.off[w] + (align - 1)) & ~(align - 1);
103 void *p = &s_scratch_arena.arena[w][base];
104 s_scratch.off[w] = base + n;
105 if (s_scratch.off[w] > s_scratch.high_water[w])
106 s_scratch.high_water[w] = s_scratch.off[w];
112 int w = cur_worker();
113 assert_single_owner(w);
114 s_scratch.off[w] = 0;
119 int w = cur_worker();
120 assert_single_owner(w);
121 return s_scratch.off[w];
126 int w = cur_worker();
127 assert_single_owner(w);
128 assert(mark <= s_scratch.off[w] &&
"scratch_release mark is above the current offset");
129 s_scratch.off[w] = mark;
134 return s_scratch.off[cur_worker()];
142 if (s_scratch.high_water[w] > peak)
143 peak = s_scratch.high_water[w];
#define DETWS_WORKER_COUNT
Number of server worker tasks (slots partitioned i % N). Default 1.
#define DETWS_SCRATCH_ARENA_SIZE
Size in bytes of the shared per-dispatch scratch arena.
size_t scratch_mark(void)
Capture the current arena offset (a savepoint for scratch_release()).
size_t scratch_capacity(void)
Total arena capacity in bytes (DETWS_SCRATCH_ARENA_SIZE).
void * scratch_alloc(size_t n, size_t align)
Borrow n bytes of scratch, aligned to align.
size_t scratch_used(void)
Bytes currently handed out (0 immediately after a reset).
size_t scratch_high_water(void)
Largest scratch_used() value seen since boot (for sizing the arena).
void scratch_reset(void)
Reclaim the whole arena (empties it).
void scratch_release(size_t mark)
Reclaim everything allocated since mark (LIFO).
Shared per-dispatch scratch arena (Layer 5, session-scoped memory).
int detws_worker_self(void)
Worker id [0, count) of the calling task; 0 by default / single-worker.
Layer 5 (Session) - server worker identity.