12#include <stk_config.h>
31 "STK_BLOCKPOOL_ALIGN_BLOCK_SIZE and BlockMemoryPool::AlignBlockSize() have diverged. "
32 "Keep both definitions in sync.");
43extern "C" void free(
void *ptr);
57 uint8_t *storage,
size_t storage_size,
const char *name)
58 :
handle(capacity, raw_block_size, storage, storage_size, name)
63 :
handle(capacity, raw_block_size, name)
159 size_t raw_block_size,
160 uint8_t *storage_ptr,
180 storage_ptr, storage_size, name);
199 pool->~stk_blockpool_t();
Collection of memory-related primitives (stk::memory namespace).
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
C language binding/interface for SuperTinyKernel RTOS.
C language binding for stk::memory::BlockMemoryPool.
static BlockPoolSlot * FindSlot(const stk_blockpool_t *pool)
static constexpr size_t StkGetWordCountForType()
void * malloc(size_t size)
static BlockPoolSlot * AcquireSlot()
static struct BlockPoolSlot s_BlockPools[(8U)]
int32_t stk_timeout_t
Timeout value.
size_t stk_blockpool_get_capacity(const stk_blockpool_t *pool)
Get the total block capacity of the pool.
void * stk_blockpool_timed_alloc(stk_blockpool_t *pool, stk_timeout_t timeout)
Allocate one block, blocking until one becomes available or the timeout expires.
void stk_blockpool_destroy(stk_blockpool_t *pool)
Destroy a pool and return its slot to the static pool.
bool stk_blockpool_is_storage_valid(const stk_blockpool_t *pool)
Verify that the backing storage is valid and the pool is ready for use.
#define STK_C_BLOCKPOOL_MAX
Maximum number of concurrent stk_blockpool_t instances (default: 8).
size_t stk_blockpool_get_used_count(const stk_blockpool_t *pool)
Get the number of currently allocated (outstanding) blocks.
struct stk_blockpool_t stk_blockpool_t
Opaque handle to a stk::memory::BlockMemoryPool instance.
void * stk_blockpool_alloc(stk_blockpool_t *pool)
Allocate one block, blocking indefinitely until one is available.
bool stk_blockpool_free(stk_blockpool_t *pool, void *ptr)
Return a previously allocated block to the pool.
void * stk_blockpool_try_alloc(stk_blockpool_t *pool)
Non-blocking allocation attempt.
stk_blockpool_t * stk_blockpool_create_static(size_t capacity, size_t raw_block_size, uint8_t *storage, size_t storage_size, const char *name)
Create a block pool backed by caller-supplied (external) storage.
stk_blockpool_t * stk_blockpool_create(size_t capacity, size_t raw_block_size, const char *name)
Create a block pool backed by heap-allocated storage.
bool stk_blockpool_is_empty(const stk_blockpool_t *pool)
Check whether all blocks are free (no outstanding allocations).
bool stk_blockpool_is_full(const stk_blockpool_t *pool)
Check whether all blocks are currently allocated (pool exhausted).
size_t stk_blockpool_get_free_count(const stk_blockpool_t *pool)
Get the number of free (available) blocks.
size_t stk_blockpool_get_block_size(const stk_blockpool_t *pool)
Get the aligned block size used internally by the allocator.
#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_size)
Compute the internally aligned block size for a given raw byte size.
Namespace of STK package.
uintptr_t Word
Native processor word type.
Memory-related primitives.
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
static void Free(void *ptr) __stk_weak
Free the memory chunk.
static void * Allocate(size_t size) __stk_weak
Allocate the memory chunk.
Fixed-size block allocator with O(1) alloc/free and proper task-blocking semantics.
size_t GetUsedCount() const
Get the number of currently allocated (outstanding) blocks.
size_t GetFreeCount() const
Get the number of free (available) blocks.
size_t GetCapacity() const
Get the total block capacity of the pool.
bool IsFull() const
Check whether all blocks are currently allocated (pool exhausted).
bool IsStorageValid() const
Verify that the backing storage is valid and the pool is ready for use.
bool Free(void *ptr)
Return a previously allocated block to the pool.
void * TryAlloc()
Non-blocking allocation attempt.
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_ticks=WAIT_INFINITE)
Allocate one block, blocking until one becomes available or the timeout expires.
size_t GetBlockSize() const
Get the aligned block size used internally by the allocator.
void * Alloc()
Allocate one block, blocking indefinitely until one is available.
bool IsEmpty() const
Check whether all blocks are free (no outstanding allocations).
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
stk_blockpool_t(size_t capacity, size_t raw_block_size, uint8_t *storage, size_t storage_size, const char *name)
stk_blockpool_t(size_t capacity, size_t raw_block_size, const char *name)
Word storage[StkGetWordCountForType< stk_blockpool_t >()]
const stk_blockpool_t * pool() const