SuperTinyKernel™ RTOS 1.08.x
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, stk::memory::BytePool and stk::memory::MemoryAllocator. More...

Collaboration diagram for STK C Memory API:

Namespaces

namespace  stk
 Namespace of STK package.

Classes

struct  stk_mem_stats_t
 Snapshot of allocator statistics. More...

Macros

#define STK_C_BLOCKPOOL_MAX   (8U)
 Maximum number of concurrent stk_blockpool_t instances (default: 8).
#define STK_C_BYTEPOOL_MAX   (8U)
 Maximum number of concurrent stk_bytepool_t instances (default: 8).
#define STK_C_MEMORY_DEFAULT_ALLOCATOR   (0)
 Selects the implementation of stk::memory::MemoryAllocator provided by the C binding (default: 0).
#define STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT   (8U)
 Default block alignment of a block pool (see stk_memory_blockpool.h).
#define STK_MEMORY_BLOCKPOOL_BITMAP   (0)
 When 1, a block pool keeps an "allocated" bit per block (O(1) double-free detection in release builds) and its storage requirement grows by capacity/8 bytes (see stk_memory_blockpool.h).
#define STK_MEMORY_BYTEPOOL_ALIGN_DEFAULT   (8U)
 Payload alignment of byte pool allocations (see stk_memory_bytepool.h).
#define STK_MEMORY_BYTEPOOL_GUARD   (0)
 When 1, every byte pool allocation carries a 4-byte overrun guard word (see stk_memory_bytepool.h).
#define STK_BLOCKPOOL_ALIGN   (STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT)
 Default block alignment in bytes (equals STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT, i.e. BlockMemoryPool::ALIGN_DEFAULT).
#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX(raw_size, align)
 Compute the internally aligned block size for a given raw byte size and alignment.
#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_size)
 Same as STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX() with the default alignment.
#define STK_BLOCKPOOL_BITMAP_BYTES(capacity)
 Size in bytes of the "allocated" bitmap of a pool of capacity blocks (0 unless STK_MEMORY_BLOCKPOOL_BITMAP is 1). Matches BlockMemoryPool::BitmapBytes().
#define STK_BLOCKPOOL_STORAGE_SIZE_EX(capacity, raw_block_size, align)
 Compute the minimum external storage buffer size in bytes (blocks plus the optional bitmap). Matches BlockMemoryPool::RequiredStorageSize().
#define STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size)
 Same as STK_BLOCKPOOL_STORAGE_SIZE_EX() with the default alignment.
#define STK_BLOCKPOOL_STORAGE_DECL_EX(name, capacity, raw_block_size, align)
 Declare a correctly sized storage array aligned to align bytes.
#define STK_BLOCKPOOL_STORAGE_DECL(name, capacity, raw_block_size)
 Declare a correctly sized and aligned external storage array (default alignment).
#define STK_BYTEPOOL_ALIGN   (STK_MEMORY_BYTEPOOL_ALIGN_DEFAULT)
 Payload alignment and block size granularity of a byte pool, in bytes (equals STK_MEMORY_BYTEPOOL_ALIGN_DEFAULT: 8, 16 on RISC-V by default).
#define STK_BYTEPOOL_HEADER_SIZE   ((STK_BYTEPOOL_ALIGN > 8U) ? STK_BYTEPOOL_ALIGN : 8U)
 Per-block header size in bytes: max(8, STK_BYTEPOOL_ALIGN). Every allocation consumes this many bytes of the pool in addition to its aligned payload.
#define STK_BYTEPOOL_GUARD_SIZE   ((STK_MEMORY_BYTEPOOL_GUARD != 0) ? 4U : 0U)
 Size of the overrun guard word of a used block (4 with STK_MEMORY_BYTEPOOL_GUARD, otherwise 0).
#define STK_BYTEPOOL_ALIGN_SIZE(raw_size)
 Round raw_size up to the nearest multiple of STK_BYTEPOOL_ALIGN.
#define STK_BYTEPOOL_REQUIRED_SIZE(payload_size)
 Pool space in bytes consumed by one allocation of payload_size bytes (aligned payload, plus the guard word if enabled, plus the block header). Sum the value for every block that must be alive simultaneously to size external storage. Matches BytePool::RequiredSize().
#define STK_BYTEPOOL_STORAGE_DECL(name, size_bytes)
 Declare an external storage array for a byte pool, aligned to STK_BYTEPOOL_ALIGN.
#define STK_MEM_MIN_ALIGNMENT   (8U)
 Minimum alignment in bytes guaranteed for pointers returned by stk_mem_allocate() (equals MemoryAllocator::MIN_ALIGNMENT).

Typedefs

typedef struct stk_blockpool_t stk_blockpool_t
 Opaque handle to a stk::memory::BlockMemoryPool instance.
typedef struct stk_bytepool_t stk_bytepool_t
 Opaque handle to a stk::memory::BytePool instance.
typedef struct stk_mem_stats_t stk_mem_stats_t
 Snapshot of allocator statistics.

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 (default alignment).
stk_blockpool_t * stk_blockpool_create_ex (size_t capacity, size_t raw_block_size, const char *name, size_t alignment)
 Create a block pool backed by heap-allocated storage with an explicit block alignment.
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 (default alignment).
stk_blockpool_t * stk_blockpool_create_static_ex (size_t capacity, size_t raw_block_size, uint8_t *storage, size_t storage_size, const char *name, size_t alignment)
 Create a block pool backed by caller-supplied storage with an explicit block alignment.
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, stk_timeout_t timeout)
 Allocate one block, blocking until one becomes available or the timeout expires.
stk_wait_result_t stk_blockpool_timed_alloc_ex (stk_blockpool_t *pool, void **block, stk_timeout_t timeout)
 Allocate one block, preserving the full wait outcome.
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_block_alignment (const stk_blockpool_t *pool)
 Get the alignment of every block of the pool.
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.
size_t stk_blockpool_get_peak_used_count (const stk_blockpool_t *pool)
 Get the high watermark: the maximum number of blocks allocated at the same time.
size_t stk_blockpool_get_alloc_fail_count (const stk_blockpool_t *pool)
 Get the number of allocation requests that failed because the pool was exhausted (timeouts and STK_NO_WAIT misses).
void stk_blockpool_get_stats (const stk_blockpool_t *pool, stk_mem_stats_t *stats)
 Get a consistent statistics snapshot (bytes: blocks * block size).
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).
stk_bytepool_t * stk_bytepool_create (size_t storage_size, const char *name)
 Create a byte pool backed by heap-allocated storage.
stk_bytepool_t * stk_bytepool_create_static (uint8_t *storage, size_t storage_size, const char *name)
 Create a byte pool backed by caller-supplied (external) storage.
void stk_bytepool_destroy (stk_bytepool_t *pool)
 Destroy a byte pool and return its slot to the static pool.
void * stk_bytepool_alloc (stk_bytepool_t *pool, size_t size)
 Allocate a block, blocking indefinitely until enough contiguous memory is available.
void * stk_bytepool_timed_alloc (stk_bytepool_t *pool, size_t size, stk_timeout_t timeout)
 Allocate a block, blocking until memory becomes available or the timeout expires.
stk_wait_result_t stk_bytepool_timed_alloc_ex (stk_bytepool_t *pool, void **block, size_t size, stk_timeout_t timeout)
 Allocate a block, preserving the full wait outcome.
void * stk_bytepool_try_alloc (stk_bytepool_t *pool, size_t size)
 Non-blocking allocation attempt.
bool stk_bytepool_free (stk_bytepool_t *pool, void *ptr)
 Return a previously allocated block to the pool.
void stk_bytepool_get_info (const stk_bytepool_t *pool, size_t *available, size_t *largest, size_t *fragments)
 Collect pool statistics (mutually consistent snapshot).
void stk_bytepool_get_stats (const stk_bytepool_t *pool, stk_mem_stats_t *stats)
 Get an O(1) statistics snapshot.
size_t stk_bytepool_get_max_scan_length (const stk_bytepool_t *pool)
 Get the length of the longest block-list walk (in blocks) of any allocation so far.
