Kinoko
v0.6
A reimplementation of Mario Kart Wii's physics engine in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
Heap.cc
Go to the documentation of this file.
1
#include "
egg/core/ExpHeap.hh
"
2
3
using namespace
Kinoko
;
4
using namespace
Abstract::Memory
;
5
6
namespace
Kinoko::EGG
{
7
9
Heap::Heap
(
MEMiHeapHead
*handle) :
m_handle
(handle),
m_children
(
Disposer
::
getLinkOffset
()) {
10
m_block
=
nullptr
;
11
m_parentHeap
=
nullptr
;
12
m_name
=
"NoName"
;
13
m_flags
.
makeAllZero
();
14
15
s_heapList
.
append
(
this
);
16
}
17
19
Heap::~Heap
() {
20
s_heapList
.
remove
(
this
);
21
}
22
24
void
Heap::dispose
() {
25
Disposer
*node =
nullptr
;
26
while
((node =
reinterpret_cast<
Disposer
*
>
(
m_children
.
getFirst
()))) {
27
node->
~Disposer
();
28
}
29
30
ASSERT
(!
m_children
.
m_headObject
&& !
m_children
.
m_tailObject
);
31
}
32
33
Heap
*
Heap::becomeAllocatableHeap
() {
34
Heap
*oldHeap =
s_allocatableHeap
;
35
s_allocatableHeap
=
this
;
36
return
oldHeap;
37
}
38
40
Heap
*
Heap::becomeCurrentHeap
() {
41
Heap
*oldHeap =
s_currentHeap
;
42
s_currentHeap
=
this
;
43
return
oldHeap;
44
}
45
47
void
*
Heap::alloc
(
size_t
size,
int
align,
Heap
*pHeap) {
48
Heap
*currentHeap =
s_currentHeap
;
49
50
if
(
s_allocatableHeap
) {
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
(),
57
s_allocatableHeap
,
s_allocatableHeap
->
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
96
void
Heap::free
(
void
*block,
Heap
*pHeap) {
97
// Deleting nullptr should be no-op
98
if
(!block) {
99
return
;
100
}
101
102
if
(!pHeap) {
103
MEMiHeapHead
*handle =
MEMiHeapHead::findContainHeap
(block);
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
117
Heap
*
Heap::findHeap
(
MEMiHeapHead
*handle) {
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
129
Heap
*
Heap::findContainHeap
(
const
void
*block) {
130
MEMiHeapHead
*handle =
MEMiHeapHead::findContainHeap
(block);
131
return
handle ?
findHeap
(handle) :
nullptr
;
132
}
133
134
void
*
egg_alloc
(
size_t
size,
s32
align,
Heap
*pHeap) {
135
return
Heap::alloc
(size, align, pHeap);
136
}
137
138
void
egg_free
(
void
*block,
Heap
*pHeap) {
139
Heap::free
(block, pHeap);
140
}
141
142
}
// namespace Kinoko::EGG
143
144
MEMList
EGG::Heap::s_heapList
=
MEMList
(
EGG::Heap::getOffset
());
145
146
EGG::Heap
*
EGG::Heap::s_currentHeap
=
nullptr
;
147
EGG::Heap
*
EGG::Heap::s_allocatableHeap
=
nullptr
;
ASSERT
#define ASSERT(e)
Definition
Logger.hh:25
WARN
#define WARN(m,...)
Definition
Logger.hh:33
Kinoko::Abstract::Memory::MEMiHeapHead
A low-level representation of a memory heap for managing dynamic memory allocation....
Definition
HeapCommon.hh:21
Kinoko::Abstract::Memory::MEMiHeapHead::findContainHeap
static MEMiHeapHead * findContainHeap(const void *block)
Definition
HeapCommon.cc:30
Kinoko::EGG::Disposer::getLinkOffset
static constexpr u16 getLinkOffset()
Definition
Disposer.hh:15
Kinoko::EGG::Disposer::~Disposer
virtual ~Disposer()
Definition
Disposer.cc:16
Kinoko::EGG::Disposer::Heap
friend class Heap
Definition
Disposer.hh:12
Kinoko::EGG::Disposer::Disposer
Disposer()
Definition
Disposer.cc:8
Kinoko::EGG::Heap
A high-level representation of a memory heap for managing dynamic memory allocation....
Definition
Heap.hh:25
Kinoko::EGG::Heap::m_parentHeap
Heap * m_parentHeap
Definition
Heap.hh:132
Kinoko::EGG::Heap::getOffset
static constexpr uintptr_t getOffset()
Definition
Heap.hh:119
Kinoko::EGG::Heap::findContainHeap
static Heap * findContainHeap(const void *block)
Definition
Heap.cc:129
Kinoko::EGG::Heap::free
virtual void free(void *block)=0
Kinoko::EGG::Heap::m_handle
Abstract::Memory::MEMiHeapHead * m_handle
Definition
Heap.hh:130
Kinoko::EGG::Heap::s_currentHeap
static Heap * s_currentHeap
Definition
Heap.hh:140
Kinoko::EGG::Heap::alloc
virtual void * alloc(size_t size, s32 align)=0
Kinoko::EGG::Heap::m_children
Abstract::Memory::MEMList m_children
Definition
Heap.hh:135
Kinoko::EGG::Heap::m_block
void * m_block
Definition
Heap.hh:131
Kinoko::EGG::Heap::getStartAddress
void * getStartAddress()
Definition
Heap.hh:75
Kinoko::EGG::Heap::getEndAddress
void * getEndAddress()
Definition
Heap.hh:79
Kinoko::EGG::Heap::getName
const char * getName() const
Definition
Heap.hh:83
Kinoko::EGG::Heap::s_allocatableHeap
static Heap * s_allocatableHeap
Definition
Heap.hh:141
Kinoko::EGG::Heap::Heap
Heap(Abstract::Memory::MEMiHeapHead *handle)
Definition
Heap.cc:9
Kinoko::EGG::Heap::s_heapList
static Abstract::Memory::MEMList s_heapList
Definition
Heap.hh:138
Kinoko::EGG::Heap::becomeCurrentHeap
Heap * becomeCurrentHeap()
Definition
Heap.cc:40
Kinoko::EGG::Heap::becomeAllocatableHeap
Heap * becomeAllocatableHeap()
Definition
Heap.cc:33
Kinoko::EGG::Heap::m_name
const char * m_name
Definition
Heap.hh:136
Kinoko::EGG::Heap::dispose
void dispose()
Definition
Heap.cc:24
Kinoko::EGG::Heap::~Heap
~Heap() override
Definition
Heap.cc:19
Kinoko::EGG::Heap::findHeap
static Heap * findHeap(Abstract::Memory::MEMiHeapHead *handle)
Definition
Heap.cc:117
Kinoko::EGG::Heap::m_flags
Flags m_flags
Definition
Heap.hh:133
Kinoko::EGG::Heap::getAllocatableSize
virtual u32 getAllocatableSize(s32 align=4) const =0
ExpHeap.hh
Kinoko::Abstract::Memory
Definition
abstract/memory/ExpHeap.cc:6
Kinoko::EGG
EGG core library.
Definition
Allocator.hh:5
Kinoko::EGG::egg_free
void egg_free(void *block, Heap *pHeap)
Definition
Heap.cc:138
Kinoko::EGG::egg_alloc
void * egg_alloc(size_t size, s32 align, Heap *pHeap)
Definition
Heap.cc:134
Kinoko
Definition
abstract/Archive.cc:5
Kinoko::f32
float f32
Definition
Memory.hh:21
Kinoko::f64
double f64
Definition
Memory.hh:22
Kinoko::s32
int32_t s32
Definition
Memory.hh:13
Kinoko::u32
uint32_t u32
Definition
Memory.hh:18
Kinoko::Abstract::Memory::MEMList
Intrusive doubly-linked list. Links are placed within the corresponding object.
Definition
List.hh:14
Kinoko::Abstract::Memory::MEMList::m_headObject
void * m_headObject
Definition
List.hh:25
Kinoko::Abstract::Memory::MEMList::getNext
void * getNext(void *object)
Definition
List.cc:61
Kinoko::Abstract::Memory::MEMList::append
void append(void *object)
Definition
List.cc:16
Kinoko::Abstract::Memory::MEMList::m_tailObject
void * m_tailObject
Definition
List.hh:26
Kinoko::Abstract::Memory::MEMList::MEMList
MEMList()
Definition
List.cc:5
Kinoko::Abstract::Memory::MEMList::remove
void remove(void *object)
Definition
List.cc:34
Kinoko::Abstract::Memory::MEMList::getFirst
void * getFirst()
Definition
List.cc:56
Kinoko::EGG::TBitFlag::makeAllZero
constexpr void makeAllZero()
Resets all the bits to zero.
Definition
BitFlag.hh:246
egg
core
Heap.cc
Made by
Malleo
. Logo by
vabold
. Website generated by
Doxygen
1.18.0