12#include <stk_config.h>
20inline void *
operator new(std::size_t,
void *ptr)
noexcept {
return ptr; }
21inline void operator delete(
void *,
void *)
noexcept { }
37 uint8_t *storage,
size_t storage_size,
const char *name)
38 :
handle(capacity, raw_block_size, storage, storage_size, name)
43 :
handle(capacity, raw_block_size, name)
126 size_t raw_block_size,
134 STK_ASSERT(storage_size >= (capacity * BlockMemoryPool::AlignBlockSize(raw_block_size)));
147 storage, storage_size, name);
164 pool->~stk_blockpool_t();
176 return pool->
handle.Alloc();
183 return pool->
handle.TimedAlloc(
static_cast<Timeout>(timeout));
190 return pool->
handle.TryAlloc();
201 return pool->
handle.Free(ptr);
212 return pool->
handle.IsStorageValid();
219 return pool->
handle.GetCapacity();
226 return pool->
handle.GetBlockSize();
233 return pool->
handle.GetUsedCount();
240 return pool->
handle.GetFreeCount();
247 return pool->
handle.IsFull();
254 return pool->
handle.IsEmpty();
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.
#define STK_STATIC_ASSERT_DESC(X, DESC)
Compile-time assertion with a custom error description. Produces a compilation error if X is false.
C language binding/interface for SuperTinyKernel (STK).
C language binding for stk::memory::BlockMemoryPool.
static BlockPoolSlot * FindSlot(const stk_blockpool_t *pool)
static BlockPoolSlot * AcquireSlot()
static struct BlockPoolSlot s_BlockPools[8]
size_t stk_blockpool_get_capacity(const stk_blockpool_t *pool)
Get the total block capacity of the pool.
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).
void * stk_blockpool_timed_alloc(stk_blockpool_t *pool, uint32_t timeout)
Allocate one block, blocking until one becomes available or the timeout expires.
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.
Namespace of STK package.
uintptr_t Word
Native processor word type.
int32_t Timeout
Timeout time (ticks).
Memory-related primitives.
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.
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[sizeof(stk_blockpool_t)/sizeof(Word)]
const stk_blockpool_t * pool() const