size_t stk_bytepool_get_alloc_fail_count (const stk_bytepool_t *pool)
 Get the number of allocation requests that failed because no suitable block was available (timeouts, STK_NO_WAIT misses and requests larger than stk_bytepool_get_max_request()).
size_t stk_bytepool_get_peak_used_size (const stk_bytepool_t *pool)
 Get the high watermark of pool consumption in bytes (headers included).
bool stk_bytepool_is_storage_valid (const stk_bytepool_t *pool)
 Verify that the backing storage is valid and the pool is ready for use.
size_t stk_bytepool_get_size (const stk_bytepool_t *pool)
 Get the total managed (aligned) area size in bytes, headers included.
size_t stk_bytepool_get_max_request (const stk_bytepool_t *pool)
 Get the largest request that can ever succeed on this pool.
bool stk_mem_is_available (void)
 Check that the memory allocator implementation is linked in.
void * stk_mem_allocate (size_t size)
 Allocate a memory chunk from the general-purpose allocator.
void stk_mem_free (void *ptr)
 Free a chunk obtained with stk_mem_allocate().
void * stk_mem_allocate_aligned (size_t size, size_t alignment)
 Allocate a chunk with an alignment stricter than STK_MEM_MIN_ALIGNMENT.
void stk_mem_free_aligned (void *ptr)
 Free a chunk obtained with stk_mem_allocate_aligned().
void stk_mem_get_stats (stk_mem_stats_t *stats)
 Get statistics of the general-purpose allocator.
stk::memory::BlockMemoryPool * stk_blockpool_get_instance (stk_blockpool_t *pool)
 Get the underlying C++ stk::memory::BlockMemoryPool object wrapped by a stk_blockpool_t handle.
stk::memory::BytePool * stk_bytepool_get_instance (stk_bytepool_t *pool)
 Get the underlying C++ stk::memory::BytePool object wrapped by a stk_bytepool_t handle.

Detailed Description

Macro Definition Documentation

◆ STK_BLOCKPOOL_ALIGN

#define STK_BLOCKPOOL_ALIGN   (STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT)

Default block alignment in bytes (equals STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT, i.e. BlockMemoryPool::ALIGN_DEFAULT).

Definition at line 178 of file stk_c_memory.h.

Referenced by STK_STATIC_ASSERT_DESC().

◆ STK_BLOCKPOOL_ALIGN_BLOCK_SIZE

#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE ( raw_size)
Value:
#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX(raw_size, align)
Compute the internally aligned block size for a given raw byte size and alignment.
#define STK_BLOCKPOOL_ALIGN
Default block alignment in bytes (equals STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT, i.e. BlockMemoryPool::AL...

Same as STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX() with the default alignment.

Definition at line 193 of file stk_c_memory.h.

193#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_size) \
194 STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX(raw_size, STK_BLOCKPOOL_ALIGN)

◆ STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX

#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX ( raw_size,
align )
Value:
(((raw_size) < (align)) \
? (align) \
: (((raw_size) + ((align) - 1U)) & ~((align) - 1U)))

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

Rounds raw_size up to the nearest multiple of align, with a minimum of align. Matches BlockMemoryPool::AlignBlockSize().

Definition at line 185 of file stk_c_memory.h.

185#define STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX(raw_size, align) \
186 (((raw_size) < (align)) \
187 ? (align) \
188 : (((raw_size) + ((align) - 1U)) & ~((align) - 1U)))

◆ STK_BLOCKPOOL_BITMAP_BYTES

#define STK_BLOCKPOOL_BITMAP_BYTES ( capacity)
Value:
((STK_MEMORY_BLOCKPOOL_BITMAP != 0) ? (((capacity) + 7U) / 8U) : 0U)
#define STK_MEMORY_BLOCKPOOL_BITMAP
When defined as 1, BlockMemoryPool keeps one "allocated" bit per block (capacity/8 bytes,...

Size in bytes of the "allocated" bitmap of a pool of capacity blocks (0 unless STK_MEMORY_BLOCKPOOL_BITMAP is 1). Matches BlockMemoryPool::BitmapBytes().

Definition at line 200 of file stk_c_memory.h.

200#define STK_BLOCKPOOL_BITMAP_BYTES(capacity) \
201 ((STK_MEMORY_BLOCKPOOL_BITMAP != 0) ? (((capacity) + 7U) / 8U) : 0U)

◆ STK_BLOCKPOOL_STORAGE_DECL

#define STK_BLOCKPOOL_STORAGE_DECL ( name,
capacity,
raw_block_size )
Value:
STK_BLOCKPOOL_STORAGE_DECL_EX(name, capacity, raw_block_size, STK_BLOCKPOOL_ALIGN)
#define STK_BLOCKPOOL_STORAGE_DECL_EX(name, capacity, raw_block_size, align)
Declare a correctly sized storage array aligned to align bytes.

Declare a correctly sized and aligned external storage array (default alignment).

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), (uint8_t *)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 (default alignment).
#define STK_BLOCKPOOL_STORAGE_DECL(name, capacity, raw_block_size)
Declare a correctly sized and aligned external storage array (default alignment).

Definition at line 245 of file stk_c_memory.h.

245#define STK_BLOCKPOOL_STORAGE_DECL(name, capacity, raw_block_size) \
246 STK_BLOCKPOOL_STORAGE_DECL_EX(name, capacity, raw_block_size, STK_BLOCKPOOL_ALIGN)

◆ STK_BLOCKPOOL_STORAGE_DECL_EX

#define STK_BLOCKPOOL_STORAGE_DECL_EX ( name,
capacity,
raw_block_size,
align )
Value:
static stk_word_t name[STK_C_CEIL_DIV(STK_BLOCKPOOL_STORAGE_SIZE_EX(capacity, raw_block_size, align), sizeof(stk_word_t))] \
__stk_c_align(align)
#define STK_C_CEIL_DIV(size, unit)
Performs integer ceiling division (rounds up to the nearest multiple).
Definition stk_c.h:140
uintptr_t stk_word_t
CPU register type.
Definition stk_c.h:156
#define STK_BLOCKPOOL_STORAGE_SIZE_EX(capacity, raw_block_size, align)
Compute the minimum external storage buffer size in bytes (blocks plus the optional bitmap)....

Declare a correctly sized storage array aligned to align bytes.

Expands to a static stk_word_t array with the required size (rounded up to whole words) and the requested alignment attribute. 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.
alignBlock alignment in bytes.

Definition at line 229 of file stk_c_memory.h.

229#define STK_BLOCKPOOL_STORAGE_DECL_EX(name, capacity, raw_block_size, align) \
230 static stk_word_t name[STK_C_CEIL_DIV(STK_BLOCKPOOL_STORAGE_SIZE_EX(capacity, raw_block_size, align), sizeof(stk_word_t))] \
231 __stk_c_align(align)

◆ STK_BLOCKPOOL_STORAGE_SIZE

#define STK_BLOCKPOOL_STORAGE_SIZE ( capacity,
raw_block_size )
Value:

Same as STK_BLOCKPOOL_STORAGE_SIZE_EX() with the default alignment.

Definition at line 217 of file stk_c_memory.h.

217#define STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) \
218 STK_BLOCKPOOL_STORAGE_SIZE_EX(capacity, raw_block_size, STK_BLOCKPOOL_ALIGN)

◆ STK_BLOCKPOOL_STORAGE_SIZE_EX

