Files
botw/lib/sead/include/heap/seadHeapMgr.h
T
Léo Lam 18c60323a9 Switch to subrepos
git subrepo clone https://github.com/open-ead/sead lib/sead

subrepo:
  subdir:   "lib/sead"
  merged:   "1b66e825d"
upstream:
  origin:   "https://github.com/open-ead/sead"
  branch:   "master"
  commit:   "1b66e825d"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

git subrepo clone (merge) https://github.com/open-ead/nnheaders lib/NintendoSDK

subrepo:
  subdir:   "lib/NintendoSDK"
  merged:   "9ee21399f"
upstream:
  origin:   "https://github.com/open-ead/nnheaders"
  branch:   "master"
  commit:   "9ee21399f"
git-subrepo:
  version:  "0.4.3"
  origin:   "ssh://git@github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

git subrepo clone https://github.com/open-ead/agl lib/agl

subrepo:
  subdir:   "lib/agl"
  merged:   "7c063271b"
upstream:
  origin:   "https://github.com/open-ead/agl"
  branch:   "master"
  commit:   "7c063271b"
git-subrepo:
  version:  "0.4.3"
  origin:   "ssh://git@github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

git subrepo clone https://github.com/open-ead/EventFlow lib/EventFlow

subrepo:
  subdir:   "lib/EventFlow"
  merged:   "c35d21b34"
upstream:
  origin:   "https://github.com/open-ead/EventFlow"
  branch:   "master"
  commit:   "c35d21b34"
git-subrepo:
  version:  "0.4.3"
  origin:   "ssh://git@github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"
2022-03-21 21:31:42 +01:00

96 lines
2.7 KiB
C++

#ifndef SEAD_HEAPMGR_H_
#define SEAD_HEAPMGR_H_
#include <container/seadPtrArray.h>
#include <heap/seadArena.h>
#include <heap/seadHeap.h>
#include <thread/seadAtomic.h>
#include <thread/seadCriticalSection.h>
namespace sead
{
class HeapMgr
{
public:
HeapMgr();
virtual ~HeapMgr() {}
Heap* getCurrentHeap();
Heap* findContainHeap(const void* ptr) const;
static bool isContainedInAnyHeap(const void* ptr);
static HeapMgr* instance() { return sInstancePtr; }
static s32 getRootHeapNum() { return sRootHeaps.size(); }
// TODO: these should be private
static HeapMgr sInstance;
static HeapMgr* sInstancePtr;
using RootHeaps = FixedPtrArray<Heap, 4>;
using IndependentHeaps = FixedPtrArray<Heap, 4>;
private:
friend class ScopedCurrentHeapSetter;
/// Set the current heap to the specified heap and returns the previous "current heap".
sead::Heap* setCurrentHeap_(sead::Heap* heap);
static Arena sDefaultArena;
static RootHeaps sRootHeaps;
static IndependentHeaps sIndependentHeaps;
static CriticalSection sHeapTreeLockCS;
void*
mAllocFailedCallback; // IAllocFailedCallback* = IDelegate1<const AllocFailedCallbackArg*>*
};
/// Sets the "current heap" to the specified heap and restores the previous "current heap"
/// when this goes out of scope.
class ScopedCurrentHeapSetter
{
public:
explicit ScopedCurrentHeapSetter(sead::Heap* heap)
{
if (heap)
setPreviousHeap_(HeapMgr::instance()->setCurrentHeap_(heap));
else
setPreviousHeapToNone_();
}
~ScopedCurrentHeapSetter()
{
if (hasPreviousHeap_())
HeapMgr::instance()->setCurrentHeap_(getPreviousHeap_());
}
protected:
/// @warning Only call this if hasPreviousHeap returns true.
Heap* getPreviousHeap_() const { return reinterpret_cast<Heap*>(mPreviousHeap); }
void setPreviousHeap_(Heap* heap) { mPreviousHeap = reinterpret_cast<uintptr_t>(heap); }
void setPreviousHeapToNone_() { mPreviousHeap = 1; }
bool hasPreviousHeap_() const
{
// XXX: We cannot just do `mPreviousHeap != 1` because that results in different codegen.
// The cast smells like implementation defined behavior, but 1 should not be a valid
// pointer on any platform that we support. In practice, this will work correctly.
return reinterpret_cast<Heap*>(mPreviousHeap) != reinterpret_cast<Heap*>(1);
}
uintptr_t mPreviousHeap;
};
class FindContainHeapCache
{
public:
FindContainHeapCache();
bool tryRemoveHeap(Heap* heap);
// getHeap and setHeap probably exist too
Atomic<uintptr_t> mHeap;
};
} // namespace sead
#endif // SEAD_HEAPMGR_H_