NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
m_heap.cpp
1#include <game/mlib/m_heap.hpp>
2#include <constants/sjis_constants.h>
3
6 nullptr,
7 GAME_HEAP_1_NAME,
8 GAME_HEAP_2_NAME,
9};
10
12
18
20 u16 ret = OPT_NONE;
21
22 if (opt & OPT_CLEAR_ALLOC) {
23 ret = MEM_HEAP_OPT_CLEAR_ALLOC;
24 }
25
26 if (opt & OPT_DEBUG_FILL) {
27 ret |= MEM_HEAP_OPT_DEBUG_FILL;
28 }
29
30 if (opt & OPT_THREAD_SAFE) {
31 ret |= MEM_HEAP_OPT_CAN_LOCK;
32 }
33
34 return ret;
35}
36
37EGG::Heap *mHeap::setCurrentHeap(EGG::Heap *heap) {
38 return heap->becomeCurrentHeap();
39}
40
41EGG::ExpHeap *mHeap::createExpHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt) {
42 if (parent == nullptr) {
43 parent = EGG::Heap::sCurrentHeap;
44 }
45
46 if (align < 0x20) {
47 align = 0x20;
48 }
49
50 if (size != -1) {
51 size = expHeapCost(size, align);
52 } else {
53 size = parent->getAllocatableSize(align);
54 }
55
56 void *buffer = parent->alloc(size, align);
57 EGG::ExpHeap *heap = nullptr;
58
59 if (buffer != nullptr) {
60 heap = EGG::ExpHeap::create(buffer, size, GetOptFlag(opt));
61 if (heap == nullptr) {
62 parent->free(buffer);
63 } else if (name != nullptr) {
64 heap->mpName = name;
65 }
66 }
67
68 return heap;
69}
70
71size_t mHeap::expHeapCost(size_t size, ulong align) {
72 return size + nw4r::ut::RoundUp<size_t>(sizeof(EGG::ExpHeap) + MEM_EXP_HEAP_HEAD_SIZE, align);
73}
74
75EGG::FrmHeap *mHeap::createFrmHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt) {
76 if (parent == nullptr) {
77 parent = EGG::Heap::sCurrentHeap;
78 }
79
80 if (align < 0x20) {
81 align = 0x20;
82 }
83
84 if (size != -1) {
85 size = frmHeapCost(size, align);
86 } else {
87 size = parent->getAllocatableSize(align);
88 }
89
90 void *buffer = parent->alloc(size, align);
91 EGG::FrmHeap *heap = nullptr;
92
93 if (buffer != nullptr) {
94 heap = EGG::FrmHeap::create(buffer, size, GetOptFlag(opt));
95 if (heap == nullptr) {
96 parent->free(buffer);
97 } else if (name != nullptr) {
98 heap->mpName = name;
99 }
100 }
101
102 return heap;
103}
104
105void mHeap::destroyFrmHeap(EGG::FrmHeap *heap) {
106 if (heap != nullptr) {
107 heap->destroy();
108 }
109}
110
111size_t mHeap::adjustFrmHeap(EGG::FrmHeap *heap) {
112 size_t totalFreeSpace = 0;
113
114 if (heap != nullptr) {
115 size_t freeSpace = heap->adjust();
116 size_t minCost = frmHeapCost(0, 4);
117 if (freeSpace >= minCost) {
118 totalFreeSpace = freeSpace - minCost;
119 }
120 }
121
122 return totalFreeSpace;
123}
124
125size_t mHeap::frmHeapCost(size_t size, ulong align) {
126 return size + nw4r::ut::RoundUp<size_t>(sizeof(EGG::FrmHeap) + MEM_FRM_HEAP_HEAD_SIZE, align);
127}
128
129EGG::UnitHeap *mHeap::createUntHeap(size_t size, ulong count, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt) {
130 if (parent == nullptr) {
131 parent = EGG::Heap::sCurrentHeap;
132 }
133
134 if (align < 0x20) {
135 align = 0x20;
136 }
137
138 size_t totalSize = untHeapCost(size, count, align);
139 EGG::UnitHeap *heap = nullptr;
140 void *buffer = parent->alloc(totalSize, align);
141
142 if (buffer != nullptr) {
143 heap = EGG::UnitHeap::create(buffer, totalSize, size, align, GetOptFlag(opt));
144 if (heap == nullptr) {
145 parent->free(buffer);
146 } else if (name != nullptr) {
147 heap->mpName = name;
148 }
149 }
150
151 return heap;
152}
153
154size_t mHeap::untHeapCost(size_t size, ulong count, ulong align) {
155 return EGG::UnitHeap::calcHeapSize(size, count, align);
156}
157
158EGG::Heap *mHeap::createHeap(size_t size, EGG::Heap *parent, const char *name) {
159 EGG::ExpHeap *heap = EGG::ExpHeap::create(size, parent, MEM_HEAP_OPT_CAN_LOCK);
160 if (heap != nullptr) {
161 heap->setAllocMode(MEM_EXP_HEAP_ALLOC_FAST);
162 if (name != nullptr) {
163 heap->mpName = name;
164 }
165 } else {
166 parent->dump();
167 }
168
169 return heap;
170}
171
173 s_SavedCurrentHeap = EGG::Heap::sCurrentHeap;
174}
175
177 s_SavedCurrentHeap->becomeCurrentHeap();
178 s_SavedCurrentHeap = nullptr;
179}
180
181EGG::FrmHeap *mHeap::createFrmHeapToCurrent(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt) {
182 EGG::FrmHeap *heap = createFrmHeap(size, parent, name, align, opt);
183 if (heap != nullptr) {
185 }
186
187 return heap;
188}
189
190EGG::Heap *mHeap::createGameHeap(int idx, size_t size, EGG::Heap *parent) {
191 if (!isValidGameHeapId(idx)) {
192 return nullptr;
193 }
194
195 g_gameHeaps[idx] = createHeap(size, parent, s_GameHeapNames[idx]);
196 if (idx == g_DefaultGameHeapId) {
198 }
199
200 return g_gameHeaps[idx];
201}
202
203EGG::Heap *mHeap::createGameHeap1(size_t size, EGG::Heap *parent) {
204 return createGameHeap(GAME_HEAP_MEM1, size, parent);
205}
206
207EGG::Heap *mHeap::createGameHeap2(size_t size, EGG::Heap *parent) {
208 return createGameHeap(GAME_HEAP_MEM2, size, parent);
209}
210
211EGG::Heap *mHeap::createArchiveHeap(size_t size, EGG::Heap *parent) {
212 g_archiveHeap = createHeap(size, parent, ARCHIVE_HEAP_NAME);
213 return g_archiveHeap;
214}
215
216EGG::Heap *mHeap::createCommandHeap(size_t size, EGG::Heap *parent) {
217 g_commandHeap = createHeap(size, parent, COMMAND_HEAP_NAME);
218 return g_commandHeap;
219}
220
221EGG::Heap *mHeap::createDylinkHeap(size_t size, EGG::Heap *parent) {
222 g_dylinkHeap = createHeap(size, parent, DYLINK_HEAP_NAME);
223 return g_dylinkHeap;
224}
225
226EGG::Heap *mHeap::createAssertHeap(EGG::Heap *parent) {
227 const char *heapName = ASSERT_HEAP_NAME;
228 size_t size = EGG::AssertHeap::getMinSizeForCreate();
229 g_assertHeap = EGG::AssertHeap::create(size, parent);
230 g_assertHeap->mpName = heapName;
231 return g_assertHeap;
232}
EGG::FrmHeap * createFrmHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates a frame heap.
Definition m_heap.cpp:75
void saveCurrentHeap()
Saves the currently active heap. The saved heap can later be restored using restoreCurrentHeap().
Definition m_heap.cpp:172
const char *const s_GameHeapNames[GAME_HEAP_COUNT]
The game heap names.
Definition m_heap.cpp:5
u16 GetOptFlag(AllocOptBit_t opt)
Converts the allocation option bits to internal MEM heap flags.
Definition m_heap.cpp:19
EGG::Heap * createAssertHeap(EGG::Heap *parent)
Creates the assert heap.
Definition m_heap.cpp:226
EGG::Heap * createGameHeap(int idx, size_t size, EGG::Heap *parent)
Creates a game heap.
Definition m_heap.cpp:190
@ GAME_HEAP_COUNT
The total number of game heaps.
Definition m_heap.hpp:41
@ GAME_HEAP_DEFAULT
The default game heap (alias of MEM1 or MEM2).
Definition m_heap.hpp:38
@ GAME_HEAP_MEM1
The game heap allocated in MEM1.
Definition m_heap.hpp:39
@ GAME_HEAP_MEM2
The game heap allocated in MEM2.
Definition m_heap.hpp:40
size_t frmHeapCost(size_t size, ulong align)
Calculates the total required size for a frame heap, including internal overhead.
Definition m_heap.cpp:125
size_t adjustFrmHeap(EGG::FrmHeap *heap)
Adjusts a frame heap to release unused memory.
Definition m_heap.cpp:111
EGG::Heap * createGameHeap2(size_t size, EGG::Heap *parent)
Creates the MEM2 game heap. See createGameHeap().
Definition m_heap.cpp:207
EGG::UnitHeap * createUntHeap(size_t size, ulong count, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates a unit heap.
Definition m_heap.cpp:129
void restoreCurrentHeap()
Restores the previously saved heap as current.
Definition m_heap.cpp:176
EGG::ExpHeap * createExpHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates an expandable heap.
Definition m_heap.cpp:41
u8 g_DefaultGameHeapId
The default game heap to be used if one isn't specified.
Definition m_heap.cpp:4
EGG::Heap * createHeap(size_t size, EGG::Heap *parent, const char *name)
Creates a generic expandable heap, with fast allocation mode and thread-safe de/allocation.
Definition m_heap.cpp:158
size_t untHeapCost(size_t size, ulong count, ulong align)
Calculates the total required size for an unit heap, including internal overhead.
Definition m_heap.cpp:154
EGG::Heap * g_gameHeaps[GAME_HEAP_COUNT]
The game heaps.
Definition m_heap.cpp:13
EGG::Heap * createGameHeap1(size_t size, EGG::Heap *parent)
Creates the MEM1 game heap. See createGameHeap().
Definition m_heap.cpp:203
EGG::Heap * createArchiveHeap(size_t size, EGG::Heap *parent)
Creates the archive heap. See createHeap().
Definition m_heap.cpp:211
void destroyFrmHeap(EGG::FrmHeap *heap)
Destroys a frame heap.
Definition m_heap.cpp:105
EGG::Heap * createCommandHeap(size_t size, EGG::Heap *parent)
Creates the DVD command heap. See createHeap().
Definition m_heap.cpp:216
EGG::Heap * g_assertHeap
The assert heap.
Definition m_heap.cpp:17
EGG::FrmHeap * createFrmHeapToCurrent(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates a frame heap and sets it as current.
Definition m_heap.cpp:181
EGG::Heap * s_SavedCurrentHeap
The saved current heap.
Definition m_heap.cpp:11
size_t expHeapCost(size_t size, ulong align)
Calculates the total required size for an expandable heap, including internal overhead.
Definition m_heap.cpp:71
EGG::Heap * g_archiveHeap
The archive resource heap.
Definition m_heap.cpp:14
EGG::Heap * g_dylinkHeap
The REL linking heap.
Definition m_heap.cpp:16
AllocOptBit_t
Bit flags controlling heap allocation behavior. These flags are translated into internal MEM heap fla...
Definition m_heap.hpp:28
@ OPT_CLEAR_ALLOC
Memory blocks are cleared upon allocation.
Definition m_heap.hpp:30
@ OPT_DEBUG_FILL
Memory blocks are filled with different values depending on the heap status.
Definition m_heap.hpp:31
@ OPT_THREAD_SAFE
Enables thread-safe memory block de/allocation.
Definition m_heap.hpp:32
@ OPT_NONE
No special allocation options.
Definition m_heap.hpp:29
EGG::Heap * createDylinkHeap(size_t size, EGG::Heap *parent)
Creates the REL linking heap. See createHeap().
Definition m_heap.cpp:221
EGG::Heap * g_commandHeap
The DVD command heap.
Definition m_heap.cpp:15
EGG::Heap * setCurrentHeap(EGG::Heap *heap)
Sets the specified heap as the current heap.
Definition m_heap.cpp:37