Diligent Engine API Reference
STDAllocator.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
28 #include <limits>
29 
30 #include "BasicTypes.h"
31 #include "MemoryAllocator.h"
32 #include "DebugUtilities.h"
33 
34 namespace Diligent
35 {
36 
37 template <typename T, typename AllocatorType>
38 struct STDAllocator
39 {
40  typedef T value_type;
41  typedef value_type* pointer;
42  typedef const value_type* const_pointer;
43  typedef value_type& reference;
44  typedef const value_type& const_reference;
45  typedef std::size_t size_type;
46  typedef std::ptrdiff_t difference_type;
47 
48  STDAllocator(AllocatorType& Allocator, const Char* dbgDescription, const Char* dbgFileName, const Int32 dbgLineNumber)noexcept :
49  m_Allocator(Allocator),
50  m_dbgDescription(dbgDescription),
51  m_dbgFileName(dbgFileName),
52  m_dbgLineNumber(dbgLineNumber)
53  {
54  }
55 
56  template <class U>
57  STDAllocator(const STDAllocator<U, AllocatorType>& other)noexcept :
58  m_Allocator(other.m_Allocator),
59  m_dbgDescription(other.m_dbgDescription),
60  m_dbgFileName(other.m_dbgFileName),
61  m_dbgLineNumber(other.m_dbgLineNumber)
62  {
63  }
64 
65  template <class U>
66  STDAllocator(STDAllocator<U, AllocatorType>&& other)noexcept :
67  m_Allocator(other.m_Allocator),
68  m_dbgDescription(other.m_dbgDescription),
69  m_dbgFileName(other.m_dbgFileName),
70  m_dbgLineNumber(other.m_dbgLineNumber)
71  {
72  }
73 
74  template <class U>
75  STDAllocator& operator = (STDAllocator<U, AllocatorType>&& other)noexcept
76  {
77  // Android build requires this operator to be defined - I have no idea why
78  VERIFY_EXPR(&m_Allocator == &other.m_Allocator);
79  m_dbgDescription = other.m_dbgDescription;
80  m_dbgFileName = other.m_dbgFileName;
81  m_dbgLineNumber = other.m_dbgLineNumber;
82  return *this;
83  }
84 
85  template< class U > struct rebind
86  {
87  typedef STDAllocator<U, AllocatorType> other;
88  };
89 
90  T* allocate(std::size_t count)
91  {
92  return reinterpret_cast<T*>( m_Allocator.Allocate(count * sizeof(T), m_dbgDescription, m_dbgFileName, m_dbgLineNumber ) );
93  }
94 
95  void deallocate(T* p, std::size_t count)
96  {
97  m_Allocator.Free(p);
98  }
99 
100  inline size_type max_size() const
101  {
102  return std::numeric_limits<size_type>::max() / sizeof(T);
103  }
104 
105  // construction/destruction
106  template< class U, class... Args >
107  void construct( U* p, Args&&... args )
108  {
109  ::new(p) U(std::forward<Args>(args)...);
110  }
111 
112  inline void destroy(pointer p)
113  {
114  p->~T();
115  }
116 
117  AllocatorType &m_Allocator;
118  const Char* m_dbgDescription;
119  const Char* m_dbgFileName;
120  Int32 m_dbgLineNumber;
121 };
122 
123 #define STD_ALLOCATOR(Type, AllocatorType, Allocator, Description) STDAllocator<Type, AllocatorType>(Allocator, Description, __FILE__, __LINE__)
124 
125 template <class T, class U, class A>
126 bool operator==(const STDAllocator<T, A>&left, const STDAllocator<U, A>&right)
127 {
128  return &left.m_Allocator == &right.m_Allocator;
129 }
130 
131 template <class T, class U, class A>
132 bool operator!=(const STDAllocator<T, A> &left, const STDAllocator<U, A> &right)
133 {
134  return !(left == right);
135 }
136 
137 template<class T> using STDAllocatorRawMem = STDAllocator<T, IMemoryAllocator>;
138 #define STD_ALLOCATOR_RAW_MEM(Type, Allocator, Description) STDAllocatorRawMem<Type>(Allocator, Description, __FILE__, __LINE__)
139 
140 template< class T, typename AllocatorType >
141 struct STDDeleter
142 {
143  STDDeleter(AllocatorType &Allocator) :
144  m_Allocator(Allocator)
145  {}
146 
147  void operator()(T *ptr)
148  {
149  ptr->~T();
150  m_Allocator.Free(ptr);
151  }
152 
153  AllocatorType &m_Allocator;
154 };
155 template<class T> using STDDeleterRawMem = STDDeleter<T, IMemoryAllocator>;
156 
157 }
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34