SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
STK C Memory API

Pure C interface for stk::memory::BlockMemoryPool. More...

Macros

#define STK_C_BLOCKPOOL_MAX   8
 Maximum number of concurrent stk_blockpool_t instances (default: 8).
#define STK_BLOCKPOOL_ALIGN   (sizeof(stk_word_t))
 Required storage alignment in bytes (equals sizeof(void*)).
#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_size)
 Compute the internally aligned block size for a given raw byte size.
#define STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size)
 Compute the minimum external storage buffer size in bytes.
#define STK_BLOCKPOOL_STORAGE_DECL(name, capacity, raw_block_size)
 Declare a correctly sized and aligned external storage array.

Typedefs

typedef struct stk_blockpool_t stk_blockpool_t
 Opaque handle to a stk::memory::BlockMemoryPool instance.

Functions

stk_blockpool_tstk_blockpool_create (size_t capacity, size_t raw_block_size, const char *name)
 Create a block pool backed by heap-allocated storage.
stk_blockpool_tstk_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.
void stk_blockpool_destroy (stk_blockpool_t *pool)
 Destroy a pool and return its slot to the static pool.
void * stk_blockpool_alloc (stk_blockpool_t *pool)
 Allocate one block, blocking indefinitely until one is available.
void * stk_blockpool_timed_alloc (stk_blockpool_t *pool, uint32_t timeout)
 Allocate one block, blocking until one becomes available or the timeout expires.
void * stk_blockpool_try_alloc (stk_blockpool_t *pool)
 Non-blocking allocation attempt.
bool stk_blockpool_free (stk_blockpool_t *pool, void *ptr)
 Return a previously allocated block to the 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.
size_t stk_blockpool_get_capacity (const stk_blockpool_t *pool)
 Get the total block capacity of the pool.
size_t stk_blockpool_get_block_size (const stk_blockpool_t *pool)
 Get the aligned block size used internally by the allocator.
size_t stk_blockpool_get_used_count (const stk_blockpool_t *pool)
 Get the number of currently allocated (outstanding) blocks.
size_t stk_blockpool_get_free_count (const stk_blockpool_t *pool)
 Get the number of free (available) blocks.
bool stk_blockpool_is_full (const stk_blockpool_t *pool)
 Check whether all blocks are currently allocated (pool exhausted).
bool stk_blockpool_is_empty (const stk_blockpool_t *pool)
 Check whether all blocks are free (no outstanding allocations).

Detailed Description

Pure C interface for stk::memory::BlockMemoryPool.

Macro Definition Documentation

◆ STK_BLOCKPOOL_ALIGN

#define STK_BLOCKPOOL_ALIGN   (sizeof(stk_word_t))

Required storage alignment in bytes (equals sizeof(void*)).

Definition at line 84 of file stk_c_memory.h.

◆ STK_BLOCKPOOL_ALIGN_BLOCK_SIZE

#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE ( raw_size)
Value:
(((raw_size) < STK_BLOCKPOOL_ALIGN) \
: (((raw_size) + (STK_BLOCKPOOL_ALIGN - 1U)) & ~(STK_BLOCKPOOL_ALIGN - 1U)))
#define STK_BLOCKPOOL_ALIGN
Required storage alignment in bytes (equals sizeof(void*)).

Compute the internally aligned block size for a given raw byte size.

Rounds raw_size up to the nearest multiple of STK_BLOCKPOOL_ALIGN, with a minimum of STK_BLOCKPOOL_ALIGN. Use this at compile time to size external storage buffers correctly.

Definition at line 92 of file stk_c_memory.h.

92#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_size) \
93 (((raw_size) < STK_BLOCKPOOL_ALIGN) \
94 ? STK_BLOCKPOOL_ALIGN \
95 : (((raw_size) + (STK_BLOCKPOOL_ALIGN - 1U)) & ~(STK_BLOCKPOOL_ALIGN - 1U)))

◆ STK_BLOCKPOOL_STORAGE_DECL

#define STK_BLOCKPOOL_STORAGE_DECL ( name,
capacity,
raw_block_size )
Value:
static stk_word_t name[STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) / sizeof(stk_word_t)]
uintptr_t stk_word_t
CPU register type.
Definition stk_c.h:83
#define STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size)
Compute the minimum external storage buffer size in bytes.

