gwnavruntime/kernel/HeapMH/HeapMH_DebugInfo.h Source File

HeapMH_DebugInfo.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 Filename : HeapMH_DebugInfo.h
10 Content : Debug and statistics implementation.
11 Created : July 14, 2008
12 Authors : Maxim Shemanarev
13 
14 **************************************************************************/
15 
16 #pragma once
17 
19 
20 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
21 
25 
26 namespace Kaim { namespace HeapMH {
27 
28 using namespace Heap;
29 
30 class AllocEngineMH;
31 
32 //------------------------------------------------------------------------
33 template<class T>
34 struct DebugDataPoolMH : ListNode<DebugDataPoolMH<T> >
35 {
36  typedef DebugDataPoolMH<T> SelfType;
37 
38  DebugDataPoolMH() : UseCount(0) {}
39 
40  static UPInt GetSize(UPInt bytes)
41  {
42  return (bytes - sizeof(SelfType)) / sizeof(T);
43  }
44 
45  T* GetElement(UPInt i)
46  {
47  return (T*)((UByte*)this + sizeof(SelfType) + i*sizeof(T));
48  }
49  UPInt UseCount;
50 };
51 
52 
53 
54 //------------------------------------------------------------------------
55 struct DebugDataMH : ListNode<DebugDataMH>
56 {
57  enum { ChainLimit = 10 };
58 
59  typedef DebugDataPoolMH<DebugDataMH> DataPool;
60 
61  void Clear()
62  {
63  DataCount = 0;
64  ChainCount = 0;
65  pParent = 0;
66  pNextData = 0;
67  RefCount = 0;
68  Address = 0;
69  Size = 0;
70  memset(&Info, 0, sizeof(Info));
71  }
72 
73  void MessUp()
74  {
75  memset(&DataCount, 0xFE, sizeof(DataCount));
76  memset(&ChainCount, 0xFE, sizeof(ChainCount));
77  memset(&pParent, 0xFE, sizeof(pParent));
78  memset(&pNextData, 0xFE, sizeof(pNextData));
79  memset(&RefCount, 0xFE, sizeof(RefCount));
80  memset(&Address, 0xFE, sizeof(Address));
81  memset(&Size, 0xFE, sizeof(Size));
82  memset(&Info, 0xFE, sizeof(Info));
83  }
84 
85 
86  UPIntHalf DataCount;
87  UPIntHalf ChainCount;
88  DataPool* pDataPool;
89  DebugDataMH* pParent;
90  DebugDataMH* pNextData; // Next data entry in chain
91  UPInt RefCount;
92  UPInt Address;
93  UPInt Size;
94  AllocInfo Info;
95 };
96 
97 
98 
99 
100 //------------------------------------------------------------------------
101 class DebugStorageMH
102 {
103  enum
104  {
105  PoolSize = Heap_DebugAllocPoolSize,
106  };
107 
108  typedef DebugDataPoolMH<DebugDataMH> DataPoolType;
109 
110 public:
111  struct DebugDataPtr
112  {
113  DebugDataPtr() : pData(0), Index(~UPInt(0)), pSelf(0), pPrev(0) {}
114  DebugDataMH* pData;
115  UPInt Index;
116  DebugDataMH* pSelf;
117  DebugDataMH* pPrev;
118  };
119 
120  DebugStorageMH(SysAlloc* alloc, LockSafe* rootLocker);
121 
122  unsigned GetStatId(PageInfoMH* page, UPInt parentAddr, const AllocInfo* info);
123 
124  bool AddAlloc(UPInt addr, UPInt size, PageInfoMH* pageInfo, const AllocInfo* info);
125 
126  bool AddAlloc(PageInfoMH* parentInfo, UPInt parentAddr, UPInt thisAddr,
127  UPInt size, PageInfoMH* pageInfo, const AllocInfo* info);
128 
129  void RemoveAlloc(UPInt addr, PageInfoMH* pageInfo);
130 
131  void RelinkAlloc(DebugDataPtr* ptr, UPInt oldAddr,
132  UPInt newAddr, UPInt newSize, PageInfoMH* newInfo);
133 
134  void CheckDataTail(const DebugDataPtr* ptr, UPInt usable);
135 
136  void FreeAll();
137 
138  void GetDebugData(UPInt addr, PageInfoMH* pageInfo, DebugDataPtr* ptr);
139 
140  void UnlinkAlloc(UPInt addr, PageInfoMH* pageInfo, DebugDataPtr* ptr);
141 
142  const DebugDataMH* GetFirstEntry() const;
143  const DebugDataMH* GetNextEntry(const DebugDataMH* entry) const;
144 
145  void VisitMem(MemVisitor* visitor, unsigned flags) const;
146 
147  bool DumpMemoryLeaks(const char* heapName);
148 
149  void UltimateCheck();
150 
151  UPInt GetUsedSpace() const;
152 
153 private:
154  bool allocDataPool();
155  void freeDataPool(DataPoolType* pool);
156 
157  DebugDataMH* getDebugData(PageInfoMH* page);
158  void setDebugData(PageInfoMH* page, DebugDataMH* data);
159  DebugDataMH* allocDebugData();
160  void freeDebugData(DebugDataMH* data);
161  void unlinkDebugData(PageInfoMH* page, DebugDataPtr* ptr);
162  void linkDebugData(PageInfoMH* page, DebugDataMH* data);
163  void findInChainWithin(DebugDataMH* chain, UPInt addr, DebugDataPtr* ptr);
164  void findInChainExact(DebugDataMH* chain, UPInt addr, DebugDataPtr* ptr);
165  void findDebugData(PageInfoMH* page, UPInt addr, DebugDataPtr* ret);
166  void fillDataTail(DebugDataMH* data, UPInt usable);
167  void reportViolation(DebugDataMH* data, const char* msg);
168 
169  SysAlloc* pAllocator;
170  List<DataPoolType> DataPools;
171  List<DebugDataMH> UsedDataList;
172  List<DebugDataMH> FreeDataList;
173  LockSafe* pRootLocker;
174 };
175 
176 }} // Kaim::HeapMH
177 
178 #endif // KY_MEMORY_ENABLE_DEBUG_INFO
179 
180 
std::uint8_t UByte
uint8_t
Definition: SF_Types.h:134
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17