![]() |
SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Memory-related primitives. More...
Classes | |
| struct | MemoryBlock |
| Intrusive free-list node overlaid on the first word of every free block. More... | |
Functions | |
| BlockMemoryPool (size_t capacity, size_t raw_block_size, uint8_t *storage, size_t storage_size, const char *name=nullptr) | |
| Construct a pool backed by caller-supplied (external) storage. | |
| BlockMemoryPool (size_t capacity, size_t raw_block_size, const char *name=nullptr) | |
| Construct a pool with heap-allocated storage. | |
| ~BlockMemoryPool () | |
| Destructor. | |
| static constexpr size_t | AlignBlockSize (size_t raw_size) |
Round a raw block size up to the nearest multiple of BLOCK_ALIGN. | |
| void * | TimedAlloc (Timeout timeout=WAIT_INFINITE) |
| Allocate one block, blocking until one becomes available or the timeout expires. | |
| template<typename T> | |
| T * | TimedAllocT (Timeout timeout=WAIT_INFINITE) |
| Allocate one typed block, blocking until one becomes available or the timeout expires. | |
| void * | Alloc () |
| Allocate one block, blocking indefinitely until one is available. | |
| template<typename T> | |
| T * | AllocT () |
| Allocate one typed block, blocking indefinitely until one is available. | |
| void * | TryAlloc () |
| Non-blocking allocation attempt. | |
| template<typename T> | |
| T * | TryAllocT () |
| Non-blocking typed allocation attempt. | |
| bool | Free (void *ptr) |
| Return a previously allocated block to the pool. | |
| bool | IsStorageValid () const |
| Verify that the backing storage is valid and the pool is ready for use. | |
| size_t | GetCapacity () const |
| Get the total block capacity of the pool. | |
| size_t | GetBlockSize () const |
| Get the aligned block size used internally by the allocator. | |
| size_t | GetUsedCount () const |
| Get the number of currently allocated (outstanding) blocks. | |
| size_t | GetFreeCount () const |
| Get the number of free (available) blocks. | |
| bool | IsFull () const |
| Check whether all blocks are currently allocated (pool exhausted). | |
| bool | IsEmpty () const |
| Check whether all blocks are free (no outstanding allocations). | |
| STK_NONCOPYABLE_CLASS (BlockMemoryPool) | |
| void | BuildFreeList () |
Initialise the intrusive free-list across m_storage. | |
| void * | PopFreeList () |
Pop the head block from the free-list, increment m_used_count, and return it. | |
Variables | |
| static const size_t | CAPACITY_MAX = 0xFFFEU |
| static const uint32_t | BLOCK_ALIGN = static_cast<uint32_t>(sizeof(MemoryBlock)) |
Minimum block alignment in bytes: sizeof(MemoryBlock). | |
| uint8_t * | m_storage |
| flat byte array holding all N blocks (owned or external) | |
| MemoryBlock * | m_free_list |
| head of the intrusive free-list (nullptr when pool is empty) | |
| sync::ConditionVariable | m_cv |
| signalled by Free() to wake one task blocked in TimedAlloc() | |
| size_t | m_block_size |
| aligned block size in bytes (>= BLOCK_ALIGN) | |
| size_t | m_capacity |
| total number of blocks | |
| uint16_t | m_used_count |
| number of blocks currently allocated (outstanding) | |
| bool | m_storage_owned |
| true -> storage is heap-allocated; free in destructor | |
Memory-related primitives.
|
staticconstexpr |
Round a raw block size up to the nearest multiple of BLOCK_ALIGN.
Enforces a minimum of BLOCK_ALIGN so the free-list link always fits inside every free block without extra metadata.
| [in] | raw_size | Requested block size in bytes. |
BLOCK_ALIGN). Definition at line 159 of file stk_memory_blockpool.h.
References BLOCK_ALIGN, and stk::Max().
| void * stk::memory::Alloc | ( | ) |
Allocate one block, blocking indefinitely until one is available.
nullptr. | T * stk::memory::AllocT | ( | ) |
Allocate one typed block, blocking indefinitely until one is available.
nullptr.
|
explicit |
Construct a pool with heap-allocated storage.
Allocates a flat byte buffer of capacity * AlignBlockSize(raw_block_size) bytes via operator new (std::nothrow). When exceptions are disabled (embedded default), check IsStorageValid() immediately after construction to detect allocation failure before first use.
| [in] | capacity | Total number of blocks. |
| [in] | raw_block_size | Requested per-block size in bytes. |
| [in] | name | Optional name forwarded to ITraceable::SetTraceName(). |
osMemoryPoolNew().
|
explicit |
Construct a pool backed by caller-supplied (external) storage.
The pool references storage directly without taking ownership. The caller must keep the buffer alive for the entire lifetime of this object. The storage is not freed on destruction.
| [in] | capacity | Total number of blocks the pool can hold. |
| [in] | raw_block_size | Requested per-block size in bytes. Rounded up internally to AlignBlockSize(raw_block_size). |
| [in] | storage | Pointer to a caller-owned byte buffer. Must be aligned to at least sizeof(void*) and sized to hold at least capacity * AlignBlockSize(raw_block_size) bytes. Asserted at construction time. |
| [in] | storage_size | Size of storage in bytes (used for the size assertion). |
| [in] | name | Optional name forwarded to ITraceable::SetTraceName(). |
mp_mem / mp_size path in osMemoryPoolNew(). Referenced by stk::test::blockpool::StorageModeTask< _AccessMode >::Run(), RunTest(), and STK_NONCOPYABLE_CLASS().
|
private |
Initialise the intrusive free-list across m_storage.
Links blocks in reverse order so after the loop m_free_list points to block_0 (lowest address), giving ascending allocation order. Each free block stores a pointer to the next free block in its own first sizeof(MemoryBlock) bytes - zero extra metadata.
| bool stk::memory::Free | ( | void * | ptr | ) |
Return a previously allocated block to the pool.
Pushes the block back onto the free-list head in O(1) and wakes exactly one task blocked inside Alloc() or TimedAlloc(), if any. The woken task is guaranteed to find a free block available.
| [in] | ptr | Pointer previously returned by Alloc(), TimedAlloc(), or TryAlloc(). Must not be nullptr and must belong to this pool instance. Bounds and alignment are validated; failures trigger an assertion in debug builds and return false in release builds. |
true - block successfully returned. false - ptr is nullptr, out of range, or misaligned; each case indicates a caller logic error. Free() to prevent this. | size_t stk::memory::GetBlockSize | ( | ) | const |
Get the aligned block size used internally by the allocator.
Equal to AlignBlockSize(raw_block_size) passed at construction. Always >= BLOCK_ALIGN. Matches osMemoryPoolGetBlockSize().
Definition at line 255 of file stk_memory_blockpool.h.
References m_block_size.
| size_t stk::memory::GetCapacity | ( | ) | const |
Get the total block capacity of the pool.
osMemoryPoolGetCapacity(). Definition at line 247 of file stk_memory_blockpool.h.
References m_capacity.
| size_t stk::memory::GetFreeCount | ( | ) | const |
Get the number of free (available) blocks.
GetCapacity() - GetUsedCount(). Matches osMemoryPoolGetSpace(). Definition at line 269 of file stk_memory_blockpool.h.
References m_capacity, and m_used_count.
Referenced by IsFull().
| size_t stk::memory::GetUsedCount | ( | ) | const |
Get the number of currently allocated (outstanding) blocks.
osMemoryPoolGetCount(). Definition at line 262 of file stk_memory_blockpool.h.
References m_used_count.
| bool stk::memory::IsEmpty | ( | ) | const |
Check whether all blocks are free (no outstanding allocations).
true if no blocks are currently allocated. Definition at line 281 of file stk_memory_blockpool.h.
References m_used_count.
| bool stk::memory::IsFull | ( | ) | const |
Check whether all blocks are currently allocated (pool exhausted).
true if no blocks are available for allocation. Definition at line 275 of file stk_memory_blockpool.h.
References GetFreeCount().
| bool stk::memory::IsStorageValid | ( | ) | const |
Verify that the backing storage is valid and the pool is ready for use.
Always true for pools constructed with external storage. For heap-constructed pools, false if operator new failed. Must be checked after the heap constructor when operating without exceptions (the typical embedded configuration).
true if the pool is ready for use. Definition at line 240 of file stk_memory_blockpool.h.
References m_storage.
|
private |
Pop the head block from the free-list, increment m_used_count, and return it.
m_free_list != nullptr.
|
private |
| void * stk::memory::TimedAlloc | ( | Timeout | timeout = WAIT_INFINITE | ) |
Allocate one block, blocking until one becomes available or the timeout expires.
If the pool is empty the calling task is suspended via the internal ConditionVariable and woken by the next Free() call. On return the caller owns the block and must eventually return it via Free().
| [in] | timeout | Maximum time to wait (ticks). WAIT_INFINITE - block indefinitely (default). NO_WAIT - return nullptr immediately if pool is empty (identical to TryAlloc(); ISR-safe). |
GetRawBlockSize() bytes, or nullptr if the timeout expired before a block became available. NO_WAIT; ISR-unsafe otherwise. References stk::WAIT_INFINITE.
| T * stk::memory::TimedAllocT | ( | Timeout | timeout = WAIT_INFINITE | ) |
Allocate one typed block, blocking until one becomes available or the timeout expires.
Thin typed wrapper around TimedAlloc(). Asserts that sizeof(T) fits within the aligned block size chosen at construction.
| [in] | timeout | Maximum time to wait (ticks). Same semantics as TimedAlloc(). |
nullptr if the timeout expired. NO_WAIT; ISR-unsafe otherwise. References stk::WAIT_INFINITE.
| void * stk::memory::TryAlloc | ( | ) |
Non-blocking allocation attempt.
Returns a block immediately if the pool is not empty, or nullptr if all blocks are currently allocated. Never suspends the calling task.
nullptr if the pool is empty. | T * stk::memory::TryAllocT | ( | ) |
Non-blocking typed allocation attempt.
Returns a typed pointer immediately if the pool is not empty, or nullptr if all blocks are currently allocated. Never suspends the calling task.
nullptr if the pool is empty. | stk::memory::~BlockMemoryPool | ( | ) |
Destructor.
Frees heap-allocated storage if m_storage_owned is true. External storage is never touched.
Alloc() or TimedAlloc() is a logical error (dangling waiters). An assertion is triggered in debug builds inside the ConditionVariable destructor.
|
staticprivate |
Minimum block alignment in bytes: sizeof(MemoryBlock).
Ensures the intrusive free-list pointer stored in the first word of every free block is naturally aligned on all supported STK targets.
Definition at line 300 of file stk_memory_blockpool.h.
Referenced by AlignBlockSize().
|
static |
Definition at line 104 of file stk_memory_blockpool.h.
|
private |
aligned block size in bytes (>= BLOCK_ALIGN)
Definition at line 318 of file stk_memory_blockpool.h.
Referenced by GetBlockSize().
|
private |
total number of blocks
Definition at line 319 of file stk_memory_blockpool.h.
Referenced by GetCapacity(), and GetFreeCount().
|
private |
signalled by Free() to wake one task blocked in TimedAlloc()
Definition at line 317 of file stk_memory_blockpool.h.
|
private |
head of the intrusive free-list (nullptr when pool is empty)
Definition at line 316 of file stk_memory_blockpool.h.
|
private |
flat byte array holding all N blocks (owned or external)
Definition at line 315 of file stk_memory_blockpool.h.
Referenced by IsStorageValid().
|
private |
true -> storage is heap-allocated; free in destructor
Definition at line 321 of file stk_memory_blockpool.h.
|
private |
number of blocks currently allocated (outstanding)
Definition at line 320 of file stk_memory_blockpool.h.
Referenced by GetFreeCount(), GetUsedCount(), and IsEmpty().