Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
allocdesc.h
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27/*
28
29Shared layout for the generic allocator descriptor. Included by both guest code
30(which builds a descriptor and registers it) and the emulator (which reads one
31out of guest memory at registration time).
32
33The tracker built on top of this never inspects guest memory to learn about a
34block: every size and pointer comes from the intercepted call itself. That is
35what lets it work against an allocator whose internal layout is unknown. The
36only guest read is the one-time read of the descriptor below, which the guest
37handed over explicitly.
38
39Argument indices are register-relative in the MIPS O32 sense: 0..3 are a0..a3,
40and 4 and up are stack arguments at sp + 0x10 + (index - 4) * 4. Use
41PCSX_ALLOCARG_NONE for a role the entry does not have.
42
43*/
44
45#pragma once
46
47#include <stdint.h>
48
49/* Bare relative form on purpose: this header is included both from guest builds
50 (include root src/mips/) and from the emulator (include root src/), and the
51 "common/hardware/..." spelling used elsewhere in this directory only resolves
52 for the first of those. */
53#include "pcsxcmd.h"
54
55#define PCSX_ALLOCARG_NONE 0xff
56
58 /* Terminator, and the value an uninitialized entry takes. */
60 /* ptr = f(.., size, ..) -- malloc and friends, including operator new. */
62 /* ptr = f(.., count, size, ..) -- the tracked size is count * size. */
64 /* ptr = f(.., oldptr, size, ..) */
66 /* f(.., ptr, ..) -- no return value is captured. */
68 /* f(.., base, size, ..) -- resets the arena and sets its bounds. */
70};
71
73 /* Address of the function's FIRST instruction. Not any address inside it:
74 the tracker reads $ra and the incoming argument registers at this point,
75 and the prologue is what destroys both. */
76 uint32_t address;
77 uint8_t kind;
78 /* Index of the argument carrying a size. For CALLOC this is the element
79 size; for INIT this is the arena length in bytes. */
80 uint8_t argSize;
81 /* Index of the argument carrying a pointer (FREE, REALLOC) or the arena
82 base (INIT). */
83 uint8_t argPtr;
84 /* Index of the argument carrying an element count. CALLOC only. */
85 uint8_t argCount;
86};
87
89 /* op = PCSX_CMD_REGISTER_ALLOCATOR. Set size to the whole thing including
90 the entry array, or the emulator cannot tell where the entries end. */
92 /* Guest pointer to a NUL-terminated label for this arena, or 0. Read once,
93 at registration. */
94 uint32_t nameAddr;
95 /* Bounds of the arena. Both 0 means "unknown": the tracker will derive a
96 display range from the allocations it observes, and will say so rather
97 than pretending the gaps are free space. An INIT entry overrides these. */
98 uint32_t heapStart;
99 uint32_t heapEnd;
100 uint32_t entryCount;
102};
103
104/* Finding kinds, matching PCSX::AllocTracker::FindingKind. Index into
105 pcsx_AllocReport::findingCounts. */
116
118 /* op = PCSX_CMD_ALLOC_REPORT */
120 /* Input: which registered arena to report on, in registration order. */
121 uint32_t arenaIndex;
122 /* Output. */
123 volatile uint32_t arenaCount;
124 volatile uint32_t liveBlocks;
125 volatile uint32_t liveBytes;
126 volatile uint32_t peakBytes;
127 volatile uint32_t allocCount;
128 volatile uint32_t freeCount;
129 volatile uint32_t pendingCaptures;
131};
132
133PCSX_INLINE void pcsx_allocReportInit(struct pcsx_AllocReport* report, uint32_t arenaIndex) {
135 report->arenaIndex = arenaIndex;
136}
137
138/* Convenience for the common case: fill in the header for a descriptor holding
139 `count` entries. The caller still fills nameAddr/heapStart/heapEnd/entries. */
142 sizeof(struct pcsx_AllocDescriptor) + count * sizeof(struct pcsx_AllocEntry));
143 descriptor->entryCount = count;
144}
PCSX_INLINE void pcsx_allocDescriptorInit(struct pcsx_AllocDescriptor *descriptor, uint32_t count)
Definition allocdesc.h:140
PCSX_INLINE void pcsx_allocReportInit(struct pcsx_AllocReport *report, uint32_t arenaIndex)
Definition allocdesc.h:133
pcsx_AllocEntryKind
Definition allocdesc.h:57
@ PCSX_ALLOC_KIND_INIT
Definition allocdesc.h:69
@ PCSX_ALLOC_KIND_FREE
Definition allocdesc.h:67
@ PCSX_ALLOC_KIND_REALLOC
Definition allocdesc.h:65
@ PCSX_ALLOC_KIND_CALLOC
Definition allocdesc.h:63
@ PCSX_ALLOC_KIND_ALLOC
Definition allocdesc.h:61
@ PCSX_ALLOC_KIND_END
Definition allocdesc.h:59
pcsx_AllocFinding
Definition allocdesc.h:106
@ PCSX_ALLOCFINDING_INTERIOR_FREE
Definition allocdesc.h:108
@ PCSX_ALLOCFINDING_FAILED_ALLOC
Definition allocdesc.h:111
@ PCSX_ALLOCFINDING_ZERO_SIZE
Definition allocdesc.h:112
@ PCSX_ALLOCFINDING_UNKNOWN_REALLOC
Definition allocdesc.h:109
@ PCSX_ALLOCFINDING_OVERLAP
Definition allocdesc.h:110
@ PCSX_ALLOCFINDING_COUNT
Definition allocdesc.h:114
@ PCSX_ALLOCFINDING_DOUBLE_OR_UNKNOWN_FREE
Definition allocdesc.h:107
report("store KSEG0:", w_c, base)
@ PCSX_CMD_REGISTER_ALLOCATOR
Definition pcsxcmd.h:94
@ PCSX_CMD_ALLOC_REPORT
Definition pcsxcmd.h:99
PCSX_INLINE void pcsx_commandInit(struct pcsx_CommandHeader *header, uint32_t op, uint32_t size)
Definition pcsxcmd.h:189
#define PCSX_INLINE
Definition pcsxcmd.h:79
Definition allocdesc.h:88
uint32_t heapEnd
Definition allocdesc.h:99
uint32_t entryCount
Definition allocdesc.h:100
uint32_t nameAddr
Definition allocdesc.h:94
struct pcsx_CommandHeader header
Definition allocdesc.h:91
uint32_t heapStart
Definition allocdesc.h:98
struct pcsx_AllocEntry entries[]
Definition allocdesc.h:101
Definition allocdesc.h:72
uint32_t address
Definition allocdesc.h:76
uint8_t argPtr
Definition allocdesc.h:83
uint8_t argSize
Definition allocdesc.h:80
uint8_t kind
Definition allocdesc.h:77
uint8_t argCount
Definition allocdesc.h:85
Definition allocdesc.h:117
struct pcsx_CommandHeader header
Definition allocdesc.h:119
volatile uint32_t liveBytes
Definition allocdesc.h:125
volatile uint32_t pendingCaptures
Definition allocdesc.h:129
volatile uint32_t arenaCount
Definition allocdesc.h:123
uint32_t arenaIndex
Definition allocdesc.h:121
volatile uint32_t allocCount
Definition allocdesc.h:127
volatile uint32_t peakBytes
Definition allocdesc.h:126
volatile uint32_t freeCount
Definition allocdesc.h:128
volatile uint32_t liveBlocks
Definition allocdesc.h:124
volatile uint32_t findingCounts[PCSX_ALLOCFINDING_COUNT]
Definition allocdesc.h:130
Definition pcsxcmd.h:143
static const void size_t count
Definition syscalls.h:196