resonance 1.0.1
Loading...
Searching...
No Matches
mem.h File Reference
#include <stddef.h>
#include <stdio.h>
Include dependency graph for mem.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void * AllocateTaggedMemory (size_t nSize, const char *pszClass)
 Allocate a block for one object of a named class.
void OperatorDeleteOverride (void *pBlock, const char *pszClass)
 Release a block that AllocateTaggedMemory() handed out.
char * MemGetCurrentTag ()
 Address the buffer the STL allocator hook bills its allocations to.
void MemSetStlTag (const char *pszKind, int nElemSize)
 Bill the next STL allocation to a container kind and element size.
void * MemAllocTagged (size_t nSize, const char *pszTag, int nLine)
 Allocate a block and record the request against a tag.
void MemFreeTagged (void *pBlock, const char *pszTag, int nLine)
 Release a block and record the release against a tag.
int MemLogFindSource (const char *pszName)
 Find or claim the report row for a source name.
void * MemReallocTagged (void *pBlock, size_t nSize, const char *pszTag, int nLine)
 Resize a block and record the move against a tag.
void MemLogWriteMarker (const char *pszText)
 Write a marker line into the memory report.
void MemCloseLogAndReport ()
 Close the memory report and print a summary of the heap.
void MemOpenLog (const char *pszPath)
 Open the memory report and paint the stack.
void MemLogCloseAndContinue ()
 Start a new report file that retains everything written so far.
void MemLogPrint (const char *pszText)
 Write text to the memory report while logging is on.
void MemLogBeginCount ()
 Clear the per-tag accounting table and start charging it.
int MemLogEndCount (char *pszReport, int nReportSize)
 Stop charging the accounting table and format its totals.
void MemLogSourceInit ()
 Allocate the per-block tracking table and clear the per-source table.
void MemLogSourceTrackRealloc (const char *pszSource, void *pNew, void *pOld, int nSize)
 Move a tracked block to its new address after a resize.
void MemLogSourceReport (const char *pszTitle, FILE *pFile)
 Print the per-source table, sorted by name.
void DumpHeapMemoryLog (int nIndex)
 Write the memory statistics files and probe the largest possible allocation.
void * HeapAlloc (size_t nSize)
 Take a block from the backing allocator.
void HeapFree (void *pBlock)
 Give a block back to the backing allocator.
void * HeapRealloc (void *pBlock, size_t nSize)
 Resize a block through the backing allocator.
void ReportHeapCapacity ()
 Log how much of the backing allocator is free, then give it all back.

Function Documentation

◆ AllocateTaggedMemory()

void * AllocateTaggedMemory ( size_t nSize,
const char * pszClass )

Allocate a block for one object of a named class.

This is the allocator every class-specific operator new in the image forwards to. Each of those is eight instructions that pass the request through and supply the class name as the second argument. 637 call sites exist and at least 41 distinct class names appear among them, including the qualified forms Rnd::Mesh and Rnd::Cam. The tag reaches the accounting table unreduced, and the failure message reads NEW ALLOCATION FAILURE, class: s, size: d. The request is raised to one byte when it is zero, and a failure is fatal.

Parameters
nSizeThe block size in bytes.
pszClassThe class name to bill the allocation to.
Returns
The block.
Ghidra address:
NTSC-U/C: 0x004a90a0
Ghidra address:
PAL: 0x004e71b0

◆ OperatorDeleteOverride()

void OperatorDeleteOverride ( void * pBlock,
const char * pszClass )

Release a block that AllocateTaggedMemory() handed out.

The class name reaches the log line unreduced. Unlike MemFreeTagged() this path does not test the block against the zones.

Parameters
pBlockThe block to release.
pszClassThe class name the allocation was billed to.
Ghidra address:
NTSC-U/C: 0x004a91e0
Ghidra address:
PAL: 0x004e72f0

◆ MemGetCurrentTag()

char * MemGetCurrentTag ( )

Address the buffer the STL allocator hook bills its allocations to.

The buffer is the one MemSetStlTag() formats into, and MemAllocTagged() rewinds it to stl_unk after every tagged allocation. Callers pass the result straight on as a tag.

Returns
The tag buffer.
Ghidra address:
NTSC-U/C: 0x004a9090
Ghidra address:
PAL: 0x004e71a0

◆ MemSetStlTag()

void MemSetStlTag ( const char * pszKind,
int nElemSize )

Bill the next STL allocation to a container kind and element size.

The two arguments are formatted as s.d. That produces a tag such as stl_vector.8. Every STL allocation hook calls this and then passes MemGetCurrentTag() to MemAllocTagged().

Parameters
pszKindThe container kind, for example stl_vector.
nElemSizeThe element size in bytes.
Ghidra address:
NTSC-U/C: 0x004a9048
Ghidra address:
PAL: 0x004e7158