Declare a correctly sized and aligned external storage array.

Expands to a static stk_word_t_t array with the required size and pointer-sized alignment. Intended for file-scope or function-scope use.

Parameters
nameC identifier for the array variable.
capacityNumber of blocks the pool will hold.
raw_block_sizeRaw per-block size in bytes.
STK_BLOCKPOOL_STORAGE_DECL(g_BufStorage, 16, sizeof(MyBuf));
16, sizeof(MyBuf), g_BufStorage, sizeof(g_BufStorage), NULL);
struct stk_blockpool_t stk_blockpool_t
Opaque handle to a stk::memory::BlockMemoryPool instance.
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.
#define STK_BLOCKPOOL_STORAGE_DECL(name, capacity, raw_block_size)
Declare a correctly sized and aligned external storage array.

Definition at line 119 of file stk_c_memory.h.

119#define STK_BLOCKPOOL_STORAGE_DECL(name, capacity, raw_block_size) \
120 static stk_word_t name[STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) / sizeof(stk_word_t)]

◆ STK_BLOCKPOOL_STORAGE_SIZE

#define STK_BLOCKPOOL_STORAGE_SIZE ( capacity,
raw_block_size )
Value:
((capacity) * STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_block_size))
#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_size)
Compute the internally aligned block size for a given raw byte size.

Compute the minimum external storage buffer size in bytes.

Parameters
capacityNumber of blocks the pool will hold.
raw_block_sizeRaw per-block size in bytes.

Definition at line 102 of file stk_c_memory.h.

102#define STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) \
103 ((capacity) * STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_block_size))

◆ STK_C_BLOCKPOOL_MAX

#define STK_C_BLOCKPOOL_MAX   8

Maximum number of concurrent stk_blockpool_t instances (default: 8).

Note
Increase if your application needs more simultaneous pools.

Definition at line 74 of file stk_c_memory.h.

Referenced by AcquireSlot(), and FindSlot().

Typedef Documentation

◆ stk_blockpool_t

typedef struct stk_blockpool_t stk_blockpool_t

Opaque handle to a stk::memory::BlockMemoryPool instance.

Definition at line 128 of file stk_c_memory.h.

Function Documentation

◆ stk_blockpool_alloc()

void * stk_blockpool_alloc ( stk_blockpool_t * pool)

Allocate one block, blocking indefinitely until one is available.

Parameters
[in]poolPool handle.
Returns
Pointer to an uninitialized block of at least raw_block_size bytes. Never returns NULL.
Warning
Not ISR-safe.

Definition at line 172 of file stk_c_memory.cpp.

173{
174 STK_ASSERT(pool != nullptr);
175
176 return pool->handle.Alloc();
177}
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
BlockMemoryPool handle

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_create()

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.

Allocates a flat byte buffer of capacity * AlignBlockSize(raw_block_size) bytes from the heap. Call stk_blockpool_is_storage_valid() immediately after creation when operating without exceptions (typical embedded configuration).

Parameters
[in]capacityTotal number of blocks.
[in]raw_block_sizeRequested per-block size in bytes.
[in]nameOptional human-readable name (may be NULL). Forwarded to ITraceable::SetTraceName().
Returns
Pool handle, or NULL if the static slot pool is exhausted (STK_C_BLOCKPOOL_MAX reached).
Note
Not ISR-safe.
See also
stk_blockpool_is_storage_valid(), stk_blockpool_destroy()

Definition at line 107 of file stk_c_memory.cpp.

108{
109 STK_ASSERT(capacity > 0U);
110 STK_ASSERT(raw_block_size > 0U);
111
113
114 BlockPoolSlot *slot = AcquireSlot();
115 if (slot == nullptr)
116 {
117 // pool exhausted — increase STK_C_BLOCKPOOL_MAX
118 STK_ASSERT(false);
119 return nullptr;
120 }
121
122 return new (slot->storage) stk_blockpool_t(capacity, raw_block_size, name);
123}
static BlockPoolSlot * AcquireSlot()
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
Definition stk_sync_cs.h:54
Word storage[sizeof(stk_blockpool_t)/sizeof(Word)]

References AcquireSlot(), STK_ASSERT, and BlockPoolSlot::storage.

