ezEngine  Milestone 9
AllocatorBase_inl.h
1 
2 EZ_ALWAYS_INLINE ezAllocatorBase::ezAllocatorBase()
3 {
4 }
5 
6 EZ_ALWAYS_INLINE ezAllocatorBase::~ezAllocatorBase()
7 {
8 }
9 
10 namespace ezInternal
11 {
12  template <typename T>
13  struct NewInstance
14  {
15  EZ_ALWAYS_INLINE NewInstance(T* pInstance, ezAllocatorBase* pAllocator)
16  {
17  m_pInstance = pInstance;
18  m_pAllocator = pAllocator;
19  }
20 
21  template <typename U>
22  EZ_ALWAYS_INLINE NewInstance(NewInstance<U>&& other)
23  {
24  m_pInstance = other.m_pInstance;
25  m_pAllocator = other.m_pAllocator;
26 
27  other.m_pInstance = nullptr;
28  other.m_pAllocator = nullptr;
29  }
30 
31  template <typename U>
32  EZ_ALWAYS_INLINE NewInstance<U> Cast()
33  {
34  return NewInstance<U>(static_cast<U*>(m_pInstance), m_pAllocator);
35  }
36 
37  EZ_ALWAYS_INLINE operator T*()
38  {
39  return m_pInstance;
40  }
41 
42  EZ_ALWAYS_INLINE T* operator->()
43  {
44  return m_pInstance;
45  }
46 
47  T* m_pInstance;
48  ezAllocatorBase* m_pAllocator;
49  };
50 
51 
52  template <typename T>
53  EZ_FORCE_INLINE void Delete(ezAllocatorBase* pAllocator, T* ptr)
54  {
55  if (ptr != nullptr)
56  {
58  pAllocator->Deallocate(ptr);
59  }
60  }
61 
62  template <typename T>
63  EZ_FORCE_INLINE T* CreateRawBuffer(ezAllocatorBase* pAllocator, size_t uiCount)
64  {
65  return static_cast<T*>(pAllocator->Allocate(sizeof(T) * uiCount, EZ_ALIGNMENT_OF(T)));
66  }
67 
68  EZ_FORCE_INLINE void DeleteRawBuffer(ezAllocatorBase* pAllocator, void* ptr)
69  {
70  if (ptr != nullptr)
71  {
72  pAllocator->Deallocate(ptr);
73  }
74  }
75 
76  template <typename T>
77  inline ezArrayPtr<T> CreateArray(ezAllocatorBase* pAllocator, ezUInt32 uiCount)
78  {
79  T* buffer = CreateRawBuffer<T>(pAllocator, uiCount);
80  ezMemoryUtils::Construct(buffer, uiCount);
81 
82  return ezArrayPtr<T>(buffer, uiCount);
83  }
84 
85  template <typename T>
86  inline void DeleteArray(ezAllocatorBase* pAllocator, ezArrayPtr<T> arrayPtr)
87  {
88  T* buffer = arrayPtr.GetPtr();
89  if (buffer != nullptr)
90  {
91  ezMemoryUtils::Destruct(buffer, arrayPtr.GetCount());
92  pAllocator->Deallocate(buffer);
93  }
94  }
95 
96  template <typename T>
97  EZ_FORCE_INLINE T* ExtendRawBuffer(T* ptr, ezAllocatorBase* pAllocator, size_t uiCurrentCount, size_t uiNewCount, ezTypeIsPod)
98  {
99  return (T*)pAllocator->Reallocate(ptr, uiCurrentCount * sizeof(T), uiNewCount * sizeof(T), EZ_ALIGNMENT_OF(T));
100  }
101 
102  template <typename T>
103  EZ_FORCE_INLINE T* ExtendRawBuffer(T* ptr, ezAllocatorBase* pAllocator, size_t uiCurrentCount, size_t uiNewCount, ezTypeIsMemRelocatable)
104  {
105  return (T*)pAllocator->Reallocate(ptr, uiCurrentCount * sizeof(T), uiNewCount * sizeof(T), EZ_ALIGNMENT_OF(T));
106  }
107 
108  template <typename T>
109  EZ_FORCE_INLINE T* ExtendRawBuffer(T* ptr, ezAllocatorBase* pAllocator, size_t uiCurrentCount, size_t uiNewCount, ezTypeIsClass)
110  {
111  EZ_CHECK_AT_COMPILETIME_MSG(!std::is_trivial<T>::value, "Pod type is treated as class, did you forget EZ_DECLARE_POD_TYPE?")
112 
113  T* pNewMem = CreateRawBuffer<T>(pAllocator, uiNewCount);
114  ezMemoryUtils::RelocateConstruct(pNewMem, ptr, uiCurrentCount);
115  DeleteRawBuffer(pAllocator, ptr);
116  return pNewMem;
117  }
118 
119  template <typename T>
120  EZ_FORCE_INLINE T* ExtendRawBuffer(T* ptr, ezAllocatorBase* pAllocator, size_t uiCurrentCount, size_t uiNewCount)
121  {
122  EZ_ASSERT_DEV(uiCurrentCount < uiNewCount, "Shrinking of a buffer is not implemented yet");
123  EZ_ASSERT_DEV(!(uiCurrentCount == uiNewCount), "Same size passed in twice.");
124  if (ptr == nullptr)
125  {
126  EZ_ASSERT_DEV(uiCurrentCount == 0, "current count must be 0 if ptr is nullptr");
127 
128  return CreateRawBuffer<T>(pAllocator, uiNewCount);
129  }
130  return ExtendRawBuffer(ptr, pAllocator, uiCurrentCount, uiNewCount, ezGetTypeClass<T>());
131  }
132 }
133 
Base class for all memory allocators.
Definition: AllocatorBase.h:21
static void Construct(T *pDestination, size_t uiCount)
Constructs uiCount objects of type T in a raw buffer at pDestination.
Definition: AllocatorBase_inl.h:13
#define EZ_ASSERT_DEV(bCondition, szErrorMsg,...)
Macro to raise an error, if a condition is not met.
Definition: Assert.h:116
static void Destruct(T *pDestination, size_t uiCount)
Destructs uiCount objects of type T at pDestination.
Type traits.
Definition: TypeTraits.h:9
EZ_ALWAYS_INLINE ezUInt32 GetCount() const
Returns the number of elements in the array.
Definition: ArrayPtr.h:182
If there is an % operator which takes a ezTypeIsMemRelocatable and returns a CompileTimeTrueType T is...
Definition: TypeTraits.h:53
EZ_ALWAYS_INLINE const PointerType GetPtr() const
Returns the pointer to the array.
Definition: ArrayPtr.h:158
This class provides functions to work on raw memory.
Definition: MemoryUtils.h:19
virtual void * Allocate(size_t uiSize, size_t uiAlign, ezMemoryUtils::DestructorFunction destructorFunc=nullptr)=0
Interface, do not use this directly, always use the new/delete macros below.
Definition: EventMessage.h:27
This class encapsulates an array and it&#39;s size. It is recommended to use this class instead of plain ...
Definition: ArrayPtr.h:82