-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiheap.cpp
More file actions
244 lines (187 loc) · 5.2 KB
/
multiheap.cpp
File metadata and controls
244 lines (187 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "SM64DS_PI.h"
#include <new>
#include <array>
#include <functional>
#define ITCM_ARENA_START 0x01ffdf40
#define MAIN_RAM_START 0x02000000
#define MAIN_RAM_CODE_START 0x02004000
#define LEVEL_OVERLAY_START 0x0214eaa0
#define INSERTED_CODE_START 0x02156aa0
#define DTCM_START 0x023c0000
#define DTCM_END 0x023c4000
#define ARM7_ARENA_START 0x023d80e0
#define FLASHCARD_CODE_START 0x023fc000
#define STR(x) #x
#define REGION(name, start, end) extern char name[(end) - (start)]; \
asm(#name " = " STR(start));
REGION(UNUSED_START_OF_RAM, MAIN_RAM_START, MAIN_RAM_CODE_START)
REGION(RAM_BEFORE_INSERTED_CODE, LEVEL_OVERLAY_START, INSERTED_CODE_START)
REGION(UNUSED_END_OF_RAM, DTCM_END, FLASHCARD_CODE_START)
struct MemoryRange
{
char* start;
char* end;
template<unsigned size>
constexpr MemoryRange(char(&arr)[size]):
start(arr),
end(arr + size)
{}
constexpr bool Contains(const void* ptr) const
{
static constexpr std::less<const void*> less = {};
return !less(ptr, start) && less(ptr, end);
}
constexpr ExpandingHeap& GetHeap() const
{
return *std::launder(reinterpret_cast<ExpandingHeap*>(start));
}
void ConstuctHeap() const
{
char* const heapStart = start + sizeof(ExpandingHeap);
const unsigned heapSize = end - heapStart;
auto* allocator = Heap::CreateExpandingHeapAllocator(heapStart, heapSize, 3);
if (!allocator) Crash();
new (start) ExpandingHeap(heapStart, heapSize, nullptr, allocator);
}
};
struct ExtraHeapIterator
{
const MemoryRange* memRange;
constexpr bool operator==(const ExtraHeapIterator& other) const = default;
constexpr ExpandingHeap& operator*() const
{
return memRange->GetHeap();
}
constexpr ExtraHeapIterator& operator++()
{
++memRange;
return *this;
}
};
struct ExtraHeaps
{
std::array<MemoryRange, 3> memRanges
{
UNUSED_START_OF_RAM,
RAM_BEFORE_INSERTED_CODE,
UNUSED_END_OF_RAM
};
void Init()
{
InitFileSystem();
OverlayInfo ovInfo;
unsigned maxLevelOverlaySize = 0;
for (unsigned i = 0; i < 52; ++i)
{
unsigned ovID = i + 103;
if (!LoadOverlayInfo(ovInfo, false, ovID))
Crash();
const unsigned overlaySize = ovInfo.loadSize + ovInfo.bssSize;
if (maxLevelOverlaySize < overlaySize)
maxLevelOverlaySize = overlaySize;
}
memRanges[1].start += maxLevelOverlaySize;
const bool momExists = LoadOverlayInfo(ovInfo, false, 155);
if (momExists)
{
const unsigned momOverlaySize = ovInfo.loadSize + ovInfo.bssSize;
memRanges[2].start += momOverlaySize;
}
for (const MemoryRange& memRange : memRanges)
memRange.ConstuctHeap();
}
constexpr ExtraHeapIterator begin()
{
return {memRanges.begin()};
}
constexpr ExtraHeapIterator end()
{
return {memRanges.end()};
}
}
constinit extraHeaps;
class MultiHeap : public ExpandingHeap
{
ExpandingHeap& GetHeap(const void* ptr)
{
for (const MemoryRange& extraHeapMemRange : extraHeaps.memRanges)
{
if (extraHeapMemRange.Contains(ptr))
return extraHeapMemRange.GetHeap();
}
return *this;
}
public:
MultiHeap(void* start, unsigned size, Heap* root, ExpandingHeapAllocator* allocator);
virtual void VDestroy() override
{
ExpandingHeap::VDestroy();
for (ExpandingHeap& extraHeap : extraHeaps)
extraHeap.Destroy();
}
virtual void* VAllocate(unsigned size, int align) override
{
for (ExpandingHeap& extraHeap : extraHeaps)
{
void* res = extraHeap.ExpandingHeap::VAllocate(size, align);
if (res) return res;
}
return ExpandingHeap::VAllocate(size, align);
}
virtual bool VDeallocate(void* ptr) override
{
return GetHeap(ptr).ExpandingHeap::VDeallocate(ptr);
}
virtual void VDeallocateAll() override
{
ExpandingHeap::VDeallocateAll();
for (ExpandingHeap& extraHeap : extraHeaps)
extraHeap.ExpandingHeap::VDeallocateAll();
}
virtual unsigned VReallocate(void* ptr, unsigned newSize) override
{
return GetHeap(ptr).ExpandingHeap::VReallocate(ptr, newSize);
}
virtual unsigned VSizeof(const void* ptr) override
{
return GetHeap(ptr).ExpandingHeap::VSizeof(ptr);
}
virtual unsigned VMaxAllocationUnitSize() override
{
unsigned maxSize = ExpandingHeap::VMaxAllocationUnitSize();
for (ExpandingHeap& extraHeap : extraHeaps)
{
const unsigned newSize = extraHeap.ExpandingHeap::VMaxAllocationUnitSize();
if (maxSize < newSize)
maxSize = newSize;
}
return maxSize;
}
virtual unsigned VMaxAllocatableSize() override
{
unsigned maxSize = ExpandingHeap::VMaxAllocatableSize();
for (ExpandingHeap& extraHeap : extraHeaps)
{
const unsigned newSize = extraHeap.ExpandingHeap::VMaxAllocatableSize();
if (maxSize < newSize)
maxSize = newSize;
}
return maxSize;
}
virtual unsigned VMemoryLeft() override
{
unsigned res = ExpandingHeap::VMemoryLeft();
for (ExpandingHeap& extraHeap : extraHeaps)
res += extraHeap.ExpandingHeap::VMemoryLeft();
return res;
}
};
[[gnu::flatten]]
MultiHeap::MultiHeap(void* start, unsigned size, Heap* root, ExpandingHeapAllocator* allocator):
ExpandingHeap(start, size, root, allocator)
{
extraHeaps.Init();
}
static_assert(sizeof(MultiHeap) == sizeof(ExpandingHeap));
asm("nsub_0201a0d8 = 0x0201a0dc");
asm("repl_0203c948 = _ZN9MultiHeapC1EPvjP4HeapP22ExpandingHeapAllocator");