ksys/phys: Add HavokMemoryAllocator

This commit is contained in:
Léo Lam
2022-03-27 19:09:01 +02:00
parent 3a206f92c8
commit f86b6dde65
11 changed files with 131 additions and 14 deletions
+3 -1
View File
@@ -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
@@ -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
@@ -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