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::MemoryAllocator Class Reference

Memory allocator for allocating dynamic memory. More...

#include <stk_memory_allocator.h>

Classes

class  Stats
 Snapshot of the memory allocator's statistics. More...

Static Public Member Functions

static void * Allocate (size_t size) __stk_weak
 Allocate the memory chunk.
static void Free (void *ptr) __stk_weak
 Free the memory chunk.
static Stats GetStats () __stk_weak
 Get stats of memory allocation.
template<typename TElement, typename... TArgs>
static TElement * AllocateOneT (TArgs &&...args)
 Allocate a single element and construct it in-place.
template<typename TElement, typename... TArgs>
static TElement * AllocateArrayT (size_t count, TArgs &&...args)
 Allocate an array of elements and default/copy-construct each one.
template<typename TElement>
static void FreeOneT (TElement *ptr)
 Destroy and free a single element allocated via AllocateOne().
template<typename TElement>
static void FreeArrayT (TElement *ptr, size_t count)
 Destroy and free an array allocated via AllocateT().

Static Public Attributes

static const size_t CAPACITY_DEFAULT = 12288U
 Default memory pool capacity in bytes.

Detailed Description

Memory allocator for allocating dynamic memory.

Note
STK does not use dynamic memory allocation but some auxiliary classes may provide such functionality for convenience. In this case you must provide your own implementation of MemoryAllocator::Allocate() and MemoryAllocator::Free(). By default STK does not provide any implementation, therefore you will get a linker error if these two functions are left unimplemented.

Example of your trivial implementation:

void *MemoryAllocator::Allocate(size_t size)
{
return malloc(size);
}
void MemoryAllocator::Free(void *ptr)
{
free(ptr);
}
void * malloc(size_t size)
void free(void *ptr)
static void Free(void *ptr) __stk_weak
Free the memory chunk.
static void * Allocate(size_t size) __stk_weak
Allocate the memory chunk.

Definition at line 55 of file stk_memory_allocator.h.

Member Function Documentation

◆ Allocate()

void * stk::memory::MemoryAllocator::Allocate ( size_t size)
static

Allocate the memory chunk.

Parameters
[in]sizeSize of the memory chunk.
Returns
Pointer to the allocated memory chunk, nullptr if allocator failed to allocate it.

Definition at line 44 of file stk_c_memory.cpp.

44{ return malloc(size); }

References malloc().

Referenced by AllocateArrayT(), AllocateOneT(), and pvPortMalloc().

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

◆ AllocateArrayT()

template<typename TElement, typename... TArgs>
TElement * stk::memory::MemoryAllocator::AllocateArrayT ( size_t count,
TArgs &&... args )
inlinestatic

Allocate an array of elements and default/copy-construct each one.

Parameters
[in]countNumber of elements to allocate.
[in]argsConstructor arguments forwarded to every element.
Returns
Pointer to the first element, nullptr if allocation failed or count is 0.
Note
Pair with FreeArrayT<TElement>() passing the same count to properly invoke destructors.

Definition at line 158 of file stk_memory_allocator.h.

159 {
160 TElement *ptr = nullptr;
161
162 if (count != 0U)
163 {
164 STK_ASSERT(Allocate != nullptr);
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 }
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:610
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:411
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Definition stk_defs.h:384

References __stk_constexpr_cpp17, Allocate(), STK_ASSERT, and STK_UNUSED.

Here is the call graph for this function:

◆ AllocateOneT()

template<typename TElement, typename... TArgs>
TElement * stk::memory::MemoryAllocator::AllocateOneT ( TArgs &&... args)
inlinestatic

Allocate a single element and construct it in-place.

Parameters
[in]argsConstructor arguments forwarded to TElement.
Returns
Pointer to the constructed element, nullptr if allocation failed.
Note
Pair with FreeOneT<TElement>() to properly invoke the destructor.

Definition at line 134 of file stk_memory_allocator.h.

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 }

References __stk_constexpr_cpp17, Allocate(), STK_ASSERT, and STK_UNUSED.

Here is the call graph for this function:

◆ Free()

void stk::memory::MemoryAllocator::Free ( void * ptr)
static

Free the memory chunk.

Parameters
[in]ptrPointer to the memory chunk. nullptr is allowed and results in noop.

Definition at line 45 of file stk_c_memory.cpp.

45{ free(ptr); }

References free().

Referenced by FreeArrayT(), FreeOneT(), and vPortFree().

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

◆ FreeArrayT()

template<typename TElement>
void stk::memory::MemoryAllocator::FreeArrayT ( TElement * ptr,
size_t count )
inlinestatic

Destroy and free an array allocated via AllocateT().

Parameters
[in]ptrPointer to the first element. nullptr is allowed and results in noop.
[in]countNumber of elements (must match the count passed to AllocateT()).

Definition at line 207 of file stk_memory_allocator.h.

208 {
209 STK_ASSERT(Free != nullptr);
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 }

References __stk_constexpr_cpp17, Free(), STK_ASSERT, and STK_UNUSED.

Referenced by stk::memory::BlockMemoryPool::~BlockMemoryPool().

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

◆ FreeOneT()

template<typename TElement>
void stk::memory::MemoryAllocator::FreeOneT ( TElement * ptr)
inlinestatic

Destroy and free a single element allocated via AllocateOne().

Parameters
[in]ptrPointer to the element. nullptr is allowed and results in noop.

Definition at line 187 of file stk_memory_allocator.h.

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 }

References __stk_constexpr_cpp17, Free(), and STK_ASSERT.

Here is the call graph for this function:

◆ GetStats()

stk::memory::MemoryAllocator::Stats stk::memory::MemoryAllocator::GetStats ( )
static

Get stats of memory allocation.

Returns
Stats structure.

Definition at line 107 of file freertos_stk.cpp.

108{
109 return s_MemStats;
110}
static stk::memory::MemoryAllocator::Stats s_MemStats(10240U)

References s_MemStats.

Member Data Documentation

◆ CAPACITY_DEFAULT

const size_t stk::memory::MemoryAllocator::CAPACITY_DEFAULT = 12288U
static

Default memory pool capacity in bytes.

See also
Stats

Definition at line 60 of file stk_memory_allocator.h.

Referenced by stk::memory::MemoryAllocator::Stats::Stats().


The documentation for this class was generated from the following files: