CommonLibSSE NG
GMemoryHeap.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RE/G/GAtomic.h"
4 #include "RE/G/GList.h"
5 #include "RE/G/GStats.h"
6 
7 namespace RE
8 {
9  class GHeapMemVisitor;
10  class GHeapSegVisitor;
11  class GStatBag;
12  class GSysAllocPaged;
13  struct GAllocDebugInfo;
14 
15  class GMemoryHeap : public GListNode<GMemoryHeap>
16  {
17  private:
18  friend class GMemoryHeapPT;
19  friend class GMemoryHeapMH;
20  friend class GHeapRoot;
21  friend class GHeapRootMH;
22 
23  public:
24  enum class MemReportType
25  {
26  kBrief,
27  kSummary,
28  kMedium,
29  kFull,
30  kSimple,
34  };
35 
36  enum class HeapFlags
37  {
38  kNone = 0,
39  kThreadUnsafe = 1 << 0,
40  kFastTinyBlocks = 1 << 1,
41  kFixedGranularity = 1 << 2,
42  kRoot = 1 << 3,
43  kNoDebugInfo = 1 << 4,
44  kUserDebug = 1 << 12
45  };
46 
48  {
50  {
51  kMinAlign = 16,
52  kGranularity = 16 * 1024,
53  kReserve = 16 * 1024,
54  kThreshold = 256 * 1024,
55  kLimit = 0
56  };
57  };
58 
59  struct HeapDesc
60  {
61  public:
63  UPInt a_minAlign = 16,
64  UPInt a_granularity = 8 * 1024,
65  UPInt a_reserve = 8 * 1024,
66  UPInt a_threshold = UPINT_MAX,
67  UPInt a_limit = 0,
68  GHeapID a_heapID = GHeapID::kReserved,
69  UPInt a_arena = 0);
70 
71  void Clear();
72 
73  // members
75  std::uint32_t pad04; // 04
76  UPInt minAlign; // 08
78  UPInt reserve; // 18
80  UPInt limit; // 28
81  GHeapID heapID; // 30
82  UPInt arena; // 38
83  };
84  static_assert(sizeof(HeapDesc) == 0x40);
85 
86  struct RootHeapDesc : public HeapDesc
87  {
88  public:
90  };
91  static_assert(sizeof(RootHeapDesc) == 0x40);
92 
93  struct HeapInfo
94  {
95  public:
96  // members
97  HeapDesc desc; // 00
98  GMemoryHeap* parent; // 40 - NULL == root heap
99  char* name; // 48
100  };
101  static_assert(sizeof(HeapInfo) == 0x50);
102 
103  struct HeapVisitor
104  {
105  public:
106  virtual ~HeapVisitor(); // 00
107 
108  // add
109  virtual void Visit(GMemoryHeap* a_parentHeap, GMemoryHeap* a_childHeap) = 0; // 01
110  };
111  static_assert(sizeof(HeapVisitor) == 0x8);
112 
114  {
115  public:
116  virtual ~LimitHandler(); // 00
117 
118  // add
119  virtual bool OnExceedLimit(GMemoryHeap* a_heap, UPInt a_overLimit) = 0; // 01
120  virtual void OnFreeSegment(GMemoryHeap* a_heap, UPInt a_freeingSize) = 0; // 02
121  };
122  static_assert(sizeof(LimitHandler) == 0x8);
123 
124  struct HeapTracer
125  {
126  public:
127  virtual ~HeapTracer(); // 00
128 
129  // add
130  virtual void OnCreateHeap(const GMemoryHeap* a_heap) = 0; // 01
131  virtual void OnDestroyHeap(const GMemoryHeap* a_heap) = 0; // 02
132  virtual void OnAlloc(const GMemoryHeap* a_heap, UPInt a_size, UPInt a_align, unsigned a_sID, const void* a_ptr) = 0; // 03
133  virtual void OnRealloc(const GMemoryHeap* a_heap, const void* a_oldPtr, UPInt a_newSize, const void* a_newPtr) = 0; // 04
134  virtual void OnFree(const GMemoryHeap* a_heap, const void* a_ptr) = 0; // 05
135  };
136  static_assert(sizeof(HeapTracer) == 0x8);
137 
138  struct RootStats
139  {
140  public:
141  // members
152  };
153  static_assert(sizeof(RootStats) == 0x50);
154 
155  protected:
156  virtual ~GMemoryHeap(); // 00
157 
158  public:
159  // add
160  virtual void CreateArena(UPInt a_arena, GSysAllocPaged* a_sysAlloc) = 0; // 01
161  virtual void DestroyArena(UPInt a_arena) = 0; // 02
162  virtual bool ArenaIsEmpty(UPInt a_arena) = 0; // 03
163  virtual GMemoryHeap* CreateHeap(const char* a_name, const HeapDesc& a_desc) = 0; // 04
164  virtual void SetLimitHandler(LimitHandler* handler) = 0; // 05
165  virtual void SetLimit(UPInt newLimit) = 0; // 06
166  virtual void AddRef() = 0; // 07
167  virtual void Release() = 0; // 08
168  virtual void* Alloc(UPInt a_size) = 0; // 09
169  virtual void* Alloc(UPInt a_size, UPInt a_align) = 0; // 0A
170  virtual void* Realloc(void* a_oldPtr, UPInt a_newSize) = 0; // 0B
171  virtual void Free(void* a_ptr) = 0; // 0C
172  virtual void* AllocAutoHeap(const void* a_this, UPInt a_size) = 0; // 0D
173  virtual void* AllocAutoHeap(const void* a_this, UPInt a_size, UPInt a_align) = 0; // 0E
174  virtual GMemoryHeap* GetAllocHeap(const void* a_this) = 0; // 0F
175  virtual UPInt GetUsableSize(const void* a_ptr) = 0; // 10
176  virtual void* AllocSysDirect(UPInt a_size) = 0; // 11
177  virtual void FreeSysDirect(void* a_ptr, UPInt a_size) = 0; // 12
178  virtual bool GetStats(GStatBag* a_bag) = 0; // 13
179  virtual UPInt GetFootprint() const = 0; // 14
180  virtual UPInt GetTotalFootprint() const = 0; // 15
181  virtual UPInt GetUsedSpace() const = 0; // 16
182  virtual UPInt GetTotalUsedSpace() const = 0; // 17
183  virtual void GetRootStats(RootStats* a_stats) = 0; // 18
184  virtual void VisitMem(GHeapMemVisitor* a_visitor, std::uint32_t a_flags) = 0; // 19
185  virtual void VisitRootSegments(GHeapSegVisitor* a_visitor) = 0; // 1A
186  virtual void VisitHeapSegments(GHeapSegVisitor* a_visitor) const = 0; // 1B
187  virtual void SetTracer(HeapTracer* a_tracer) = 0; // 1C - { return; }
188 
189  protected:
190  virtual void DestroyItself() = 0; // 1D
191  virtual void UltimateCheck_Internal() = 0; // 1E - { return; }
192  virtual void ReleaseCachedMem() = 0; // 1F
193  virtual bool DumpMemoryLeaks_Internal() = 0; // 20 - { return false; }
194  virtual void CheckIntegrity_Internal() const = 0; // 21
195  virtual void GetUserDebugStats(RootStats* a_stats) const = 0; // 22
196 
197  public:
198  GMemoryHeap* CreateHeap(const char* a_name,
199  HeapFlags a_flags = HeapFlags::kNone,
200  UPInt a_minAlign = 16,
201  UPInt a_granularity = 16 * 1024,
202  UPInt a_reserve = 16 * 1024,
203  UPInt a_threshold = UPINT_MAX,
204  UPInt a_limit = 0,
205  GHeapID a_heapID = GHeapID::kReserved,
206  UPInt a_arena = 0);
207 
208  void GetHeapInfo(HeapInfo* a_info) const;
209  const char* GetName() const;
210  GHeapID GetID() const;
214  UPInt GetLimit() const;
215  bool IsThreadSafe() const;
216  void ReleaseOnFree(void* a_ptr);
221 
222  protected:
224 
225  // members
227  volatile std::uint32_t _refCount; // 20
228  std::uint32_t _pad24; // 24
230  void* _autoRelease; // 30 - auto frees heap when freed
233  mutable GLock _heapLock; // 98
234  bool _useLocks; // C0
235  bool _trackDebugInfo; // C1
236  std::uint16_t _padC2; // C2
237  std::uint32_t _padC4; // C4
238  };
239  static_assert(sizeof(GMemoryHeap) == 0xC8);
240 
241  class GHeapAllocEngine;
242  class GHeapDebugStorage;
243 
244  class GMemoryHeapPT : public GMemoryHeap
245  {
246  private:
247  // members
248  [[maybe_unused]] GHeapAllocEngine* _engine; // C8
249  [[maybe_unused]] GHeapDebugStorage* _debugStorage; // D0
250  };
251  static_assert(sizeof(GMemoryHeapPT) == 0xD8);
252 }
Definition: GAtomic.h:8
Definition: GMemoryHeap.h:245
Definition: GMemoryHeap.h:16
virtual void SetTracer(HeapTracer *a_tracer)=0
UPInt GetGranularity() const
std::uint32_t _padC4
Definition: GMemoryHeap.h:237
GLock _heapLock
Definition: GMemoryHeap.h:233
virtual void FreeSysDirect(void *a_ptr, UPInt a_size)=0
virtual GMemoryHeap * GetAllocHeap(const void *a_this)=0
virtual void CreateArena(UPInt a_arena, GSysAllocPaged *a_sysAlloc)=0
virtual void VisitRootSegments(GHeapSegVisitor *a_visitor)=0
const char * GetName() const
bool _useLocks
Definition: GMemoryHeap.h:234
virtual void * AllocAutoHeap(const void *a_this, UPInt a_size)=0
virtual void DestroyArena(UPInt a_arena)=0
virtual bool ArenaIsEmpty(UPInt a_arena)=0
void CheckIntegrity()
virtual void * Realloc(void *a_oldPtr, UPInt a_newSize)=0
std::uint32_t _pad24
Definition: GMemoryHeap.h:228
GMemoryHeap * CreateHeap(const char *a_name, HeapFlags a_flags=HeapFlags::kNone, UPInt a_minAlign=16, UPInt a_granularity=16 *1024, UPInt a_reserve=16 *1024, UPInt a_threshold=UPINT_MAX, UPInt a_limit=0, GHeapID a_heapID=GHeapID::kReserved, UPInt a_arena=0)
virtual void * Alloc(UPInt a_size, UPInt a_align)=0
bool IsThreadSafe() const
HeapFlags
Definition: GMemoryHeap.h:37
virtual ~GMemoryHeap()
virtual void GetRootStats(RootStats *a_stats)=0
virtual void VisitMem(GHeapMemVisitor *a_visitor, std::uint32_t a_flags)=0
UPInt _ownerThreadID
Definition: GMemoryHeap.h:229
GMemoryHeap * GetParentHeap() const
friend class GHeapRootMH
Definition: GMemoryHeap.h:21
virtual void ReleaseCachedMem()=0
virtual UPInt GetTotalFootprint() const =0
virtual void Free(void *a_ptr)=0
GHeapID GetID() const
virtual UPInt GetUsableSize(const void *a_ptr)=0
void ReleaseOnFree(void *a_ptr)
void UltimateCheck()
virtual UPInt GetTotalUsedSpace() const =0
virtual void SetLimit(UPInt newLimit)=0
virtual void DestroyItself()=0
virtual void Release()=0
virtual void SetLimitHandler(LimitHandler *handler)=0
UPInt _selfSize
Definition: GMemoryHeap.h:226
MemReportType
Definition: GMemoryHeap.h:25
HeapInfo _info
Definition: GMemoryHeap.h:231
virtual void * AllocAutoHeap(const void *a_this, UPInt a_size, UPInt a_align)=0
virtual void GetUserDebugStats(RootStats *a_stats) const =0
virtual bool GetStats(GStatBag *a_bag)=0
virtual void CheckIntegrity_Internal() const =0
virtual GMemoryHeap * CreateHeap(const char *a_name, const HeapDesc &a_desc)=0
virtual void UltimateCheck_Internal()=0
HeapFlags GetFlags() const
ChildListType _childHeaps
Definition: GMemoryHeap.h:232
virtual void * Alloc(UPInt a_size)=0
virtual UPInt GetFootprint() const =0
void * _autoRelease
Definition: GMemoryHeap.h:230
friend class GHeapRoot
Definition: GMemoryHeap.h:20
bool DumpMemoryLeaks()
UPInt GetLimit() const
std::uint16_t _padC2
Definition: GMemoryHeap.h:236
bool _trackDebugInfo
Definition: GMemoryHeap.h:235
void AssignToCurrentThread()
virtual UPInt GetUsedSpace() const =0
void GetHeapInfo(HeapInfo *a_info) const
friend class GMemoryHeapMH
Definition: GMemoryHeap.h:19
volatile std::uint32_t _refCount
Definition: GMemoryHeap.h:227
virtual void AddRef()=0
virtual void VisitHeapSegments(GHeapSegVisitor *a_visitor) const =0
virtual void * AllocSysDirect(UPInt a_size)=0
virtual bool DumpMemoryLeaks_Internal()=0
Definition: GSysAllocPaged.h:11
Definition: AbsorbEffect.h:6
std::size_t UPInt
Definition: SFTypes.h:5
GHeapID
Definition: GStats.h:79
constexpr UPInt UPINT_MAX
Definition: SFTypes.h:6
Definition: GList.h:7
Definition: GMemoryHeap.h:60
HeapDesc(HeapFlags a_flags=HeapFlags::kNone, UPInt a_minAlign=16, UPInt a_granularity=8 *1024, UPInt a_reserve=8 *1024, UPInt a_threshold=UPINT_MAX, UPInt a_limit=0, GHeapID a_heapID=GHeapID::kReserved, UPInt a_arena=0)
std::uint32_t pad04
Definition: GMemoryHeap.h:75
UPInt granularity
Definition: GMemoryHeap.h:77
UPInt limit
Definition: GMemoryHeap.h:80
UPInt arena
Definition: GMemoryHeap.h:82
UPInt reserve
Definition: GMemoryHeap.h:78
stl::enumeration< HeapFlags, std::uint32_t > flags
Definition: GMemoryHeap.h:74
GHeapID heapID
Definition: GMemoryHeap.h:81
UPInt minAlign
Definition: GMemoryHeap.h:76
UPInt threshold
Definition: GMemoryHeap.h:79
Definition: GMemoryHeap.h:94
GMemoryHeap * parent
Definition: GMemoryHeap.h:98
char * name
Definition: GMemoryHeap.h:99
HeapDesc desc
Definition: GMemoryHeap.h:97
Definition: GMemoryHeap.h:125
virtual void OnAlloc(const GMemoryHeap *a_heap, UPInt a_size, UPInt a_align, unsigned a_sID, const void *a_ptr)=0
virtual void OnCreateHeap(const GMemoryHeap *a_heap)=0
virtual void OnRealloc(const GMemoryHeap *a_heap, const void *a_oldPtr, UPInt a_newSize, const void *a_newPtr)=0
virtual void OnFree(const GMemoryHeap *a_heap, const void *a_ptr)=0
virtual void OnDestroyHeap(const GMemoryHeap *a_heap)=0
Definition: GMemoryHeap.h:104
virtual void Visit(GMemoryHeap *a_parentHeap, GMemoryHeap *a_childHeap)=0
Definition: GMemoryHeap.h:114
virtual bool OnExceedLimit(GMemoryHeap *a_heap, UPInt a_overLimit)=0
virtual void OnFreeSegment(GMemoryHeap *a_heap, UPInt a_freeingSize)=0
Definition: GMemoryHeap.h:87
Definition: GMemoryHeap.h:48
RootHeapParameter
Definition: GMemoryHeap.h:50
@ kReserve
Definition: GMemoryHeap.h:53
@ kMinAlign
Definition: GMemoryHeap.h:51
@ kGranularity
Definition: GMemoryHeap.h:52
@ kLimit
Definition: GMemoryHeap.h:55
@ kThreshold
Definition: GMemoryHeap.h:54
Definition: GMemoryHeap.h:139
UPInt debugInfoUsedSpace
Definition: GMemoryHeap.h:149
UPInt bookkeepingFootprint
Definition: GMemoryHeap.h:146
UPInt pageMapUsedSpace
Definition: GMemoryHeap.h:145
UPInt pageMapFootprint
Definition: GMemoryHeap.h:144
UPInt sysMemFootprint
Definition: GMemoryHeap.h:142
UPInt sysMemUsedSpace
Definition: GMemoryHeap.h:143
UPInt debugInfoFootprint
Definition: GMemoryHeap.h:148
UPInt bookkeepingUsedSpace
Definition: GMemoryHeap.h:147
UPInt userDebugFootprint
Definition: GMemoryHeap.h:150
UPInt userDebugUsedSpace
Definition: GMemoryHeap.h:151