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
10namespace Host {
11
12class Context;
13
14} // namespace Host
15
16namespace EGG {
17
18class ExpHeap;
19
22class Heap : Disposer {
23 friend class Host::Context;
24
25public:
26 enum class Kind {
27 None,
28 Expanded,
29 Frame,
30 Unit,
31 Assert,
32 };
33
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
117protected:
118 enum class eFlags {
119 Lock = 0,
120 };
122
124 void *m_block;
125 Heap *m_parentHeap;
126 Flags m_flags;
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;
145void operator delete(void *block) noexcept;
146void operator delete[](void *block) noexcept;
A low-level representation of a memory heap for managing dynamic memory allocation....
Definition HeapCommon.hh:19
An interface for ensuring certain structures and classes are destroyed with the heap.
Definition Disposer.hh:11
High-level implementation of a memory heap for managing dynamic memory allocation....
Definition ExpHeap.hh:15
A high-level representation of a memory heap for managing dynamic memory allocation....
Definition Heap.hh:22
Contexts can be used to restore a previous memory state for the current session.
Definition Context.hh:59
EGG core library.
Definition Archive.cc:6
Represents the host application.
Definition HeapCommon.hh:9
Intrusive doubly-linked list. Links are placed within the corresponding object.
Definition List.hh:14
Wrapper around an integral type with an enum corresponding to its bits.
Definition BitFlag.hh:16
constexpr TBitFlag< T, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
Definition BitFlag.hh:68
constexpr bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
Definition BitFlag.hh:103
constexpr TBitFlag< T, E > & setBit(Es... es)
Sets the corresponding bits for the provided enum values.
Definition BitFlag.hh:57