![]() |
SuperTinyKernel™ RTOS 1.06.0
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Fixed-size block allocator with O(1) alloc/free and proper task-blocking semantics. More...
#include <stk_memory_blockpool.h>
Fixed-size block allocator with O(1) alloc/free and proper task-blocking semantics.
BlockMemoryPool provides a deterministic, fragmentation-free allocator for scenarios where the same block size is repeatedly allocated and released - such as packet buffers, sensor sample records, or task-local state objects.
The pool uses an intrusive singly-linked free-list inside the storage array. When a block is free, its first sizeof(void*) bytes hold a pointer to the next free block; no separate metadata array is needed. Alloc and Free are therefore O(1) with a minimal critical section.
Memory layout (pool storage, contiguous):
[ block_0 | block_1 | ... | block_{n-1} ]
^ ^
m_storage (m_storage + n * m_block_size)
At construction all blocks are chained: block_i->next = block_{i+1},
last->next = nullptr. m_free_list points to block_0.
Two storage modes** mirror the CMSIS osMemoryPoolAttr_t mp_mem / mp_size fields and allow fully static (zero-heap) deployments:
| Mode | Constructor | Frees storage? |
|---|---|---|
| External storage | BlockMemoryPool(cap, blksz, buf, bufsz [, name]) | No - caller |
| Heap storage | BlockMemoryPool(cap, blksz [, name]) | Yes - dtor |
Blocking semantics**: TimedAlloc() suspends the calling task via a ConditionVariable until Free() returns a block, or the timeout expires. This replaces the spin-yield polling loop that was used in the old CMSIS wrapper, giving up the CPU completely while blocked instead of burning cycles.
ISR safety**: TryAlloc() and Free() are ISR-safe (guarded by a critical section). Alloc() and TimedAlloc() with a non-zero timeout must only be called from task context.