resonance 1.0.1
Loading...
Searching...
No Matches
heap.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
11constexpr int kHeapHeaderSize = 0x28;
12
14constexpr int kHeapNodePrologueSize = 8;
15
17constexpr int kHeapFreeNodeSize = 0x10;
18
20constexpr int kHeapMinPayloadSize = 8;
21
23constexpr int kHeapPayloadAlignment = 8;
24
31constexpr int kHeapSplitThreshold = 0x18;
32
34constexpr unsigned kHeapFlagFirstFit = 0x1;
35
37constexpr unsigned kHeapFlagBestFit = 0x2;
38
40constexpr unsigned kHeapFlagFatalWhenFull = 0x4;
41
43constexpr unsigned kHeapFlagOwnsBlock = 0x8000;
44
62
93class Heap {
94public:
112 static Heap *Create(void *pBlock, unsigned nSize, unsigned nFlags);
113
128 void *Alloc(unsigned nSize, const char *pszFile, int nLine);
129
142 void Free(void *pBlock, const char *pszFile, int nLine);
143
160 void *Realloc(void *pBlock, unsigned nSize, const char *pszFile, int nLine);
161
174 int Shrink(unsigned nSize);
175
176private:
177 friend void HHeapDelete(Heap *pHeap);
178 friend void HHeapDump(Heap *pHeap, const char *pszPath);
179 friend void HHeapLogBasicStats(Heap *pHeap);
180
181 // Alloc(), Free(), and Realloc() each repeat these three blocks of free-list relinking, and
182 // each body appears in full at every one of those sites, which is what makes them
183 // reconstructible as members. None is a trivial accessor.
184 void UnlinkFreeNode(HeapNode *pNode);
185 void PushFreeNode(HeapNode *pNode);
186 void ReplaceFreeNode(HeapNode *pFrom, HeapNode *pTo);
187
188 HeapNode *mStart; // +0x00 the first node, which follows this header
189 unsigned mLength; // +0x04 usable bytes, excluding this header
190 HeapNode *mFreeList; // +0x08
191 unsigned mFlags; // +0x0c
192 int mUsedNodes; // +0x10
193 int mFreeNodes; // +0x14
194 int mCallsAlloc; // +0x18
195 int mCallsRealloc; // +0x1c
196 int mCallsFree; // +0x20
197 unsigned mBytes; // +0x24 payload bytes handed out
198};
199
210void HHeapDelete(Heap *pHeap);
211
223void HHeapDump(Heap *pHeap, const char *pszPath);
224
233
241extern Heap *gpPythonHeap;
Boundary-tagged heap over one contiguous block.
Definition heap.h:93
int Shrink(unsigned nSize)
Shorten the whole heap.
static Heap * Create(void *pBlock, unsigned nSize, unsigned nFlags)
Build a heap over a block.
void * Alloc(unsigned nSize, const char *pszFile, int nLine)
Take a block.
friend void HHeapDelete(Heap *pHeap)
Release a heap's backing block.
friend void HHeapLogBasicStats(Heap *pHeap)
Write a heap's counters to the log and to the memory report.
friend void HHeapDump(Heap *pHeap, const char *pszPath)
Write a heap's header, counters, timings, and every node to a file.
void Free(void *pBlock, const char *pszFile, int nLine)
Give a block back.
void * Realloc(void *pBlock, unsigned nSize, const char *pszFile, int nLine)
Resize a block.
constexpr unsigned kHeapFlagBestFit
Take the smallest free node that fits.
Definition heap.h:37
Heap * gpPythonHeap
The one interpreter heap, built by Py_Initialize() over the whole of the zone called python.
void HHeapDelete(Heap *pHeap)
Release a heap's backing block.
constexpr unsigned kHeapFlagFirstFit
Take the first free node that fits rather than the smallest.
Definition heap.h:34
void HHeapLogBasicStats(Heap *pHeap)
Write a heap's counters to the log and to the memory report.
void HHeapDump(Heap *pHeap, const char *pszPath)
Write a heap's header, counters, timings, and every node to a file.
constexpr int kHeapPayloadAlignment
Every payload size is rounded up to a multiple of this.
Definition heap.h:23
constexpr unsigned kHeapFlagFatalWhenFull
Treat exhaustion as fatal rather than reporting a null block.
Definition heap.h:40
constexpr int kHeapHeaderSize
The number of bytes the header occupies at the front of the block.
Definition heap.h:11
constexpr unsigned kHeapFlagOwnsBlock
The heap allocated its own backing block and releases it when destroyed.
Definition heap.h:43
constexpr int kHeapMinPayloadSize
The smallest payload a node may serve.
Definition heap.h:20
constexpr int kHeapFreeNodeSize
The number of bytes a free node occupies, which is all four of its words.
Definition heap.h:17
constexpr int kHeapSplitThreshold
A remainder smaller than this is not worth splitting off.
Definition heap.h:31
constexpr int kHeapNodePrologueSize
The number of bytes of a node that precede its payload.
Definition heap.h:14
One node of a heap, which is the boundary tag in front of a block.
Definition heap.h:56
HeapNode * mNext
Definition heap.h:58
uintptr_t mPrevAndFree
Definition heap.h:57
HeapNode * mFreePrev
Definition heap.h:59
HeapNode * mFreeNext
Definition heap.h:60