Skip to content

Commit

Permalink
Add heaps to scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
vabold committed Oct 12, 2024
1 parent 298f8b2 commit b58d352
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 64 deletions.
6 changes: 6 additions & 0 deletions source/egg/core/Disposer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ class Heap;
class Disposer {
friend class Heap;

public:
[[nodiscard]] static constexpr u16 getLinkOffset() {
return reinterpret_cast<uintptr_t>(&reinterpret_cast<Disposer *>(NULL)->m_link);
}

protected:
Disposer();
virtual ~Disposer();

private:
Heap *m_heap;
Abstract::Memory::MEMLink m_link;
};

} // namespace EGG
19 changes: 0 additions & 19 deletions source/egg/core/ExpHeap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,6 @@ const MEMiExpHeapHead *ExpHeap::dynamicCastHandleToExp() const {
return reinterpret_cast<MEMiExpHeapHead *>(m_handle);
}

void ExpHeap::initRootHeap(void *startAddress, size_t size) {
MEMiHeapHead::OptFlag opt;
opt.setBit(MEMiHeapHead::eOptFlag::ZeroFillAlloc);

#ifdef BUILD_DEBUG
opt.setBit(MEMiHeapHead::eOptFlag::DebugFillAlloc);
#endif

s_rootHeap = create(startAddress, size, 2);
s_rootHeap->setName("EGGRoot");
s_rootHeap->becomeCurrentHeap();
}

ExpHeap *ExpHeap::getRootHeap() {
return s_rootHeap;
}

/// @addr{0x80226DD0}
ExpHeap::GroupSizeRecord::GroupSizeRecord() {
reset();
Expand All @@ -163,6 +146,4 @@ void ExpHeap::GroupSizeRecord::addSize(u16 groupID, size_t size) {
m_entries[groupID] += size;
}

ExpHeap *ExpHeap::s_rootHeap = nullptr;

} // namespace EGG
6 changes: 0 additions & 6 deletions source/egg/core/ExpHeap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@ public:
[[nodiscard]] static ExpHeap *create(void *startAddress, size_t size, u16 opt);
[[nodiscard]] static ExpHeap *create(size_t size, Heap *heap, u16 opt);

static void initRootHeap(void *startAddress, size_t size);

[[nodiscard]] static ExpHeap *getRootHeap();

private:
ExpHeap(Abstract::Memory::MEMiHeapHead *handle);

