From b3cd791146af91ba3bf6717e73469b5793053a2a Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 26 Sep 2018 18:25:31 +0200 Subject: [PATCH] Make possible for the user to use a custom pool memory allocator --- CHANGELOG.md | 1 + CMakeLists.txt | 4 +-- src/body/CollisionBody.h | 2 +- src/collision/ContactManifold.h | 2 +- .../broadphase/BroadPhaseAlgorithm.cpp | 8 ++--- .../narrowphase/NarrowPhaseAlgorithm.h | 2 +- src/collision/shapes/CollisionShape.h | 2 +- ...Allocator.cpp => DefaultPoolAllocator.cpp} | 16 ++++----- ...PoolAllocator.h => DefaultPoolAllocator.h} | 10 +++--- src/memory/DefaultSingleFrameAllocator.cpp | 4 +-- src/memory/MemoryManager.cpp | 2 ++ src/memory/MemoryManager.h | 34 +++++++++++++------ 12 files changed, 51 insertions(+), 36 deletions(-) rename src/memory/{PoolAllocator.cpp => DefaultPoolAllocator.cpp} (95%) rename src/memory/{PoolAllocator.h => DefaultPoolAllocator.h} (95%) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5c7bcd9..ff7b1ba9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Make possible for the user to get vertices, normals and triangle indices of a ConcaveMeshShape - Make possible for the user to get vertices and height values of the HeightFieldShape + - Make possible for the user to use a custom single frame and pool memory allocator ### Fixed diff --git a/CMakeLists.txt b/CMakeLists.txt index 9793b4b5..410ff4c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,7 +148,7 @@ SET (REACTPHYSICS3D_HEADERS "src/mathematics/Vector3.h" "src/mathematics/Ray.h" "src/memory/MemoryAllocator.h" - "src/memory/PoolAllocator.h" + "src/memory/DefaultPoolAllocator.h" "src/memory/DefaultSingleFrameAllocator.h" "src/memory/DefaultAllocator.h" "src/memory/MemoryManager.h" @@ -226,7 +226,7 @@ SET (REACTPHYSICS3D_SOURCES "src/mathematics/Transform.cpp" "src/mathematics/Vector2.cpp" "src/mathematics/Vector3.cpp" - "src/memory/PoolAllocator.cpp" + "src/memory/DefaultPoolAllocator.cpp" "src/memory/DefaultSingleFrameAllocator.cpp" "src/memory/MemoryManager.cpp" "src/utils/Profiler.cpp" diff --git a/src/body/CollisionBody.h b/src/body/CollisionBody.h index 4123a055..ace349a6 100644 --- a/src/body/CollisionBody.h +++ b/src/body/CollisionBody.h @@ -42,7 +42,7 @@ class ProxyShape; class CollisionWorld; class CollisionShape; struct RaycastInfo; -class PoolAllocator; +class DefaultPoolAllocator; class Profiler; /// Enumeration for the type of a body diff --git a/src/collision/ContactManifold.h b/src/collision/ContactManifold.h index 3c2207e3..73f26a6e 100644 --- a/src/collision/ContactManifold.h +++ b/src/collision/ContactManifold.h @@ -38,7 +38,7 @@ class ContactManifoldInfo; struct ContactPointInfo; class CollisionBody; class ContactPoint; -class PoolAllocator; +class DefaultPoolAllocator; // Structure ContactManifoldListElement /** diff --git a/src/collision/broadphase/BroadPhaseAlgorithm.cpp b/src/collision/broadphase/BroadPhaseAlgorithm.cpp index 43a1122e..18618c4c 100644 --- a/src/collision/broadphase/BroadPhaseAlgorithm.cpp +++ b/src/collision/broadphase/BroadPhaseAlgorithm.cpp @@ -40,7 +40,7 @@ BroadPhaseAlgorithm::BroadPhaseAlgorithm(CollisionDetection& collisionDetection) mNbPotentialPairs(0), mNbAllocatedPotentialPairs(8), mCollisionDetection(collisionDetection) { - PoolAllocator& poolAllocator = collisionDetection.getMemoryManager().getPoolAllocator(); + MemoryAllocator& poolAllocator = collisionDetection.getMemoryManager().getPoolAllocator(); // Allocate memory for the array of potential overlapping pairs mPotentialPairs = static_cast(poolAllocator.allocate(mNbAllocatedPotentialPairs * sizeof(BroadPhasePair))); @@ -58,7 +58,7 @@ BroadPhaseAlgorithm::BroadPhaseAlgorithm(CollisionDetection& collisionDetection) BroadPhaseAlgorithm::~BroadPhaseAlgorithm() { // Get the memory pool allocatory - PoolAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); + MemoryAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); // Release the memory for the array of potential overlapping pairs poolAllocator.release(mPotentialPairs, mNbAllocatedPotentialPairs * sizeof(BroadPhasePair)); @@ -234,7 +234,7 @@ void BroadPhaseAlgorithm::computeOverlappingPairs(MemoryManager& memoryManager) // number of overlapping pairs if (mNbPotentialPairs < mNbAllocatedPotentialPairs / 4 && mNbPotentialPairs > 8) { - PoolAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); + MemoryAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); // Reduce the number of allocated potential overlapping pairs BroadPhasePair* oldPairs = mPotentialPairs; @@ -260,7 +260,7 @@ void BroadPhaseAlgorithm::addOverlappingNodes(int referenceNodeId, const LinkedL // If we need to allocate more memory for the array of potential overlapping pairs if (mNbPotentialPairs == mNbAllocatedPotentialPairs) { - PoolAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); + MemoryAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); // Allocate more memory for the array of potential pairs BroadPhasePair* oldPairs = mPotentialPairs; diff --git a/src/collision/narrowphase/NarrowPhaseAlgorithm.h b/src/collision/narrowphase/NarrowPhaseAlgorithm.h index e7cdff25..dbb5ce49 100644 --- a/src/collision/narrowphase/NarrowPhaseAlgorithm.h +++ b/src/collision/narrowphase/NarrowPhaseAlgorithm.h @@ -34,7 +34,7 @@ namespace reactphysics3d { class CollisionDetection; class Body; class ContactManifoldInfo; -class PoolAllocator; +class DefaultPoolAllocator; class OverlappingPair; struct NarrowPhaseInfo; struct ContactPointInfo; diff --git a/src/collision/shapes/CollisionShape.h b/src/collision/shapes/CollisionShape.h index 4e1206d3..1f4bb8ad 100644 --- a/src/collision/shapes/CollisionShape.h +++ b/src/collision/shapes/CollisionShape.h @@ -35,7 +35,7 @@ namespace reactphysics3d { // Declarations class Profiler; -class PoolAllocator; +class DefaultPoolAllocator; class AABB; class Transform; struct Ray; diff --git a/src/memory/PoolAllocator.cpp b/src/memory/DefaultPoolAllocator.cpp similarity index 95% rename from src/memory/PoolAllocator.cpp rename to src/memory/DefaultPoolAllocator.cpp index f73ab150..5d4f4ff0 100644 --- a/src/memory/PoolAllocator.cpp +++ b/src/memory/DefaultPoolAllocator.cpp @@ -24,7 +24,7 @@ ********************************************************************************/ // Libraries -#include "PoolAllocator.h" +#include "DefaultPoolAllocator.h" #include "MemoryManager.h" #include #include @@ -32,12 +32,12 @@ using namespace reactphysics3d; // Initialization of static variables -bool PoolAllocator::isMapSizeToHeadIndexInitialized = false; -size_t PoolAllocator::mUnitSizes[NB_HEAPS]; -int PoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1]; +bool DefaultPoolAllocator::isMapSizeToHeadIndexInitialized = false; +size_t DefaultPoolAllocator::mUnitSizes[NB_HEAPS]; +int DefaultPoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1]; // Constructor -PoolAllocator::PoolAllocator() { +DefaultPoolAllocator::DefaultPoolAllocator() { // Allocate some memory to manage the blocks mNbAllocatedMemoryBlocks = 64; @@ -79,7 +79,7 @@ PoolAllocator::PoolAllocator() { } // Destructor -PoolAllocator::~PoolAllocator() { +DefaultPoolAllocator::~DefaultPoolAllocator() { // Release the memory allocated for each block for (uint i=0; iallocate(size); - case AllocationType::Pool: return mPoolAllocator.allocate(size); + case AllocationType::Pool: return mPoolAllocator->allocate(size); case AllocationType::Frame: return mSingleFrameAllocator->allocate(size); } @@ -118,14 +125,14 @@ inline void MemoryManager::release(AllocationType allocationType, void* pointer, switch (allocationType) { case AllocationType::Base: mBaseAllocator->release(pointer, size); break; - case AllocationType::Pool: mPoolAllocator.release(pointer, size); break; + case AllocationType::Pool: mPoolAllocator->release(pointer, size); break; case AllocationType::Frame: mSingleFrameAllocator->release(pointer, size); break; } } // Return the pool allocator -inline PoolAllocator& MemoryManager::getPoolAllocator() { - return mPoolAllocator; +inline MemoryAllocator& MemoryManager::getPoolAllocator() { + return *mPoolAllocator; } // Return the single frame stack allocator @@ -144,8 +151,13 @@ inline void MemoryManager::setBaseAllocator(MemoryAllocator* baseAllocator) { } // Set the base memory allocator -inline void MemoryManager::setSingleFrameAllocator(SingleFrameAllocator* allocator) { - mSingleFrameAllocator = allocator; +inline void MemoryManager::setSingleFrameAllocator(SingleFrameAllocator* singleFrameAllocator) { + mSingleFrameAllocator = singleFrameAllocator; +} + +// Set the pool memory allocator +inline void MemoryManager::setPoolAllocator(MemoryAllocator* poolAllocator) { + mPoolAllocator = poolAllocator; } // Reset the single frame allocator