From ca87fb624d78c7e84a55e22c228a6dbcf4e517fe Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Thu, 28 Feb 2019 17:25:37 +0100 Subject: [PATCH] Remove mutexes and do not use static pool and single frame memory allocators --- src/engine/CollisionWorld.h | 8 ++++++++ src/memory/DefaultAllocator.h | 10 ---------- src/memory/DefaultPoolAllocator.cpp | 6 ------ src/memory/DefaultPoolAllocator.h | 4 ---- src/memory/DefaultSingleFrameAllocator.cpp | 6 ------ src/memory/DefaultSingleFrameAllocator.h | 4 ---- src/memory/MemoryManager.cpp | 10 ++++++---- src/memory/MemoryManager.h | 14 +++++++------- 8 files changed, 21 insertions(+), 41 deletions(-) diff --git a/src/engine/CollisionWorld.h b/src/engine/CollisionWorld.h index 1d359a9c..d292c728 100644 --- a/src/engine/CollisionWorld.h +++ b/src/engine/CollisionWorld.h @@ -165,6 +165,9 @@ class CollisionWorld { /// Test and report collisions between all shapes of the world void testCollision(CollisionCallback* callback); + /// Return a reference to the memory manager of the world + MemoryManager& getMemoryManager(); + #ifdef IS_PROFILING_ACTIVE /// Return a reference to the profiler @@ -255,6 +258,11 @@ inline void CollisionWorld::testOverlap(CollisionBody* body, OverlapCallback* ov mCollisionDetection.testOverlap(body, overlapCallback, categoryMaskBits); } +// Return a reference to the memory manager of the world +inline MemoryManager& CollisionWorld::getMemoryManager() { + return mMemoryManager; +} + // Return the name of the world /** * @return Name of the world diff --git a/src/memory/DefaultAllocator.h b/src/memory/DefaultAllocator.h index c8432438..fcfb8e15 100644 --- a/src/memory/DefaultAllocator.h +++ b/src/memory/DefaultAllocator.h @@ -29,7 +29,6 @@ // Libraries #include "memory/MemoryAllocator.h" #include -#include /// ReactPhysics3D namespace namespace reactphysics3d { @@ -42,9 +41,6 @@ class DefaultAllocator : public MemoryAllocator { protected: - /// Mutex - std::mutex mMutex; - public: /// Destructor @@ -57,18 +53,12 @@ class DefaultAllocator : public MemoryAllocator { /// allocated memory. virtual void* allocate(size_t size) override { - // Lock the method with a mutex - std::lock_guard lock(mMutex); - return malloc(size); } /// Release previously allocated memory. virtual void release(void* pointer, size_t size) override { - // Lock the method with a mutex - std::lock_guard lock(mMutex); - free(pointer); } }; diff --git a/src/memory/DefaultPoolAllocator.cpp b/src/memory/DefaultPoolAllocator.cpp index fddcc2f8..39f84eee 100644 --- a/src/memory/DefaultPoolAllocator.cpp +++ b/src/memory/DefaultPoolAllocator.cpp @@ -100,9 +100,6 @@ DefaultPoolAllocator::~DefaultPoolAllocator() { // allocated memory. void* DefaultPoolAllocator::allocate(size_t size) { - // Lock the method with a mutex - std::lock_guard lock(mMutex); - // We cannot allocate zero bytes if (size == 0) return nullptr; @@ -176,9 +173,6 @@ void* DefaultPoolAllocator::allocate(size_t size) { // Release previously allocated memory. void DefaultPoolAllocator::release(void* pointer, size_t size) { - // Lock the method with a mutex - std::lock_guard lock(mMutex); - // Cannot release a 0-byte allocated memory if (size == 0) return; diff --git a/src/memory/DefaultPoolAllocator.h b/src/memory/DefaultPoolAllocator.h index 8610da0c..172a3c6c 100644 --- a/src/memory/DefaultPoolAllocator.h +++ b/src/memory/DefaultPoolAllocator.h @@ -29,7 +29,6 @@ // Libraries #include "configuration.h" #include "MemoryAllocator.h" -#include /// ReactPhysics3D namespace namespace reactphysics3d { @@ -102,9 +101,6 @@ class DefaultPoolAllocator : public MemoryAllocator { /// True if the mMapSizeToHeapIndex array has already been initialized static bool isMapSizeToHeadIndexInitialized; - /// Mutex - std::mutex mMutex; - /// Pointers to the first free memory unit for each heap MemoryUnit* mFreeMemoryUnits[NB_HEAPS]; diff --git a/src/memory/DefaultSingleFrameAllocator.cpp b/src/memory/DefaultSingleFrameAllocator.cpp index 3037488e..de76ce57 100644 --- a/src/memory/DefaultSingleFrameAllocator.cpp +++ b/src/memory/DefaultSingleFrameAllocator.cpp @@ -54,9 +54,6 @@ DefaultSingleFrameAllocator::~DefaultSingleFrameAllocator() { // allocated memory. void* DefaultSingleFrameAllocator::allocate(size_t size) { - // Lock the method with a mutex - std::lock_guard lock(mMutex); - // Check that there is enough remaining memory in the buffer if (mCurrentOffset + size > mTotalSizeBytes) { @@ -80,9 +77,6 @@ void* DefaultSingleFrameAllocator::allocate(size_t size) { // Release previously allocated memory. void DefaultSingleFrameAllocator::release(void* pointer, size_t size) { - // Lock the method with a mutex - std::lock_guard lock(mMutex); - // If allocated memory is not within the single frame allocation range char* p = static_cast(pointer); if (p < mMemoryBufferStart || p > mMemoryBufferStart + mTotalSizeBytes) { diff --git a/src/memory/DefaultSingleFrameAllocator.h b/src/memory/DefaultSingleFrameAllocator.h index 1ad98769..5e4c736a 100644 --- a/src/memory/DefaultSingleFrameAllocator.h +++ b/src/memory/DefaultSingleFrameAllocator.h @@ -29,7 +29,6 @@ // Libraries #include "MemoryAllocator.h" #include "configuration.h" -#include /// ReactPhysics3D namespace namespace reactphysics3d { @@ -54,9 +53,6 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator { // -------------------- Attributes -------------------- // - /// Mutex - std::mutex mMutex; - /// Cached memory allocator used on construction MemoryAllocator* mBaseMemoryAllocator; diff --git a/src/memory/MemoryManager.cpp b/src/memory/MemoryManager.cpp index 7427bcdd..e9954f55 100644 --- a/src/memory/MemoryManager.cpp +++ b/src/memory/MemoryManager.cpp @@ -30,9 +30,11 @@ using namespace reactphysics3d; // Static variables DefaultAllocator MemoryManager::mDefaultAllocator; -DefaultSingleFrameAllocator MemoryManager::mDefaultSingleFrameAllocator; -DefaultPoolAllocator MemoryManager::mDefaultPoolAllocator; MemoryAllocator* MemoryManager::mBaseAllocator = &mDefaultAllocator; -MemoryAllocator* MemoryManager::mPoolAllocator = &mDefaultPoolAllocator; -SingleFrameAllocator* MemoryManager::mSingleFrameAllocator = &mDefaultSingleFrameAllocator; +// Constructor +MemoryManager::MemoryManager() { + + mSingleFrameAllocator = &mDefaultSingleFrameAllocator; + mPoolAllocator = &mDefaultPoolAllocator; +} diff --git a/src/memory/MemoryManager.h b/src/memory/MemoryManager.h index b4269cbb..d0495543 100644 --- a/src/memory/MemoryManager.h +++ b/src/memory/MemoryManager.h @@ -51,19 +51,19 @@ class MemoryManager { static DefaultAllocator mDefaultAllocator; /// Default single frame memory allocator - static DefaultSingleFrameAllocator mDefaultSingleFrameAllocator; + DefaultSingleFrameAllocator mDefaultSingleFrameAllocator; /// Default pool memory allocator - static DefaultPoolAllocator mDefaultPoolAllocator; + DefaultPoolAllocator mDefaultPoolAllocator; /// Pointer to the base memory allocator to use static MemoryAllocator* mBaseAllocator; /// Single frame stack allocator - static SingleFrameAllocator* mSingleFrameAllocator; + SingleFrameAllocator* mSingleFrameAllocator; /// Memory pool allocator - static MemoryAllocator* mPoolAllocator; + MemoryAllocator* mPoolAllocator; public: @@ -75,7 +75,7 @@ class MemoryManager { }; /// Constructor - MemoryManager() = default; + MemoryManager(); /// Destructor ~MemoryManager() = default; @@ -99,10 +99,10 @@ class MemoryManager { static void setBaseAllocator(MemoryAllocator* memoryAllocator); /// Set the single frame memory allocator - static void setSingleFrameAllocator(SingleFrameAllocator* singleFrameAllocator); + void setSingleFrameAllocator(SingleFrameAllocator* singleFrameAllocator); /// Set the pool memory allocator - static void setPoolAllocator(MemoryAllocator* poolAllocator); + void setPoolAllocator(MemoryAllocator* poolAllocator); /// Reset the single frame allocator void resetFrameAllocator();