20 #ifndef INC_KY_Kernel_HeapPT_AllocLite_H
21 #define INC_KY_Kernel_HeapPT_AllocLite_H
29 namespace Heap {
class SegVisitor; }
40 TreeSeg* AddrChild[2];
51 struct DualTNode : ListNode<DualTNode>
55 DualTNode* AddrParent;
56 DualTNode* AddrChild[2];
61 static KY_INLINE UByte* GetAlignedPtr(UByte* start, UPInt alignMask)
63 UPInt aligned = (UPInt(start) + alignMask) & ~alignMask;
64 UPInt head = aligned - UPInt(start);
65 while (head && head < 16*
sizeof(
void*))
67 aligned += alignMask+1;
70 return (UByte*)aligned;
73 KY_INLINE
bool AlignmentIsOK(UPInt blocks, UPInt shift, UPInt alignMask)
const
75 UPInt bytes = blocks << shift;
76 UPInt aligned = UPInt(GetAlignedPtr((UByte*)
this, alignMask));
77 return aligned + bytes <= UPInt(
this) + (Size << shift);
84 typedef DualTNode FreeNode;
90 static UPInt GetKey (
const FreeNode* n) {
return n->Size; }
91 static FreeNode* GetChild ( FreeNode* n, UPInt i) {
return n->Child[i]; }
92 static const FreeNode* GetChild (
const FreeNode* n, UPInt i) {
return n->Child[i]; }
93 static FreeNode** GetChildPtr( FreeNode* n, UPInt i) {
return &n->Child[i]; }
95 static FreeNode* GetParent ( FreeNode* n) {
return n->Parent; }
96 static const FreeNode* GetParent (
const FreeNode* n) {
return n->Parent; }
98 static void SetParent (FreeNode* n, FreeNode* p) { n->Parent = p; }
99 static void SetChild (FreeNode* n, UPInt i, FreeNode* c) { n->Child[i] = c; }
100 static void ClearLinks(FreeNode* n) { n->Parent = n->Child[0] = n->Child[1] = 0; }
106 static UPInt GetKey (
const FreeNode* n) {
return UPInt(n); }
107 static FreeNode* GetChild ( FreeNode* n, UPInt i) {
return n->AddrChild[i]; }
108 static const FreeNode* GetChild (
const FreeNode* n, UPInt i) {
return n->AddrChild[i]; }
109 static FreeNode** GetChildPtr( FreeNode* n, UPInt i) {
return &n->AddrChild[i]; }
111 static FreeNode* GetParent ( FreeNode* n) {
return n->AddrParent; }
112 static const FreeNode* GetParent (
const FreeNode* n) {
return n->AddrParent; }
114 static void SetParent (FreeNode* n, FreeNode* p) { n->AddrParent = p; }
115 static void SetChild (FreeNode* n, UPInt i, FreeNode* c) { n->AddrChild[i] = c; }
116 static void ClearLinks(FreeNode* n) { n->AddrParent = n->AddrChild[0] = n->AddrChild[1] = 0; }
125 ReallocSucceeded = 0,
126 ReallocShrinkedAtTail = 1,
128 ReallocFailedAtTail = 3
131 AllocLite(UPInt minSize);
133 void InitSegment(TreeSeg* seg);
134 void ReleaseSegment(TreeSeg* seg);
136 void* Alloc(UPInt size, UPInt alignSize, TreeSeg** allocSeg);
137 void Free(TreeSeg* seg,
void* ptr, UPInt size, UPInt alignSize);
139 ReallocResult ReallocInPlace(TreeSeg* seg,
void* ptr, UPInt oldSize,
140 UPInt newSize, UPInt alignSize);
142 void Extend(TreeSeg* seg, UPInt incSize);
143 bool TrimAt(TreeSeg* seg, UByte* ptrAt);
145 UPInt GetMinSize()
const {
return MinSize; }
146 UPInt GetFreeBytes()
const {
return FreeBlocks << MinShift; }
147 void VisitMem(MemVisitor* visitor, MemVisitor::Category cat)
const;
148 void VisitUnused(SegVisitor* visitor,
unsigned cat)
const;
152 typedef RadixTreeMulti<FreeNode, SizeAccessor> SizeTreeType;
153 typedef RadixTree <FreeNode, AddrAccessor> AddrTreeType;
156 void pushNode(FreeNode* node, TreeSeg* seg, UPInt blocks);
157 void pullNode(FreeNode* node);
158 FreeNode* pullBest(UPInt blocks);
159 FreeNode* pullBest(UPInt blocks, UPInt alignMask);
160 void splitNode(FreeNode* node, UByte* start, UPInt size);
162 void visitTree(
const FreeNode* root,
165 MemVisitor::Category cat)
const;
167 void visitUnusedNode(
const FreeNode* node,
171 void visitUnusedInTree(
const FreeNode* root,
179 SizeTreeType SizeTree;
180 AddrTreeType AddrTree;
Definition: gamekitcrowddispersion.h:20