wavelength 1.0.0
Loading...
Searching...
No Matches
Heap Class Reference

Region of memory that hands out word-aligned blocks from a free list, best fit first. More...

#include <heap.h>

Public Member Functions

void Init (const char *pszName, int nNum, int *pStart, int nSizeWords, int bHandles)
 Take over a region as one free block, starting at the first word whose next word is 16-byte aligned.
void InsertFreeBlock (FreeBlock *pBlock, int nSizeWords, FreeBlock *pPrev, FreeBlock *pNext)
 Make a free block of a region and link it between two free blocks.
void GetInfo (int *pnBlocks, int *pnFreeBytes, int *pnBiggest)
 Report the free blocks.
void Resize (int nSizeWords)
 Change the size of the heap, growing or shrinking its last free block.
void * Alloc (int nSizeWords, const char *pszTag, int nAlignShift, int *pnSizeWords)
 Hand out a block from the smallest free block that fits.
void * Realloc (void *pData, int nSizeWords, int *pnSizeWords)
 Shrink a block of the heap in place, returning its spare words.
void FindNeighbors (void *pAddress, FreeBlock **ppPrev, FreeBlock **ppNext)
 Find the free blocks on each side of an address.
bool Free (void *pData)
 Return a block to the free list, merging it with its free neighbours.
void Print (PrnStream &stream, bool bSummary)
 Write the blocks of the heap in address order.
const char * Name () const
 Report the name of the heap.
void Compact (int nByteBudget, bool bStrict)
 Move the handle blocks after the first free block that has some down over it, within a budget, and update the handles.
int * GetStart () const
int GetSizeWords () const
const char * GetName () const
void Init (const char *name, int num, int *start, int sizeWords, bool flag)
 Take over a region of memory as one free run.
void FreeBlockStats (int *numBlocks, int *freeBytes, int *biggestBlock)
 Report the free runs.
void Resize (int sizeWords)
 Grow or shrink the heap by moving its end.
void * Alloc (int sizeWords, const char *name, int alignBits, int *allocatedWords)
 Allocate from the smallest free run that fits.
bool Free (void *mem)
 Return an allocation to the free list, merging it with its neighbours.
void Print (PrnStream &stream, bool summary)
 Print every run of the heap.
int * Start () const
 Report the first word of the heap.
int SizeWords () const
 Report the size of the heap.
const char * Name () const
 Report the heap's name.

Detailed Description

Region of memory that hands out word-aligned blocks from a free list, best fit first.

Region of memory carved into allocations with a sorted free list.

The class is not polymorphic. The name is inferred. The heaps are entries of a table that MemRegisterHeap() fills, and PoolMemAlloc() allocates from the heap MemPushHeap() selected.

The object is 0x18 bytes. The memory manager keeps sixteen of them. The name is inferred from the assertions of the memory manager.

Member Function Documentation

◆ Init() [1/2]

void Heap::Init ( const char * pszName,
int nNum,
int * pStart,
int nSizeWords,
int bHandles )

Take over a region as one free block, starting at the first word whose next word is 16-byte aligned.

Parameters
pszNameThe name of the heap.
nNumThe index of the heap in the table.
pStartThe region.
nSizeWordsThe size of the region in words.
bHandlesNon-zero for a heap of handle blocks Compact() may move.
Ghidra address:
NTSC-U/C: 0x00299fb8
Ghidra address:
PAL: 0x002a3bd8

◆ InsertFreeBlock()

void Heap::InsertFreeBlock ( FreeBlock * pBlock,
int nSizeWords,
FreeBlock * pPrev,
FreeBlock * pNext )

Make a free block of a region and link it between two free blocks.

Parameters
pBlockThe region.
nSizeWordsThe size of the region in words.
pPrevThe free block before, or null for the head of the list.
pNextThe free block after, or null.
Ghidra address:
NTSC-U/C: 0x00299f40
Ghidra address:
PAL: 0x002a3b60

◆ GetInfo()

void Heap::GetInfo ( int * pnBlocks,
int * pnFreeBytes,
int * pnBiggest )

Report the free blocks.

Parameters
pnBlocksReceives the number of free blocks.
pnFreeBytesReceives the free bytes.
pnBiggestReceives the bytes of the biggest free block.
Ghidra address:
NTSC-U/C: 0x00299f60
Ghidra address:
PAL: 0x002a3b80

◆ Resize() [1/2]

void Heap::Resize ( int nSizeWords)

Change the size of the heap, growing or shrinking its last free block.

The last free block is taken to end the heap.

Parameters
nSizeWordsThe size in words.
Ghidra address:
NTSC-U/C: 0x0029a018
Ghidra address:
PAL: 0x002a3c38

◆ Alloc() [1/2]

void * Heap::Alloc ( int nSizeWords,
const char * pszTag,
int nAlignShift,
int * pnSizeWords )

Hand out a block from the smallest free block that fits.

Padding of 9 or more words before the aligned block, and spare words of 9 or more after it, go back to the free list. A failure is reported and then dereferences a null block.

Parameters
nSizeWordsThe words the block needs, its header included.
pszTagThe allocation tag, which the report of a failure leaves out.
nAlignShiftThe alignment of the data as a power of two of words.
pnSizeWordsReceives the words the block takes.
Returns
The data of the block, after its header.
Ghidra address:
NTSC-U/C: 0x0029a070
Ghidra address:
PAL: 0x002a3c90

