Plasma Engine  2.0
Loading...
Searching...
No Matches
MemoryTracker.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Memory/Allocator.h>
5#include <Foundation/Time/Time.h>
6#include <Foundation/Types/ArrayPtr.h>
7#include <Foundation/Types/Bitflags.h>
8
9enum class plAllocatorTrackingMode : plUInt32
10{
11 Nothing,
12 Basics,
13 AllocationStats,
14 AllocationStatsIgnoreLeaks,
15 AllocationStatsAndStacktraces,
16
17 Default = PL_ALLOC_TRACKING_DEFAULT,
18};
19
21class PL_FOUNDATION_DLL plMemoryTracker
22{
23public:
25 {
26 PL_DECLARE_POD_TYPE();
27
28 PL_FORCE_INLINE AllocationInfo()
29
30 = default;
31
32 void** m_pStackTrace = nullptr;
33 size_t m_uiSize = 0;
34 plUInt16 m_uiAlignment = 0;
35 plUInt16 m_uiStackTraceLength = 0;
36
37 PL_ALWAYS_INLINE const plArrayPtr<void*> GetStackTrace() const { return plArrayPtr<void*>(m_pStackTrace, (plUInt32)m_uiStackTraceLength); }
38
39 PL_ALWAYS_INLINE plArrayPtr<void*> GetStackTrace() { return plArrayPtr<void*>(m_pStackTrace, (plUInt32)m_uiStackTraceLength); }
40
41 PL_FORCE_INLINE void SetStackTrace(plArrayPtr<void*> stackTrace)
42 {
43 m_pStackTrace = stackTrace.GetPtr();
44 PL_ASSERT_DEV(stackTrace.GetCount() < 0xFFFF, "stack trace too long");
45 m_uiStackTraceLength = (plUInt16)stackTrace.GetCount();
46 }
47 };
48
49 class PL_FOUNDATION_DLL Iterator
50 {
51 public:
52 ~Iterator();
53
54 plAllocatorId Id() const;
55 plStringView Name() const;
56 plAllocatorId ParentId() const;
57 const plAllocator::Stats& Stats() const;
58
59 void Next();
60 bool IsValid() const;
61
62 PL_ALWAYS_INLINE void operator++() { Next(); }
63
64 private:
65 friend class plMemoryTracker;
66
67 PL_ALWAYS_INLINE Iterator(void* pData)
68 : m_pData(pData)
69 {
70 }
71
72 void* m_pData;
73 };
74
75 static plAllocatorId RegisterAllocator(plStringView sName, plAllocatorTrackingMode mode, plAllocatorId parentId);
76 static void DeregisterAllocator(plAllocatorId allocatorId);
77
78 static void AddAllocation(plAllocatorId allocatorId, plAllocatorTrackingMode mode, const void* pPtr, size_t uiSize, size_t uiAlign, plTime allocationTime);
79 static void RemoveAllocation(plAllocatorId allocatorId, const void* pPtr);
80 static void RemoveAllAllocations(plAllocatorId allocatorId);
81 static void SetAllocatorStats(plAllocatorId allocatorId, const plAllocator::Stats& stats);
82
83 static void ResetPerFrameAllocatorStats();
84
85 static plStringView GetAllocatorName(plAllocatorId allocatorId);
86 static const plAllocator::Stats& GetAllocatorStats(plAllocatorId allocatorId);
87 static plAllocatorId GetAllocatorParentId(plAllocatorId allocatorId);
88 static const AllocationInfo& GetAllocationInfo(plAllocatorId allocatorId, const void* pPtr);
89
90 static Iterator GetIterator();
91
93 using PrintFunc = void (*)(const char* szLine);
94
98 static plUInt32 PrintMemoryLeaks(PrintFunc printfunc);
99
103 static void DumpMemoryLeaks();
104};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
PL_ALWAYS_INLINE plUInt32 GetCount() const
Returns the number of elements in the array.
Definition ArrayPtr.h:142
PL_ALWAYS_INLINE PointerType GetPtr() const
Returns the pointer to the array.
Definition ArrayPtr.h:118
Definition MemoryTracker.h:50
Memory tracker which keeps track of all allocations and constructions.
Definition MemoryTracker.h:22
void(*)(const char *szLine) PrintFunc
Callback for printing strings.
Definition MemoryTracker.h:93
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
Definition MemoryTracker.h:25
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12