2#include "gaia/config/config.h"
6#include "gaia/mem/raw_data_holder.h"
18 void (*dtor)(
void*, uint32_t);
24 GAIA_MSVC_WARNING_PUSH()
25 GAIA_MSVC_WARNING_DISABLE(4324)
29 template <uint32_t CapacityInBytes = 1024>
38 uint32_t m_posPrev = 0;
40 uint32_t m_allocs = 0;
45 const auto bufferMemAddr = (uintptr_t)((uint8_t*)m_buffer);
46 m_posPrev = m_pos = padding<alignof(alloc_info)>(bufferMemAddr);
63 GAIA_NODISCARD T*
alloc(uint32_t cnt) {
64 constexpr auto sizeT = (uint32_t)
sizeof(T);
65 const auto addrBuff = (uintptr_t)((uint8_t*)m_buffer);
66 const auto addrAllocInfo = align<alignof(alloc_info)>(addrBuff + m_pos);
67 const auto addrAllocData = align<alignof(T)>(addrAllocInfo +
sizeof(
alloc_info));
68 const auto off = (uint32_t)(addrAllocData - addrAllocInfo);
71 const bool isFull = (uint32_t)(addrAllocData - addrBuff) + sizeT * cnt >= CapacityInBytes;
72 if GAIA_UNLIKELY (isFull) {
73 GAIA_ASSERT(!isFull &&
"Allocation space exceeded on StackAllocator");
79 pInfo->prev = m_posPrev;
82 pInfo->dtor = [](
void* ptr, uint32_t cnt) {
83 core::call_dtor_n((T*)ptr, cnt);
87 auto* pData = (T*)addrAllocData;
88 core::call_ctor_raw_n(pData, cnt);
91 m_posPrev = (uint32_t)(addrAllocInfo - addrBuff);
93 m_pos = m_posPrev + pInfo->off + sizeT * cnt;
102 void free([[maybe_unused]]
void* pData, [[maybe_unused]] uint32_t cnt) {
103 GAIA_ASSERT(pData !=
nullptr);
104 GAIA_ASSERT(cnt > 0);
105 GAIA_ASSERT(m_allocs > 0);
107 const auto addrBuff = (uintptr_t)((uint8_t*)m_buffer);
110 const auto addrAllocInfo = addrBuff + m_posPrev;
112 const auto addrAllocData = addrAllocInfo + pInfo->off;
113 void* pInfoData = (
void*)addrAllocData;
114 GAIA_ASSERT(pData == pInfoData);
115 GAIA_ASSERT(pInfo->cnt == cnt);
116 pInfo->dtor(pInfoData, pInfo->cnt);
119 m_posPrev = pInfo->prev;
125 const auto addrBuff = (uintptr_t)((uint8_t*)m_buffer);
128 auto pos = m_posPrev;
129 while (m_allocs > 0) {
130 const auto addrAllocInfo = addrBuff + pos;
132 const auto addrAllocData = addrAllocInfo + pInfo->off;
133 pInfo->dtor((
void*)addrAllocData, pInfo->cnt);
139 GAIA_ASSERT(m_allocs == 0);
146 GAIA_NODISCARD
constexpr uint32_t capacity() {
147 return CapacityInBytes;
151 GAIA_MSVC_WARNING_POP()
Stack allocator capable of instantiating any default-constructible object on stack....
Definition stack_allocator.h:30
void reset()
Frees all allocated objects from the buffer.
Definition stack_allocator.h:124
GAIA_NODISCARD T * alloc(uint32_t cnt)
Allocates.
Definition stack_allocator.h:63
void free(void *pData, uint32_t cnt)
Frees the last allocated object from the stack.
Definition stack_allocator.h:102
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition stack_allocator.h:11
uint32_t cnt
The number of requested bytes to allocate.
Definition stack_allocator.h:17
uint32_t off
Offset of data area from info area in bytes.
Definition stack_allocator.h:15
uint32_t prev
Byte offset of the previous allocation.
Definition stack_allocator.h:13
Definition raw_data_holder.h:12