Kinoko
A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Heap.hh
1
#pragma once
2
3
#include "egg/core/Disposer.hh"
4
5
#include <abstract/memory/HeapCommon.hh>
6
7
#include <list>
8
#include <new>
9
10
namespace
Host
{
11
12
class
Context;
13
14
}
// namespace Host
15
16
namespace
EGG
{
17
18
class
ExpHeap;
19
22
class
Heap
:
Disposer
{
23
friend
class
Host::Context
;
24
25
public
:
26
enum class
Kind {
27
None,
28
Expanded,
29
Frame,
30
Unit,
31
Assert,
32
};
33
34
Heap
(
Abstract::Memory::MEMiHeapHead
*handle);
35
~Heap
()
override
;
36
37
virtual
void
destroy() = 0;
38
virtual
Kind getHeapKind()
const
= 0;
39
virtual
void
*alloc(
size_t
size,
s32
align) = 0;
40
virtual
void
free(
void
*block) = 0;
41
virtual
u32
getAllocatableSize(
s32
align = 4)
const
= 0;
42
43
void
dispose();
44
45
void
disableAllocation() {
46
m_flags.
setBit
(eFlags::Lock);
47
}
48
49
void
enableAllocation() {
50
m_flags.
resetBit
(eFlags::Lock);
51
}
52
53
[[nodiscard]]
bool
tstDisableAllocation()
const
{
54
return
m_flags.
onBit
(eFlags::Lock);
55
}
56
57
void
appendDisposer(
Disposer
*disposer) {
58
m_children.append(disposer);
59
}
60
61
void
removeDisposer(
Disposer
*disposer) {
62
m_children.remove(disposer);
63
}
64
65
Heap
*becomeAllocatableHeap();
66
Heap
*becomeCurrentHeap();
67
68
void
registerHeapBuffer(
void
*buffer) {
69
m_block = buffer;
70
}
71
72
[[nodiscard]]
void
*getStartAddress() {
73
return
this
;
74
}
75
76
[[nodiscard]]
void
*getEndAddress() {
77
return
m_handle->getHeapEnd();
78
}
79
80
[[nodiscard]]
const
char
*getName()
const
{
81
return
m_name;
82
}
83
85
[[nodiscard]]
Heap
*getParentHeap()
const
{
86
return
m_parentHeap;
87
}
88
89
void
setName(
const
char
*name) {
90
m_name = name;
91
}
92
93
void
setParentHeap(
Heap
*heap) {
94
m_parentHeap = heap;
95
}
96
97
static
void
initialize();
98
[[nodiscard]]
static
void
*alloc(
size_t
size,
int
align,
Heap
*pHeap);
99
static
void
free(
void
*block,
Heap
*pHeap);
100
101
[[nodiscard]]
static
Heap
*findHeap(
Abstract::Memory::MEMiHeapHead
*handle);
102
[[nodiscard]]
static
Heap
*findContainHeap(
const
void
*block);
103
104
[[nodiscard]]
static
ExpHeap
*dynamicCastToExp(
Heap
*heap) {
105
return
heap->getHeapKind() == Kind::Expanded ?
reinterpret_cast<
ExpHeap
*
>
(heap) :
nullptr
;
106
}
107
108
[[nodiscard]]
static
Heap
*getCurrentHeap() {
109
return
s_currentHeap;
110
}
111
112
[[nodiscard]]
static
constexpr
uintptr_t getOffset() {
113
// offsetof doesn't work, so instead of hardcoding an offset, we derive it ourselves
114
return
reinterpret_cast<
uintptr_t
>
(&
reinterpret_cast<
Heap
*
>
(NULL)->m_link);
115
}
116
117
protected
:
118
enum class
eFlags {
119
Lock = 0,
120
};
121
typedef
TBitFlag<u16, eFlags>
Flags
;
122
123
Abstract::Memory::MEMiHeapHead
*m_handle;
124
void
*m_block;
125
Heap
*m_parentHeap;
126
Flags
m_flags;
127
Abstract::Memory::MEMLink
m_link;
128
Abstract::Memory::MEMList
m_children;
129
const
char
*m_name;
130
131
static
Abstract::Memory::MEMList
s_heapList;
132
133
static
Heap
*s_currentHeap;
134
static
Heap
*s_allocatableHeap;
135
};
136
137
}
// namespace EGG
138
139
[[nodiscard]]
void
*
operator
new
(
size_t
size)
noexcept
;
140
[[nodiscard]]
void
*
operator
new
(
size_t
size,
int
align)
noexcept
;
141
[[nodiscard]]
void
*
operator
new
(
size_t
size,
EGG::Heap
*heap,
int
align)
noexcept
;
142
[[nodiscard]]
void
*
operator
new
[](
size_t
size)
noexcept
;
143
[[nodiscard]]
void
*
operator
new
[](
size_t
size,
int
align)
noexcept
;
144
[[nodiscard]]
void
*
operator
new
[](
size_t
size,
EGG::Heap
*heap,
int
align)
noexcept
;
145
void
operator
delete
(
void
*block)
noexcept
;
146
void
operator
delete
[](
void
*block)
noexcept
;
Abstract::Memory::MEMiHeapHead
A low-level representation of a memory heap for managing dynamic memory allocation....
Definition
HeapCommon.hh:19
EGG::Disposer
An interface for ensuring certain structures and classes are destroyed with the heap.
Definition
Disposer.hh:11
EGG::ExpHeap
High-level implementation of a memory heap for managing dynamic memory allocation....
Definition
ExpHeap.hh:15
EGG::Heap
A high-level representation of a memory heap for managing dynamic memory allocation....
Definition
Heap.hh:22
Host::Context
Contexts can be used to restore a previous memory state for the current session.
Definition
Context.hh:59
int32_t
uint32_t
EGG
EGG core library.
Definition
Archive.cc:6
Host
Represents the host application.
Definition
HeapCommon.hh:9
Abstract::Memory::MEMLink
Link of an instrusive doubly-linked list.
Definition
List.hh:8
Abstract::Memory::MEMList
Intrusive doubly-linked list. Links are placed within the corresponding object.
Definition
List.hh:14
EGG::TBitFlag
Wrapper around an integral type with an enum corresponding to its bits.
Definition
BitFlag.hh:16
EGG::TBitFlag::resetBit
constexpr TBitFlag< T, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
Definition
BitFlag.hh:68
EGG::TBitFlag::onBit
constexpr bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
Definition
BitFlag.hh:103
EGG::TBitFlag::setBit
constexpr TBitFlag< T, E > & setBit(Es... es)
Sets the corresponding bits for the provided enum values.
Definition
BitFlag.hh:57
egg
core
Heap.hh
Made by
Malleo
. Logo by
vabold
. Website generated by
Doxygen
1.12.0