diff options
| author | Léo Lam <leo@leolam.fr> | 2022-03-27 19:09:01 +0200 |
|---|---|---|
| committer | Léo Lam <leo@leolam.fr> | 2022-03-27 19:38:12 +0200 |
| commit | f86b6dde654d6c82b49f1bd5a6aaa81fb783153e (patch) | |
| tree | 318fda5a594a763bd6cfad4ae3eb2c3cd73f7a5b /src/KingSystem/Physics | |
| parent | 3a206f92c8b9f75f7ef7f428bea3e09ca5534758 (diff) | |
ksys/phys: Add HavokMemoryAllocator
Diffstat (limited to 'src/KingSystem/Physics')
| -rw-r--r-- | src/KingSystem/Physics/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/KingSystem/Physics/physHavokMemoryAllocator.cpp | 37 | ||||
| -rw-r--r-- | src/KingSystem/Physics/physHavokMemoryAllocator.h | 32 |
3 files changed, 72 insertions, 1 deletions
diff --git a/src/KingSystem/Physics/CMakeLists.txt b/src/KingSystem/Physics/CMakeLists.txt index 46b3d815..b358868f 100644 --- a/src/KingSystem/Physics/CMakeLists.txt +++ b/src/KingSystem/Physics/CMakeLists.txt @@ -174,9 +174,11 @@ target_sources(uking PRIVATE System/physUserTag.cpp System/physUserTag.h + physConversions.h physDefines.cpp physDefines.h - physConversions.h + physHavokMemoryAllocator.cpp + physHavokMemoryAllocator.h physHeapUtil.h physLayerMaskBuilder.h physMaterialMask.cpp diff --git a/src/KingSystem/Physics/physHavokMemoryAllocator.cpp b/src/KingSystem/Physics/physHavokMemoryAllocator.cpp new file mode 100644 index 00000000..efe356ce --- /dev/null +++ b/src/KingSystem/Physics/physHavokMemoryAllocator.cpp @@ -0,0 +1,37 @@ +#include "KingSystem/Physics/physHavokMemoryAllocator.h" +#include "KingSystem/Utils/HeapUtil.h" + +namespace ksys::phys { + +HavokMemoryAllocator::HavokMemoryAllocator(int align) : hkMallocAllocator(align) {} + +HavokMemoryAllocator::~HavokMemoryAllocator() = default; + +void HavokMemoryAllocator::initDualHeap(sead::Heap* heap1, sead::Heap* heap2, u32 size, + const sead::SafeString& name) { + mHeap = util::DualHeap::create(size, name, heap1, heap2, alignof(void*), + sead::Heap::cHeapDirection_Forward, true); +} + +void* HavokMemoryAllocator::blockAlloc(int numBytes) { + return mHeap->tryAlloc(numBytes, m_align); +} + +void HavokMemoryAllocator::blockFree(void* p, int numBytes) { + mHeap->free(p); +} + +size_t HavokMemoryAllocator::getHeapSize() const { + return mHeap->getSize(); +} + +size_t HavokMemoryAllocator::getHeapFreeSize() const { + return sead::DynamicCast<util::DualHeap>(mHeap)->getFreeSize(); +} + +int HavokMemoryAllocator::getAllocatedSize(const void* obj, int nbytes) const { + // sead::ExpHeap::getAllocatedSize isn't const-correct :/ + return static_cast<int>(mHeap->getAllocatedSize(const_cast<void*>(obj))); +} + +} // namespace ksys::phys diff --git a/src/KingSystem/Physics/physHavokMemoryAllocator.h b/src/KingSystem/Physics/physHavokMemoryAllocator.h new file mode 100644 index 00000000..c429670d --- /dev/null +++ b/src/KingSystem/Physics/physHavokMemoryAllocator.h @@ -0,0 +1,32 @@ +#pragma once + +#include <Havok/Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h> +#include <basis/seadTypes.h> +#include <hostio/seadHostIONode.h> +#include <prim/seadSafeString.h> + +namespace sead { +class ExpHeap; +} + +namespace ksys::phys { + +class HavokMemoryAllocator : public hkMallocAllocator, public sead::hostio::Node { +public: + explicit HavokMemoryAllocator(int align = HK_REAL_ALIGNMENT); + ~HavokMemoryAllocator() override; + + void initDualHeap(sead::Heap* heap1, sead::Heap* heap2, u32 size, const sead::SafeString& name); + + void* blockAlloc(int numBytes) override; + void blockFree(void* p, int numBytes) override; + int getAllocatedSize(const void* obj, int nbytes) const override; + + size_t getHeapSize() const; + size_t getHeapFreeSize() const; + +private: + sead::ExpHeap* mHeap{}; +}; + +} // namespace ksys::phys |