#define STK_BLOCKPOOL_STORAGE_SIZE_EX ( capacity,
raw_block_size,
align )
Value:
(((capacity) * STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX(raw_block_size, align)) + STK_BLOCKPOOL_BITMAP_BYTES(capacity))
#define STK_BLOCKPOOL_BITMAP_BYTES(capacity)
Size in bytes of the "allocated" bitmap of a pool of capacity blocks (0 unless STK_MEMORY_BLOCKPOOL_B...

Compute the minimum external storage buffer size in bytes (blocks plus the optional bitmap). Matches BlockMemoryPool::RequiredStorageSize().

Parameters
capacityNumber of blocks the pool will hold.
raw_block_sizeRaw per-block size in bytes.
alignBlock alignment in bytes (power of two, not less than sizeof(void *)).
Note
If the buffer start can not be guaranteed to be aligned add align - 1.

Definition at line 211 of file stk_c_memory.h.

211#define STK_BLOCKPOOL_STORAGE_SIZE_EX(capacity, raw_block_size, align) \
212 (((capacity) * STK_BLOCKPOOL_ALIGN_BLOCK_SIZE_EX(raw_block_size, align)) + STK_BLOCKPOOL_BITMAP_BYTES(capacity))

◆ STK_BYTEPOOL_ALIGN

#define STK_BYTEPOOL_ALIGN   (STK_MEMORY_BYTEPOOL_ALIGN_DEFAULT)

Payload alignment and block size granularity of a byte pool, in bytes (equals STK_MEMORY_BYTEPOOL_ALIGN_DEFAULT: 8, 16 on RISC-V by default).

Definition at line 256 of file stk_c_memory.h.

Referenced by STK_STATIC_ASSERT_DESC().

◆ STK_BYTEPOOL_ALIGN_SIZE

#define STK_BYTEPOOL_ALIGN_SIZE ( raw_size)
Value:
(((raw_size) + (STK_BYTEPOOL_ALIGN - 1U)) & ~(STK_BYTEPOOL_ALIGN - 1U))
#define STK_BYTEPOOL_ALIGN
Payload alignment and block size granularity of a byte pool, in bytes (equals STK_MEMORY_BYTEPOOL_ALI...

Round raw_size up to the nearest multiple of STK_BYTEPOOL_ALIGN.

Definition at line 272 of file stk_c_memory.h.

272#define STK_BYTEPOOL_ALIGN_SIZE(raw_size) \
273 (((raw_size) + (STK_BYTEPOOL_ALIGN - 1U)) & ~(STK_BYTEPOOL_ALIGN - 1U))

◆ STK_BYTEPOOL_GUARD_SIZE

#define STK_BYTEPOOL_GUARD_SIZE   ((STK_MEMORY_BYTEPOOL_GUARD != 0) ? 4U : 0U)

Size of the overrun guard word of a used block (4 with STK_MEMORY_BYTEPOOL_GUARD, otherwise 0).

Definition at line 267 of file stk_c_memory.h.

◆ STK_BYTEPOOL_HEADER_SIZE

#define STK_BYTEPOOL_HEADER_SIZE   ((STK_BYTEPOOL_ALIGN > 8U) ? STK_BYTEPOOL_ALIGN : 8U)

Per-block header size in bytes: max(8, STK_BYTEPOOL_ALIGN). Every allocation consumes this many bytes of the pool in addition to its aligned payload.

Definition at line 262 of file stk_c_memory.h.

Referenced by STK_STATIC_ASSERT_DESC().

◆ STK_BYTEPOOL_REQUIRED_SIZE

#define STK_BYTEPOOL_REQUIRED_SIZE ( payload_size)
Value:
#define STK_BYTEPOOL_ALIGN_SIZE(raw_size)
Round raw_size up to the nearest multiple of STK_BYTEPOOL_ALIGN.
#define STK_BYTEPOOL_HEADER_SIZE
Per-block header size in bytes: max(8, STK_BYTEPOOL_ALIGN). Every allocation consumes this many bytes...
#define STK_BYTEPOOL_GUARD_SIZE
Size of the overrun guard word of a used block (4 with STK_MEMORY_BYTEPOOL_GUARD, otherwise 0).

Pool space in bytes consumed by one allocation of payload_size bytes (aligned payload, plus the guard word if enabled, plus the block header). Sum the value for every block that must be alive simultaneously to size external storage. Matches BytePool::RequiredSize().

Definition at line 281 of file stk_c_memory.h.

281#define STK_BYTEPOOL_REQUIRED_SIZE(payload_size) \
282 (STK_BYTEPOOL_ALIGN_SIZE((payload_size) + STK_BYTEPOOL_GUARD_SIZE) + STK_BYTEPOOL_HEADER_SIZE)

◆ STK_BYTEPOOL_STORAGE_DECL

#define STK_BYTEPOOL_STORAGE_DECL ( name,
size_bytes )
Value:
static uint64_t name[((size_bytes) + (sizeof(uint64_t) - 1U)) / sizeof(uint64_t)] \
__stk_c_align(STK_BYTEPOOL_ALIGN)

Declare an external storage array for a byte pool, aligned to STK_BYTEPOOL_ALIGN.

Expands to a static uint64_t array (so that the start address is at least 8-byte aligned on every target) with the alignment attribute STK_BYTEPOOL_ALIGN. Use sizeof(name) as the storage_size argument.

Parameters
nameC identifier for the array variable.
size_bytesRequired pool size in bytes (headers included), e.g. a sum of STK_BYTEPOOL_REQUIRED_SIZE() values.

Definition at line 293 of file stk_c_memory.h.

293#define STK_BYTEPOOL_STORAGE_DECL(name, size_bytes) \
294 static uint64_t name[((size_bytes) + (sizeof(uint64_t) - 1U)) / sizeof(uint64_t)] \
295 __stk_c_align(STK_BYTEPOOL_ALIGN)

◆ STK_C_BLOCKPOOL_MAX

#define STK_C_BLOCKPOOL_MAX   (8U)

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

Note
Increase if your application needs more simultaneous pools.

Definition at line 105 of file stk_c_memory.h.

◆ STK_C_BYTEPOOL_MAX

#define STK_C_BYTEPOOL_MAX   (8U)

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

Note
Increase if your application needs more simultaneous pools.

Definition at line 113 of file stk_c_memory.h.

◆ STK_C_MEMORY_DEFAULT_ALLOCATOR

#define STK_C_MEMORY_DEFAULT_ALLOCATOR   (0)

Selects the implementation of stk::memory::MemoryAllocator provided by the C binding (default: 0).

  • 0: wraps the C library malloc() / free() (the lock hooks of the C library must be implemented, e.g. __malloc_lock for newlib). Statistics are not available (all zero).
  • 1: uses the deterministic, ISR-safe allocator on top of a static BytePool (stk_memory_allocator_default.h), heap size is STK_MEMORY_DEFAULT_HEAP_SIZE.
    Note
    Has no effect if STK_MEMORY_NO_HEAP is 1 (no allocator is provided then).

Definition at line 126 of file stk_c_memory.h.

◆ STK_MEM_MIN_ALIGNMENT

#define STK_MEM_MIN_ALIGNMENT   (8U)

Minimum alignment in bytes guaranteed for pointers returned by stk_mem_allocate() (equals MemoryAllocator::MIN_ALIGNMENT).

Definition at line 782 of file stk_c_memory.h.

Referenced by STK_STATIC_ASSERT_DESC().

◆ STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT

#define STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT   (8U)

Default block alignment of a block pool (see stk_memory_blockpool.h).

Definition at line 140 of file stk_c_memory.h.

◆ STK_MEMORY_BLOCKPOOL_BITMAP

#define STK_MEMORY_BLOCKPOOL_BITMAP   (0)

When 1, a block pool keeps an "allocated" bit per block (O(1) double-free detection in release builds) and its storage requirement grows by capacity/8 bytes (see stk_memory_blockpool.h).

Definition at line 149 of file stk_c_memory.h.

◆ STK_MEMORY_BYTEPOOL_ALIGN_DEFAULT

#define STK_MEMORY_BYTEPOOL_ALIGN_DEFAULT   (8U)

Payload alignment of byte pool allocations (see stk_memory_bytepool.h).

Definition at line 159 of file stk_c_memory.h.

◆ STK_MEMORY_BYTEPOOL_GUARD

#define STK_MEMORY_BYTEPOOL_GUARD   (0)

When 1, every byte pool allocation carries a 4-byte overrun guard word (see stk_memory_bytepool.h).

Definition at line 167 of file stk_c_memory.h.

Typedef Documentation

◆ stk_blockpool_t

typedef struct stk_blockpool_t stk_blockpool_t

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

Definition at line 303 of file stk_c_memory.h.

◆ stk_bytepool_t

typedef struct stk_bytepool_t stk_bytepool_t

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

Definition at line 307 of file stk_c_memory.h.

◆ stk_mem_stats_t

typedef struct stk_mem_stats_t stk_mem_stats_t

Snapshot of allocator statistics.

Note
Layout-compatible in meaning with stk::memory::MemoryAllocator::Stats. Byte counters of pools are in bytes (BlockMemoryPool: blocks * block size; BytePool: used blocks including their headers).
See also
stk_blockpool_get_stats(), stk_bytepool_get_stats(), stk_mem_get_stats()

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. Returns NULL only if the wait was cancelled (via IKernel::CancelTaskWait()) or the pool is invalid (see stk_blockpool_is_storage_valid()); never on timeout. Use stk_blockpool_timed_alloc_ex() to tell these apart.
Warning
Not ISR-safe.

Definition at line 379 of file stk_c_memory.cpp.

380{
381 STK_ASSERT(pool != nullptr);
382 // stk_blockpool_alloc() blocks indefinitely and must never be called from an ISR.
383 // Use stk_blockpool_try_alloc() or stk_blockpool_timed_alloc(..., STK_NO_WAIT) instead.
385
386 return pool->handle.Alloc();
387}
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
void * Alloc()
Allocate one block, blocking indefinitely until one is available.
BlockMemoryPool handle

References stk::memory::BlockMemoryPool::Alloc(), stk_blockpool_t::handle, stk::hw::IsInsideISR(), and STK_ASSERT.

Here is the call graph for this function:

◆ 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 (default alignment).

Allocates a flat byte buffer of STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) bytes (plus alignment slack) via the memory allocator. 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) or heap support is compiled out (STK_MEMORY_NO_HEAP). A non-NULL handle does not guarantee the backing storage was successfully heap-allocated; always call stk_blockpool_is_storage_valid() immediately after creation when operating without exceptions.
Note
Not ISR-safe.
See also
stk_blockpool_create_ex(), stk_blockpool_is_storage_valid(), stk_blockpool_destroy()

