17 #ifndef INC_KY_Kernel_HeapMH_Root_H
18 #define INC_KY_Kernel_HeapMH_Root_H
25 namespace Kaim {
namespace HeapMH {
28 struct PageMH :
public ListNode<PageMH>
33 PageMask = PageSize-1,
35 UnitSize = 1 << UnitShift,
36 UnitMask = UnitSize - 1,
37 BitSetBytes = PageSize/UnitSize/4,
52 TableSize = 1 << Index1Shift,
53 Index0Mask = TableSize - 1
62 Level0Entry Entries[TableSize];
70 enum { MagicValue = 0x5FC0 };
75 DebugDataMH* DebugHeader;
76 #ifndef KY_64BIT_POINTERS
80 UInt32 GetIndex0()
const {
return Index & PageTableMH::Index0Mask; }
81 UInt32 GetIndex1()
const {
return Index >> PageTableMH::Index1Shift; }
87 enum { Align4 = 0, Align8 = 1, Align16 = 2, MoreInfo = 3 };
92 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
93 DebugDataMH* DebugHeader;
98 static UPInt GetNodeSize(UPInt align)
100 UPInt size = 4*
sizeof(
void*);
101 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
102 size +=
sizeof(
void*);
104 return (align <= 16) ? size : size +
sizeof(UPInt);
107 MemoryHeapMH* GetHeap()
const
109 return (MemoryHeapMH*)(pHeap & ~UPInt(3));
112 UPInt GetAlign()
const
114 return ((pHeap & 3) < 3) ? (UPInt(1) << ((pHeap & 3) + 2)) : Align;
117 void SetHeap(MemoryHeapMH* heap, UPInt align)
121 case 1:
case 2:
case 4:
126 pHeap = UPInt(heap) | Align8;
130 pHeap = UPInt(heap) | Align16;
134 pHeap = UPInt(heap) | MoreInfo;
143 struct TreeNodeAccessor
145 static UPInt GetKey (
const NodeMH* n) {
return UPInt(n); }
146 static NodeMH* GetChild ( NodeMH* n, UPInt i) {
return n->Child[i]; }
147 static const NodeMH* GetChild (
const NodeMH* n, UPInt i) {
return n->Child[i]; }
148 static NodeMH** GetChildPtr( NodeMH* n, UPInt i) {
return &n->Child[i]; }
150 static NodeMH* GetParent ( NodeMH* n) {
return n->Parent; }
151 static const NodeMH* GetParent (
const NodeMH* n) {
return n->Parent; }
153 static void SetParent (NodeMH* n, NodeMH* p) { n->Parent = p; }
154 static void SetChild (NodeMH* n, UPInt i, NodeMH* c) { n->Child[i] = c; }
155 static void ClearLinks(NodeMH* n) { n->Parent = n->Child[0] = n->Child[1] = 0; }
160 struct MagicHeadersInfo
162 MagicHeader* Header1;
163 MagicHeader* Header2;
175 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
176 DebugDataMH** DebugHeaders[3];
184 void GetMagicHeaders(UPInt pageStart, MagicHeadersInfo* headers);
190 typedef RadixTree<NodeMH, TreeNodeAccessor> HeapTreeType;
192 RootMH(SysAlloc* sysAlloc);
197 SysAlloc* GetSysAlloc() {
return pSysAlloc; }
199 MemoryHeapMH* CreateHeap(
const char* name,
200 MemoryHeapMH* parent,
201 const MemoryHeap::HeapDesc& desc);
203 void DestroyHeap(MemoryHeapMH* heap);
205 LockSafe* GetLock() {
return &RootLock; }
207 PageMH* AllocPage(MemoryHeapMH* heap);
208 void FreePage(PageMH* page);
209 UInt32 GetPageIndex(
const PageMH* page)
const;
211 PageMH* ResolveAddress(UPInt addr)
const;
213 NodeMH* AddToGlobalTree(UByte* ptr, UPInt size, UPInt align, MemoryHeapMH* heap)
215 NodeMH* node = (NodeMH*)(ptr + size);
216 node->SetHeap(heap, align);
217 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
218 node->DebugHeader = 0;
220 HeapTree.Insert(node);
224 NodeMH* FindNodeInGlobalTree(UByte* ptr)
226 return (NodeMH*)HeapTree.FindGrEq(UPInt(ptr));
229 void RemoveFromGlobalTree(NodeMH* node)
231 HeapTree.Remove(node);
234 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
235 void OnAlloc(
const MemoryHeapMH* heap, UPInt size, UPInt align,
unsigned sid,
const void* ptr);
236 void OnRealloc(
const MemoryHeapMH* heap,
const void* oldPtr, UPInt newSize,
const void* newPtr);
237 void OnFree(
const MemoryHeapMH* heap,
const void* ptr);
242 bool allocPagePool();
243 static void setMagic(UByte* pageStart,
unsigned magicValue);
246 mutable LockSafe RootLock;
247 List<PageMH> FreePages;
249 HeapTreeType HeapTree;
251 static MemoryHeap::HeapTracer* pTracer;
254 extern RootMH* GlobalRootMH;
255 extern PageTableMH GlobalPageTableMH;
256 extern PageMH GlobalEmptyPage;
Definition: gamekitcrowddispersion.h:20