20 #ifndef INC_KY_Kernel_HeapPT_Granulator_H
21 #define INC_KY_Kernel_HeapPT_Granulator_H
26 namespace Kaim {
namespace HeapPT {
55 struct HdrPage : ListNode<HdrPage>
66 typedef TreeSeg Segment;
70 struct SegListAccessor
72 static void SetPrev(Segment*
self, Segment* what) {
self->AddrChild[0] = what; }
73 static void SetNext(Segment*
self, Segment* what) {
self->AddrChild[1] = what; }
74 static const Segment* GetPrev(
const Segment*
self) {
return self->AddrChild[0]; }
75 static const Segment* GetNext(
const Segment*
self) {
return self->AddrChild[1]; }
76 static Segment* GetPrev(Segment*
self) {
return self->AddrChild[0]; }
77 static Segment* GetNext(Segment*
self) {
return self->AddrChild[1]; }
81 struct SegTreeAccessor
83 static UPInt GetKey (
const Segment* n) {
return UPInt(n->Buffer); }
84 static Segment* GetChild ( Segment* n, UPInt i) {
return n->AddrChild[i]; }
85 static const Segment* GetChild (
const Segment* n, UPInt i) {
return n->AddrChild[i]; }
86 static Segment** GetChildPtr( Segment* n, UPInt i) {
return &n->AddrChild[i]; }
88 static Segment* GetParent ( Segment* n) {
return n->AddrParent; }
89 static const Segment* GetParent (
const Segment* n) {
return n->AddrParent; }
91 static void SetParent (Segment* n, Segment* p) { n->AddrParent = p; }
92 static void SetChild (Segment* n, UPInt i, Segment* c) { n->AddrChild[i] = c; }
93 static void ClearLinks(Segment* n) { n->AddrParent = n->AddrChild[0] = n->AddrChild[1] = 0; }
98 typedef List<HdrPage> HdrListType;
99 typedef List2<Segment, SegListAccessor> SegListType;
100 typedef RadixTree<Segment, SegTreeAccessor> SegTreeType;
106 Granulator(SysAllocPaged* sysAlloc,
108 UPInt granularity = 64*1024,
109 UPInt hdrPageSize = Heap_PageSize);
111 SysAllocPaged* GetSysAlloc() {
return pSysAlloc; }
113 UPInt GetGranularity() {
return Granularity; }
115 void* Alloc(UPInt size, UPInt alignSize);
116 bool ReallocInPlace(
void* oldPtr, UPInt oldSize,
117 UPInt newSize, UPInt alignSize);
119 bool Free(
void* ptr, UPInt size, UPInt alignSize);
121 UPInt GetFootprint()
const {
return Footprint; }
122 UPInt GetUsedSpace()
const {
return Footprint - Allocator.GetFreeBytes(); }
124 UPInt GetBase()
const {
return pSysAlloc->GetBase(); }
125 UPInt GetSize()
const {
return pSysAlloc->GetSize(); }
127 const TreeSeg* GetAllocSegment(
const void* ptr)
const;
129 void VisitMem(MemVisitor* visitor,
130 MemVisitor::Category catSegm,
131 MemVisitor::Category catFree)
const;
133 void VisitSegments(SegVisitor* visitor,
unsigned catSeg,
unsigned catUnused)
const;
135 UPInt GetMinBase()
const;
139 bool allocSegment(UPInt size, UPInt alignSize);
140 bool freeSegment(Segment* seg);
141 void* alloc(UPInt size, UPInt alignSize);
142 bool hasHeaders(
const Segment* seg)
const
144 return (UByte*)seg->Headers + HdrPageSize == seg->Buffer;
146 UByte* getSegBase(Segment* seg)
148 UPInt hdrSize = hasHeaders(seg) ? HdrPageSize : 0;
149 return seg->Buffer - hdrSize - seg->HeadBytes;
151 UPInt getSegSize(
const Segment* seg)
const
153 UPInt hdrSize = hasHeaders(seg) ? HdrPageSize : 0;
154 UPInt tailBytes = seg->HeadBytes ? Allocator.GetMinSize() - seg->HeadBytes : 0;
155 return seg->Size + hdrSize + seg->HeadBytes + tailBytes;
157 void visitSegments(
const Segment* node, SegVisitor* visitor,
unsigned cat)
const;
160 SysAllocPaged* pSysAlloc;
Definition: gamekitcrowddispersion.h:20