24 namespace Kaim {
namespace HeapMH {
27 struct PageMH :
public ListNode<PageMH>
32 PageMask = PageSize-1,
34 UnitSize = 1 << UnitShift,
35 UnitMask = UnitSize - 1,
36 BitSetBytes = PageSize/UnitSize/4,
51 TableSize = 1 << Index1Shift,
52 Index0Mask = TableSize - 1
61 Level0Entry Entries[TableSize];
69 enum { MagicValue = 0x5FC0 };
74 DebugDataMH* DebugHeader;
75 #ifndef KY_64BIT_POINTERS
79 UInt32 GetIndex0()
const {
return Index & PageTableMH::Index0Mask; }
80 UInt32 GetIndex1()
const {
return Index >> PageTableMH::Index1Shift; }
86 enum { Align4 = 0, Align8 = 1, Align16 = 2, MoreInfo = 3 };
91 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
92 DebugDataMH* DebugHeader;
97 static UPInt GetNodeSize(UPInt align)
99 UPInt size = 4*
sizeof(
void*);
100 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
101 size +=
sizeof(
void*);
103 return (align <= 16) ? size : size +
sizeof(UPInt);
106 MemoryHeapMH* GetHeap()
const
108 return (MemoryHeapMH*)(pHeap & ~UPInt(3));
111 UPInt GetAlign()
const
113 return ((pHeap & 3) < 3) ? (UPInt(1) << ((pHeap & 3) + 2)) : Align;
116 void SetHeap(MemoryHeapMH* heap, UPInt align)
120 case 1:
case 2:
case 4:
125 pHeap = UPInt(heap) | Align8;
129 pHeap = UPInt(heap) | Align16;
133 pHeap = UPInt(heap) | MoreInfo;
142 struct TreeNodeAccessor
144 static UPInt GetKey (
const NodeMH* n) {
return UPInt(n); }
145 static NodeMH* GetChild ( NodeMH* n, UPInt i) {
return n->Child[i]; }
146 static const NodeMH* GetChild (
const NodeMH* n, UPInt i) {
return n->Child[i]; }
147 static NodeMH** GetChildPtr( NodeMH* n, UPInt i) {
return &n->Child[i]; }
149 static NodeMH* GetParent ( NodeMH* n) {
return n->Parent; }
150 static const NodeMH* GetParent (
const NodeMH* n) {
return n->Parent; }
152 static void SetParent (NodeMH* n, NodeMH* p) { n->Parent = p; }
153 static void SetChild (NodeMH* n, UPInt i, NodeMH* c) { n->Child[i] = c; }
154 static void ClearLinks(NodeMH* n) { n->Parent = n->Child[0] = n->Child[1] = 0; }
159 struct MagicHeadersInfo
161 MagicHeader* Header1;
162 MagicHeader* Header2;
174 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
175 DebugDataMH** DebugHeaders[3];
183 void GetMagicHeaders(UPInt pageStart, MagicHeadersInfo* headers);
189 typedef RadixTree<NodeMH, TreeNodeAccessor> HeapTreeType;
191 RootMH(SysAlloc* sysAlloc);
196 SysAlloc* GetSysAlloc() {
return pSysAlloc; }
198 MemoryHeapMH* CreateHeap(
const char* name,
199 MemoryHeapMH* parent,
200 const MemoryHeap::HeapDesc& desc);
202 void DestroyHeap(MemoryHeapMH* heap);
204 LockSafe* GetLock() {
return &RootLock; }
206 PageMH* AllocPage(MemoryHeapMH* heap);
207 void FreePage(PageMH* page);
208 UInt32 GetPageIndex(
const PageMH* page)
const;
210 PageMH* ResolveAddress(UPInt addr)
const;
212 NodeMH* AddToGlobalTree(
UByte* ptr, UPInt size, UPInt align, MemoryHeapMH* heap)
214 NodeMH* node = (NodeMH*)(ptr + size);
215 node->SetHeap(heap, align);
216 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
217 node->DebugHeader = 0;
219 HeapTree.Insert(node);
223 NodeMH* FindNodeInGlobalTree(
UByte* ptr)
225 return (NodeMH*)HeapTree.FindGrEq(UPInt(ptr));
228 void RemoveFromGlobalTree(NodeMH* node)
230 HeapTree.Remove(node);
233 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
234 void OnAlloc(
const MemoryHeapMH* heap, UPInt size, UPInt align,
unsigned sid,
const void* ptr);
235 void OnRealloc(
const MemoryHeapMH* heap,
const void* oldPtr, UPInt newSize,
const void* newPtr);
236 void OnFree(
const MemoryHeapMH* heap,
const void* ptr);
241 bool allocPagePool();
242 static void setMagic(
UByte* pageStart,
unsigned magicValue);
245 mutable LockSafe RootLock;
246 List<PageMH> FreePages;
248 HeapTreeType HeapTree;
250 static MemoryHeap::HeapTracer* pTracer;
253 extern RootMH* GlobalRootMH;
254 extern PageTableMH GlobalPageTableMH;
255 extern PageMH GlobalEmptyPage;
std::uint16_t UInt16
uint16_t
Definition: SF_Types.h:136
std::uint8_t UByte
uint8_t
Definition: SF_Types.h:134
std::uint32_t UInt32
uint32_t
Definition: SF_Types.h:137
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17