Definition at line 313 of file stk_c_memory.cpp.

314{
315 return stk_blockpool_create_ex(capacity, raw_block_size, name, BlockMemoryPool::ALIGN_DEFAULT);
316}
stk_blockpool_t * stk_blockpool_create_ex(size_t capacity, size_t raw_block_size, const char *name, size_t alignment)
Create a block pool backed by heap-allocated storage with an explicit block alignment.
static const size_t ALIGN_DEFAULT
Default block alignment in bytes (see STK_MEMORY_BLOCKPOOL_ALIGN_DEFAULT).

References stk::memory::BlockMemoryPool::ALIGN_DEFAULT, and stk_blockpool_create_ex().

Here is the call graph for this function:

◆ stk_blockpool_create_ex()

stk_blockpool_t * stk_blockpool_create_ex ( size_t capacity,
size_t raw_block_size,
const char * name,
size_t alignment )

Create a block pool backed by heap-allocated storage with an explicit block alignment.

Parameters
[in]capacityTotal number of blocks.
[in]raw_block_sizeRequested per-block size in bytes.
[in]nameOptional human-readable name (may be NULL).
[in]alignmentBlock alignment in bytes, a power of two not less than sizeof(void *).
Returns
See stk_blockpool_create(). An invalid alignment results in an invalid pool (check stk_blockpool_is_storage_valid()).
Note
Not ISR-safe.

Definition at line 282 of file stk_c_memory.cpp.

286{
287 STK_ASSERT(capacity > 0U);
288 STK_ASSERT(raw_block_size > 0U);
289
290 stk_blockpool_t *result = nullptr;
291
292#if STK_MEMORY_HAS_HEAP
294
295 BlockPoolSlot *const slot = AcquireSlot();
296
297 // pool exhausted — increase STK_C_BLOCKPOOL_MAX
298 STK_ASSERT(slot != nullptr);
299
300 if (slot != nullptr)
301 {
302 result = ConstructIn<stk_blockpool_t>(slot->mem, capacity, raw_block_size, name, alignment);
303 }
304#else
305 // heap support is compiled out (STK_MEMORY_NO_HEAP): use stk_blockpool_create_static*()
306 STK_UNUSED(name);
307 STK_UNUSED(alignment);
308#endif
309
310 return result;
311}
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
static T * ConstructIn(Mem &mem, Args &&... args)
Construct object T in-place inside a C memory container.
Definition stk_c.h:2970
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
Definition stk_sync_cs.h:58

References stk::ConstructIn(), STK_ASSERT, and STK_UNUSED.

Referenced by stk_blockpool_create().

