summaryrefslogtreecommitdiff
path: root/src/m/m_allocator.cpp
blob: 6211fa00b1f7cd77192712f2a114a1dea9fe9d99 (plain)
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
#include <egg/core/eggAllocator.h>
#include <m/m_allocator.h>
#include <m/m_heap.h>

// TODO this include is weird
#include <egg/core/eggAssertHeap.h>
#include <egg/core/eggHeap.h>

mAllocator_c::mAllocator_c() : EGG::Allocator(mHeap::g_assertHeap, 0x04) {}

mAllocator_c::~mAllocator_c() {}

bool mAllocator_c::attach(EGG::Heap *heap, int align) {
    if (heap == nullptr) {
        heap = EGG::Heap::sCurrentHeap;
    }
    this->align = align;
    this->mHeap = heap;
    heap->initAllocator(this, align);
    return true;
}

void *mAllocator_c::alloc(u32 size) {
    return EGG::Allocator::alloc(size);
}

void mAllocator_c::free(void *block) {
    EGG::Allocator::free(block);
}

mHeapAllocator_c::mHeapAllocator_c() {}
mHeapAllocator_c::~mHeapAllocator_c() {
    destroyHeap();
}

bool mHeapAllocator_c::replaceWithNewFrmHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 attrs) {
    destroyHeap();
    EGG::Heap *heap = mHeap::createFrmHeap(size, newHeap, heapName, align, attrs);
    if (heap == nullptr) {
        return false;
    }
    attach(heap, align);
    return true;
}

bool mHeapAllocator_c::replaceWithNewExpHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 attrs) {
    destroyHeap();
    EGG::Heap *heap = mHeap::createExpHeap(size, newHeap, heapName, align, attrs);
    if (heap == nullptr) {
        return false;
    }
    attach(heap, align);
    return true;
}

void mHeapAllocator_c::destroyHeap() {
    EGG::Heap *assertHeap = mHeap::g_assertHeap;
    if (mHeap != assertHeap) {
        mHeap->destroy();
        mHeap = assertHeap;
    }
}

inline EGG::Heap *getHeapOfKind(EGG::Heap *heap, EGG::Heap::eHeapKind kind) {
    if (heap != nullptr && heap->getHeapKind() == kind) {
        return heap;
    }
    return nullptr;
}

s32 mHeapAllocator_c::adjustFrmHeap() {
    EGG::Heap *heap = mHeap;
    if (heap == mHeap::g_assertHeap) {
        return 0;
    }
    return mHeap::adjustFrmHeap(static_cast<EGG::FrmHeap *>(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_FRAME)));
}

s32 mHeapAllocator_c::adjustExpHeap() {
    EGG::Heap *heap = mHeap;
    if (heap == mHeap::g_assertHeap) {
        return 0;
    }
    return mHeap::adjustExpHeap(static_cast<EGG::ExpHeap *>(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_EXPANDED)));
}

bool mHeapAllocator_c::createNewTempFrmHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 attrs) {
    if (!replaceWithNewFrmHeap(size, newHeap, heapName, align, attrs)) {
        return false;
    }
    mHeap::saveCurrentHeap();
    mHeap::setCurrentHeap(mHeap);
    return true;
}

void mHeapAllocator_c::adjustFrmHeapRestoreCurrent() {
    mHeap::restoreCurrentHeap();
    EGG::Heap *heap = mHeap;
    mHeap::adjustFrmHeap(static_cast<EGG::FrmHeap *>(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_FRAME)));
}

void *operator new[](size_t size, mAllocator_c *allocator) {
    return allocator->alloc(size);
}