◆ MemAllocTagged()

void * MemAllocTagged ( size_t nSize,
const char * pszTag,
int nLine )

Allocate a block and record the request against a tag.

The tag is normally the caller's __FILE__ or the class name. Totals per tag accumulate into the accounting table, and the log line uses only the tag's basename. A failure is fatal.

Parameters
nSizeThe block size in bytes.
pszTagThe tag to bill the allocation to.
nLineThe caller's line number.
Returns
The block.
Ghidra address:
NTSC-U/C: 0x004a8520
Ghidra address:
PAL: 0x004e6630

◆ MemFreeTagged()

void MemFreeTagged ( void * pBlock,
const char * pszTag,
int nLine )

Release a block and record the release against a tag.

Releasing a block that belongs to a zone is fatal, because a zone hands out bump-pointer slices that cannot be reclaimed individually.

Parameters
pBlockThe block to release.
pszTagThe tag the allocation was billed to.
nLineThe caller's line number.
Ghidra address:
NTSC-U/C: 0x004a94e8
Ghidra address:
PAL: 0x004e75f8

◆ MemLogFindSource()

int MemLogFindSource ( const char * pszName)

Find or claim the report row for a source name.

The name is reduced to its basename, then matched against the interned source table of 128 rows. A name of 40 characters or more, and an exhausted table, are both fatal. The table is separate from the per-tag accounting table.

Parameters
pszNameThe source name, normally a __FILE__.
Returns
The row index.
Ghidra address:
NTSC-U/C: 0x004a86c0
Ghidra address:
PAL: 0x004e67d0

◆ MemReallocTagged()

void * MemReallocTagged ( void * pBlock,
size_t nSize,
const char * pszTag,
int nLine )

Resize a block and record the move against a tag.

Resizing a block that belongs to a zone is fatal, for the same reason releasing one is. On failure the report uses the tag and line unchanged.

Parameters
pBlockThe block to resize.
nSizeThe new size in bytes.
pszTagThe tag the allocation was billed to.
nLineThe caller's line number.
Returns
The block, which differs from pBlock only when it moved.
Ghidra address:
NTSC-U/C: 0x004a93b8
Ghidra address:
PAL: 0x004e74c8

◆ MemLogWriteMarker()

void MemLogWriteMarker ( const char * pszText)

Write a marker line into the memory report.

Performs no work while logging is off. The frame loop and HHeapLogBasicStats() are the callers.

Parameters
pszTextThe text to mark.
Ghidra address:
NTSC-U/C: 0x004a8e18
Ghidra address:
PAL: 0x004e6f28

◆ MemCloseLogAndReport()

void MemCloseLogAndReport ( )

Close the memory report and print a summary of the heap.

Closing is skipped when the report was never opened, and otherwise also clears the logging flag. The summary is the ten fields of the C library's mallinfo() and, once MemOpenLog() has painted the stack, the stack depth reached. DumpHeapMemoryLog(0) runs last. Fatal() calls this, and MemOpenLog() registers it with atexit().

Ghidra address:
NTSC-U/C: 0x004a7d30
Ghidra address:
PAL: 0x004e5e40

◆ MemOpenLog()

void MemOpenLog ( const char * pszPath)

Open the memory report and paint the stack.

A path opens the report for writing and turns logging on when the file opens. Either way the three linker symbols _stack, _stack_size, and _end are logged, every byte of the stack below its top 0x2000 bytes is set to u so that a later report can measure the depth reached, and MemCloseLogAndReport() is registered with atexit().

The name is inferred.

Parameters
pszPathThe report path, or null to paint the stack only.
Ghidra address:
NTSC-U/C: 0x004a7c40
Ghidra address:
PAL: 0x004e5d50

◆ MemLogCloseAndContinue()

void MemLogCloseAndContinue ( )

Start a new report file that retains everything written so far.

The open report is closed, its content is copied into a file named after the original path with _N before the extension, and writing continues there. The mallinfo summary and the stack depth are logged afterwards whether or not a report was open. The title is the one its log line gives.

Ghidra address:
NTSC-U/C: 0x004a7ef8
Ghidra address:
PAL: 0x004e6008

◆ MemLogPrint()

void MemLogPrint ( const char * pszText)

Write text to the memory report while logging is on.

The image has no caller. The name is inferred.

Parameters
pszTextThe text.
Ghidra address:
NTSC-U/C: 0x004a8e50
Ghidra address:
PAL: 0x004e6f60

◆ MemLogBeginCount()

void MemLogBeginCount ( )

Clear the per-tag accounting table and start charging it.

Record 0 is labelled Other_Sources. Rnd::AsyncLoader's poll brackets a load with this and MemLogEndCount().

