Plasma Engine  2.0
Loading...
Searching...
No Matches
Allocator.h
1#pragma once
2
4
5#include <Foundation/Time/Time.h>
6#include <Foundation/Types/ArrayPtr.h>
7#include <Foundation/Types/Id.h>
8#include <utility>
9
10
11#ifdef new
12# undef new
13#endif
14
15#ifdef delete
16# undef delete
17#endif
18
20
22class PL_FOUNDATION_DLL plAllocator
23{
24public:
25 struct Stats
26 {
27 PL_DECLARE_POD_TYPE();
28
29 plUInt64 m_uiNumAllocations = 0;
30 plUInt64 m_uiNumDeallocations = 0;
31 plUInt64 m_uiAllocationSize = 0;
32
33 plUInt64 m_uiPerFrameAllocationSize = 0;
35 };
36
38 virtual ~plAllocator();
39
41 virtual void* Allocate(size_t uiSize, size_t uiAlign, plMemoryUtils::DestructorFunction destructorFunc = nullptr) = 0;
42 virtual void Deallocate(void* pPtr) = 0;
43 virtual void* Reallocate(void* pPtr, size_t uiCurrentSize, size_t uiNewSize, size_t uiAlign);
44
50 virtual size_t AllocatedSize(const void* pPtr) = 0;
51
52 virtual plAllocatorId GetId() const = 0;
53 virtual Stats GetStats() const = 0;
54
55private:
56 PL_DISALLOW_COPY_AND_ASSIGN(plAllocator);
57};
58
59#include <Foundation/Memory/Implementation/Allocator_inl.h>
60
62#define PL_NEW(allocator, type, ...) \
63 plInternal::NewInstance<type>( \
64 new ((allocator)->Allocate(sizeof(type), PL_ALIGNMENT_OF(type), plMemoryUtils::MakeDestructorFunction<type>())) type(__VA_ARGS__), (allocator))
65
67#define PL_DELETE(allocator, ptr) \
68 { \
69 plInternal::Delete(allocator, ptr); \
70 ptr = nullptr; \
71 }
72
74#define PL_NEW_ARRAY(allocator, type, count) plInternal::CreateArray<type>(allocator, count)
75
77#define PL_DELETE_ARRAY(allocator, arrayPtr) \
78 { \
79 plInternal::DeleteArray(allocator, arrayPtr); \
80 arrayPtr.Clear(); \
81 }
82
84#define PL_NEW_RAW_BUFFER(allocator, type, count) plInternal::CreateRawBuffer<type>(allocator, count)
85
87#define PL_DELETE_RAW_BUFFER(allocator, ptr) \
88 { \
89 plInternal::DeleteRawBuffer(allocator, ptr); \
90 ptr = nullptr; \
91 }
92
94#define PL_EXTEND_RAW_BUFFER(allocator, ptr, oldSize, newSize) plInternal::ExtendRawBuffer(ptr, allocator, oldSize, newSize)
95
96
97
99#define PL_DEFAULT_NEW(type, ...) PL_NEW(plFoundation::GetDefaultAllocator(), type, __VA_ARGS__)
100
102#define PL_DEFAULT_DELETE(ptr) PL_DELETE(plFoundation::GetDefaultAllocator(), ptr)
103
105#define PL_DEFAULT_NEW_ARRAY(type, count) PL_NEW_ARRAY(plFoundation::GetDefaultAllocator(), type, count)
106
108#define PL_DEFAULT_DELETE_ARRAY(arrayPtr) PL_DELETE_ARRAY(plFoundation::GetDefaultAllocator(), arrayPtr)
109
111#define PL_DEFAULT_NEW_RAW_BUFFER(type, count) PL_NEW_RAW_BUFFER(plFoundation::GetDefaultAllocator(), type, count)
112
114#define PL_DEFAULT_DELETE_RAW_BUFFER(ptr) PL_DELETE_RAW_BUFFER(plFoundation::GetDefaultAllocator(), ptr)
Base class for all memory allocators.
Definition Allocator.h:23
virtual void * Allocate(size_t uiSize, size_t uiAlign, plMemoryUtils::DestructorFunction destructorFunc=nullptr)=0
Interface, do not use this directly, always use the new/delete macros below.
virtual size_t AllocatedSize(const void *pPtr)=0
Returns the number of bytes allocated at this address.
Definition Allocator.h:26
plTime m_PerFrameAllocationTime
time spend on allocations in this frame
Definition Allocator.h:34
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12