A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
HeapCommon.cc
1#include "HeapCommon.hh"
2
3#include <cstring>
4
5namespace Abstract::Memory {
6
7MEMList &MEMiHeapHead::getChildList() {
8 return m_childList;
9}
10
11void *MEMiHeapHead::getHeapStart() {
12 return m_heapStart;
13}
14
15void *MEMiHeapHead::getHeapEnd() {
16 return m_heapEnd;
17}
18
19MEMList &MEMiHeapHead::getRootList() {
20 return s_rootList;
21}
22
23u32 MEMiHeapHead::getFillVal(FillType type) {
24 size_t t = static_cast<std::underlying_type_t<FillType>>(type);
25 ASSERT(t < 3);
26 return s_fillVals[t];
27}
28
30MEMiHeapHead *MEMiHeapHead::findContainHeap(const void *block) {
31 return findContainHeap(&s_rootList, block);
32}
33
35MEMiHeapHead::MEMiHeapHead(u32 signature, void *heapStart, void *heapEnd, const OptFlag &opt)
36 : m_childList(MEMList(getLinkOffset())) {
37 m_signature = signature;
38 m_heapStart = heapStart;
39 m_heapEnd = heapEnd;
40 m_optFlag = opt;
41
42 fillNoUseMemory(heapStart, GetAddrNum(heapEnd) - GetAddrNum(heapStart));
43
44 findListContainHeap().append(this);
45}
46
48MEMiHeapHead::~MEMiHeapHead() {
49 findListContainHeap().remove(this);
50 m_signature = 0;
51}
52
53void MEMiHeapHead::fillNoUseMemory(void *address, u32 size) {
54 if (m_optFlag.onBit(eOptFlag::DebugFillAlloc)) {
55 memset(address, getFillVal(FillType::NoUse), size);
56 }
57}
58
59void MEMiHeapHead::fillAllocMemory(void *address, u32 size) {
60 if (m_optFlag.onBit(eOptFlag::DebugFillAlloc)) {
61 memset(address, getFillVal(FillType::Alloc), size);
62 } else if (m_optFlag.onBit(eOptFlag::ZeroFillAlloc)) {
63 memset(address, 0, size);
64 }
65}
66
67void MEMiHeapHead::fillFreeMemory(void *address, u32 size) {
68 if (m_optFlag.onBit(eOptFlag::DebugFillAlloc)) {
69 memset(address, getFillVal(FillType::Free), size);
70 }
71}
72
74MEMiHeapHead *MEMiHeapHead::findContainHeap(MEMList *list, const void *block) {
75 MEMiHeapHead *heap = nullptr;
76
77 while ((heap = reinterpret_cast<MEMiHeapHead *>(list->getNext(heap)))) {
78 if (GetAddrNum(heap->m_heapStart) > GetAddrNum(block) ||
79 GetAddrNum(block) >= GetAddrNum(heap->m_heapEnd)) {
80 continue;
81 }
82
83 MEMiHeapHead *search = findContainHeap(&heap->m_childList, block);
84 if (search) {
85 return search;
86 }
87
88 return heap;
89 }
90
91 return nullptr;
92}
93
94MEMList &MEMiHeapHead::findListContainHeap() const {
95 MEMiHeapHead *containHeap = findContainHeap(this);
96 return containHeap ? containHeap->getChildList() : getRootList();
97}
98
99MEMList MEMiHeapHead::s_rootList = MEMList(MEMiHeapHead::getLinkOffset());
100
101} // namespace Abstract::Memory