ezEngine  Milestone 9
AllocatorBase.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <Foundation/Types/ArrayPtr.h>
6 #include <Foundation/Types/Id.h>
7 #include <utility>
8 
9 
10 #ifdef new
11 #undef new
12 #endif
13 
14 #ifdef delete
15 #undef delete
16 #endif
17 
19 
21 class EZ_FOUNDATION_DLL ezAllocatorBase
22 {
23 public:
24  struct Stats
25  {
27 
28  EZ_ALWAYS_INLINE Stats() { ezMemoryUtils::ZeroFill(this, 1); }
29 
30  ezUInt64 m_uiNumAllocations;
32  ezUInt64 m_uiAllocationSize;
33  };
34 
36  virtual ~ezAllocatorBase();
37 
39  virtual void* Allocate(size_t uiSize, size_t uiAlign, ezMemoryUtils::DestructorFunction destructorFunc = nullptr) = 0;
40  virtual void Deallocate(void* ptr) = 0;
41  virtual void* Reallocate(void* ptr, size_t uiCurrentSize, size_t uiNewSize, size_t uiAlign);
42  virtual size_t AllocatedSize(const void* ptr) = 0;
43 
44  virtual ezAllocatorId GetId() const = 0;
45  virtual Stats GetStats() const = 0;
46 
47 private:
48  EZ_DISALLOW_COPY_AND_ASSIGN(ezAllocatorBase);
49 };
50 
51 #include <Foundation/Memory/Implementation/AllocatorBase_inl.h>
52 
54 #define EZ_NEW(allocator, type, ...) \
55  ezInternal::NewInstance<type>( \
56  new ((allocator)->Allocate(sizeof(type), EZ_ALIGNMENT_OF(type), ezMemoryUtils::MakeDestructorFunction<type>())) type(__VA_ARGS__), \
57  (allocator))
58 
60 #define EZ_DELETE(allocator, ptr) \
61  { \
62  ezInternal::Delete(allocator, ptr); \
63  ptr = nullptr; \
64  }
65 
67 #define EZ_NEW_ARRAY(allocator, type, count) ezInternal::CreateArray<type>(allocator, count)
68 
70 #define EZ_DELETE_ARRAY(allocator, arrayPtr) \
71  { \
72  ezInternal::DeleteArray(allocator, arrayPtr); \
73  arrayPtr.Clear(); \
74  }
75 
77 #define EZ_NEW_RAW_BUFFER(allocator, type, count) ezInternal::CreateRawBuffer<type>(allocator, count)
78 
80 #define EZ_DELETE_RAW_BUFFER(allocator, ptr) \
81  { \
82  ezInternal::DeleteRawBuffer(allocator, ptr); \
83  ptr = nullptr; \
84  }
85 
87 #define EZ_EXTEND_RAW_BUFFER(allocator, ptr, oldSize, newSize) ezInternal::ExtendRawBuffer(ptr, allocator, oldSize, newSize)
88 
89 
90 
92 #define EZ_DEFAULT_NEW(type, ...) EZ_NEW(ezFoundation::GetDefaultAllocator(), type, __VA_ARGS__)
93 
95 #define EZ_DEFAULT_DELETE(ptr) EZ_DELETE(ezFoundation::GetDefaultAllocator(), ptr)
96 
98 #define EZ_DEFAULT_NEW_ARRAY(type, count) EZ_NEW_ARRAY(ezFoundation::GetDefaultAllocator(), type, count)
99 
101 #define EZ_DEFAULT_DELETE_ARRAY(arrayPtr) EZ_DELETE_ARRAY(ezFoundation::GetDefaultAllocator(), arrayPtr)
102 
104 #define EZ_DEFAULT_NEW_RAW_BUFFER(type, count) EZ_NEW_RAW_BUFFER(ezFoundation::GetDefaultAllocator(), type, count)
105 
107 #define EZ_DEFAULT_DELETE_RAW_BUFFER(ptr) EZ_DELETE_RAW_BUFFER(ezFoundation::GetDefaultAllocator(), ptr)
108 
Base class for all memory allocators.
Definition: AllocatorBase.h:21
Definition: AllocatorBase.h:24
#define EZ_DECLARE_POD_TYPE()
Embed this into a class to mark it as a POD type. POD types will get special treatment from allocator...
Definition: TypeTraits.h:128
ezUInt64 m_uiNumDeallocations
total number of deallocations
Definition: AllocatorBase.h:31
static void ZeroFill(T *pDestination, size_t uiCount)
Zeros out buffer of a raw memory.
ezUInt64 m_uiAllocationSize
total allocation size in bytes
Definition: AllocatorBase.h:32
ezUInt64 m_uiNumAllocations
total number of allocations
Definition: AllocatorBase.h:30