Plasma Engine  2.0
Loading...
Searching...
No Matches
LinearAllocator_inl.h
1template <plAllocatorTrackingMode TrackingMode, bool OverwriteMemoryOnReset>
3 : plAllocatorWithPolicy<typename plLinearAllocator<TrackingMode, OverwriteMemoryOnReset>::PolicyStack, TrackingMode>(sName, pParent)
4 , m_DestructData(pParent)
5 , m_PtrToDestructDataIndexTable(pParent)
6{
7}
8
9template <plAllocatorTrackingMode TrackingMode, bool OverwriteMemoryOnReset>
11{
12 Reset();
13}
14
15template <plAllocatorTrackingMode TrackingMode, bool OverwriteMemoryOnReset>
16void* plLinearAllocator<TrackingMode, OverwriteMemoryOnReset>::Allocate(size_t uiSize, size_t uiAlign, plMemoryUtils::DestructorFunction destructorFunc)
17{
18 PL_LOCK(m_Mutex);
20 void* ptr = plAllocatorWithPolicy<typename plLinearAllocator<TrackingMode, OverwriteMemoryOnReset>::PolicyStack, TrackingMode>::Allocate(uiSize, uiAlign, destructorFunc);
21
22 if (destructorFunc != nullptr)
23 {
24 plUInt32 uiIndex = m_DestructData.GetCount();
25 m_PtrToDestructDataIndexTable.Insert(ptr, uiIndex);
26
27 auto& data = m_DestructData.ExpandAndGetRef();
28 data.m_Func = destructorFunc;
29 data.m_Ptr = ptr;
30 }
31
32 return ptr;
33}
34
35template <plAllocatorTrackingMode TrackingMode, bool OverwriteMemoryOnReset>
37{
38 PL_LOCK(m_Mutex);
39
40 plUInt32 uiIndex;
41 if (m_PtrToDestructDataIndexTable.Remove(pPtr, &uiIndex))
42 {
43 auto& data = m_DestructData[uiIndex];
44 data.m_Func = nullptr;
45 data.m_Ptr = nullptr;
46 }
47
49}
50
51PL_MSVC_ANALYSIS_WARNING_PUSH
52
53// Disable warning for incorrect operator (compiler complains about the TrackingMode bitwise and in the case that flags = None)
54// even with the added guard of a check that it can't be 0.
55PL_MSVC_ANALYSIS_WARNING_DISABLE(6313)
56
57template <plAllocatorTrackingMode TrackingMode, bool OverwriteMemoryOnReset>
58void plLinearAllocator<TrackingMode, OverwriteMemoryOnReset>::Reset()
59{
60 PL_LOCK(m_Mutex);
61
62 for (plUInt32 i = m_DestructData.GetCount(); i-- > 0;)
63 {
64 auto& data = m_DestructData[i];
65 if (data.m_Func != nullptr)
66 data.m_Func(data.m_Ptr);
67 }
68
69 m_DestructData.Clear();
70 m_PtrToDestructDataIndexTable.Clear();
71
72 this->m_allocator.Reset();
73 if constexpr (TrackingMode >= plAllocatorTrackingMode::AllocationStats)
74 {
75 plMemoryTracker::RemoveAllAllocations(this->m_Id);
76 }
77 else if constexpr (TrackingMode >= plAllocatorTrackingMode::Basics)
78 {
80 this->m_allocator.FillStats(stats);
81
82 plMemoryTracker::SetAllocatorStats(this->m_Id, stats);
83 }
84}
85PL_MSVC_ANALYSIS_WARNING_POP
Base class for all memory allocators.
Definition Allocator.h:23
Policy based allocator implementation of the plAllocator interface.
Definition AllocatorWithPolicy.h:19
Definition LinearAllocator.h:12
virtual void * Allocate(size_t uiSize, size_t uiAlign, plMemoryUtils::DestructorFunction destructorFunc) override
Interface, do not use this directly, always use the new/delete macros below.
Definition LinearAllocator_inl.h:16
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Definition Allocator.h:26