ksys/phys: Add HavokMemoryAllocator

This commit is contained in:
Léo Lam 2022-03-27 19:09:01 +02:00
parent 3a206f92c8
commit f86b6dde65
No known key found for this signature in database
GPG Key ID: 0DF30F9081000741
11 changed files with 131 additions and 14 deletions

View File

@ -93745,7 +93745,7 @@ Address,Quality,Size,Name
0x000000710122c254,U,001028, 0x000000710122c254,U,001028,
0x000000710122c658,U,000984, 0x000000710122c658,U,000984,
0x000000710122ca30,U,000004,j_nullsub_5077 0x000000710122ca30,U,000004,j_nullsub_5077
0x000000710122ca34,U,000008, 0x000000710122ca34,O,000008,_ZN17hkMemoryAllocator20getExtendedInterfaceEv
0x000000710122ca3c,U,000716, 0x000000710122ca3c,U,000716,
0x000000710122cd08,U,000088, 0x000000710122cd08,U,000088,
0x000000710122cd60,U,000028, 0x000000710122cd60,U,000028,
@ -95437,15 +95437,15 @@ Address,Quality,Size,Name
0x00000071012b1780,U,000008, 0x00000071012b1780,U,000008,
0x00000071012b1788,U,000008, 0x00000071012b1788,U,000008,
0x00000071012b1790,U,000008, 0x00000071012b1790,U,000008,
0x00000071012b1798,U,000036,HavokMainHeap::ctor 0x00000071012b1798,O,000036,_ZN4ksys4phys20HavokMemoryAllocatorC1Ei
0x00000071012b17bc,U,000004,j_nullsub_5077_0 0x00000071012b17bc,O,000004,_ZN4ksys4phys20HavokMemoryAllocatorD1Ev
0x00000071012b17c0,U,000004,j_nullsub_5077_1 0x00000071012b17c0,O,000004,_ZN4ksys4phys20HavokMemoryAllocatorD0Ev
0x00000071012b17c4,U,000076,HavokMainHeap::initDualHeap 0x00000071012b17c4,O,000076,_ZN4ksys4phys20HavokMemoryAllocator12initDualHeapEPN4sead4HeapES4_jRKNS2_14SafeStringBaseIcEE
0x00000071012b1810,U,000028, 0x00000071012b1810,O,000028,_ZN4ksys4phys20HavokMemoryAllocator10blockAllocEi
0x00000071012b182c,U,000016, 0x00000071012b182c,O,000016,_ZN4ksys4phys20HavokMemoryAllocator9blockFreeEPvi
0x00000071012b183c,U,000016,HavokMainHeap::getHeapSize 0x00000071012b183c,O,000016,_ZNK4ksys4phys20HavokMemoryAllocator11getHeapSizeEv
0x00000071012b184c,U,000140,HavokMainHeap::getHeapFreeSize 0x00000071012b184c,O,000140,_ZNK4ksys4phys20HavokMemoryAllocator15getHeapFreeSizeEv
0x00000071012b18d8,U,000024, 0x00000071012b18d8,O,000024,_ZNK4ksys4phys20HavokMemoryAllocator16getAllocatedSizeEPKvi
0x00000071012b18f0,U,000056, 0x00000071012b18f0,U,000056,
0x00000071012b1928,U,000004, 0x00000071012b1928,U,000004,
0x00000071012b192c,U,000008, 0x00000071012b192c,U,000008,

Can't render this file because it is too large.

View File

@ -57,6 +57,7 @@ add_library(hkStubs OBJECT
Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h
Havok/Common/Base/Memory/Allocator/Lifo/hkLifoAllocator.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/Router/hkMemoryRouter.h
Havok/Common/Base/Memory/Util/hkMemUtil.h Havok/Common/Base/Memory/Util/hkMemUtil.h

View File

@ -0,0 +1,25 @@
#pragma once
#include <Havok/Common/Base/hkBase.h>
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;
};

View File

@ -2,6 +2,11 @@
#include <Havok/Common/Base/Types/hkBaseTypes.h> #include <Havok/Common/Base/Types/hkBaseTypes.h>
#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; class hkMemorySnapshot;
template <typename T> template <typename T>
@ -16,6 +21,8 @@ struct hkSizeOfTypeOrVoid<void> {
class hkMemoryAllocator { class hkMemoryAllocator {
public: public:
HK_DECLARE_PLACEMENT_ALLOCATOR()
using MemoryWalkCallback = void (*)(void* start, hk_size_t size, bool allocated, int pool, using MemoryWalkCallback = void (*)(void* start, hk_size_t size, bool allocated, int pool,
void* param); void* param);

View File

@ -2,6 +2,13 @@
class hkBaseObject { class hkBaseObject {
public: public:
HK_DECLARE_PLACEMENT_ALLOCATOR()
HK_FORCE_INLINE explicit hkBaseObject(hkFinishLoadedObjectFlag flag) {}
virtual ~hkBaseObject() = default; virtual ~hkBaseObject() = default;
virtual void __first_virtual_table_function__() {} // NOLINT(bugprone-reserved-identifier) virtual void __first_virtual_table_function__() {} // NOLINT(bugprone-reserved-identifier)
protected:
HK_FORCE_INLINE hkBaseObject() = default;
}; };

View File

@ -1,4 +1,4 @@
#include <Havok/Common/Base/Object/hkReferencedObject.h> #include <Havok/Common/Base/hkBase.h>
const hkClass* hkReferencedObject::getClassType() const { const hkClass* hkReferencedObject::getClassType() const {
return nullptr; return nullptr;

View File

@ -9,6 +9,9 @@ using hkFloat32 = float;
using hkDouble64 = double; using hkDouble64 = double;
using hkReal = hkFloat32; using hkReal = hkFloat32;
#define HK_REAL_IS_FLOAT #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 hkChar = char;
using hkInt8 = std::int8_t; using hkInt8 = std::int8_t;

View File

@ -1,9 +1,12 @@
#pragma once #pragma once
#include <Havok/Common/Base/Types/hkBaseDefs.h>
#include <Havok/Common/Base/Types/hkBaseTypes.h>
#include <Havok/Common/Base/Memory/Allocator/hkMemoryAllocator.h>
#include <Havok/Common/Base/Object/hkBaseObject.h> #include <Havok/Common/Base/Object/hkBaseObject.h>
#include <Havok/Common/Base/Object/hkReferencedObject.h> #include <Havok/Common/Base/Object/hkReferencedObject.h>
#include <Havok/Common/Base/Types/hkBaseDefs.h>
#include <Havok/Common/Base/Types/hkBaseTypes.h>
#include <Havok/Common/Base/Types/hkRefPtr.h> #include <Havok/Common/Base/Types/hkRefPtr.h>
#include <Havok/Common/Base/Types/hkRefVariant.h> #include <Havok/Common/Base/Types/hkRefVariant.h>
#include <Havok/Common/Base/Types/hkUFloat8.h> #include <Havok/Common/Base/Types/hkUFloat8.h>

View File

@ -174,9 +174,11 @@ target_sources(uking PRIVATE
System/physUserTag.cpp System/physUserTag.cpp
System/physUserTag.h System/physUserTag.h
physConversions.h
physDefines.cpp physDefines.cpp
physDefines.h physDefines.h
physConversions.h physHavokMemoryAllocator.cpp
physHavokMemoryAllocator.h
physHeapUtil.h physHeapUtil.h
physLayerMaskBuilder.h physLayerMaskBuilder.h
physMaterialMask.cpp physMaterialMask.cpp

View File

@ -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

View File

@ -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