A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Heap.cc
Go to the documentation of this file.
1#include "egg/core/ExpHeap.hh"
2
3using namespace Kinoko;
4using namespace Abstract::Memory;
5
6namespace Kinoko::EGG {
7
10 m_block = nullptr;
11 m_parentHeap = nullptr;
12 m_name = "NoName";
14
15 s_heapList.append(this);
16}
17
20 s_heapList.remove(this);
21}
22
25 Disposer *node = nullptr;
26 while ((node = reinterpret_cast<Disposer *>(m_children.getFirst()))) {
27 node->~Disposer();
28 }
29
31}
32
34 Heap *oldHeap = s_allocatableHeap;
35 s_allocatableHeap = this;
36 return oldHeap;
37}
38
41 Heap *oldHeap = s_currentHeap;
42 s_currentHeap = this;
43 return oldHeap;
44}
45
47void *Heap::alloc(size_t size, int align, Heap *pHeap) {
48 Heap *currentHeap = s_currentHeap;
49
51 if (currentHeap && !pHeap) {
52 pHeap = currentHeap;
53 }
54
55 if (pHeap != s_allocatableHeap) {
56 WARN("HEAP ALLOC FAIL (%p, %s): Allocatable heap is %p (%s)", pHeap, pHeap->getName(),
58
59 return nullptr;
60 }
61 }
62
63 if (pHeap) {
64 return pHeap->alloc(size, align);
65 }
66
67 if (currentHeap) {
68 void *block = currentHeap->alloc(size, align);
69
70 if (!block) {
71 u32 heapFreeSize = currentHeap->getAllocatableSize(0x4);
72 s32 heapSize = GetAddrNum(currentHeap->getEndAddress()) -
73 GetAddrNum(currentHeap->getStartAddress());
74
75 constexpr f32 BYTES_TO_MBYTES = 1024.0f * 1024.0f;
76 f32 heapSizeMB = static_cast<f32>(heapSize) / BYTES_TO_MBYTES;
77 f32 heapFreeSizeMB = static_cast<f32>(heapFreeSize) / BYTES_TO_MBYTES;
78 f32 sizeMB = static_cast<f32>(size) / BYTES_TO_MBYTES;
79
80 WARN("HEAP ALLOC FAIL (%p, %s):\nTotal bytes: %d (%.1fMBytes)\nFree bytes: %d "
81 "(%.1fMBytes)\nAlloc bytes: %zu "
82 "(%.1fMBytes)\nAlign: %d",
83 currentHeap, currentHeap->getName(), heapSize, static_cast<f64>(heapSizeMB),
84 heapFreeSize, static_cast<f64>(heapFreeSizeMB), size, static_cast<f64>(sizeMB),
85 align);
86 }
87
88 return block;
89 }
90
91 WARN("HEAP ALLOC FAIL: Cannot allocate %zu from heap %p", size, pHeap);
92 return nullptr;
93}
94
96void Heap::free(void *block, Heap *pHeap) {
97 // Deleting nullptr should be no-op
98 if (!block) {
99 return;
100 }
101
102 if (!pHeap) {
104 if (!handle) {
105 return;
106 }
107
108 pHeap = findHeap(handle);
109 if (!pHeap) {
110 return;
111 }
112 }
113
114 pHeap->free(block);
115}
116
118 Heap *node = nullptr;
119 while ((node = reinterpret_cast<Heap *>(s_heapList.getNext(node)))) {
120 if (node->m_handle == handle) {
121 return node;
122 }
123 }
124
125 return nullptr;
126}
127
129Heap *Heap::findContainHeap(const void *block) {
131 return handle ? findHeap(handle) : nullptr;
132}
133
134void *egg_alloc(size_t size, s32 align, Heap *pHeap) {
135 return Heap::alloc(size, align, pHeap);
136}
137
138void egg_free(void *block, Heap *pHeap) {
139 Heap::free(block, pHeap);
140}
141
142} // namespace Kinoko::EGG
143
145
#define ASSERT(e)
Definition Logger.hh:25
#define WARN(m,...)
Definition Logger.hh:33
A low-level representation of a memory heap for managing dynamic memory allocation....
Definition HeapCommon.hh:21
static MEMiHeapHead * findContainHeap(const void *block)
Definition HeapCommon.cc:30
static constexpr u16 getLinkOffset()
Definition Disposer.hh:15
friend class Heap
Definition Disposer.hh:12
A high-level representation of a memory heap for managing dynamic memory allocation....
Definition Heap.hh:25
Heap * m_parentHeap
Definition Heap.hh:132
static constexpr uintptr_t getOffset()
Definition Heap.hh:119
static Heap * findContainHeap(const void *block)
Definition Heap.cc:129
virtual void free(void *block)=0
Abstract::Memory::MEMiHeapHead * m_handle
Definition Heap.hh:130
static Heap * s_currentHeap
Definition Heap.hh:140
virtual void * alloc(size_t size, s32 align)=0
Abstract::Memory::MEMList m_children
Definition Heap.hh:135
void * m_block
Definition Heap.hh:131
void * getStartAddress()
Definition Heap.hh:75
void * getEndAddress()
Definition Heap.hh:79
const char * getName() const
Definition Heap.hh:83
static Heap * s_allocatableHeap
Definition Heap.hh:141
Heap(Abstract::Memory::MEMiHeapHead *handle)
Definition Heap.cc:9
static Abstract::Memory::MEMList s_heapList
Definition Heap.hh:138
Heap * becomeCurrentHeap()
Definition Heap.cc:40
Heap * becomeAllocatableHeap()
Definition Heap.cc:33
const char * m_name
Definition Heap.hh:136
void dispose()
Definition Heap.cc:24
~Heap() override
Definition Heap.cc:19
static Heap * findHeap(Abstract::Memory::MEMiHeapHead *handle)
Definition Heap.cc:117
virtual u32 getAllocatableSize(s32 align=4) const =0
EGG core library.
Definition Allocator.hh:5
void egg_free(void *block, Heap *pHeap)
Definition Heap.cc:138
void * egg_alloc(size_t size, s32 align, Heap *pHeap)
Definition Heap.cc:134
float f32
Definition Memory.hh:21
double f64
Definition Memory.hh:22
int32_t s32
Definition Memory.hh:13
uint32_t u32
Definition Memory.hh:18
Intrusive doubly-linked list. Links are placed within the corresponding object.
Definition List.hh:14
void * getNext(void *object)
Definition List.cc:61
void append(void *object)
Definition List.cc:16
void remove(void *object)
Definition List.cc:34
constexpr void makeAllZero()
Resets all the bits to zero.
Definition BitFlag.hh:246