Here is the call graph for this function:

◆ stk_blockpool_create_static()

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.

The pool references storage directly without taking ownership. The caller must keep the buffer alive for the entire lifetime of the pool. The storage is not freed on destruction.

Parameters
[in]capacityTotal number of blocks the pool can hold.
[in]raw_block_sizeRequested per-block size in bytes.
[in]storagePointer to a caller-owned byte buffer. Must be aligned to at least sizeof(void*) and large enough to hold at least STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) bytes. Asserted at construction time.
[in]storage_sizeSize of storage in bytes (used for the size assertion).
[in]nameOptional human-readable name (may be NULL).
Returns
Pool handle, or NULL if the static slot pool is exhausted (STK_C_BLOCKPOOL_MAX reached).
Note
Not ISR-safe.
See also
stk_blockpool_destroy()

Definition at line 125 of file stk_c_memory.cpp.

130{
131 STK_ASSERT(capacity > 0U);
132 STK_ASSERT(raw_block_size > 0U);
133 STK_ASSERT(storage != nullptr);
134 STK_ASSERT(storage_size >= (capacity * BlockMemoryPool::AlignBlockSize(raw_block_size)));
135
137
138 BlockPoolSlot *slot = AcquireSlot();
139 if (slot == nullptr)
140 {
141 // pool exhausted — increase STK_C_BLOCKPOOL_MAX
142 STK_ASSERT(false);
143 return nullptr;
144 }
145
146 return new (slot->storage) stk_blockpool_t(capacity, raw_block_size,
147 storage, storage_size, name);
148}

References AcquireSlot(), STK_ASSERT, and BlockPoolSlot::storage.

Here is the call graph for this function:

◆ stk_blockpool_destroy()

void stk_blockpool_destroy ( stk_blockpool_t * pool)

Destroy a pool and return its slot to the static pool.

If the pool owns heap storage, it is freed. External storage is never touched.

Parameters
[in]poolPool handle obtained via stk_blockpool_create() or stk_blockpool_create_static().
Warning
Destroying a pool while tasks are blocked in stk_blockpool_alloc() or stk_blockpool_timed_alloc() is a logic error and triggers an assertion in debug builds.
Note
Not ISR-safe.

Definition at line 150 of file stk_c_memory.cpp.

151{
152 STK_ASSERT(pool != nullptr);
153
155
156 BlockPoolSlot *slot = FindSlot(pool);
157
158 // pool not found: double-destroy or corruption
159 STK_ASSERT(slot != nullptr);
160 if (slot == nullptr)
161 return;
162
163 // Explicitly run the destructor (frees heap storage if owned).
164 pool->~stk_blockpool_t();
165 slot->busy = false;
166}
static BlockPoolSlot * FindSlot(const stk_blockpool_t *pool)

References BlockPoolSlot::busy, FindSlot(), and STK_ASSERT.

Here is the call graph for this function:

◆ stk_blockpool_free()

bool stk_blockpool_free ( stk_blockpool_t * pool,
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 stk_blockpool_alloc() or stk_blockpool_timed_alloc(), if any.

Parameters
[in]poolPool handle.
[in]ptrPointer previously returned by stk_blockpool_alloc(), stk_blockpool_timed_alloc(), or stk_blockpool_try_alloc(). Must belong to pool. Bounds and alignment are validated; failures trigger an assertion in debug builds.
Returns
true on success. false if ptr is NULL, out of range, or misaligned - each case indicates a caller logic error.
Note
ISR-safe.
Warning
Null the pointer after stk_blockpool_free() to prevent double-free.

Definition at line 197 of file stk_c_memory.cpp.

198{
199 STK_ASSERT(pool != nullptr);
200
201 return pool->handle.Free(ptr);
202}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_get_block_size()

size_t stk_blockpool_get_block_size ( const stk_blockpool_t * pool)

Get the aligned block size used internally by the allocator.

Equal to STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_block_size) as passed at construction. Always >= STK_BLOCKPOOL_ALIGN.

Parameters
[in]poolPool handle.
Returns
Aligned block size in bytes.
Note
ISR-safe.

Definition at line 222 of file stk_c_memory.cpp.

223{
224 STK_ASSERT(pool != nullptr);
225
226 return pool->handle.GetBlockSize();
227}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_get_capacity()

