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_memory_allocator.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_MEMORY_ALLOCATOR_H_
11#define STK_MEMORY_ALLOCATOR_H_
12
13#include "stk_defs.h"
14
19#ifndef STK_MEMORY_PLACEMENT_NEW
20 #define STK_MEMORY_PLACEMENT_NEW (1)
21#endif
22
23#if STK_MEMORY_PLACEMENT_NEW
24#include <type_traits>
25#include <new>
26#endif
27
31
32namespace stk {
33namespace memory {
34
56{
60 static const size_t CAPACITY_DEFAULT = 12288U;
61
65 struct Stats
66 {
67 Stats(size_t _capacity = CAPACITY_DEFAULT)
68 : capacity(_capacity), allocated(0U), allocate_count(0U), free_count(0U),
69 min_ever_free(_capacity)
70 {}
71
72 const size_t capacity;
73 volatile size_t allocated;
74 volatile size_t allocate_count;
75 volatile size_t free_count;
76 volatile size_t min_ever_free;
77
81 size_t GetCapacity() const { return capacity; }
82
86 size_t GetAvailable() const { return ((allocated < capacity) ? (capacity - allocated) : 0U); }
87
91 void RecordAllocate(size_t size)
92 {
93 allocated += size;
96 }
97
101 void RecordFree(size_t size)
102 {
103 STK_ASSERT(allocated >= size);
104
105 allocated -= size;
106 ++free_count;
107 }
108 };
109
114 static void *Allocate(size_t size) __stk_weak;
115
119 static void Free(void *ptr) __stk_weak;
120
124 static Stats GetStats() __stk_weak;
125
126#if STK_MEMORY_PLACEMENT_NEW
127
133 template <typename TElement, typename... TArgs>
134 static inline TElement *AllocateOneT(TArgs &&...args)
135 {
136 STK_ASSERT(Allocate != nullptr);
137
138 TElement *ptr = reinterpret_cast<TElement *>(Allocate(sizeof(TElement)));
139 if (ptr != nullptr)
140 {
141 if __stk_constexpr_cpp17 (!std::is_trivially_constructible<TElement, TArgs...>())
142 {
143 auto const elm = new (ptr) TElement(static_cast<TArgs &&>(args)...);
144 STK_UNUSED(elm);
145 }
146 }
147
148 return ptr;
149 }
150
157 template <typename TElement, typename... TArgs>
158 static inline TElement *AllocateArrayT(size_t count, TArgs &&...args)
159 {
160 TElement *ptr = nullptr;
161
162 if (count != 0U)
163 {
165
166 ptr = reinterpret_cast<TElement *>(Allocate(count * sizeof(TElement)));
167 if (ptr != nullptr)
168 {
169 if __stk_constexpr_cpp17 (!std::is_trivially_constructible<TElement, TArgs...>())
170 {
171 for (size_t i = 0U; i < count; ++i)
172 {
173 auto const elm = new (&ptr[i]) TElement(static_cast<TArgs &&>(args)...);
174 STK_UNUSED(elm);
175 }
176 }
177 }
178 }
179
180 return ptr;
181 }
182
186 template <typename TElement>
187 static inline void FreeOneT(TElement *ptr)
188 {
189 STK_ASSERT(Free != nullptr);
190
191 if (ptr != nullptr)
192 {
193 if __stk_constexpr_cpp17 (!std::is_trivially_destructible<TElement>())
194 {
195 ptr->~TElement();
196 }
197
198 Free(ptr);
199 }
200 }
201
206 template <typename TElement>
207 static inline void FreeArrayT(TElement ptr[], size_t count)
208 {
210
211 if (ptr != nullptr)
212 {
213 if __stk_constexpr_cpp17 (!std::is_trivially_destructible<TElement>())
214 {
215 // destroy in reverse order (mirrors stack unwinding)
216 for (size_t i = count; i > 0U; --i)
217 {
218 ptr[i - 1].~TElement();
219 }
220 }
221 else
222 {
223 STK_UNUSED(count);
224 }
225
226 Free(ptr);
227 }
228 }
229
230#endif // STK_MEMORY_PLACEMENT_NEW
231};
232
233} // namespace memory
234} // namespace stk
235
236#endif /* STK_MEMORY_ALLOCATOR_H_ */
Compiler and platform low-level definitions for STK.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:608
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:409
#define __stk_weak
Marks a function or variable as weak, allowing it to be overridden by the user.
Definition stk_defs.h:198
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Definition stk_defs.h:382
Namespace of STK package.
static constexpr T Min(T a, T b)
Compile-time minimum of two values.
Definition stk_defs.h:639
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:106
Memory allocator for allocating dynamic memory.
static void Free(void *ptr) __stk_weak
Free the memory chunk.
static TElement * AllocateOneT(TArgs &&...args)
Allocate a single element and construct it in-place.
static void FreeOneT(TElement *ptr)
Destroy and free a single element allocated via AllocateOne().
static const size_t CAPACITY_DEFAULT
Default memory pool capacity in bytes.
static TElement * AllocateArrayT(size_t count, TArgs &&...args)
Allocate an array of elements and default/copy-construct each one.
static void FreeArrayT(TElement ptr[], size_t count)
Destroy and free an array allocated via AllocateT().
static void * Allocate(size_t size) __stk_weak
Allocate the memory chunk.
static Stats GetStats() __stk_weak
Get stats of memory allocation.
volatile size_t allocated
Number of bytes currently allocated (dynamic value).
volatile size_t allocate_count
Number of succesful allocations with Allocate().
Stats(size_t _capacity=CAPACITY_DEFAULT)
size_t GetAvailable() const
Get number of bytes currently available for allocation.
void RecordAllocate(size_t size)
Record sucessful allocation.
const size_t capacity
Total capacity of the memory pool in bytes.
volatile size_t free_count
Number of succesful frees with Free().
volatile size_t min_ever_free
Minimum free bytes recorded since system start (watermark).
size_t GetCapacity() const
Get total capacity of the memory pool.
void RecordFree(size_t size)
Record sucessful deallocation.