Go to the documentation of this file.
35 #include "../../Primitives/interface/BasicTypes.h"
36 #include "../../Primitives/interface/MemoryAllocator.h"
37 #include "../../Platforms/Basic/interface/DebugUtilities.hpp"
56 m_pAllocator{&Allocator},
57 m_BlockSize{BlockSize}
69 for (
auto& block : m_Blocks)
71 m_pAllocator->
Free(block.Data);
75 m_pAllocator =
nullptr;
80 for (
auto& block : m_Blocks)
82 block.CurrPtr = block.Data;
86 NODISCARD
void*
Allocate(
size_t size,
size_t align)
91 for (
auto& block : m_Blocks)
93 auto* Ptr =
AlignUp(block.CurrPtr, align);
94 if (Ptr + size <= block.Data + block.Size)
96 block.CurrPtr = Ptr + size;
102 size_t BlockSize = m_BlockSize;
103 while (BlockSize < size + align - 1)
105 m_Blocks.emplace_back(m_pAllocator->
Allocate(BlockSize,
"dynamic linear allocator page", __FILE__, __LINE__), BlockSize);
107 auto& block = m_Blocks.back();
108 auto* Ptr =
AlignUp(block.Data, align);
109 VERIFY(Ptr + size <= block.Data + block.Size,
"Not enough space in the new block - this is a bug");
110 block.CurrPtr = Ptr + size;
114 template <
typename T>
117 return reinterpret_cast<T*
>(
Allocate(
sizeof(T) * count,
alignof(T)));
120 template <
typename T,
typename... Args>
123 T* Ptr = Allocate<T>(1);
124 new (Ptr) T{std::forward<Args>(args)...};
128 template <
typename T,
typename... Args>
131 T* Ptr = Allocate<T>(count);
132 for (
size_t i = 0; i < count; ++i)
134 new (Ptr + i) T{args...};
139 template <
typename T>
142 T* Dst = Allocate<T>(count);
143 for (
size_t i = 0; i < count; ++i)
145 new (Dst + i) T{Src[i]};
160 Char* Dst = Allocate<Char>(len + 1);
161 std::memcpy(Dst, Str,
sizeof(
Char) * len);
176 auto* Dst = Allocate<wchar_t>(len + 1);
177 for (
size_t i = 0; i < len; ++i)
179 Dst[i] =
static_cast<wchar_t>(Str[i]);
198 uint8_t*
const Data =
nullptr;
199 size_t const Size = 0;
200 uint8_t* CurrPtr =
nullptr;
202 Block(
void* _Data,
size_t _Size) :
203 Data{
static_cast<uint8_t*
>(_Data)}, Size{_Size}, CurrPtr{Data} {}
206 std::vector<Block> m_Blocks;
207 const Uint32 m_BlockSize = 4 << 10;
208 IMemoryAllocator* m_pAllocator =
nullptr;
Implementation of a linear allocator on fixed memory pages.
Definition: DynamicLinearAllocator.hpp:45
char Char
Definition: BasicTypes.h:64
NODISCARD void * Allocate(size_t size, size_t align)
Definition: DynamicLinearAllocator.hpp:86
NODISCARD wchar_t * CopyWString(const char *Str, size_t len=0)
Definition: DynamicLinearAllocator.hpp:166
NODISCARD Char * CopyString(const String &Str)
Definition: DynamicLinearAllocator.hpp:185
void Free()
Definition: DynamicLinearAllocator.hpp:67
DynamicLinearAllocator & operator=(const DynamicLinearAllocator &)=delete
DynamicLinearAllocator(IMemoryAllocator &Allocator, Uint32 BlockSize=4<< 10)
Definition: DynamicLinearAllocator.hpp:55
NODISCARD T * Allocate(size_t count=1)
Definition: DynamicLinearAllocator.hpp:115
NODISCARD T * Construct(Args &&... args)
Definition: DynamicLinearAllocator.hpp:121
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
void Discard()
Definition: DynamicLinearAllocator.hpp:78
NODISCARD Char * CopyString(const Char *Str, size_t len=0)
Definition: DynamicLinearAllocator.hpp:150
bool IsPowerOfTwo(T val)
Definition: Align.hpp:41
DynamicLinearAllocator(const DynamicLinearAllocator &)=delete
NODISCARD T * ConstructArray(size_t count, const Args &... args)
Definition: DynamicLinearAllocator.hpp:129
~DynamicLinearAllocator()
Definition: DynamicLinearAllocator.hpp:62
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
NODISCARD T * CopyArray(const T *Src, size_t count)
Definition: DynamicLinearAllocator.hpp:140
std::basic_string< Char > String
String variable.
Definition: BasicTypes.h:66
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
NODISCARD wchar_t * CopyWString(const String &Str)
Definition: DynamicLinearAllocator.hpp:190
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
T2 ::type AlignUp(T1 val, T2 alignment)
Definition: Align.hpp:47
virtual void Free(void *Ptr)=0
Releases memory.
virtual void * Allocate(size_t Size, const Char *dbgDescription, const char *dbgFileName, const Int32 dbgLineNumber)=0
Allocates block of memory.
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37