![]() |
SuperTinyKernel™ RTOS 1.06.0
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
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_t * | stk_blockpool_create (size_t capacity, size_t raw_block_size, const char *name) |
| Create a block pool backed by heap-allocated storage. | |
| 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. | |
| 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). | |
Pure C interface for stk::memory::BlockMemoryPool.
| #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.
| #define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE | ( | raw_size | ) |
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.
| #define STK_BLOCKPOOL_STORAGE_DECL | ( | name, | |
| capacity, | |||
| raw_block_size ) |
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.
| name | C identifier for the array variable. |
| capacity | Number of blocks the pool will hold. |
| raw_block_size | Raw per-block size in bytes. |
Definition at line 119 of file stk_c_memory.h.
| #define STK_BLOCKPOOL_STORAGE_SIZE | ( | capacity, | |
| raw_block_size ) |
Compute the minimum external storage buffer size in bytes.
| capacity | Number of blocks the pool will hold. |
| raw_block_size | Raw per-block size in bytes. |
Definition at line 102 of file stk_c_memory.h.
| #define STK_C_BLOCKPOOL_MAX 8 |
Maximum number of concurrent stk_blockpool_t instances (default: 8).
Definition at line 74 of file stk_c_memory.h.
Referenced by AcquireSlot(), and FindSlot().
| 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.
| void * stk_blockpool_alloc | ( | stk_blockpool_t * | pool | ) |
Allocate one block, blocking indefinitely until one is available.
| [in] | pool | Pool handle. |
NULL. Definition at line 172 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| 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).
| [in] | capacity | Total number of blocks. |
| [in] | raw_block_size | Requested per-block size in bytes. |
| [in] | name | Optional human-readable name (may be NULL). Forwarded to ITraceable::SetTraceName(). |
NULL if the static slot pool is exhausted (STK_C_BLOCKPOOL_MAX reached). Definition at line 107 of file stk_c_memory.cpp.
References AcquireSlot(), STK_ASSERT, and BlockPoolSlot::storage.
| 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.
| [in] | capacity | Total number of blocks the pool can hold. |
| [in] | raw_block_size | Requested per-block size in bytes. |
| [in] | storage | Pointer 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_size | Size of storage in bytes (used for the size assertion). |
| [in] | name | Optional human-readable name (may be NULL). |
NULL if the static slot pool is exhausted (STK_C_BLOCKPOOL_MAX reached). Definition at line 125 of file stk_c_memory.cpp.
References AcquireSlot(), STK_ASSERT, and BlockPoolSlot::storage.
| 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.
| [in] | pool | Pool handle obtained via stk_blockpool_create() or stk_blockpool_create_static(). |
stk_blockpool_alloc() or stk_blockpool_timed_alloc() is a logic error and triggers an assertion in debug builds. Definition at line 150 of file stk_c_memory.cpp.
References BlockPoolSlot::busy, FindSlot(), and STK_ASSERT.
| 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.
| [in] | pool | Pool handle. |
| [in] | ptr | Pointer 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. |
true on success. false if ptr is NULL, out of range, or misaligned - each case indicates a caller logic error. stk_blockpool_free() to prevent double-free. Definition at line 197 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| 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.
| [in] | pool | Pool handle. |
Definition at line 222 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| size_t stk_blockpool_get_capacity | ( | const stk_blockpool_t * | pool | ) |
Get the total block capacity of the pool.
| [in] | pool | Pool handle. |
Definition at line 215 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| size_t stk_blockpool_get_free_count | ( | const stk_blockpool_t * | pool | ) |
Get the number of free (available) blocks.
| [in] | pool | Pool handle. |
Definition at line 236 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| size_t stk_blockpool_get_used_count | ( | const stk_blockpool_t * | pool | ) |
Get the number of currently allocated (outstanding) blocks.
| [in] | pool | Pool handle. |
Definition at line 229 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| bool stk_blockpool_is_empty | ( | const stk_blockpool_t * | pool | ) |
Check whether all blocks are free (no outstanding allocations).
| [in] | pool | Pool handle. |
true if no blocks are currently allocated. Definition at line 250 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| bool stk_blockpool_is_full | ( | const stk_blockpool_t * | pool | ) |
Check whether all blocks are currently allocated (pool exhausted).
| [in] | pool | Pool handle. |
true if no blocks are available for allocation. Definition at line 243 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| 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.
| [in] | pool | Pool handle. |
true if the pool is ready for use. Definition at line 208 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| void * stk_blockpool_timed_alloc | ( | stk_blockpool_t * | pool, |
| uint32_t | timeout ) |
Allocate one block, blocking until one becomes available or the timeout expires.
| [in] | pool | Pool handle. |
| [in] | timeout | Maximum 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(). |
NULL if the timeout expired before a block became available. STK_NO_WAIT; not ISR-safe otherwise. Definition at line 179 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.
| 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.
| [in] | pool | Pool handle. |
NULL if the pool is empty. Definition at line 186 of file stk_c_memory.cpp.
References stk_blockpool_t::handle, and STK_ASSERT.