Diligent Engine  v.2.4.g
STDAllocator.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 #include <limits>
33 
34 #include "../../Primitives/interface/BasicTypes.h"
35 #include "../../Primitives/interface/MemoryAllocator.h"
36 #include "../../Platforms/Basic/interface/DebugUtilities.hpp"
37 
38 namespace Diligent
39 {
40 
41 template <typename T>
42 typename std::enable_if<std::is_destructible<T>::value, void>::type Destruct(T* ptr)
43 {
44  ptr->~T();
45 }
46 
47 template <typename T>
48 typename std::enable_if<!std::is_destructible<T>::value, void>::type Destruct(T* ptr)
49 {
50 }
51 
52 template <typename T, typename AllocatorType>
54 {
55  using value_type = T;
56  using pointer = value_type*;
57  using const_pointer = const value_type*;
59  using const_reference = const value_type&;
60  using size_type = std::size_t;
61  using difference_type = std::ptrdiff_t;
62 
63  STDAllocator(AllocatorType& Allocator, const Char* Description, const Char* FileName, const Int32 LineNumber) noexcept :
64  // clang-format off
65  m_Allocator {Allocator}
66 #ifdef DILIGENT_DEVELOPMENT
67  , m_dvpDescription{Description}
68  , m_dvpFileName {FileName }
69  , m_dvpLineNumber {LineNumber }
70 #endif
71  // clang-format on
72  {
73  }
74 
75  template <class U>
77  // clang-format off
78  m_Allocator {other.m_Allocator}
79 #ifdef DILIGENT_DEVELOPMENT
80  , m_dvpDescription{other.m_dvpDescription}
81  , m_dvpFileName {other.m_dvpFileName }
82  , m_dvpLineNumber {other.m_dvpLineNumber }
83 #endif
84  // clang-format on
85  {
86  }
87 
88  template <class U>
90  // clang-format off
91  m_Allocator {other.m_Allocator}
92 #ifdef DILIGENT_DEVELOPMENT
93  , m_dvpDescription{other.m_dvpDescription}
94  , m_dvpFileName {other.m_dvpFileName }
95  , m_dvpLineNumber {other.m_dvpLineNumber }
96 #endif
97  // clang-format on
98  {
99  }
100 
101  template <class U>
103  {
104  // Android build requires this operator to be defined - I have no idea why.
105  // There is no default constructor to create null allocator, so all fields must be
106  // initialized.
107  DEV_CHECK_ERR(&m_Allocator == &other.m_Allocator, "Inconsistent allocators");
108 #ifdef DILIGENT_DEVELOPMENT
109  DEV_CHECK_ERR(m_dvpDescription == other.m_dvpDescription, "Incosistent allocator descriptions");
110  DEV_CHECK_ERR(m_dvpFileName == other.m_dvpFileName, "Incosistent allocator file names");
111  DEV_CHECK_ERR(m_dvpLineNumber == other.m_dvpLineNumber, "Incosistent allocator line numbers");
112 #endif
113  return *this;
114  }
115 
116  template <class U> struct rebind
117  {
119  };
120 
121  T* allocate(std::size_t count)
122  {
123 #ifndef DILIGENT_DEVELOPMENT
124  static constexpr const char* m_dvpDescription = "<Unavailable in release build>";
125  static constexpr const char* m_dvpFileName = "<Unavailable in release build>";
126  static constexpr Int32 m_dvpLineNumber = -1;
127 #endif
128  return reinterpret_cast<T*>(m_Allocator.Allocate(count * sizeof(T), m_dvpDescription, m_dvpFileName, m_dvpLineNumber));
129  }
130 
131  pointer address(reference r) { return &r; }
133 
134  void deallocate(T* p, std::size_t count)
135  {
136  m_Allocator.Free(p);
137  }
138 
139  inline size_type max_size() const
140  {
141  return std::numeric_limits<size_type>::max() / sizeof(T);
142  }
143 
144  // construction/destruction
145  template <class U, class... Args>
146  void construct(U* p, Args&&... args)
147  {
148  ::new (p) U(std::forward<Args>(args)...);
149  }
150 
151  inline void destroy(pointer p)
152  {
153  p->~T();
154  }
155 
156  AllocatorType& m_Allocator;
157 #ifdef DILIGENT_DEVELOPMENT
158  const Char* const m_dvpDescription;
159  const Char* const m_dvpFileName;
160  Int32 const m_dvpLineNumber;
161 #endif
162 };
163 
164 #define STD_ALLOCATOR(Type, AllocatorType, Allocator, Description) STDAllocator<Type, AllocatorType>(Allocator, Description, __FILE__, __LINE__)
165 
166 template <class T, class U, class A>
167 bool operator==(const STDAllocator<T, A>& left, const STDAllocator<U, A>& right)
168 {
169  return &left.m_Allocator == &right.m_Allocator;
170 }
171 
172 template <class T, class U, class A>
173 bool operator!=(const STDAllocator<T, A>& left, const STDAllocator<U, A>& right)
174 {
175  return !(left == right);
176 }
177 
179 #define STD_ALLOCATOR_RAW_MEM(Type, Allocator, Description) STDAllocatorRawMem<Type>(Allocator, Description, __FILE__, __LINE__)
180 
181 template <class T, typename AllocatorType>
183 {
184  STDDeleter() noexcept {}
185 
186  STDDeleter(AllocatorType& Allocator) noexcept :
187  m_Allocator{&Allocator}
188  {}
189 
190  STDDeleter(const STDDeleter&) = default;
191  STDDeleter& operator=(const STDDeleter&) = default;
192 
193  STDDeleter(STDDeleter&& rhs) noexcept :
194  m_Allocator{rhs.m_Allocator}
195  {
196  rhs.m_Allocator = nullptr;
197  }
198 
199  STDDeleter& operator=(STDDeleter&& rhs) noexcept
200  {
201  m_Allocator = rhs.m_Allocator;
202  rhs.m_Allocator = nullptr;
203  return *this;
204  }
205 
206  void operator()(T* ptr) noexcept
207  {
208  VERIFY(m_Allocator != nullptr, "The deleter has been moved away or never initialized, and can't be used");
209  Destruct(ptr);
210  m_Allocator->Free(ptr);
211  }
212 
213 private:
214  AllocatorType* m_Allocator = nullptr;
215 };
217 
218 } // namespace Diligent
Diligent::Char
char Char
Definition: BasicTypes.h:64
Diligent::STDAllocator::address
pointer address(reference r)
Definition: STDAllocator.hpp:131
Diligent::STDAllocator::rebind::other
STDAllocator< U, AllocatorType > other
Definition: STDAllocator.hpp:118
Diligent::STDAllocator::const_pointer
const value_type * const_pointer
Definition: STDAllocator.hpp:57
Diligent::STDAllocator::max_size
size_type max_size() const
Definition: STDAllocator.hpp:139
Diligent::STDAllocator::const_reference
const value_type & const_reference
Definition: STDAllocator.hpp:59
Diligent::STDAllocator::m_Allocator
AllocatorType & m_Allocator
Definition: STDAllocator.hpp:156
Diligent::operator==
bool operator==(const Plane3D &p1, const Plane3D &p2)
Definition: AdvancedMath.hpp:442
DEV_CHECK_ERR
#define DEV_CHECK_ERR(...)
Definition: DebugUtilities.hpp:90
Diligent::STDDeleter::operator()
void operator()(T *ptr) noexcept
Definition: STDAllocator.hpp:206
Diligent::STDAllocator::STDAllocator
STDAllocator(STDAllocator< U, AllocatorType > &&other) noexcept
Definition: STDAllocator.hpp:89
Diligent::max
Vector3< T > max(const Vector3< T > &a, const Vector3< T > &b)
Definition: BasicMath.hpp:1660
Diligent::STDAllocator::operator=
STDAllocator & operator=(STDAllocator< U, AllocatorType > &&other) noexcept
Definition: STDAllocator.hpp:102
Diligent::STDAllocator
Definition: STDAllocator.hpp:53
Diligent::STDAllocator::deallocate
void deallocate(T *p, std::size_t count)
Definition: STDAllocator.hpp:134
Diligent::STDDeleter::STDDeleter
STDDeleter(AllocatorType &Allocator) noexcept
Definition: STDAllocator.hpp:186
Diligent::STDAllocator::address
const_pointer address(const_reference r)
Definition: STDAllocator.hpp:132
Diligent::Int32
int32_t Int32
32-bit signed integer
Definition: BasicTypes.h:46
Diligent::STDAllocator::difference_type
std::ptrdiff_t difference_type
Definition: STDAllocator.hpp:61
Diligent::STDAllocator::size_type
std::size_t size_type
Definition: STDAllocator.hpp:60
Diligent::STDDeleter::operator=
STDDeleter & operator=(STDDeleter &&rhs) noexcept
Definition: STDAllocator.hpp:199
Diligent::STDDeleter::STDDeleter
STDDeleter() noexcept
Definition: STDAllocator.hpp:184
Diligent::STDAllocator::STDAllocator
STDAllocator(AllocatorType &Allocator, const Char *Description, const Char *FileName, const Int32 LineNumber) noexcept
Definition: STDAllocator.hpp:63
Diligent::STDDeleter::STDDeleter
STDDeleter(STDDeleter &&rhs) noexcept
Definition: STDAllocator.hpp:193
Diligent::STDAllocator::construct
void construct(U *p, Args &&... args)
Definition: STDAllocator.hpp:146
Diligent::STDDeleter
Definition: STDAllocator.hpp:182
Diligent::STDAllocator::allocate
T * allocate(std::size_t count)
Definition: STDAllocator.hpp:121
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::STDAllocator::rebind
Definition: STDAllocator.hpp:116
Diligent::STDAllocator::pointer
value_type * pointer
Definition: STDAllocator.hpp:56
Diligent::STDAllocator::value_type
T value_type
Definition: STDAllocator.hpp:55
Diligent::STDAllocator::destroy
void destroy(pointer p)
Definition: STDAllocator.hpp:151
Diligent::STDDeleter::operator=
STDDeleter & operator=(const STDDeleter &)=default
Diligent::STDAllocator::reference
value_type & reference
Definition: STDAllocator.hpp:58
Diligent::operator!=
bool operator!=(const STDAllocator< T, A > &left, const STDAllocator< U, A > &right)
Definition: STDAllocator.hpp:173
Diligent::STDAllocator::STDAllocator
STDAllocator(const STDAllocator< U, AllocatorType > &other) noexcept
Definition: STDAllocator.hpp:76
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::Destruct
std::enable_if< std::is_destructible< T >::value, void >::type Destruct(T *ptr)
Definition: STDAllocator.hpp:42