Plasma Engine  2.0
Loading...
Searching...
No Matches
AllocatorMixin_inl.h
1namespace plInternal
2{
3 template <typename AllocationPolicy, plAllocatorTrackingMode TrackingMode>
5 {
6 public:
9
10 // plAllocator implementation
11 virtual void* Allocate(size_t uiSize, size_t uiAlign, plMemoryUtils::DestructorFunction destructorFunc = nullptr) override;
12 virtual void Deallocate(void* pPtr) override;
13 virtual size_t AllocatedSize(const void* pPtr) override;
14 virtual plAllocatorId GetId() const override;
15 virtual Stats GetStats() const override;
16
17 plAllocator* GetParent() const;
18
19 protected:
20 AllocationPolicy m_allocator;
21
22 plAllocatorId m_Id;
23 plThreadID m_ThreadID;
24 };
25
26 template <typename AllocationPolicy, plAllocatorTrackingMode TrackingMode, bool HasReallocate>
27 class plAllocatorMixinReallocate : public plAllocatorImpl<AllocationPolicy, TrackingMode>
28 {
29 public:
31 };
32
33 template <typename AllocationPolicy, plAllocatorTrackingMode TrackingMode>
34 class plAllocatorMixinReallocate<AllocationPolicy, TrackingMode, true> : public plAllocatorImpl<AllocationPolicy, TrackingMode>
35 {
36 public:
38 virtual void* Reallocate(void* pPtr, size_t uiCurrentSize, size_t uiNewSize, size_t uiAlign) override;
39 };
40}; // namespace plInternal
41
42template <typename A, plAllocatorTrackingMode TrackingMode>
44 : m_allocator(pParent)
45 , m_ThreadID(plThreadUtils::GetCurrentThreadID())
46{
47 if constexpr (TrackingMode >= plAllocatorTrackingMode::Basics)
48 {
49 this->m_Id = plMemoryTracker::RegisterAllocator(sName, TrackingMode, pParent != nullptr ? pParent->GetId() : plAllocatorId());
50 }
51}
52
53template <typename A, plAllocatorTrackingMode TrackingMode>
55{
56 if constexpr (TrackingMode >= plAllocatorTrackingMode::Basics)
57 {
58 plMemoryTracker::DeregisterAllocator(this->m_Id);
59 }
60}
61
62template <typename A, plAllocatorTrackingMode TrackingMode>
63void* plInternal::plAllocatorImpl<A, TrackingMode>::Allocate(size_t uiSize, size_t uiAlign, plMemoryUtils::DestructorFunction destructorFunc)
64{
65 // zero size allocations always return nullptr without tracking (since deallocate nullptr is ignored)
66 if (uiSize == 0)
67 return nullptr;
68
69 PL_ASSERT_DEBUG(plMath::IsPowerOf2((plUInt32)uiAlign), "Alignment must be power of two");
70
71 plTime fAllocationTime = plTime::Now();
72
73 void* ptr = m_allocator.Allocate(uiSize, uiAlign);
74 PL_ASSERT_DEV(ptr != nullptr, "Could not allocate {0} bytes. Out of memory?", uiSize);
75
76 if constexpr (TrackingMode >= plAllocatorTrackingMode::AllocationStats)
77 {
78 plMemoryTracker::AddAllocation(this->m_Id, TrackingMode, ptr, uiSize, uiAlign, plTime::Now() - fAllocationTime);
79 }
80
81 return ptr;
82}
83
84template <typename A, plAllocatorTrackingMode TrackingMode>
86{
87 if constexpr (TrackingMode >= plAllocatorTrackingMode::AllocationStats)
88 {
89 plMemoryTracker::RemoveAllocation(this->m_Id, pPtr);
90 }
91
92 m_allocator.Deallocate(pPtr);
93}
94
95template <typename A, plAllocatorTrackingMode TrackingMode>
97{
98 if constexpr (TrackingMode >= plAllocatorTrackingMode::AllocationStats)
99 {
100 return plMemoryTracker::GetAllocationInfo(this->m_Id, pPtr).m_uiSize;
101 }
102 else
103 {
104 return 0;
105 }
106}
107
108template <typename A, plAllocatorTrackingMode TrackingMode>
110{
111 return this->m_Id;
112}
113
114template <typename A, plAllocatorTrackingMode TrackingMode>
116{
117 if constexpr (TrackingMode >= plAllocatorTrackingMode::Basics)
118 {
119 return plMemoryTracker::GetAllocatorStats(this->m_Id);
120 }
121 else
122 {
123 return Stats();
124 }
125}
126
127template <typename A, plAllocatorTrackingMode TrackingMode>
129{
130 return m_allocator.GetParent();
131}
132
133template <typename A, plAllocatorTrackingMode TrackingMode, bool HasReallocate>
135 : plAllocatorImpl<A, TrackingMode>(sName, pParent)
136{
137}
138
139template <typename A, plAllocatorTrackingMode TrackingMode>
141 : plAllocatorImpl<A, TrackingMode>(sName, pParent)
142{
143}
144
145template <typename A, plAllocatorTrackingMode TrackingMode>
146void* plInternal::plAllocatorMixinReallocate<A, TrackingMode, true>::Reallocate(void* pPtr, size_t uiCurrentSize, size_t uiNewSize, size_t uiAlign)
147{
148 if constexpr (TrackingMode >= plAllocatorTrackingMode::AllocationStats)
149 {
150 plMemoryTracker::RemoveAllocation(this->m_Id, pPtr);
151 }
152
153 plTime fAllocationTime = plTime::Now();
154
155 void* pNewMem = this->m_allocator.Reallocate(pPtr, uiCurrentSize, uiNewSize, uiAlign);
156
157 if constexpr (TrackingMode >= plAllocatorTrackingMode::AllocationStats)
158 {
159 plMemoryTracker::AddAllocation(this->m_Id, TrackingMode, pNewMem, uiNewSize, uiAlign, plTime::Now() - fAllocationTime);
160 }
161
162 return pNewMem;
163}
Base class for all memory allocators.
Definition Allocator.h:23
Definition AllocatorMixin_inl.h:5
virtual void * Allocate(size_t uiSize, size_t uiAlign, plMemoryUtils::DestructorFunction destructorFunc=nullptr) override
Interface, do not use this directly, always use the new/delete macros below.
Definition AllocatorMixin_inl.h:63
virtual size_t AllocatedSize(const void *pPtr) override
Returns the number of bytes allocated at this address.
Definition AllocatorMixin_inl.h:96
Definition AllocatorMixin_inl.h:28
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Contains general thread functions.
Definition ThreadUtils.h:12
constexpr PL_FORCE_INLINE bool IsPowerOf2(plInt32 value)
Returns true, if there exists some x with 2^x == value.
Definition Math_inl.h:260
Definition Allocator.h:26
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
static plTime Now()
Gets the current time.
Definition Time_Posix.h:12