diff --git a/source/lib/allocators/DynamicArena.h b/source/lib/allocators/DynamicArena.h index 5107f2dfe8..6d8a149250 100644 --- a/source/lib/allocators/DynamicArena.h +++ b/source/lib/allocators/DynamicArena.h @@ -25,8 +25,8 @@ #include "lib/bits.h" -#include // bad_alloc #include // unique_ptr +#include // bad_alloc #include namespace Allocators { @@ -43,23 +43,25 @@ template class DynamicArena { protected: - struct Block + class Block { + public: bool Available(size_t n, size_t alignment) const { - return n + ROUND_UP(size, alignment) <= BLOCK_SIZE; + return n + ROUND_UP(m_Size, alignment) <= BLOCK_SIZE; } uint8_t* Allocate(size_t n, size_t alignment) { - size = ROUND_UP(size, alignment); - uint8_t* ptr = &block[size]; - size += n; + m_Size = ROUND_UP(m_Size, alignment); + uint8_t* ptr = &m_Data[m_Size]; + m_Size += n; return ptr; } - alignas(8) uint8_t block[BLOCK_SIZE]; - size_t size = 0; + private: + size_t m_Size = 0; + std::unique_ptr m_Data = std::make_unique(BLOCK_SIZE); }; NONCOPYABLE(DynamicArena); @@ -72,8 +74,7 @@ public: void AllocateNewBlock() { - m_Blocks.emplace_back(std::make_unique()); - m_NewestBlock = m_Blocks.back().get(); + m_Blocks.emplace_back(); } void* allocate(size_t n, const void*, size_t alignment) @@ -88,10 +89,10 @@ public: throw std::bad_alloc(); } - if (!m_NewestBlock->Available(n, alignment)) + if (!m_Blocks.back().Available(n, alignment)) AllocateNewBlock(); - return reinterpret_cast(m_NewestBlock->Allocate(n, alignment)); + return reinterpret_cast(m_Blocks.back().Allocate(n, alignment)); } void deallocate(void* UNUSED(p), size_t UNUSED(n)) @@ -101,14 +102,12 @@ public: void clear() { - m_NewestBlock = nullptr; m_Blocks.clear(); AllocateNewBlock(); } protected: - std::vector> m_Blocks; - Block* m_NewestBlock = nullptr; + std::vector m_Blocks; }; } // namespace Allocators