Ghidra address:
NTSC-U/C: 0x004a8e88
Ghidra address:
PAL: 0x004e6f98

◆ MemLogEndCount()

int MemLogEndCount ( char * pszReport,
int nReportSize )

Stop charging the accounting table and format its totals.

The report begins Memory Allocated: d and adds one line per labelled record. Record 0 appears only when it has been charged. A line that would leave less than 0x40 bytes of the buffer is replaced by ...REPORT TOO LONG FOR BUFFER! and ends the report.

Parameters
pszReportReceives the report.
nReportSizeThe size of the buffer.
Returns
The total bytes charged since MemLogBeginCount().
Ghidra address:
NTSC-U/C: 0x004a8ef8
Ghidra address:
PAL: 0x004e7008

◆ MemLogSourceInit()

void MemLogSourceInit ( )

Allocate the per-block tracking table and clear the per-source table.

The block table is 32 MB, 0x200000 slots of 16 bytes. The image has no caller, so block tracking never runs in the shipped build. The name is inferred.

Ghidra address:
NTSC-U/C: 0x004a95c8
Ghidra address:
PAL: 0x004e76d8

◆ MemLogSourceTrackRealloc()

void MemLogSourceTrackRealloc ( const char * pszSource,
void * pNew,
void * pOld,
int nSize )

Move a tracked block to its new address after a resize.

An untracked old block is reported and recorded as a new allocation. Otherwise the entry takes the new address and size and its source's byte totals move by the difference. The title is the one its log line gives.

Parameters
pszSourceThe source the block is billed to.
pNewThe block after the resize.
pOldThe block before the resize.
nSizeThe new size in bytes.
Ghidra address:
NTSC-U/C: 0x004a87d0
Ghidra address:
PAL: 0x004e68e0

◆ MemLogSourceReport()

void MemLogSourceReport ( const char * pszTitle,
FILE * pFile )

Print the per-source table, sorted by name.

Performs no work before MemLogSourceInit(). DumpHeapMemoryLog() is the one caller. The name is inferred.

Parameters
pszTitleThe heading line.
pFileThe stream to write, or null for standard output.
Ghidra address:
NTSC-U/C: 0x004a8a68
Ghidra address:
PAL: 0x004e6b78

◆ DumpHeapMemoryLog()

void DumpHeapMemoryLog ( int nIndex)

Write the memory statistics files and probe the largest possible allocation.

It writes memdump_d.txt through MemLogSourceReport(). It then writes memstat_d.txt with the largest single allocation, probed downward from 128 megabytes in tenths, and the number of 2048-byte and 128-byte blocks that fit, each counted to at most 65536 and released again. Each line also goes to the log. Neither file open is checked.

Parameters
nIndexThe number the file names carry.
Ghidra address:
NTSC-U/C: 0x0054b348
Ghidra address:
PAL: 0x0058b878

◆ HeapAlloc()

void * HeapAlloc ( size_t nSize)

Take a block from the backing allocator.

The routine behaves as newlib's malloc, toolchain C library linked as shipped. It loads the reentrancy structure at 0x007819cc and calls _malloc_r at 0x0059b528. It is not the Heap class in os/heap.h, the arena the embedded Python allocates from.

Parameters
nSizeThe block size in bytes.
Returns
The block, or null when the request cannot be met.
Ghidra address:
NTSC-U/C: 0x004bfd48
Ghidra address:
PAL: 0x004fdde8

◆ HeapFree()

void HeapFree ( void * pBlock)

Give a block back to the backing allocator.

The routine behaves as newlib's free, toolchain C library linked as shipped. It loads the same reentrancy structure and tail-calls _free_r at 0x005da3b0.

Parameters
pBlockThe block to release.
Ghidra address:
NTSC-U/C: 0x004bfd70
Ghidra address:
PAL: 0x004fde10

◆ HeapRealloc()

void * HeapRealloc ( void * pBlock,
size_t nSize )

Resize a block through the backing allocator.

The toolchain routine underneath is at 0x00589278. Unlike the other two wrappers this one has no out-of-line copy, because MemReallocTagged() is its only caller and inlines it.

Parameters
pBlockThe block to resize.
nSizeThe new size in bytes.
Returns
The block, which differs from pBlock only when it moved.
Ghidra address:
NTSC-U/C: 0x00589278
Ghidra address:
PAL: 0x005cc4f0

◆ ReportHeapCapacity()

void ReportHeapCapacity ( )

Log how much of the backing allocator is free, then give it all back.

Blocks of 2048 bytes are taken through HeapAlloc() until a request fails, the count and the total are logged, and every block is released through HeapFree(). The name is inferred. The binary places the routine at the head of the TexturePairRecord unit, and it has no caller.

Ghidra address:
NTSC-U/C: 0x00246c18
Ghidra address:
PAL: 0x0025bd58