A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
FrmHeap.cc
1#include "FrmHeap.hh"
2
3namespace Kinoko::EGG {
4
5using namespace Abstract::Memory;
6
7FrmHeap::FrmHeap(MEMiHeapHead *handle) : Heap(handle) {}
8
9FrmHeap::~FrmHeap() {
10 dispose();
11 dynamicCastHandleToFrm()->destroy();
12}
13
14FrmHeap *FrmHeap::create(void *startAddress, size_t size, u16 opt) {
15 FrmHeap *heap = nullptr;
16 void *buffer = startAddress;
17
18 void *endAddress = RoundDown(AddOffset(startAddress, size), 4);
19 startAddress = RoundUp(startAddress, 4);
20
21 size_t addrRange = GetAddrNum(endAddress) - GetAddrNum(startAddress);
22 if (startAddress > endAddress || addrRange < sizeof(FrmHeap) + 4) {
23 return nullptr;
24 }
25
26 void *handleStart = AddOffset(startAddress, sizeof(FrmHeap));
27 MEMiFrmHeapHead *handle =
28 MEMiFrmHeapHead::create(handleStart, addrRange - sizeof(FrmHeap), opt);
29 if (handle) {
30 heap = new (startAddress) FrmHeap(handle);
31 heap->registerHeapBuffer(buffer);
32 }
33
34 return heap;
35}
36
37FrmHeap *FrmHeap::create(size_t size, Heap *pHeap, u16 opt) {
38 FrmHeap *heap = nullptr;
39
40 if (!pHeap) {
41 pHeap = Heap::getCurrentHeap();
42 }
43
44 if (size == std::numeric_limits<size_t>::max()) {
45 size = pHeap->getAllocatableSize();
46 }
47
48 void *block = pHeap->alloc(size, 4);
49 if (block) {
50 heap = create(block, size, opt);
51 if (heap) {
52 heap->setParentHeap(pHeap);
53 } else {
54 pHeap->free(block);
55 }
56 }
57
58 return heap;
59}
60
61void FrmHeap::destroy() {
62 Heap *pParent = getParentHeap();
63 this->~FrmHeap();
64 if (pParent) {
65 pParent->free(this);
66 }
67}
68
69void *FrmHeap::alloc(size_t size, s32 align) {
70 return dynamicCastHandleToFrm()->alloc(size, align);
71}
72
73// Frame heaps can only free to the heap start or heap end depending on flags
74// Freeing by block is invalid because we don't recover the free memory, so this is no-op
75void FrmHeap::free(void *) {}
76
77u32 FrmHeap::getAllocatableSize(s32 align) const {
78 return dynamicCastHandleToFrm()->getAllocatableSize(align);
79}
80
81MEMiFrmHeapHead *FrmHeap::dynamicCastHandleToFrm() {
82 return reinterpret_cast<MEMiFrmHeapHead *>(m_handle);
83}
84
85const MEMiFrmHeapHead *FrmHeap::dynamicCastHandleToFrm() const {
86 return reinterpret_cast<MEMiFrmHeapHead *>(m_handle);
87}
88
89} // namespace Kinoko::EGG
A low-level representation of a memory heap for managing dynamic memory allocation....
Definition HeapCommon.hh:21
A high-level representation of a memory heap for managing dynamic memory allocation....
Definition Heap.hh:25
EGG core library.
Definition Allocator.hh:5