10#ifndef STK_MEMORY_ALLOCATOR_H_
11#define STK_MEMORY_ALLOCATOR_H_
19#ifndef STK_MEMORY_PLACEMENT_NEW
20 #define STK_MEMORY_PLACEMENT_NEW (1)
23#if STK_MEMORY_PLACEMENT_NEW
126#if STK_MEMORY_PLACEMENT_NEW
133 template <
typename TElement,
typename... TArgs>
138 TElement *ptr =
reinterpret_cast<TElement *
>(
Allocate(
sizeof(TElement)));
143 auto const elm =
new (ptr) TElement(
static_cast<TArgs &&
>(args)...);
157 template <
typename TElement,
typename... TArgs>
160 TElement *ptr =
nullptr;
166 ptr =
reinterpret_cast<TElement *
>(
Allocate(count *
sizeof(TElement)));
171 for (
size_t i = 0U; i < count; ++i)
173 auto const elm =
new (&ptr[i]) TElement(
static_cast<TArgs &&
>(args)...);
186 template <
typename TElement>
206 template <
typename TElement>
216 for (
size_t i = count; i > 0U; --i)
218 ptr[i - 1].~TElement();
Compiler and platform low-level definitions for STK.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define __stk_weak
Marks a function or variable as weak, allowing it to be overridden by the user.
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Namespace of STK package.
static constexpr T Min(T a, T b)
Compile-time minimum of two values.
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
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.