From f86b6dde654d6c82b49f1bd5a6aaa81fb783153e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 27 Mar 2022 19:09:01 +0200 Subject: [PATCH] ksys/phys: Add HavokMemoryAllocator --- data/uking_functions.csv | 20 +++++----- lib/hkStubs/CMakeLists.txt | 1 + .../Allocator/Malloc/hkMallocAllocator.h | 25 +++++++++++++ .../Base/Memory/Allocator/hkMemoryAllocator.h | 7 ++++ .../Havok/Common/Base/Object/hkBaseObject.h | 7 ++++ .../Common/Base/Object/hkReferencedObject.cpp | 2 +- .../Havok/Common/Base/Types/hkBaseTypes.h | 3 ++ lib/hkStubs/Havok/Common/Base/hkBase.h | 7 +++- src/KingSystem/Physics/CMakeLists.txt | 4 +- .../Physics/physHavokMemoryAllocator.cpp | 37 +++++++++++++++++++ .../Physics/physHavokMemoryAllocator.h | 32 ++++++++++++++++ 11 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 lib/hkStubs/Havok/Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h create mode 100644 src/KingSystem/Physics/physHavokMemoryAllocator.cpp create mode 100644 src/KingSystem/Physics/physHavokMemoryAllocator.h diff --git a/data/uking_functions.csv b/data/uking_functions.csv index 7881399d..c0ec33f5 100644 --- a/data/uking_functions.csv +++ b/data/uking_functions.csv @@ -93745,7 +93745,7 @@ Address,Quality,Size,Name 0x000000710122c254,U,001028, 0x000000710122c658,U,000984, 0x000000710122ca30,U,000004,j_nullsub_5077 -0x000000710122ca34,U,000008, +0x000000710122ca34,O,000008,_ZN17hkMemoryAllocator20getExtendedInterfaceEv 0x000000710122ca3c,U,000716, 0x000000710122cd08,U,000088, 0x000000710122cd60,U,000028, @@ -95437,15 +95437,15 @@ Address,Quality,Size,Name 0x00000071012b1780,U,000008, 0x00000071012b1788,U,000008, 0x00000071012b1790,U,000008, -0x00000071012b1798,U,000036,HavokMainHeap::ctor -0x00000071012b17bc,U,000004,j_nullsub_5077_0 -0x00000071012b17c0,U,000004,j_nullsub_5077_1 -0x00000071012b17c4,U,000076,HavokMainHeap::initDualHeap -0x00000071012b1810,U,000028, -0x00000071012b182c,U,000016, -0x00000071012b183c,U,000016,HavokMainHeap::getHeapSize -0x00000071012b184c,U,000140,HavokMainHeap::getHeapFreeSize -0x00000071012b18d8,U,000024, +0x00000071012b1798,O,000036,_ZN4ksys4phys20HavokMemoryAllocatorC1Ei +0x00000071012b17bc,O,000004,_ZN4ksys4phys20HavokMemoryAllocatorD1Ev +0x00000071012b17c0,O,000004,_ZN4ksys4phys20HavokMemoryAllocatorD0Ev +0x00000071012b17c4,O,000076,_ZN4ksys4phys20HavokMemoryAllocator12initDualHeapEPN4sead4HeapES4_jRKNS2_14SafeStringBaseIcEE +0x00000071012b1810,O,000028,_ZN4ksys4phys20HavokMemoryAllocator10blockAllocEi +0x00000071012b182c,O,000016,_ZN4ksys4phys20HavokMemoryAllocator9blockFreeEPvi +0x00000071012b183c,O,000016,_ZNK4ksys4phys20HavokMemoryAllocator11getHeapSizeEv +0x00000071012b184c,O,000140,_ZNK4ksys4phys20HavokMemoryAllocator15getHeapFreeSizeEv +0x00000071012b18d8,O,000024,_ZNK4ksys4phys20HavokMemoryAllocator16getAllocatedSizeEPKvi 0x00000071012b18f0,U,000056, 0x00000071012b1928,U,000004, 0x00000071012b192c,U,000008, diff --git a/lib/hkStubs/CMakeLists.txt b/lib/hkStubs/CMakeLists.txt index 86e00c61..0a495baa 100644 --- a/lib/hkStubs/CMakeLists.txt +++ b/lib/hkStubs/CMakeLists.txt @@ -57,6 +57,7 @@ add_library(hkStubs OBJECT Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h Havok/Common/Base/Memory/Allocator/Lifo/hkLifoAllocator.h + Havok/Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h Havok/Common/Base/Memory/Router/hkMemoryRouter.h Havok/Common/Base/Memory/Util/hkMemUtil.h diff --git a/lib/hkStubs/Havok/Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h b/lib/hkStubs/Havok/Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h new file mode 100644 index 00000000..fab10966 --- /dev/null +++ b/lib/hkStubs/Havok/Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +class hkMallocAllocator : public hkMemoryAllocator { +public: + explicit hkMallocAllocator(int align = HK_REAL_ALIGNMENT) + : m_align(align), m_currentUsed(0), m_peakUse(0) {} + + void* blockAlloc(int numBytes) override; + void blockFree(void* p, int numBytes) override; + + void getMemoryStatistics(hkMemoryAllocator::MemoryStatistics& u) const override; + int getAllocatedSize(const void* obj, int nbytes) const override; + void resetPeakMemoryStatistics() override; + + int m_align; + + static hkMemoryAllocator* m_defaultMallocAllocator; + +protected: + hkUint32 m_unused; + hkUint32 m_currentUsed; + hkUint32 m_peakUse; +}; diff --git a/lib/hkStubs/Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h b/lib/hkStubs/Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h index b024b565..521a57b1 100644 --- a/lib/hkStubs/Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h +++ b/lib/hkStubs/Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h @@ -2,6 +2,11 @@ #include +#define HK_DECLARE_PLACEMENT_ALLOCATOR() \ + HK_FORCE_INLINE void* operator new(hk_size_t, void* p) { return p; } \ + HK_FORCE_INLINE void operator delete(void*, void*) {} \ + HK_FORCE_INLINE void operator delete(void*, hk_size_t) {} + class hkMemorySnapshot; template @@ -16,6 +21,8 @@ struct hkSizeOfTypeOrVoid { class hkMemoryAllocator { public: + HK_DECLARE_PLACEMENT_ALLOCATOR() + using MemoryWalkCallback = void (*)(void* start, hk_size_t size, bool allocated, int pool, void* param); diff --git a/lib/hkStubs/Havok/Common/Base/Object/hkBaseObject.h b/lib/hkStubs/Havok/Common/Base/Object/hkBaseObject.h index 5089ff85..39571f13 100644 --- a/lib/hkStubs/Havok/Common/Base/Object/hkBaseObject.h +++ b/lib/hkStubs/Havok/Common/Base/Object/hkBaseObject.h @@ -2,6 +2,13 @@ class hkBaseObject { public: + HK_DECLARE_PLACEMENT_ALLOCATOR() + + HK_FORCE_INLINE explicit hkBaseObject(hkFinishLoadedObjectFlag flag) {} + virtual ~hkBaseObject() = default; virtual void __first_virtual_table_function__() {} // NOLINT(bugprone-reserved-identifier) + +protected: + HK_FORCE_INLINE hkBaseObject() = default; }; diff --git a/lib/hkStubs/Havok/Common/Base/Object/hkReferencedObject.cpp b/lib/hkStubs/Havok/Common/Base/Object/hkReferencedObject.cpp index 2029b86a..f3c7f780 100644 --- a/lib/hkStubs/Havok/Common/Base/Object/hkReferencedObject.cpp +++ b/lib/hkStubs/Havok/Common/Base/Object/hkReferencedObject.cpp @@ -1,4 +1,4 @@ -#include +#include const hkClass* hkReferencedObject::getClassType() const { return nullptr; diff --git a/lib/hkStubs/Havok/Common/Base/Types/hkBaseTypes.h b/lib/hkStubs/Havok/Common/Base/Types/hkBaseTypes.h index d2e63472..2067471d 100644 --- a/lib/hkStubs/Havok/Common/Base/Types/hkBaseTypes.h +++ b/lib/hkStubs/Havok/Common/Base/Types/hkBaseTypes.h @@ -9,6 +9,9 @@ using hkFloat32 = float; using hkDouble64 = double; using hkReal = hkFloat32; #define HK_REAL_IS_FLOAT +#define HK_FLOAT_ALIGNMENT 16 +#define HK_DOUBLE_ALIGNMENT 32 +#define HK_REAL_ALIGNMENT HK_FLOAT_ALIGNMENT using hkChar = char; using hkInt8 = std::int8_t; diff --git a/lib/hkStubs/Havok/Common/Base/hkBase.h b/lib/hkStubs/Havok/Common/Base/hkBase.h index a94eaf9b..c5f6e072 100644 --- a/lib/hkStubs/Havok/Common/Base/hkBase.h +++ b/lib/hkStubs/Havok/Common/Base/hkBase.h @@ -1,9 +1,12 @@ #pragma once +#include +#include + +#include + #include #include -#include -#include #include #include #include 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(mHeap)->getFreeSize(); +} + +int HavokMemoryAllocator::getAllocatedSize(const void* obj, int nbytes) const { + // sead::ExpHeap::getAllocatedSize isn't const-correct :/ + return static_cast(mHeap->getAllocatedSize(const_cast(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 +#include +#include +#include + +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