Diligent Engine  v.2.4.g
DynamicLinearAllocator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include <vector>
34 
35 #include "../../Primitives/interface/BasicTypes.h"
36 #include "../../Primitives/interface/MemoryAllocator.h"
37 #include "../../Platforms/Basic/interface/DebugUtilities.hpp"
38 #include "CompilerDefinitions.h"
39 #include "Align.hpp"
40 
41 namespace Diligent
42 {
43 
46 {
47 public:
48  // clang-format off
53  // clang-format on
54 
55  explicit DynamicLinearAllocator(IMemoryAllocator& Allocator, Uint32 BlockSize = 4 << 10) :
56  m_pAllocator{&Allocator},
57  m_BlockSize{BlockSize}
58  {
59  VERIFY(IsPowerOfTwo(BlockSize), "Block size (", BlockSize, ") is not power of two");
60  }
61 
63  {
64  Free();
65  }
66 
67  void Free()
68  {
69  for (auto& block : m_Blocks)
70  {
71  m_pAllocator->Free(block.Data);
72  }
73  m_Blocks.clear();
74 
75  m_pAllocator = nullptr;
76  }
77 
78  void Discard()
79  {
80  for (auto& block : m_Blocks)
81  {
82  block.CurrPtr = block.Data;
83  }
84  }
85 
86  NODISCARD void* Allocate(size_t size, size_t align)
87  {
88  if (size == 0)
89  return nullptr;
90 
91  for (auto& block : m_Blocks)
92  {
93  auto* Ptr = AlignUp(block.CurrPtr, align);
94  if (Ptr + size <= block.Data + block.Size)
95  {
96  block.CurrPtr = Ptr + size;
97  return Ptr;
98  }
99  }
100 
101  // Create a new block
102  size_t BlockSize = m_BlockSize;
103  while (BlockSize < size + align - 1)
104  BlockSize *= 2;
105  m_Blocks.emplace_back(m_pAllocator->Allocate(BlockSize, "dynamic linear allocator page", __FILE__, __LINE__), BlockSize);
106 
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;
111  return Ptr;
112  }
113 
114  template <typename T>
115  NODISCARD T* Allocate(size_t count = 1)
116  {
117  return reinterpret_cast<T*>(Allocate(sizeof(T) * count, alignof(T)));
118  }
119 
120  template <typename T, typename... Args>
121  NODISCARD T* Construct(Args&&... args)
122  {
123  T* Ptr = Allocate<T>(1);
124  new (Ptr) T{std::forward<Args>(args)...};
125  return Ptr;
126  }
127 
128  template <typename T, typename... Args>
129  NODISCARD T* ConstructArray(size_t count, const Args&... args)
130  {
131  T* Ptr = Allocate<T>(count);
132  for (size_t i = 0; i < count; ++i)
133  {
134  new (Ptr + i) T{args...};
135  }
136  return Ptr;
137  }
138 
139  template <typename T>
140  NODISCARD T* CopyArray(const T* Src, size_t count)
141  {
142  T* Dst = Allocate<T>(count);
143  for (size_t i = 0; i < count; ++i)
144  {
145  new (Dst + i) T{Src[i]};
146  }
147  return Dst;
148  }
149 
150  NODISCARD Char* CopyString(const Char* Str, size_t len = 0)
151  {
152  if (Str == nullptr)
153  return nullptr;
154 
155  if (len == 0)
156  len = strlen(Str);
157  else
158  VERIFY_EXPR(len <= strlen(Str));
159 
160  Char* Dst = Allocate<Char>(len + 1);
161  std::memcpy(Dst, Str, sizeof(Char) * len);
162  Dst[len] = 0;
163  return Dst;
164  }
165 
166  NODISCARD wchar_t* CopyWString(const char* Str, size_t len = 0)
167  {
168  if (Str == nullptr)
169  return nullptr;
170 
171  if (len == 0)
172  len = strlen(Str);
173  else
174  VERIFY_EXPR(len <= strlen(Str));
175 
176  auto* Dst = Allocate<wchar_t>(len + 1);
177  for (size_t i = 0; i < len; ++i)
178  {
179  Dst[i] = static_cast<wchar_t>(Str[i]);
180  }
181  Dst[len] = 0;
182  return Dst;
183  }
184 
185  NODISCARD Char* CopyString(const String& Str)
186  {
187  return CopyString(Str.c_str(), Str.length());
188  }
189 
190  NODISCARD wchar_t* CopyWString(const String& Str)
191  {
192  return CopyWString(Str.c_str(), Str.length());
193  }
194 
195 private:
196  struct Block
197  {
198  uint8_t* const Data = nullptr;
199  size_t const Size = 0;
200  uint8_t* CurrPtr = nullptr;
201 
202  Block(void* _Data, size_t _Size) :
203  Data{static_cast<uint8_t*>(_Data)}, Size{_Size}, CurrPtr{Data} {}
204  };
205 
206  std::vector<Block> m_Blocks;
207  const Uint32 m_BlockSize = 4 << 10;
208  IMemoryAllocator* m_pAllocator = nullptr;
209 };
210 
211 } // namespace Diligent
Diligent::DynamicLinearAllocator
Implementation of a linear allocator on fixed memory pages.
Definition: DynamicLinearAllocator.hpp:45
Diligent::Char
char Char
Definition: BasicTypes.h:64
Align.hpp
Diligent::DynamicLinearAllocator::Allocate
NODISCARD void * Allocate(size_t size, size_t align)
Definition: DynamicLinearAllocator.hpp:86
Diligent::DynamicLinearAllocator::CopyWString
NODISCARD wchar_t * CopyWString(const char *Str, size_t len=0)
Definition: DynamicLinearAllocator.hpp:166
CompilerDefinitions.h
Diligent::DynamicLinearAllocator::CopyString
NODISCARD Char * CopyString(const String &Str)
Definition: DynamicLinearAllocator.hpp:185
Diligent::DynamicLinearAllocator::Free
void Free()
Definition: DynamicLinearAllocator.hpp:67
Diligent::DynamicLinearAllocator::operator=
DynamicLinearAllocator & operator=(const DynamicLinearAllocator &)=delete
Diligent::DynamicLinearAllocator::DynamicLinearAllocator
DynamicLinearAllocator(IMemoryAllocator &Allocator, Uint32 BlockSize=4<< 10)
Definition: DynamicLinearAllocator.hpp:55
Diligent::DynamicLinearAllocator::Allocate
NODISCARD T * Allocate(size_t count=1)
Definition: DynamicLinearAllocator.hpp:115
Diligent::DynamicLinearAllocator::Construct
NODISCARD T * Construct(Args &&... args)
Definition: DynamicLinearAllocator.hpp:121
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::DynamicLinearAllocator::Discard
void Discard()
Definition: DynamicLinearAllocator.hpp:78
Diligent::DynamicLinearAllocator::CopyString
NODISCARD Char * CopyString(const Char *Str, size_t len=0)
Definition: DynamicLinearAllocator.hpp:150
Diligent::IsPowerOfTwo
bool IsPowerOfTwo(T val)
Definition: Align.hpp:41
Diligent::DynamicLinearAllocator::DynamicLinearAllocator
DynamicLinearAllocator(const DynamicLinearAllocator &)=delete
Diligent::DynamicLinearAllocator::ConstructArray
NODISCARD T * ConstructArray(size_t count, const Args &... args)
Definition: DynamicLinearAllocator.hpp:129
Diligent::DynamicLinearAllocator::~DynamicLinearAllocator
~DynamicLinearAllocator()
Definition: DynamicLinearAllocator.hpp:62
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
Diligent::DynamicLinearAllocator::CopyArray
NODISCARD T * CopyArray(const T *Src, size_t count)
Definition: DynamicLinearAllocator.hpp:140
Diligent::String
std::basic_string< Char > String
String variable.
Definition: BasicTypes.h:66
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
Diligent::DynamicLinearAllocator::CopyWString
NODISCARD wchar_t * CopyWString(const String &Str)
Definition: DynamicLinearAllocator.hpp:190
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::AlignUp
T2 ::type AlignUp(T1 val, T2 alignment)
Definition: Align.hpp:47
Diligent::IMemoryAllocator::Free
virtual void Free(void *Ptr)=0
Releases memory.
Diligent::IMemoryAllocator::Allocate
virtual void * Allocate(size_t Size, const Char *dbgDescription, const char *dbgFileName, const Int32 dbgLineNumber)=0
Allocates block of memory.
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37