static ExpHeap *s_rootHeap;
};

} // namespace EGG
30 changes: 5 additions & 25 deletions source/egg/core/Heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ using namespace Abstract::Memory;
namespace EGG {

/// @addr{0x802296E8}
Heap::Heap(MEMiHeapHead *handle) : m_handle(handle), m_children() {
if (!s_isHeapInitialized) {
PANIC("Cannot create a heap before calling Heap::initalize");
}

Heap::Heap(MEMiHeapHead *handle) : m_handle(handle), m_children(Disposer::getLinkOffset()) {
m_block = nullptr;
m_parentHeap = nullptr;
m_name = "NoName";
Expand All @@ -25,12 +21,12 @@ Heap::~Heap() {

/// @addr{0x80229C5C}
void Heap::dispose() {
for (auto *&node : m_children) {
Disposer *node = nullptr;
while ((node = reinterpret_cast<Disposer *>(m_children.getFirst()))) {
node->~Disposer();
node = nullptr;
}

m_children.clear();
ASSERT(!m_children.m_headObject && !m_children.m_tailObject);
}

void Heap::disableAllocation() {
Expand All @@ -46,7 +42,7 @@ bool Heap::tstDisableAllocation() const {
}

void Heap::appendDisposer(Disposer *disposer) {
m_children.push_back(disposer);
m_children.append(disposer);
}

void Heap::removeDisposer(Disposer *disposer) {
Expand Down Expand Up @@ -95,16 +91,6 @@ void Heap::setParentHeap(Heap *heap) {
m_parentHeap = heap;
}

/// @addr{0x802296A8}
void Heap::initialize() {
constexpr size_t MEMORY_SPACE_SIZE = 0x1000000;

s_isHeapInitialized = true;

s_memorySpace = malloc(MEMORY_SPACE_SIZE);
ExpHeap::initRootHeap(s_memorySpace, MEMORY_SPACE_SIZE);
}

/// @addr{0x80229814}
void *Heap::alloc(size_t size, int align, Heap *pHeap) {
Heap *currentHeap = s_currentHeap;
Expand Down Expand Up @@ -195,10 +181,6 @@ Heap *Heap::getCurrentHeap() {
return s_currentHeap;
}

void *Heap::getMemorySpace() {
return s_memorySpace;
}

} // namespace EGG

/// @addr{0x80229DCC}
Expand Down Expand Up @@ -252,6 +234,4 @@ void operator delete[](void *block, size_t /* size */) noexcept {
MEMList EGG::Heap::s_heapList = MEMList(EGG::Heap::getOffset()); ///< @addr{0x80384320}

EGG::Heap *EGG::Heap::s_currentHeap = nullptr; ///< @addr{0x80386EA0}
bool EGG::Heap::s_isHeapInitialized = false; ///< @addr{0x80386EA4}
EGG::Heap *EGG::Heap::s_allocatableHeap = nullptr; ///< @addr{0x80386EA8}
void *EGG::Heap::s_memorySpace = nullptr;
6 changes: 1 addition & 5 deletions source/egg/core/Heap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public:

[[nodiscard]] static ExpHeap *dynamicCastToExp(Heap *heap);
[[nodiscard]] static Heap *getCurrentHeap();
[[nodiscard]] static void *getMemorySpace();

[[nodiscard]] static constexpr uintptr_t getOffset() {
// offsetof doesn't work, so instead of hardcoding an offset, we derive it ourselves
Expand All @@ -81,16 +80,13 @@ protected:
Heap *m_parentHeap;
Flags m_flags;
Abstract::Memory::MEMLink m_link;
std::list<Disposer *> m_children;
Abstract::Memory::MEMList m_children;
const char *m_name;

static Abstract::Memory::MEMList s_heapList;

static Heap *s_currentHeap;
static bool s_isHeapInitialized;
static Heap *s_allocatableHeap;
static ExpHeap *s_rootHeap;
static void *s_memorySpace;
};

} // namespace EGG
Expand Down
7 changes: 7 additions & 0 deletions source/egg/core/Scene.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "Scene.hh"

#include "egg/core/SceneManager.hh"

namespace EGG {

/// @addr{0x8023AD10}
Scene::Scene() {
m_heap = SceneManager::heapForCreateScene();
m_parent = nullptr;
m_child = nullptr;
m_id = -1;
Expand All @@ -29,6 +32,10 @@ void Scene::setSceneMgr(SceneManager *sceneMgr) {
m_sceneMgr = sceneMgr;
}

Heap *Scene::heap() const {
return m_heap;
}

Scene *Scene::parent() const {
return m_parent;
}
Expand Down
2 changes: 2 additions & 0 deletions source/egg/core/Scene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public:
/// @endSetters

/// @beginGetters
[[nodiscard]] Heap *heap() const;
[[nodiscard]] Scene *parent() const;
[[nodiscard]] Scene *child() const;
[[nodiscard]] int id() const;
Expand All @@ -52,6 +53,7 @@ protected:
Members
*-----------*/

Heap *m_heap;
Scene *m_parent;
Scene *m_child;
int m_id;
Expand Down
39 changes: 33 additions & 6 deletions source/egg/core/SceneManager.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "SceneManager.hh"

#include "egg/core/ExpHeap.hh"

#include <host/System.hh>

namespace EGG {

/*------------*
Expand Down Expand Up @@ -84,6 +88,24 @@ void SceneManager::createChildScene(int id, Scene *parent) {

/// @addr{0x8023B0E4}
void SceneManager::createScene(int id, Scene *parent) {
Heap *parentHeap = parent ? parent->heap() : Host::KSystem::Instance().rootHeap();

// We need to preserve the locked status to reinstate it later
bool locked = parentHeap->tstDisableAllocation();
if (locked) {
parentHeap->enableAllocation();
}

ExpHeap *newHeap = ExpHeap::create(-1, parentHeap, sHeapOptionFlg);
sHeapForCreateScene = newHeap;

if (locked) {
parentHeap->disableAllocation();
}

newHeap->becomeCurrentHeap();
newHeap->setName("DefaultSceneHeap");

Scene *newScene = m_creator->create(id);

if (parent) {
Expand All @@ -101,19 +123,21 @@ void SceneManager::createScene(int id, Scene *parent) {
void SceneManager::destroyScene(Scene *scene) {
scene->exit();
if (scene->child()) {
destroyScene(scene);
destroyScene(scene->child());
}

Scene *parent = scene->parent();
delete m_currentScene;
m_creator->destroy(scene->id());
m_currentScene = nullptr;

if (!parent) {
return;
if (parent) {
parent->setChild(nullptr);
m_currentScene = parent;
}

parent->setChild(nullptr);
m_currentScene = parent;
scene->heap()->destroy();
Heap *parentHeap = parent ? parent->heap() : Host::KSystem::Instance().rootHeap();
parentHeap->becomeCurrentHeap();
}

/// @addr{0x8023AF84}
Expand Down Expand Up @@ -257,4 +281,7 @@ void SceneManager::resetAfterFadeType() {
m_fadeType = FadeType::Idle;
}

Heap *SceneManager::sHeapForCreateScene = nullptr;
u16 SceneManager::sHeapOptionFlg = 2;

} // namespace EGG
7 changes: 7 additions & 0 deletions source/egg/core/SceneManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public:
return m_currentSceneId;
}

[[nodiscard]] static Heap *heapForCreateScene() {
return sHeapForCreateScene;
}

/*----------*
Setters
*----------*/
Expand All @@ -97,6 +101,9 @@ private:
int m_nextSceneId;
int m_currentSceneId;
int m_prevSceneId;

static Heap *sHeapForCreateScene;
static u16 sHeapOptionFlg;
};

} // namespace EGG
5 changes: 5 additions & 0 deletions source/game/kart/KartObjectManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ KartObjectManager::~KartObjectManager() {
}

delete[] m_objects;

// If the proxy list is not cleared when we're done with the KartObjectManager, the list's
// destructor calls delete on all of the links remaining in the list. Since the heaps are
// gone by that point, this results in a segmentation fault. So, we clear the links here.
KartObjectProxy::proxyList().clear();
}

KartObjectManager *KartObjectManager::s_instance = nullptr; ///< @addr{0x809C18F8}
Expand Down
4 changes: 4 additions & 0 deletions source/game/kart/KartObjectProxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ std::pair<EGG::Vector3f, EGG::Vector3f> KartObjectProxy::getCannonPosRot() {
return std::pair(cannonPos + local60 * temp0, cannonRot);
}

std::list<KartObjectProxy *> &KartObjectProxy::proxyList() {
return s_proxyList;
}

/// @addr{0x805901D0}
void KartObjectProxy::apply(size_t idx) {
m_accessor = KartObjectManager::Instance()->object(idx)->accessor();
Expand Down
2 changes: 2 additions & 0 deletions source/game/kart/KartObjectProxy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public:
[[nodiscard]] u16 tireCount() const;
[[nodiscard]] bool hasFloorCollision(const WheelPhysics *wheelPhysics) const;
[[nodiscard]] std::pair<EGG::Vector3f, EGG::Vector3f> getCannonPosRot();

[[nodiscard]] static std::list<KartObjectProxy *> &proxyList();
/// @endGetters

protected:
Expand Down
1 change: 1 addition & 0 deletions source/game/scene/GameScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Scene {

/// @addr{0x8051A1E0}
GameScene::GameScene() {
m_heap->setName("DefaultGameSceneHeap");
m_nextSceneId = -1;
}

Expand Down
4 changes: 3 additions & 1 deletion source/game/scene/RaceScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace Scene {

/// @addr{0x80553B88}
RaceScene::RaceScene() = default;
RaceScene::RaceScene() {
m_heap->setName("RaceSceneHeap");
}

/// @addr{0x80553BD4}
RaceScene::~RaceScene() = default;
Expand Down
4 changes: 3 additions & 1 deletion source/game/scene/RootScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace Scene {

/// @addr{0x80542878}
RootScene::RootScene() = default;
RootScene::RootScene() {
m_heap->setName("RootSceneHeap");
}

/// @addr{0x805429A8}
RootScene::~RootScene() = default;
Expand Down
Loading

0 comments on commit b58d352

Please sign in to comment.