◆ Realloc()

void * Heap::Realloc ( void * pData,
int nSizeWords,
int * pnSizeWords )

Shrink a block of the heap in place, returning its spare words.

A block is never grown.

Parameters
pDataThe data of the block.
nSizeWordsThe words the data needs.
pnSizeWordsReceives the words the block takes.
Returns
pData, or null when the block is not in the heap.
Ghidra address:
NTSC-U/C: 0x0029a2b8
Ghidra address:
PAL: 0x002a3ed8

◆ FindNeighbors()

void Heap::FindNeighbors ( void * pAddress,
FreeBlock ** ppPrev,
FreeBlock ** ppNext )

Find the free blocks on each side of an address.

Parameters
pAddressThe address.
ppPrevReceives the last free block before the address, or null.
ppNextReceives the first free block at or after the address, or null.
Ghidra address:
NTSC-U/C: 0x0029a3f8
Ghidra address:
PAL: 0x002a4018

◆ Free() [1/2]

bool Heap::Free ( void * pData)

Return a block to the free list, merging it with its free neighbours.

Parameters
pDataThe data of the block.
Returns
Whether the block was in the heap.
Ghidra address:
NTSC-U/C: 0x0029a468
Ghidra address:
PAL: 0x002a4088

◆ Print() [1/2]

void Heap::Print ( PrnStream & stream,
bool bSummary )

Write the blocks of the heap in address order.

Parameters
streamThe stream to write to.
bSummaryWhether each run of allocated blocks is written as one line before each free block, rather than each allocated block on its own line.
Ghidra address:
NTSC-U/C: 0x0029a548
Ghidra address:
PAL: 0x002a4168

◆ Name() [1/2]

const char * Heap::Name ( ) const

Report the name of the heap.

Returns
mName.
Ghidra address:
NTSC-U/C: 0x0029ae28
Ghidra address:
PAL: 0x002a4a48

◆ Compact()

void Heap::Compact ( int nByteBudget,
bool bStrict )

Move the handle blocks after the first free block that has some down over it, within a budget, and update the handles.

Parameters
nByteBudgetThe most bytes to move.
bStrictWhether a block that would cross the budget stays.
Ghidra address:
NTSC-U/C: 0x0029aff0
Ghidra address:
PAL: 0x002a4c10

◆ GetStart()

int * Heap::GetStart ( ) const
inline
Returns
The first word of the heap.

◆ GetSizeWords()

int Heap::GetSizeWords ( ) const
inline
Returns
The size of the heap in words.

◆ GetName()

const char * Heap::GetName ( ) const
inline
Returns
The name of the heap.

◆ Init() [2/2]

void Heap::Init ( const char * name,
int num,
int * start,
int sizeWords,
bool flag )

Take over a region of memory as one free run.

The start is rounded up to 16 bytes, and the size shrinks by the words skipped.

Parameters
nameThe heap's name.
numThe heap's index.
startThe first word of the region.
sizeWordsThe size of the region in words.
flagThe third value of the heap's configuration entry.
Ghidra address:
0x10010280

◆ FreeBlockStats()

void Heap::FreeBlockStats ( int * numBlocks,
int * freeBytes,
int * biggestBlock )

Report the free runs.

Parameters
numBlocksReceives the number of free runs.
freeBytesReceives the bytes in all free runs.
biggestBlockReceives the bytes in the largest free run.
Ghidra address:
0x10010230

◆ Resize() [2/2]

void Heap::Resize ( int sizeWords)

Grow or shrink the heap by moving its end.

The last free run must end at the heap's end.

Parameters
sizeWordsThe new size in words.
Ghidra address:
0x100102f0

◆ Alloc() [2/2]

void * Heap::Alloc ( int sizeWords,
const char * name,
int alignBits,
int * allocatedWords )

Allocate from the smallest free run that fits.

A failure prints the heap's state through the diagnostic stream.

Parameters
sizeWordsThe words wanted, the header included.
nameThe allocation's name. The routine does not read it.
alignBitsThe allocation is aligned to 1 << alignBits words.
allocatedWordsReceives the words taken, padding included.
Returns
The allocation, after its header.
Ghidra address:
0x10010380

◆ Free() [2/2]

bool Heap::Free ( void * mem)

Return an allocation to the free list, merging it with its neighbours.

Parameters
memThe allocation.
Returns
Whether the allocation lies in this heap.
Ghidra address:
0x10010590

◆ Print() [2/2]

void Heap::Print ( PrnStream & stream,
bool summary )

Print every run of the heap.

Parameters
streamThe destination.
summaryWhen set, runs of allocations print as one line each with their count and free runs print too. When clear, every allocation prints alone and free runs are omitted.
Ghidra address:
0x10010620

◆ Start()

int * Heap::Start ( ) const
inline

Report the first word of the heap.

Returns
The first word.

◆ SizeWords()

int Heap::SizeWords ( ) const
inline

Report the size of the heap.

Returns
The size in words.

◆ Name() [2/2]

const char * Heap::Name ( ) const
inline

Report the heap's name.

Returns
The name.

The documentation for this class was generated from the following files: