gwnavruntime/kernel/HeapMH/HeapMH_DebugInfo.h Source File

HeapMH_DebugInfo.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 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 #ifndef INC_KY_Kernel_HeapMH_DebugInfo_H
17 #define INC_KY_Kernel_HeapMH_DebugInfo_H
18 
20 
21 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
22 
26 
27 namespace Kaim { namespace HeapMH {
28 
29 using namespace Heap;
30 
31 class AllocEngineMH;
32 
33 //------------------------------------------------------------------------
34 template<class T>
35 struct DebugDataPoolMH : ListNode<DebugDataPoolMH<T> >
36 {
37  typedef DebugDataPoolMH<T> SelfType;
38 
39  DebugDataPoolMH() : UseCount(0) {}
40 
41  static UPInt GetSize(UPInt bytes)
42  {
43  return (bytes - sizeof(SelfType)) / sizeof(T);
44  }
45 
46  T* GetElement(UPInt i)
47  {
48  return (T*)((UByte*)this + sizeof(SelfType) + i*sizeof(T));
49  }
50  UPInt UseCount;
51 };
52 
53 
54 
55 //------------------------------------------------------------------------
56 struct DebugDataMH : ListNode<DebugDataMH>
57 {
58  enum { ChainLimit = 10 };
59 
60  typedef DebugDataPoolMH<DebugDataMH> DataPool;
61 
62  void Clear()
63  {
64  DataCount = 0;
65  ChainCount = 0;
66  pParent = 0;
67  pNextData = 0;
68  RefCount = 0;
69  Address = 0;
70  Size = 0;
71  memset(&Info, 0, sizeof(Info));
72  }
73 
74  void MessUp()
75  {
76  memset(&DataCount, 0xFE, sizeof(DataCount));
77  memset(&ChainCount, 0xFE, sizeof(ChainCount));
78  memset(&pParent, 0xFE, sizeof(pParent));
79  memset(&pNextData, 0xFE, sizeof(pNextData));
80  memset(&RefCount, 0xFE, sizeof(RefCount));
81  memset(&Address, 0xFE, sizeof(Address));
82  memset(&Size, 0xFE, sizeof(Size));
83  memset(&Info, 0xFE, sizeof(Info));
84  }
85 
86 
87  UPIntHalf DataCount;
88  UPIntHalf ChainCount;
89  DataPool* pDataPool;
90  DebugDataMH* pParent;
91  DebugDataMH* pNextData; // Next data entry in chain
92  UPInt RefCount;
93  UPInt Address;
94  UPInt Size;
95  AllocInfo Info;
96 };
97 
98 
99 
100 
101 //------------------------------------------------------------------------
102 class DebugStorageMH
103 {
104  enum
105  {
106  PoolSize = Heap_DebugAllocPoolSize,
107  };
108 
109  typedef DebugDataPoolMH<DebugDataMH> DataPoolType;
110 
111 public:
112  struct DebugDataPtr
113  {
114  DebugDataPtr() : pData(0), Index(~UPInt(0)), pSelf(0), pPrev(0) {}
115  DebugDataMH* pData;
116  UPInt Index;
117  DebugDataMH* pSelf;
118  DebugDataMH* pPrev;
119  };
120 
121  DebugStorageMH(SysAlloc* alloc, LockSafe* rootLocker);
122 
123  unsigned GetStatId(PageInfoMH* page, UPInt parentAddr, const AllocInfo* info);
124 
125  bool AddAlloc(UPInt addr, UPInt size, PageInfoMH* pageInfo, const AllocInfo* info);
126 
127  bool AddAlloc(PageInfoMH* parentInfo, UPInt parentAddr, UPInt thisAddr,
128  UPInt size, PageInfoMH* pageInfo, const AllocInfo* info);
129 
130  void RemoveAlloc(UPInt addr, PageInfoMH* pageInfo);
131 
132  void RelinkAlloc(DebugDataPtr* ptr, UPInt oldAddr,
133  UPInt newAddr, UPInt newSize, PageInfoMH* newInfo);
134 
135  void CheckDataTail(const DebugDataPtr* ptr, UPInt usable);
136 
137  void FreeAll();
138 
139  void GetDebugData(UPInt addr, PageInfoMH* pageInfo, DebugDataPtr* ptr);
140 
141  void UnlinkAlloc(UPInt addr, PageInfoMH* pageInfo, DebugDataPtr* ptr);
142 
143  void GetStats(AllocEngineMH* allocator, StatBag* bag) const;
144 
145  const DebugDataMH* GetFirstEntry() const;
146  const DebugDataMH* GetNextEntry(const DebugDataMH* entry) const;
147 
148  void VisitMem(MemVisitor* visitor, unsigned flags) const;
149 
150  bool DumpMemoryLeaks(const char* heapName);
151 
152  void UltimateCheck();
153 
154  UPInt GetUsedSpace() const;
155 
156 private:
157  bool allocDataPool();
158  void freeDataPool(DataPoolType* pool);
159 
160  DebugDataMH* getDebugData(PageInfoMH* page);
161  void setDebugData(PageInfoMH* page, DebugDataMH* data);
162  DebugDataMH* allocDebugData();
163  void freeDebugData(DebugDataMH* data);
164  void unlinkDebugData(PageInfoMH* page, DebugDataPtr* ptr);
165  void linkDebugData(PageInfoMH* page, DebugDataMH* data);
166  void findInChainWithin(DebugDataMH* chain, UPInt addr, DebugDataPtr* ptr);
167  void findInChainExact(DebugDataMH* chain, UPInt addr, DebugDataPtr* ptr);
168  void findDebugData(PageInfoMH* page, UPInt addr, DebugDataPtr* ret);
169  void fillDataTail(DebugDataMH* data, UPInt usable);
170  void reportViolation(DebugDataMH* data, const char* msg);
171 
172  SysAlloc* pAllocator;
173  List<DataPoolType> DataPools;
174  List<DebugDataMH> UsedDataList;
175  List<DebugDataMH> FreeDataList;
176  LockSafe* pRootLocker;
177 };
178 
179 }} // Kaim::HeapMH
180 
181 #endif // KY_MEMORY_ENABLE_DEBUG_INFO
182 
183 #endif
184 
Definition: gamekitcrowddispersion.h:20