DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
scratch.cpp File Reference

Per-worker per-dispatch scratch arenas - implementation. More...

#include "scratch.h"
#include "network_drivers/session/worker.h"
#include <assert.h>
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

Go to the source code of this file.

Functions

void * scratch_alloc (size_t n, size_t align)
 Borrow n bytes of scratch, aligned to align.
 
void scratch_reset (void)
 Reclaim the whole arena (empties it).
 
size_t scratch_mark (void)
 Capture the current arena offset (a savepoint for scratch_release()).
 
void scratch_release (size_t mark)
 Reclaim everything allocated since mark (LIFO).
 
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).
 
size_t scratch_capacity (void)
 Total arena capacity in bytes (DETWS_SCRATCH_ARENA_SIZE).
 

Detailed Description

Per-worker per-dispatch scratch arenas - implementation.

One bump allocator per worker (DETWS_WORKER_COUNT arenas in BSS), selected by the caller's worker id. Each arena has exactly one accessor (its worker), so the lock-free / fixed-size guarantees in scratch.h hold per worker. With DETWS_WORKER_COUNT == 1 this is byte-for-byte the original single arena.

Definition in file scratch.cpp.

Function Documentation

◆ scratch_alloc()

void * scratch_alloc ( size_t  n,
size_t  align 
)

Borrow n bytes of scratch, aligned to align.

The returned pointer is valid only until the next scratch_reset() (i.e. only within the current session dispatch). Returns nullptr if the request does not fit the remaining arena - callers MUST handle null and fail closed.

Parameters
nbytes requested (0 yields a valid non-null pointer when space remains).
alignrequired alignment in bytes, a power of two (0 selects the platform default).
Returns
pointer to n writable bytes, or nullptr if it does not fit.

Definition at line 85 of file scratch.cpp.

References DETWS_SCRATCH_ARENA_SIZE.

Referenced by ssh_conn_close_channel(), ssh_conn_open_forwarded(), ssh_conn_send(), ssh_pkt_recv(), ssh_pkt_send(), and ws_send_frame().

◆ scratch_reset()

void scratch_reset ( void  )

Reclaim the whole arena (empties it).

Called by server_tick() before each event dispatch. Invalidates every pointer previously returned by scratch_alloc().

Definition at line 110 of file scratch.cpp.

◆ scratch_mark()

size_t scratch_mark ( void  )

Capture the current arena offset (a savepoint for scratch_release()).

Returns
an opaque mark to pass to scratch_release().

Definition at line 117 of file scratch.cpp.

◆ scratch_release()

void scratch_release ( size_t  mark)

Reclaim everything allocated since mark (LIFO).

Restores the arena to a previous scratch_mark(), freeing every scratch_alloc() made in between. Marks must be released in reverse order (nested scopes). Use ScratchScope for return-safe scoping.

Parameters
marka value previously returned by scratch_mark() (must be <= the current offset).

Definition at line 124 of file scratch.cpp.

Referenced by ScratchScope::~ScratchScope().

◆ scratch_used()

size_t scratch_used ( void  )

Bytes currently handed out (0 immediately after a reset).

Definition at line 132 of file scratch.cpp.

◆ scratch_high_water()

size_t scratch_high_water ( void  )

Largest scratch_used() value seen since boot (for sizing the arena).

Definition at line 137 of file scratch.cpp.

References DETWS_WORKER_COUNT.

◆ scratch_capacity()

size_t scratch_capacity ( void  )

Total arena capacity in bytes (DETWS_SCRATCH_ARENA_SIZE).

Definition at line 147 of file scratch.cpp.

References DETWS_SCRATCH_ARENA_SIZE.