size_t stk_blockpool_get_capacity ( const stk_blockpool_t * pool)

Get the total block capacity of the pool.

Parameters
[in]poolPool handle.
Returns
Maximum number of blocks that can be simultaneously allocated.
Note
ISR-safe.

Definition at line 215 of file stk_c_memory.cpp.

216{
217 STK_ASSERT(pool != nullptr);
218
219 return pool->handle.GetCapacity();
220}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_get_free_count()

size_t stk_blockpool_get_free_count ( const stk_blockpool_t * pool)

Get the number of free (available) blocks.

Parameters
[in]poolPool handle.
Returns
Point-in-time snapshot of capacity - used_count.
Note
ISR-safe.

Definition at line 236 of file stk_c_memory.cpp.

237{
238 STK_ASSERT(pool != nullptr);
239
240 return pool->handle.GetFreeCount();
241}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_get_used_count()

size_t stk_blockpool_get_used_count ( const stk_blockpool_t * pool)

Get the number of currently allocated (outstanding) blocks.

Parameters
[in]poolPool handle.
Returns
Point-in-time snapshot. May be stale immediately after return in a multi-task environment.
Note
ISR-safe on targets where a 16-bit aligned read is atomic.

Definition at line 229 of file stk_c_memory.cpp.

230{
231 STK_ASSERT(pool != nullptr);
232
233 return pool->handle.GetUsedCount();
234}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_is_empty()

bool stk_blockpool_is_empty ( const stk_blockpool_t * pool)

Check whether all blocks are free (no outstanding allocations).

Parameters
[in]poolPool handle.
Returns
true if no blocks are currently allocated.
Note
ISR-safe.

Definition at line 250 of file stk_c_memory.cpp.

251{
252 STK_ASSERT(pool != nullptr);
253
254 return pool->handle.IsEmpty();
255}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_is_full()

bool stk_blockpool_is_full ( const stk_blockpool_t * pool)

Check whether all blocks are currently allocated (pool exhausted).

Parameters
[in]poolPool handle.
Returns
true if no blocks are available for allocation.
Note
ISR-safe.

Definition at line 243 of file stk_c_memory.cpp.

244{
245 STK_ASSERT(pool != nullptr);
246
247 return pool->handle.IsFull();
248}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_is_storage_valid()

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.

Always true for pools created with stk_blockpool_create_static(). For heap-constructed pools (stk_blockpool_create()), false if operator new failed. Must be checked after heap construction when operating without exceptions.

Parameters
[in]poolPool handle.
Returns
true if the pool is ready for use.
Note
ISR-safe.

Definition at line 208 of file stk_c_memory.cpp.

209{
210 STK_ASSERT(pool != nullptr);
211
212 return pool->handle.IsStorageValid();
213}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_timed_alloc()

void * stk_blockpool_timed_alloc ( stk_blockpool_t * pool,
uint32_t timeout )

Allocate one block, blocking until one becomes available or the timeout expires.

Parameters
[in]poolPool handle.
[in]timeoutMaximum time to wait in ticks. Pass STK_WAIT_INFINITE to block indefinitely (same as stk_blockpool_alloc()), or STK_NO_WAIT for a non-blocking attempt identical to stk_blockpool_try_alloc().
Returns
Pointer to an uninitialized block, or NULL if the timeout expired before a block became available.
Warning
ISR-safe only when timeout = STK_NO_WAIT; not ISR-safe otherwise.

Definition at line 179 of file stk_c_memory.cpp.

180{
181 STK_ASSERT(pool != nullptr);
182
183 return pool->handle.TimedAlloc(static_cast<Timeout>(timeout));
184}
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:123

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_try_alloc()

void * stk_blockpool_try_alloc ( stk_blockpool_t * pool)

Non-blocking allocation attempt.

Returns a block immediately if one is available, or NULL if the pool is empty. Never suspends the calling task.

Parameters
[in]poolPool handle.
Returns
Pointer to an uninitialized block, or NULL if the pool is empty.
Note
ISR-safe.

Definition at line 186 of file stk_c_memory.cpp.

187{
188 STK_ASSERT(pool != nullptr);
189
190 return pool->handle.TryAlloc();
191}

References stk_blockpool_t::handle, and STK_ASSERT.