NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
m_heap.hpp
1#pragma once
2#include <lib/egg/core/eggAssertHeap.h>
3#include <lib/egg/core/eggFrmHeap.h>
4#include <lib/egg/core/eggExpHeap.h>
5#include <lib/egg/core/eggUnitHeap.h>
6
7/**
8 * @brief Provides high-level heap management utilities built on top of the @ref EGG heap system.
9 * @details The mHeap namespace wraps and extends the functionality of the EGG heap framework, providing
10 * helper functions for creating and managing different heap types used by the game.
11 *
12 * It supports:
13 * - @ref EGG::ExpHeap "Expandable heaps".
14 * - @ref EGG::FrmHeap "Frame heaps".
15 * - @ref EGG::UnitHeap "Unit heaps".
16 *
17 * It also manages various game-specific heaps:
18 * - The @ref g_gameHeaps "game heaps", used for most game-related content. One game heap is created in MEM1 and one in MEM2.
19 * - The @ref g_archiveHeap "archive heap", used for loading resource files.
20 * - The @ref g_commandHeap "command heap", used for @ref mDvd_command_c "DVD commands".
21 * - The @ref g_dylinkHeap "dylink heap", used for loading and linking the game's REL files.
22 * - The @ref g_assertHeap "assert heap".
23 */
24namespace mHeap {
25
26 /// @brief Bit flags controlling heap allocation behavior.
27 /// These flags are translated into internal MEM heap flags via GetOptFlag().
29 OPT_NONE = 0, ///< No special allocation options.
30 OPT_CLEAR_ALLOC = BIT_FLAG(0), ///< Memory blocks are cleared upon allocation.
31 OPT_DEBUG_FILL = BIT_FLAG(1), ///< Memory blocks are filled with different values depending on the heap status.
32 OPT_THREAD_SAFE = BIT_FLAG(2) ///< Enables thread-safe memory block de/allocation.
33 };
34
35 /// @brief The identifiers for the predefined game heaps.
36 /// @unofficial
38 GAME_HEAP_DEFAULT, ///< The default game heap (alias of MEM1 or MEM2).
39 GAME_HEAP_MEM1, ///< The game heap allocated in MEM1.
40 GAME_HEAP_MEM2, ///< The game heap allocated in MEM2.
41 GAME_HEAP_COUNT, ///< The total number of game heaps.
42 };
43
44 /// @brief Converts the allocation option bits to internal MEM heap flags.
45 /// @param opt The allocation option bits.
46 /// @return The corresponding MEM heap flag mask.
48
49 /// @brief Sets the specified heap as the current heap.
50 /// @param heap The heap to become current.
51 /// @return The previously current heap.
52 EGG::Heap *setCurrentHeap(EGG::Heap *heap);
53
54 /// @brief Saves the currently active heap.
55 /// The saved heap can later be restored using restoreCurrentHeap().
56 void saveCurrentHeap();
57 void restoreCurrentHeap(); ///< Restores the previously saved heap as current.
58
59 /// @brief Calculates the total required size for an @ref EGG::ExpHeap "expandable heap", including internal overhead.
60 /// @param size The requested usable size.
61 /// @param align The lignment requirement.
62 /// @return The total allocation size required.
63 size_t expHeapCost(size_t size, ulong align);
64
65 /// @brief Calculates the total required size for a @ref EGG::FrmHeap "frame heap", including internal overhead.
66 /// @param size The requested usable size.
67 /// @param align The lignment requirement.
68 /// @return The total allocation size required.
69 size_t frmHeapCost(size_t size, ulong align);
70
71 /**
72 * @brief Calculates the total required size for an @ref EGG::UnitHeap "unit heap", including internal overhead.
73 * @param size The requested usable size.
74 * @param count The number of units.
75 * @param align The lignment requirement.
76 * @return The total allocation size required.
77 */
78 size_t untHeapCost(size_t size, ulong count, ulong align);
79
80 /// @brief Destroys a frame heap.
81 /// @param heap The frame heap to destroy, or @p nullptr .
82 void destroyFrmHeap(EGG::FrmHeap *heap);
83
84 /// @brief Adjusts a frame heap to release unused memory.
85 /// @param heap The frame heap to adjust, or @p nullptr .
86 /// @return The total available space in the heap, or @p 0 if the adjust operation failed.
87 size_t adjustFrmHeap(EGG::FrmHeap *heap);
88
89 /**
90 * @brief Creates an expandable heap.
91 * @param size The size of the heap, or -1 to use all space available.
92 * @param parent The parent heap.
93 * @param name The heap name, or @p nullptr .
94 * @param align The heap alignment (minimum 0x20).
95 * @param opt The allocation options.
96 * @return A pointer to the created heap, or @p nullptr on failure.
97 */
98 EGG::ExpHeap *createExpHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt);
99
100 /**
101 * @brief Creates a frame heap and sets it as current.
102 * @param size The size of the heap, or -1 to use all space available.
103 * @param parent The parent heap.
104 * @param name The heap name, or @p nullptr .
105 * @param align The heap alignment (minimum 0x20).
106 * @param opt The allocation options.
107 * @return A pointer to the created heap, or @p nullptr on failure.
108 */
109 EGG::FrmHeap *createFrmHeapToCurrent(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt);
110
111 /**
112 * @brief Creates a frame heap.
113 * @param size The size of the heap, or -1 to use all space available.
114 * @param parent The parent heap.
115 * @param name The heap name, or @p nullptr .
116 * @param align The heap alignment (minimum 0x20).
117 * @param opt The allocation options.
118 * @return A pointer to the created heap, or @p nullptr on failure.
119 */
120 EGG::FrmHeap *createFrmHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt);
121
122 /**
123 * @brief Creates a unit heap.
124 * @param size The size of the heap, or -1 to use all space available.
125 * @param count The number of units.
126 * @param parent The parent heap.
127 * @param name The heap name, or @p nullptr .
128 * @param align The heap alignment (minimum 0x20).
129 * @param opt The allocation options.
130 * @return A pointer to the created heap, or @p nullptr on failure.
131 */
132 EGG::UnitHeap *createUntHeap(size_t size, ulong count, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt);
133
134 inline bool isValidGameHeapId(u32 idx) {
135 return GAME_HEAP_MEM1 <= idx && idx <= GAME_HEAP_MEM2;
136 }
137
138 /**
139 * @brief Creates a generic expandable heap, with @ref MEM_EXP_HEAP_ALLOC_FAST "fast allocation mode" and @ref OPT_THREAD_SAFE "thread-safe de/allocation".
140 * @param size The heap size.
141 * @param parent The parent heap.
142 * @param name The heap name, or @p nullptr .
143 * @return A pointer to the created heap, or @p nullptr on failure.
144 */
145 EGG::Heap *createHeap(size_t size, EGG::Heap *parent, const char *name);
146
147 /**
148 * @brief Creates a game heap.
149 * @param idx The game heap index.
150 * @param size The heap size.
151 * @param parent The parent heap.
152 * @return A pointer to the created heap, or @p nullptr if invalid index.
153 */
154 EGG::Heap *createGameHeap(int idx, size_t size, EGG::Heap* parent);
155 EGG::Heap *createGameHeap1(size_t size, EGG::Heap *parent); ///< Creates the MEM1 game heap. See createGameHeap().
156 EGG::Heap *createGameHeap2(size_t size, EGG::Heap *parent); ///< Creates the MEM2 game heap. See createGameHeap().
157 EGG::Heap *createArchiveHeap(size_t size, EGG::Heap *parent); ///< Creates the archive heap. See createHeap().
158 EGG::Heap *createCommandHeap(size_t size, EGG::Heap *parent); ///< Creates the DVD command heap. See createHeap().
159 EGG::Heap *createDylinkHeap(size_t size, EGG::Heap *parent); ///< Creates the REL linking heap. See createHeap().
160
161 /// @brief Creates the assert heap.
162 /// @details The size is determined automatically using EGG::AssertHeap::getMinSizeForCreate().
163 EGG::Heap *createAssertHeap(EGG::Heap *parent);
164
165 extern u8 g_DefaultGameHeapId; ///< The default game heap to be used if one isn't specified.
166 extern const char * const s_GameHeapNames[GAME_HEAP_COUNT]; ///< The game heap names.
167
168 extern EGG::Heap *s_SavedCurrentHeap; ///< The saved current heap.
169 extern EGG::Heap *g_gameHeaps[GAME_HEAP_COUNT]; ///< The game heaps.
170 extern EGG::Heap *g_archiveHeap; ///< The archive resource heap.
171 extern EGG::Heap *g_commandHeap; ///< The DVD command heap.
172 extern EGG::Heap *g_dylinkHeap; ///< The REL linking heap.
173 extern EGG::Heap *g_assertHeap; ///< The assert heap.
174
175}; // namespace mHeap
Provides high-level heap management utilities built on top of the EGG heap system.
Definition m_heap.hpp:24
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_e
The identifiers for the predefined game heaps.
Definition m_heap.hpp:37
@ 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