Here is the call graph for this function:
Here is the caller 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 (default alignment).

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. A misaligned start address is corrected internally (the unaligned head bytes are not used) and the capacity is trimmed to what fits (asserted in debug builds).

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. Should be aligned to STK_BLOCKPOOL_ALIGN 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.
[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_create_static_ex(), stk_blockpool_destroy()

Definition at line 347 of file stk_c_memory.cpp.

352{
353 return stk_blockpool_create_static_ex(capacity, raw_block_size, storage_ptr, storage_size, name,
355}
stk_blockpool_t * stk_blockpool_create_static_ex(size_t capacity, size_t raw_block_size, uint8_t *storage_ptr, size_t storage_size, const char *name, size_t alignment)
Create a block pool backed by caller-supplied storage with an explicit block alignment.

References stk::memory::BlockMemoryPool::ALIGN_DEFAULT, and stk_blockpool_create_static_ex().

Here is the call graph for this function:

◆ stk_blockpool_create_static_ex()

stk_blockpool_t * stk_blockpool_create_static_ex ( size_t capacity,
size_t raw_block_size,
uint8_t * storage,
size_t storage_size,
const char * name,
size_t alignment )

Create a block pool backed by caller-supplied storage with an explicit block alignment.

Parameters
[in]capacityTotal number of blocks the pool can hold.
[in]raw_block_sizeRequested per-block size in bytes.
[in]storageCaller-owned byte buffer, at least STK_BLOCKPOOL_STORAGE_SIZE_EX(capacity, raw_block_size, alignment) bytes.
[in]storage_sizeSize of storage in bytes.
[in]nameOptional human-readable name (may be NULL).
[in]alignmentBlock alignment in bytes, a power of two not less than sizeof(void *).
Returns
Pool handle, or NULL if the static slot pool is exhausted.
Note
Not ISR-safe.

Definition at line 318 of file stk_c_memory.cpp.

324{
325 STK_ASSERT(capacity > 0U);
326 STK_ASSERT(raw_block_size > 0U);
327 STK_ASSERT(storage_ptr != nullptr);
328 STK_ASSERT(storage_size >= BlockMemoryPool::RequiredStorageSize(capacity, raw_block_size, alignment));
329
331
332 BlockPoolSlot *const slot = AcquireSlot();
333
334 // pool exhausted — increase STK_C_BLOCKPOOL_MAX
335 STK_ASSERT(slot != nullptr);
336
337 stk_blockpool_t *result = nullptr;
338 if (slot != nullptr)
339 {
340 result = ConstructIn<stk_blockpool_t>(slot->mem, capacity, raw_block_size,
341 storage_ptr, storage_size, name, alignment);
342 }
343
344 return result;
345}
static constexpr size_t RequiredStorageSize(size_t capacity, size_t raw_block_size, size_t alignment=ALIGN_DEFAULT)
Compute the size of the storage buffer required for an external-storage pool.

References stk::ConstructIn(), stk::memory::BlockMemoryPool::RequiredStorageSize(), and STK_ASSERT.

Referenced by stk_blockpool_create_static().

Here is the call graph for this function:
Here is the caller 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 one of the stk_blockpool_create*() functions.
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. All blocks must be returned before destruction.
Set the pool pointer to NULL after this call to prevent accidental use-after-destroy (the handle slot may be reused by a subsequent stk_blockpool_create() call).
Note
Not ISR-safe.

Definition at line 357 of file stk_c_memory.cpp.

358{
359 STK_ASSERT(pool != nullptr);
360
362
363 BlockPoolSlot *const slot = FindSlot(pool);
364
365 // pool not found: double-destroy or corruption
366 STK_ASSERT(slot != nullptr);
367 if (slot != nullptr)
368 {
369 // Explicitly run the destructor (frees heap storage if owned).
370 pool->~stk_blockpool_t();
371 slot->busy = false;
372 }
373}

References STK_ASSERT.

◆ 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 an allocation function of this 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, misaligned or already free (double-free; release-build detection requires STK_MEMORY_BLOCKPOOL_BITMAP) - 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 415 of file stk_c_memory.cpp.

416{
417 STK_ASSERT(pool != nullptr);
418
419 return pool->handle.Free(ptr);
420}
bool Free(void *ptr)
Return a previously allocated block to the pool.

References stk::memory::BlockMemoryPool::Free(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_blockpool_get_alloc_fail_count()

size_t stk_blockpool_get_alloc_fail_count ( const stk_blockpool_t * pool)

Get the number of allocation requests that failed because the pool was exhausted (timeouts and STK_NO_WAIT misses).

Parameters
[in]poolPool handle.
Note
ISR-safe on targets where an aligned size_t read is a single instruction.

Definition at line 475 of file stk_c_memory.cpp.

476{
477 STK_ASSERT(pool != nullptr);
478
479 return pool->handle.GetAllocFailCount();
480}
size_t GetAllocFailCount() const
Get the number of allocation requests that failed because the pool was exhausted (timeouts and NO_WAI...

References stk::memory::BlockMemoryPool::GetAllocFailCount(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_blockpool_get_block_alignment()

size_t stk_blockpool_get_block_alignment ( const stk_blockpool_t * pool)

Get the alignment of every block of the pool.

Parameters
[in]poolPool handle.
Returns
Alignment in bytes.
Note
ISR-safe.

Definition at line 447 of file stk_c_memory.cpp.

448{
449 STK_ASSERT(pool != nullptr);
450
451 return pool->handle.GetBlockAlignment();
452}
size_t GetBlockAlignment() const
Get the alignment (bytes) of every block.

References stk::memory::BlockMemoryPool::GetBlockAlignment(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ 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_EX(raw_block_size, alignment) as passed at construction. Always >= the pool alignment.

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

Definition at line 440 of file stk_c_memory.cpp.

441{
442 STK_ASSERT(pool != nullptr);
443
444 return pool->handle.GetBlockSize();
445}
size_t GetBlockSize() const
Get the aligned block size used internally by the allocator.

References stk::memory::BlockMemoryPool::GetBlockSize(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ 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. Can be lower than requested if the supplied buffer was too small, 0 for an invalid pool.
Note
ISR-safe.

Definition at line 433 of file stk_c_memory.cpp.

434{
435 STK_ASSERT(pool != nullptr);
436
437 return pool->handle.GetCapacity();
438}
size_t GetCapacity() const
Get the total block capacity of the pool.

References stk::memory::BlockMemoryPool::GetCapacity(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ 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 461 of file stk_c_memory.cpp.

462{
463 STK_ASSERT(pool != nullptr);
464
465 return pool->handle.GetFreeCount();
466}
size_t GetFreeCount() const
Get the number of free (available) blocks.

References stk::memory::BlockMemoryPool::GetFreeCount(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_blockpool_get_instance()

stk::memory::BlockMemoryPool * stk_blockpool_get_instance ( stk_blockpool_t * pool)

Get the underlying C++ stk::memory::BlockMemoryPool object wrapped by a stk_blockpool_t handle.

Parameters
[in]poolPool handle obtained via stk_blockpool_create*().
Returns
Pointer to the wrapped stk::memory::BlockMemoryPool instance. Never NULL for a valid handle.
Note
C++ callers only (guarded by __cplusplus); not part of the C ABI.

Definition at line 503 of file stk_c_memory.cpp.

504{
505 STK_ASSERT(pool != nullptr);
506
507 return &pool->handle;
508}

References stk_blockpool_t::handle, and STK_ASSERT.

◆ stk_blockpool_get_peak_used_count()

size_t stk_blockpool_get_peak_used_count ( const stk_blockpool_t * pool)

Get the high watermark: the maximum number of blocks allocated at the same time.

Run your worst-case workload and use this value to size the pool.

Parameters
[in]poolPool handle.
Note
ISR-safe on targets where an aligned size_t read is a single instruction.

Definition at line 468 of file stk_c_memory.cpp.

469{
470 STK_ASSERT(pool != nullptr);
471
472 return pool->handle.GetPeakUsedCount();
473}
size_t GetPeakUsedCount() const
Get the high watermark: the maximum number of blocks that were allocated at the same time.

References stk::memory::BlockMemoryPool::GetPeakUsedCount(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_blockpool_get_stats()

void stk_blockpool_get_stats ( const stk_blockpool_t * pool,
stk_mem_stats_t * stats )

Get a consistent statistics snapshot (bytes: blocks * block size).

Parameters
[in]poolPool handle.
[out]statsReceives the snapshot. Must not be NULL.
Note
ISR-safe.

Definition at line 482 of file stk_c_memory.cpp.

483{
484 STK_ASSERT(pool != nullptr);
485
486 CopyStats(pool->handle.GetStats(), stats);
487}
MemoryAllocator::Stats GetStats() const
Get a consistent statistics snapshot in the same format as MemoryAllocator::GetStats().

References stk::memory::BlockMemoryPool::GetStats(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ 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 an aligned size_t read is a single instruction.

Definition at line 454 of file stk_c_memory.cpp.

455{
456 STK_ASSERT(pool != nullptr);
457
458 return pool->handle.GetUsedCount();
459}
size_t GetUsedCount() const
Get the number of currently allocated (outstanding) blocks.

References stk::memory::BlockMemoryPool::GetUsedCount(), stk_blockpool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ 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 496 of file stk_c_memory.cpp.

497{
498 STK_ASSERT(pool != nullptr);
499
500 return pool->handle.IsEmpty();
501}
bool IsEmpty() const
Check whether all blocks are free (no outstanding allocations).

References stk_blockpool_t::handle, stk::memory::BlockMemoryPool::IsEmpty(), and STK_ASSERT.

Here is the call graph for this function:

◆ 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 489 of file stk_c_memory.cpp.

490{
491 STK_ASSERT(pool != nullptr);
492
493 return pool->handle.IsFull();
494}
bool IsFull() const
Check whether all blocks are currently allocated (pool exhausted).

References stk_blockpool_t::handle, stk::memory::BlockMemoryPool::IsFull(), and STK_ASSERT.

Here is the call graph for this function:

◆ 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 a sufficiently large external buffer. For heap-constructed pools, false if the heap allocation failed. false as well if the arguments were invalid or the buffer is too small for a single block.

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

Definition at line 426 of file stk_c_memory.cpp.

427{
428 STK_ASSERT(pool != nullptr);
429
430 return pool->handle.IsStorageValid();
431}
bool IsStorageValid() const
Verify that the backing storage is valid and the pool is ready for use.

References stk_blockpool_t::handle, stk::memory::BlockMemoryPool::IsStorageValid(), and STK_ASSERT.

Here is the call graph for this function:

◆ stk_blockpool_timed_alloc()

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.

Parameters
[in]poolPool handle.
[in]timeoutMaximum time to wait in OS 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, the wait was cancelled or the pool is invalid.
Note
Collapses timeout and cancellation. Use stk_blockpool_timed_alloc_ex() to tell them apart.
If OS tick rate is 1 kHz then 1 tick = 1 ms. Use stk_ticks_from_ms() to convert a millisecond value to ticks for best portability across different tick rates.
Warning
ISR-safe only when timeout = STK_NO_WAIT; not ISR-safe otherwise.

Definition at line 389 of file stk_c_memory.cpp.

390{
391 STK_ASSERT(pool != nullptr);
392
393 return pool->handle.TimedAlloc(timeout);
394}
void * TimedAlloc(Timeout timeout_ticks=WAIT_INFINITE)
Allocate one block, blocking until one becomes available or the timeout expires.

References stk_blockpool_t::handle, STK_ASSERT, and stk::memory::BlockMemoryPool::TimedAlloc().

Here is the call graph for this function:

◆ stk_blockpool_timed_alloc_ex()

stk_wait_result_t stk_blockpool_timed_alloc_ex ( stk_blockpool_t * pool,
void ** block,
stk_timeout_t timeout )

Allocate one block, preserving the full wait outcome.

Parameters
[in]poolPool handle.
[out]blockReceives the allocated block on STK_WAIT_RESULT_SIGNAL, NULL on any other result. Must not be NULL.
[in]timeoutMaximum time to wait in OS ticks (see stk_blockpool_timed_alloc()).
Returns
STK_WAIT_RESULT_SIGNAL - a block was allocated, STK_WAIT_RESULT_TIMEOUT - no block became available before the timeout (or STK_NO_WAIT on an empty pool), STK_WAIT_RESULT_CANCELED - the wait was cancelled, nothing was allocated, STK_WAIT_RESULT_FAIL - invalid arguments, invalid pool or the kernel rejected the wait.
Warning
ISR-safe only when timeout = STK_NO_WAIT; not ISR-safe otherwise.

Definition at line 396 of file stk_c_memory.cpp.

397{
398 STK_ASSERT(pool != nullptr);
399 STK_ASSERT(block != nullptr);
400
401 return static_cast<stk_wait_result_t>(pool->handle.TimedAllocEx(block, timeout));
402}
stk_wait_result_t
Outcome of a cancellable, timed kernel wait (see stk_cv_wait_ex(), stk_mutex_timed_lock_ex(),...
Definition stk_c.h:210
EWaitResult TimedAllocEx(void **block, Timeout timeout_ticks=WAIT_INFINITE)
Allocate one block, preserving the full wait outcome.

References stk_blockpool_t::handle, STK_ASSERT, and stk::memory::BlockMemoryPool::TimedAllocEx().

Here is the call graph for this function:

◆ 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 404 of file stk_c_memory.cpp.

405{
406 STK_ASSERT(pool != nullptr);
407
408 return pool->handle.TryAlloc();
409}
void * TryAlloc()
Non-blocking allocation attempt.

References stk_blockpool_t::handle, STK_ASSERT, and stk::memory::BlockMemoryPool::TryAlloc().

Here is the call graph for this function:

◆ stk_bytepool_alloc()

void * stk_bytepool_alloc ( stk_bytepool_t * pool,
size_t size )

Allocate a block, blocking indefinitely until enough contiguous memory is available.

Parameters
[in]poolPool handle.
[in]sizeRequested payload size in bytes (> 0 and <= stk_bytepool_get_max_request()).
Returns
Pointer to an uninitialized block aligned to STK_BYTEPOOL_ALIGN, or NULL if the request is invalid (zero, larger than the pool) or the wait was cancelled. Never returns NULL because of a timeout.
Warning
Not ISR-safe.

Definition at line 589 of file stk_c_memory.cpp.

590{
591 STK_ASSERT(pool != nullptr);
592 // stk_bytepool_alloc() blocks indefinitely and must never be called from an ISR.
593 // Use stk_bytepool_try_alloc() or stk_bytepool_timed_alloc(..., STK_NO_WAIT) instead.
595
596 return pool->handle.Alloc(size);
597}
void * Alloc(size_t size)
Allocate a block, blocking indefinitely until memory is available.

References stk::memory::BytePool::Alloc(), stk_bytepool_t::handle, stk::hw::IsInsideISR(), and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_create()

stk_bytepool_t * stk_bytepool_create ( size_t storage_size,
const char * name )

Create a byte pool backed by heap-allocated storage.

Allocates storage_size bytes (plus alignment slack) via the memory allocator. The size includes block headers (see STK_BYTEPOOL_REQUIRED_SIZE()). Call stk_bytepool_is_storage_valid() immediately after creation when operating without exceptions.

Parameters
[in]storage_sizeSize of the pool area in bytes (headers included).
[in]nameOptional human-readable name (may be NULL).
Returns
Pool handle, or NULL if the static slot pool is exhausted (STK_C_BYTEPOOL_MAX reached) or heap support is compiled out (STK_MEMORY_NO_HEAP). A non-NULL handle does not guarantee that the heap allocation succeeded; check stk_bytepool_is_storage_valid().
Note
Not ISR-safe.
See also
stk_bytepool_is_storage_valid(), stk_bytepool_destroy()

Definition at line 518 of file stk_c_memory.cpp.

519{
521
522 stk_bytepool_t *result = nullptr;
523
524#if STK_MEMORY_HAS_HEAP
526
527 BytePoolSlot *const slot = AcquireBytePoolSlot();
528
529 // pool exhausted — increase STK_C_BYTEPOOL_MAX
530 STK_ASSERT(slot != nullptr);
531
532 if (slot != nullptr)
533 {
534 result = ConstructIn<stk_bytepool_t>(slot->mem, storage_size, name);
535 }
536#else
537 // heap support is compiled out (STK_MEMORY_NO_HEAP): use stk_bytepool_create_static()
538 STK_UNUSED(name);
539#endif
540
541 return result;
542}
static const size_t HEADER_SIZE
Size of the per-block header in bytes: max(8, BLOCK_ALIGN).
static const size_t BLOCK_ALIGN
Payload alignment and block size granularity in bytes.

References stk::memory::BytePool::BLOCK_ALIGN, stk::ConstructIn(), stk::memory::BytePool::HEADER_SIZE, STK_ASSERT, and STK_UNUSED.

Here is the call graph for this function:

◆ stk_bytepool_create_static()

stk_bytepool_t * stk_bytepool_create_static ( uint8_t * storage,
size_t storage_size,
const char * name )

Create a byte 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. A misaligned start address is corrected internally (unaligned head/tail bytes are unused).

Parameters
[in]storagePointer to a caller-owned byte buffer, preferably aligned to STK_BYTEPOOL_ALIGN (see STK_BYTEPOOL_STORAGE_DECL()).
[in]storage_sizeSize of storage in bytes. Must be at least STK_BYTEPOOL_HEADER_SIZE + STK_BYTEPOOL_ALIGN (asserted).
[in]nameOptional human-readable name (may be NULL).
Returns
Pool handle, or NULL if the static slot pool is exhausted (STK_C_BYTEPOOL_MAX reached).
Note
Not ISR-safe.
See also
stk_bytepool_destroy()

Definition at line 544 of file stk_c_memory.cpp.

547{
548 STK_ASSERT(storage != nullptr);
550
552
553 BytePoolSlot *const slot = AcquireBytePoolSlot();
554
555 // pool exhausted — increase STK_C_BYTEPOOL_MAX
556 STK_ASSERT(slot != nullptr);
557
558 stk_bytepool_t *result = nullptr;
559 if (slot != nullptr)
560 {
561 result = ConstructIn<stk_bytepool_t>(slot->mem, storage, storage_size, name);
562 }
563
564 return result;
565}

References stk::memory::BytePool::BLOCK_ALIGN, stk::ConstructIn(), stk::memory::BytePool::HEADER_SIZE, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_destroy()

void stk_bytepool_destroy ( stk_bytepool_t * pool)

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

Heap storage is freed if owned. External storage is never touched.

Parameters
[in]poolHandle obtained via stk_bytepool_create() or stk_bytepool_create_static().
Warning
Destroying a pool while tasks are blocked in stk_bytepool_alloc() or stk_bytepool_timed_alloc() is a logic error. All blocks must be returned before destruction.
Set the pool pointer to NULL after this call; the slot may be reused.
Note
Not ISR-safe.

Definition at line 567 of file stk_c_memory.cpp.

568{
569 STK_ASSERT(pool != nullptr);
570
572
573 BytePoolSlot *const slot = FindBytePoolSlot(pool);
574
575 // pool not found: double-destroy or corruption
576 STK_ASSERT(slot != nullptr);
577 if (slot != nullptr)
578 {
579 // Explicitly run the destructor (frees heap storage if owned).
580 pool->~stk_bytepool_t();
581 slot->busy = false;
582 }
583}

References STK_ASSERT.

◆ stk_bytepool_free()

bool stk_bytepool_free ( stk_bytepool_t * pool,
void * ptr )

Return a previously allocated block to the pool.

Marks the block free, merges it with following free blocks and wakes all tasks blocked in stk_bytepool_alloc() / stk_bytepool_timed_alloc().

Parameters
[in]poolPool handle.
[in]ptrPointer previously returned by an allocation function of this pool.
Returns
true on success; false if ptr is NULL, outside the pool, misaligned, not in use (double-free) or - with STK_MEMORY_BYTEPOOL_GUARD - its guard word was overwritten (the block is kept, i.e. leaked, to protect the pool metadata).
Note
ISR-safe, but execution time is proportional to the number of blocks scanned.
Warning
Null the pointer after stk_bytepool_free() to prevent double-free.

Definition at line 625 of file stk_c_memory.cpp.

626{
627 STK_ASSERT(pool != nullptr);
628
629 return pool->handle.Free(ptr);
630}
bool Free(void *ptr)
Return a previously allocated block to the pool.

References stk::memory::BytePool::Free(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_get_alloc_fail_count()

size_t stk_bytepool_get_alloc_fail_count ( const stk_bytepool_t * pool)

Get the number of allocation requests that failed because no suitable block was available (timeouts, STK_NO_WAIT misses and requests larger than stk_bytepool_get_max_request()).

Parameters
[in]poolPool handle.
Note
ISR-safe on targets where an aligned size_t read is a single instruction.

Definition at line 668 of file stk_c_memory.cpp.

669{
670 STK_ASSERT(pool != nullptr);
671
672 return pool->handle.GetAllocFailCount();
673}
size_t GetAllocFailCount() const
Get the number of allocation requests that failed because no suitable block was available (timeouts,...

References stk::memory::BytePool::GetAllocFailCount(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_get_info()

void stk_bytepool_get_info ( const stk_bytepool_t * pool,
size_t * available,
size_t * largest,
size_t * fragments )

Collect pool statistics (mutually consistent snapshot).

Parameters
[in]poolPool handle.
[out]availableTotal allocatable payload bytes over all free fragments (may be NULL).
[out]largestPayload size of the largest free fragment, i.e. the biggest request that would succeed right now (may be NULL).
[out]fragmentsNumber of free fragments; 1 means no external fragmentation (may be NULL).
Note
ISR-safe, but O(n) in the number of blocks. See stk_bytepool_get_stats() for an O(1) alternative.

Definition at line 636 of file stk_c_memory.cpp.

640{
641 STK_ASSERT(pool != nullptr);
642
643 size_t avail = 0U;
644 size_t large = 0U;
645 size_t frags = 0U;
646
647 pool->handle.GetInfo(avail, large, frags);
648
649 if (available != nullptr) { *available = avail; }
650 if (largest != nullptr) { *largest = large; }
651 if (fragments != nullptr) { *fragments = frags; }
652}
void GetInfo(size_t &available, size_t &largest, size_t &fragments) const
Collect pool statistics.

References stk::memory::BytePool::GetInfo(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_get_instance()

stk::memory::BytePool * stk_bytepool_get_instance ( stk_bytepool_t * pool)

Get the underlying C++ stk::memory::BytePool object wrapped by a stk_bytepool_t handle.

Parameters
[in]poolPool handle obtained via stk_bytepool_create() or stk_bytepool_create_static().
Returns
Pointer to the wrapped stk::memory::BytePool instance. Never NULL for a valid handle.
Note
C++ callers only (guarded by __cplusplus); not part of the C ABI.

Definition at line 703 of file stk_c_memory.cpp.

704{
705 STK_ASSERT(pool != nullptr);
706
707 return &pool->handle;
708}

References stk_bytepool_t::handle, and STK_ASSERT.

◆ stk_bytepool_get_max_request()

size_t stk_bytepool_get_max_request ( const stk_bytepool_t * pool)

Get the largest request that can ever succeed on this pool.

Parameters
[in]poolPool handle.
Returns
Payload size of a single block spanning the whole pool (less the guard word, if enabled), or 0 if the storage is invalid. Larger requests fail immediately instead of blocking forever.
Note
ISR-safe.

Definition at line 696 of file stk_c_memory.cpp.

697{
698 STK_ASSERT(pool != nullptr);
699
700 return pool->handle.GetMaxRequest();
701}
size_t GetMaxRequest() const
Get the largest request that can ever succeed on this pool.

References stk::memory::BytePool::GetMaxRequest(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_get_max_scan_length()

size_t stk_bytepool_get_max_scan_length ( const stk_bytepool_t * pool)

Get the length of the longest block-list walk (in blocks) of any allocation so far.

The walk is executed inside a critical section, so this measures the worst-case time interrupts/scheduling were blocked by the allocator.

Parameters
[in]poolPool handle.
Note
ISR-safe on targets where an aligned size_t read is a single instruction.

Definition at line 661 of file stk_c_memory.cpp.

662{
663 STK_ASSERT(pool != nullptr);
664
665 return pool->handle.GetMaxScanLength();
666}
size_t GetMaxScanLength() const
Get the length of the longest block-list walk (in blocks) of any allocation so far.

References stk::memory::BytePool::GetMaxScanLength(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_get_peak_used_size()

size_t stk_bytepool_get_peak_used_size ( const stk_bytepool_t * pool)

Get the high watermark of pool consumption in bytes (headers included).

Parameters
[in]poolPool handle.
Note
ISR-safe on targets where an aligned size_t read is a single instruction.

Definition at line 675 of file stk_c_memory.cpp.

676{
677 STK_ASSERT(pool != nullptr);
678
679 return pool->handle.GetPeakUsedSize();
680}
size_t GetPeakUsedSize() const
Get the high watermark of pool consumption in bytes (headers included).

References stk::memory::BytePool::GetPeakUsedSize(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_get_size()

size_t stk_bytepool_get_size ( const stk_bytepool_t * pool)

Get the total managed (aligned) area size in bytes, headers included.

Parameters
[in]poolPool handle.
Returns
Managed area size, or 0 if the storage is invalid.
Note
ISR-safe.

Definition at line 689 of file stk_c_memory.cpp.

690{
691 STK_ASSERT(pool != nullptr);
692
693 return pool->handle.GetSize();
694}
size_t GetSize() const
Get the total size of the managed (aligned) area in bytes, headers included.

References stk::memory::BytePool::GetSize(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_get_stats()

void stk_bytepool_get_stats ( const stk_bytepool_t * pool,
stk_mem_stats_t * stats )

Get an O(1) statistics snapshot.

capacity is the managed area size and allocated the bytes held by used blocks, both including headers (the real pool consumption).

Parameters
[in]poolPool handle.
[out]statsReceives the snapshot. Must not be NULL.
Note
ISR-safe.

Definition at line 654 of file stk_c_memory.cpp.

655{
656 STK_ASSERT(pool != nullptr);
657
658 CopyStats(pool->handle.GetStats(), stats);
659}
MemoryAllocator::Stats GetStats() const
Get a consistent O(1) statistics snapshot in the same format as MemoryAllocator::GetStats().

References stk::memory::BytePool::GetStats(), stk_bytepool_t::handle, and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_is_storage_valid()

bool stk_bytepool_is_storage_valid ( const stk_bytepool_t * pool)

Verify that the backing storage is valid and the pool is ready for use.

Parameters
[in]poolPool handle.
Returns
true if the pool is ready; false if heap allocation failed or the supplied area is too small for even one block.
Note
ISR-safe.

Definition at line 682 of file stk_c_memory.cpp.

683{
684 STK_ASSERT(pool != nullptr);
685
686 return pool->handle.IsStorageValid();
687}
bool IsStorageValid() const
Verify that the backing storage is valid and the pool is ready for use.

References stk_bytepool_t::handle, stk::memory::BytePool::IsStorageValid(), and STK_ASSERT.

Here is the call graph for this function:

◆ stk_bytepool_timed_alloc()

void * stk_bytepool_timed_alloc ( stk_bytepool_t * pool,
size_t size,
stk_timeout_t timeout )

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

The timeout is a total budget, not per wake-up.

Parameters
[in]poolPool handle.
[in]sizeRequested payload size in bytes.
[in]timeoutMaximum time to wait in OS ticks. STK_WAIT_INFINITE blocks indefinitely; STK_NO_WAIT is identical to stk_bytepool_try_alloc().
Returns
Pointer to an uninitialized block, or NULL on timeout, cancellation or invalid arguments. Use stk_bytepool_timed_alloc_ex() to tell them apart.
Warning
ISR-safe only when timeout = STK_NO_WAIT.

Definition at line 599 of file stk_c_memory.cpp.

600{
601 STK_ASSERT(pool != nullptr);
602
603 return pool->handle.TimedAlloc(size, timeout);
604}
void * TimedAlloc(size_t size, Timeout timeout_ticks=WAIT_INFINITE)
Allocate a block, blocking until memory becomes available or the timeout expires.

References stk_bytepool_t::handle, STK_ASSERT, and stk::memory::BytePool::TimedAlloc().

Here is the call graph for this function:

◆ stk_bytepool_timed_alloc_ex()

stk_wait_result_t stk_bytepool_timed_alloc_ex ( stk_bytepool_t * pool,
void ** block,
size_t size,
stk_timeout_t timeout )

Allocate a block, preserving the full wait outcome.

Parameters
[in]poolPool handle.
[out]blockReceives the allocated block on STK_WAIT_RESULT_SIGNAL, NULL on any other result. Must not be NULL.
[in]sizeRequested payload size in bytes.
[in]timeoutMaximum time to wait in OS ticks (see stk_bytepool_timed_alloc()).
Returns
STK_WAIT_RESULT_SIGNAL - a block was allocated (immediately or after waiting), STK_WAIT_RESULT_TIMEOUT - no suitable block became available before the timeout (or STK_NO_WAIT was passed), STK_WAIT_RESULT_CANCELED - the wait was cancelled, STK_WAIT_RESULT_FAIL - invalid arguments (block is NULL, size is 0 or exceeds stk_bytepool_get_max_request(), no valid storage) or the kernel rejected the wait.
Warning
ISR-safe only when timeout = STK_NO_WAIT.

Definition at line 606 of file stk_c_memory.cpp.

607{
608 STK_ASSERT(pool != nullptr);
609 STK_ASSERT(block != nullptr);
610
611 return static_cast<stk_wait_result_t>(pool->handle.TimedAllocEx(block, size, timeout));
612}
EWaitResult TimedAllocEx(void **block, size_t size, Timeout timeout_ticks=WAIT_INFINITE)
Allocate a block, preserving the full wait outcome.

References stk_bytepool_t::handle, STK_ASSERT, and stk::memory::BytePool::TimedAllocEx().

Here is the call graph for this function:

◆ stk_bytepool_try_alloc()

void * stk_bytepool_try_alloc ( stk_bytepool_t * pool,
size_t size )

Non-blocking allocation attempt.

Parameters
[in]poolPool handle.
[in]sizeRequested payload size in bytes.
Returns
Pointer to an uninitialized block, or NULL if no suitable free block exists.
Note
ISR-safe, but execution time is proportional to the number of blocks scanned.

Definition at line 614 of file stk_c_memory.cpp.

615{
616 STK_ASSERT(pool != nullptr);
617
618 return pool->handle.TryAlloc(size);
619}
void * TryAlloc(size_t size)
Non-blocking allocation attempt.

References stk_bytepool_t::handle, STK_ASSERT, and stk::memory::BytePool::TryAlloc().

Here is the call graph for this function:

◆ stk_mem_allocate()

void * stk_mem_allocate ( size_t size)

Allocate a memory chunk from the general-purpose allocator.

Parameters
[in]sizeSize in bytes.
Returns
Pointer aligned to at least STK_MEM_MIN_ALIGNMENT, or NULL on failure (or if size is 0).
Note
Thread-safe. ISR-safe only with STK_C_MEMORY_DEFAULT_ALLOCATOR = 1.

Definition at line 723 of file stk_c_memory.cpp.

724{
725 void *result = nullptr;
726
728 {
729 result = MemoryAllocator::Allocate(size);
730 }
731
732 return result;
733}
bool stk_mem_is_available(void)
Check that the memory allocator implementation is linked in.
static void * Allocate(size_t size)
Allocate the memory chunk (checked).

References stk::memory::MemoryAllocator::Allocate(), and stk_mem_is_available().

Here is the call graph for this function:

◆ stk_mem_allocate_aligned()

void * stk_mem_allocate_aligned ( size_t size,
size_t alignment )

Allocate a chunk with an alignment stricter than STK_MEM_MIN_ALIGNMENT.

The result must be released with stk_mem_free_aligned(), never with stk_mem_free().

Parameters
[in]sizeSize in bytes.
[in]alignmentRequired alignment, a power of two.
Returns
Aligned pointer, or NULL on failure, if alignment is not a power of two or heap support is compiled out.

Definition at line 743 of file stk_c_memory.cpp.

744{
745 void *result = nullptr;
746
747#if STK_MEMORY_HAS_HEAP
748 result = MemoryAllocator::AllocateAligned(size, alignment);
749#else
750 STK_UNUSED(size);
751 STK_UNUSED(alignment);
752#endif
753
754 return result;
755}
static void * AllocateAligned(size_t size, size_t alignment, size_t offset=0U)
Allocate memory with an alignment stricter than MIN_ALIGNMENT and an optional offset.

References stk::memory::MemoryAllocator::AllocateAligned(), and STK_UNUSED.

Here is the call graph for this function:

◆ stk_mem_free()

void stk_mem_free ( void * ptr)

Free a chunk obtained with stk_mem_allocate().

Parameters
[in]ptrPointer to the chunk. NULL is allowed and results in noop.
Note
Thread-safe. ISR-safe only with STK_C_MEMORY_DEFAULT_ALLOCATOR = 1.

Definition at line 735 of file stk_c_memory.cpp.

736{
737 if ((ptr != nullptr) && stk_mem_is_available())
738 {
740 }
741}
static void Free(void *ptr)
Free the memory chunk (checked).

References stk::memory::MemoryAllocator::Free(), and stk_mem_is_available().

Here is the call graph for this function:

◆ stk_mem_free_aligned()

void stk_mem_free_aligned ( void * ptr)

Free a chunk obtained with stk_mem_allocate_aligned().

Parameters
[in]ptrPointer returned by stk_mem_allocate_aligned(). NULL is allowed and results in noop.

Definition at line 757 of file stk_c_memory.cpp.

758{
759#if STK_MEMORY_HAS_HEAP
761#else
762 STK_UNUSED(ptr);
763#endif
764}
static void FreeAligned(void *ptr)
Free memory obtained with AllocateAligned().

References stk::memory::MemoryAllocator::FreeAligned(), and STK_UNUSED.

Here is the call graph for this function:

◆ stk_mem_get_stats()

void stk_mem_get_stats ( stk_mem_stats_t * stats)

Get statistics of the general-purpose allocator.

Parameters
[out]statsReceives the snapshot. Must not be NULL. All zero if the implementation does not provide statistics (STK_C_MEMORY_DEFAULT_ALLOCATOR = 0).

Definition at line 766 of file stk_c_memory.cpp.

767{
768 STK_ASSERT(stats != nullptr);
769
771 {
772 CopyStats(MemoryAllocator::GetStats(), stats);
773 }
774 else
775 {
776 CopyStats(MemoryAllocator::Stats(), stats);
777 }
778}
static Stats GetStats()
Get stats of memory allocation (checked).
Snapshot of the memory allocator's statistics.

References stk::memory::MemoryAllocator::GetStats(), STK_ASSERT, and stk_mem_is_available().

Here is the call graph for this function:

◆ stk_mem_is_available()

bool stk_mem_is_available ( void )

Check that the memory allocator implementation is linked in.

Returns
true if allocation functions are available (always true unless STK_MEMORY_NO_HEAP is 1).
Note
ISR-safe.

Definition at line 714 of file stk_c_memory.cpp.

715{
716#if STK_MEMORY_NO_HEAP
717 return false;
718#else
720#endif
721}
static bool IsAvailable()
Check that Weak::Allocate() and Weak::Free() were provided by the application.

References stk::memory::MemoryAllocator::IsAvailable().

Referenced by stk_mem_allocate(), stk_mem_free(), and stk_mem_get_stats().

Here is the call graph for this function:
Here is the caller graph for this function: