SuperTinyKernel™ RTOS 1.06.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.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_C_MEMORY_H_
11#define STK_C_MEMORY_H_
12
13#include "stk_c.h"
14
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65// =============================================================================
66// Configuration macros
67// =============================================================================
68
73#ifndef STK_C_BLOCKPOOL_MAX
74 #define STK_C_BLOCKPOOL_MAX (8U)
75#endif
76
77// =============================================================================
78// Storage helper macros
79// =============================================================================
80
84#define STK_BLOCKPOOL_ALIGN (sizeof(stk_word_t))
85
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)))
96
102#define STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) \
103 ((capacity) * STK_BLOCKPOOL_ALIGN_BLOCK_SIZE(raw_block_size))
104
122#define STK_BLOCKPOOL_STORAGE_DECL(name, capacity, raw_block_size) \
123 static stk_word_t name[STK_BLOCKPOOL_STORAGE_SIZE(capacity, raw_block_size) / sizeof(stk_word_t)]
124
125// =============================================================================
126// Types
127// =============================================================================
128
132
133// =============================================================================
134// Lifecycle - heap storage
135// =============================================================================
136
155 size_t raw_block_size,
156 const char *name);
157
176 size_t raw_block_size,
177 uint8_t *storage,
178 size_t storage_size,
179 const char *name);
180
195
196// =============================================================================
197// Allocation
198// =============================================================================
199
207
218
227
228// =============================================================================
229// Deallocation
230// =============================================================================
231
247bool stk_blockpool_free(stk_blockpool_t *pool, void *ptr);
248
249// =============================================================================
250// Query
251// =============================================================================
252
263
270
279
290
297
303bool stk_blockpool_is_full(const stk_blockpool_t *pool);
304
311
312#ifdef __cplusplus
313}
314#endif
315
317
318#endif /* STK_C_MEMORY_H_ */
C language binding/interface for SuperTinyKernel RTOS.
int32_t stk_timeout_t
Timeout value.
Definition stk_c.h:113
size_t stk_blockpool_get_capacity(const stk_blockpool_t *pool)
Get the total block capacity of the pool.
void * stk_blockpool_timed_alloc(stk_blockpool_t *pool, stk_timeout_t timeout)
Allocate one block, blocking until one becomes available or the timeout expires.
void stk_blockpool_destroy(stk_blockpool_t *pool)
Destroy a pool and return its slot to the static pool.
bool stk_blockpool_is_storage_valid(const stk_blockpool_t *pool)
Verify that the backing storage is valid and the pool is ready for use.
size_t stk_blockpool_get_used_count(const stk_blockpool_t *pool)
Get the number of currently allocated (outstanding) blocks.
void * stk_blockpool_alloc(stk_blockpool_t *pool)
Allocate one block, blocking indefinitely until one is available.
bool stk_blockpool_free(stk_blockpool_t *pool, void *ptr)
Return a previously allocated block to the pool.
void * stk_blockpool_try_alloc(stk_blockpool_t *pool)
Non-blocking allocation attempt.
stk_blockpool_t * stk_blockpool_create_static(size_t capacity, size_t raw_block_size, uint8_t *storage, size_t storage_size, const char *name)
Create a block pool backed by caller-supplied (external) storage.
stk_blockpool_t * stk_blockpool_create(size_t capacity, size_t raw_block_size, const char *name)
Create a block pool backed by heap-allocated storage.
bool stk_blockpool_is_empty(const stk_blockpool_t *pool)
Check whether all blocks are free (no outstanding allocations).
bool stk_blockpool_is_full(const stk_blockpool_t *pool)
Check whether all blocks are currently allocated (pool exhausted).
size_t stk_blockpool_get_free_count(const stk_blockpool_t *pool)
Get the number of free (available) blocks.
size_t stk_blockpool_get_block_size(const stk_blockpool_t *pool)
Get the aligned block size used internally by the allocator.