1#include <game/mlib/m_heap.hpp>
2#include <constants/sjis_constants.h>
23 ret = MEM_HEAP_OPT_CLEAR_ALLOC;
27 ret |= MEM_HEAP_OPT_DEBUG_FILL;
31 ret |= MEM_HEAP_OPT_CAN_LOCK;
38 return heap->becomeCurrentHeap();
42 if (parent ==
nullptr) {
43 parent = EGG::Heap::sCurrentHeap;
53 size = parent->getAllocatableSize(align);
56 void *buffer = parent->alloc(size, align);
57 EGG::ExpHeap *heap =
nullptr;
59 if (buffer !=
nullptr) {
60 heap = EGG::ExpHeap::create(buffer, size,
GetOptFlag(opt));
61 if (heap ==
nullptr) {
63 }
else if (name !=
nullptr) {
72 return size + nw4r::ut::RoundUp<size_t>(
sizeof(EGG::ExpHeap) + MEM_EXP_HEAP_HEAD_SIZE, align);
76 if (parent ==
nullptr) {
77 parent = EGG::Heap::sCurrentHeap;
87 size = parent->getAllocatableSize(align);
90 void *buffer = parent->alloc(size, align);
91 EGG::FrmHeap *heap =
nullptr;
93 if (buffer !=
nullptr) {
94 heap = EGG::FrmHeap::create(buffer, size,
GetOptFlag(opt));
95 if (heap ==
nullptr) {
97 }
else if (name !=
nullptr) {
106 if (heap !=
nullptr) {
112 size_t totalFreeSpace = 0;
114 if (heap !=
nullptr) {
115 size_t freeSpace = heap->adjust();
117 if (freeSpace >= minCost) {
118 totalFreeSpace = freeSpace - minCost;
122 return totalFreeSpace;
126 return size + nw4r::ut::RoundUp<size_t>(
sizeof(EGG::FrmHeap) + MEM_FRM_HEAP_HEAD_SIZE, align);
130 if (parent ==
nullptr) {
131 parent = EGG::Heap::sCurrentHeap;
138 size_t totalSize =
untHeapCost(size, count, align);
139 EGG::UnitHeap *heap =
nullptr;
140 void *buffer = parent->alloc(totalSize, align);
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) {
155 return EGG::UnitHeap::calcHeapSize(size, count, align);
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) {
182 EGG::FrmHeap *heap =
createFrmHeap(size, parent, name, align, opt);
183 if (heap !=
nullptr) {
191 if (!isValidGameHeapId(idx)) {
227 const char *heapName = ASSERT_HEAP_NAME;
228 size_t size = EGG::AssertHeap::getMinSizeForCreate();
EGG::FrmHeap * createFrmHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates a frame heap.
void saveCurrentHeap()
Saves the currently active heap. The saved heap can later be restored using restoreCurrentHeap().
const char *const s_GameHeapNames[GAME_HEAP_COUNT]
The game heap names.
u16 GetOptFlag(AllocOptBit_t opt)
Converts the allocation option bits to internal MEM heap flags.
EGG::Heap * createAssertHeap(EGG::Heap *parent)
Creates the assert heap.
EGG::Heap * createGameHeap(int idx, size_t size, EGG::Heap *parent)
Creates a game heap.
@ GAME_HEAP_COUNT
The total number of game heaps.
@ GAME_HEAP_DEFAULT
The default game heap (alias of MEM1 or MEM2).
@ GAME_HEAP_MEM1
The game heap allocated in MEM1.
@ GAME_HEAP_MEM2
The game heap allocated in MEM2.
size_t frmHeapCost(size_t size, ulong align)
Calculates the total required size for a frame heap, including internal overhead.
size_t adjustFrmHeap(EGG::FrmHeap *heap)
Adjusts a frame heap to release unused memory.
EGG::Heap * createGameHeap2(size_t size, EGG::Heap *parent)
Creates the MEM2 game heap. See createGameHeap().
EGG::UnitHeap * createUntHeap(size_t size, ulong count, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates a unit heap.
void restoreCurrentHeap()
Restores the previously saved heap as current.
EGG::ExpHeap * createExpHeap(size_t size, EGG::Heap *parent, const char *name, ulong align, AllocOptBit_t opt)
Creates an expandable heap.
u8 g_DefaultGameHeapId
The default game heap to be used if one isn't specified.
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.
size_t untHeapCost(size_t size, ulong count, ulong align)
Calculates the total required size for an unit heap, including internal overhead.
EGG::Heap * g_gameHeaps[GAME_HEAP_COUNT]
The game heaps.
EGG::Heap * createGameHeap1(size_t size, EGG::Heap *parent)
Creates the MEM1 game heap. See createGameHeap().
EGG::Heap * createArchiveHeap(size_t size, EGG::Heap *parent)
Creates the archive heap. See createHeap().
void destroyFrmHeap(EGG::FrmHeap *heap)
Destroys a frame heap.
EGG::Heap * createCommandHeap(size_t size, EGG::Heap *parent)
Creates the DVD command heap. See createHeap().
EGG::Heap * g_assertHeap
The assert heap.
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.
EGG::Heap * s_SavedCurrentHeap
The saved current heap.
size_t expHeapCost(size_t size, ulong align)
Calculates the total required size for an expandable heap, including internal overhead.
EGG::Heap * g_archiveHeap
The archive resource heap.
EGG::Heap * g_dylinkHeap
The REL linking heap.
AllocOptBit_t
Bit flags controlling heap allocation behavior. These flags are translated into internal MEM heap fla...
@ OPT_CLEAR_ALLOC
Memory blocks are cleared upon allocation.
@ OPT_DEBUG_FILL
Memory blocks are filled with different values depending on the heap status.
@ OPT_THREAD_SAFE
Enables thread-safe memory block de/allocation.
@ OPT_NONE
No special allocation options.
EGG::Heap * createDylinkHeap(size_t size, EGG::Heap *parent)
Creates the REL linking heap. See createHeap().
EGG::Heap * g_commandHeap
The DVD command heap.
EGG::Heap * setCurrentHeap(EGG::Heap *heap)
Sets the specified heap as the current heap.