From 2dfe254c86a9c3c9247a6a36e0522a5f0d048b66 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 27 Nov 2019 22:41:38 +0100 Subject: [PATCH] Use memory allocators from ecs branch --- CMakeLists.txt | 8 +-- src/memory/DefaultAllocator.h | 5 +- src/memory/MemoryAllocator.h | 29 +-------- src/memory/MemoryManager.cpp | 8 +-- src/memory/MemoryManager.h | 61 ++++++------------- ...ultPoolAllocator.cpp => PoolAllocator.cpp} | 18 +++--- ...DefaultPoolAllocator.h => PoolAllocator.h} | 12 ++-- ...Allocator.cpp => SingleFrameAllocator.cpp} | 33 +++++----- ...rameAllocator.h => SingleFrameAllocator.h} | 19 +++--- 9 files changed, 65 insertions(+), 128 deletions(-) rename src/memory/{DefaultPoolAllocator.cpp => PoolAllocator.cpp} (94%) rename src/memory/{DefaultPoolAllocator.h => PoolAllocator.h} (94%) rename src/memory/{DefaultSingleFrameAllocator.cpp => SingleFrameAllocator.cpp} (79%) rename src/memory/{DefaultSingleFrameAllocator.h => SingleFrameAllocator.h} (86%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 351600d0..24464719 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -166,8 +166,8 @@ SET (REACTPHYSICS3D_HEADERS "src/mathematics/Vector3.h" "src/mathematics/Ray.h" "src/memory/MemoryAllocator.h" - "src/memory/DefaultPoolAllocator.h" - "src/memory/DefaultSingleFrameAllocator.h" + "src/memory/PoolAllocator.h" + "src/memory/SingleFrameAllocator.h" "src/memory/DefaultAllocator.h" "src/memory/MemoryManager.h" "src/containers/Stack.h" @@ -263,8 +263,8 @@ SET (REACTPHYSICS3D_SOURCES "src/mathematics/Transform.cpp" "src/mathematics/Vector2.cpp" "src/mathematics/Vector3.cpp" - "src/memory/DefaultPoolAllocator.cpp" - "src/memory/DefaultSingleFrameAllocator.cpp" + "src/memory/PoolAllocator.cpp" + "src/memory/SingleFrameAllocator.cpp" "src/memory/MemoryManager.cpp" "src/utils/Profiler.cpp" "src/utils/Logger.cpp" diff --git a/src/memory/DefaultAllocator.h b/src/memory/DefaultAllocator.h index f3562928..ce4d1741 100644 --- a/src/memory/DefaultAllocator.h +++ b/src/memory/DefaultAllocator.h @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -39,8 +39,6 @@ namespace reactphysics3d { */ class DefaultAllocator : public MemoryAllocator { - protected: - public: /// Destructor @@ -61,7 +59,6 @@ class DefaultAllocator : public MemoryAllocator { /// Release previously allocated memory. virtual void release(void* pointer, size_t size) override { - free(pointer); } }; diff --git a/src/memory/MemoryAllocator.h b/src/memory/MemoryAllocator.h index 63712edd..9811387b 100644 --- a/src/memory/MemoryAllocator.h +++ b/src/memory/MemoryAllocator.h @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -57,33 +57,6 @@ class MemoryAllocator { virtual void release(void* pointer, size_t size)=0; }; -/** - * Abstract class with the basic interface of all single frames memory allocators - */ -class SingleFrameAllocator : public MemoryAllocator{ - - public: - - /// Constructor - SingleFrameAllocator() = default; - - /// Destructor - virtual ~SingleFrameAllocator() override = default; - - /// Assignment operator - SingleFrameAllocator& operator=(SingleFrameAllocator& allocator) = default; - - /// Allocate memory of a given size (in bytes) and return a pointer to the - /// allocated memory. - virtual void* allocate(size_t size) override =0; - - /// Release previously allocated memory. - virtual void release(void* pointer, size_t size) override =0; - - /// Reset the marker of the current allocated memory - virtual void reset()=0; -}; - } #endif diff --git a/src/memory/MemoryManager.cpp b/src/memory/MemoryManager.cpp index 8752c311..7dc80543 100644 --- a/src/memory/MemoryManager.cpp +++ b/src/memory/MemoryManager.cpp @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -32,9 +32,3 @@ using namespace reactphysics3d; DefaultAllocator MemoryManager::mDefaultAllocator; MemoryAllocator* MemoryManager::mBaseAllocator = &mDefaultAllocator; -// Constructor -MemoryManager::MemoryManager() { - - mSingleFrameAllocator = &mDefaultSingleFrameAllocator; - mPoolAllocator = &mDefaultPoolAllocator; -} diff --git a/src/memory/MemoryManager.h b/src/memory/MemoryManager.h index 423a5303..65797218 100644 --- a/src/memory/MemoryManager.h +++ b/src/memory/MemoryManager.h @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -28,9 +28,8 @@ // Libraries #include "memory/DefaultAllocator.h" -#include "memory/DefaultPoolAllocator.h" -#include "memory/MemoryAllocator.h" -#include "memory/DefaultSingleFrameAllocator.h" +#include "memory/PoolAllocator.h" +#include "memory/SingleFrameAllocator.h" /// Namespace ReactPhysics3D namespace reactphysics3d { @@ -46,24 +45,18 @@ class MemoryAllocator; class MemoryManager { private: - + /// Default malloc/free memory allocator static DefaultAllocator mDefaultAllocator; - - /// Default single frame memory allocator - DefaultSingleFrameAllocator mDefaultSingleFrameAllocator; - - /// Default pool memory allocator - DefaultPoolAllocator mDefaultPoolAllocator; /// Pointer to the base memory allocator to use static MemoryAllocator* mBaseAllocator; - /// Single frame stack allocator - SingleFrameAllocator* mSingleFrameAllocator; - /// Memory pool allocator - MemoryAllocator* mPoolAllocator; + PoolAllocator mPoolAllocator; + + /// Single frame stack allocator + SingleFrameAllocator mSingleFrameAllocator; public: @@ -75,7 +68,7 @@ class MemoryManager { }; /// Constructor - MemoryManager(); + MemoryManager() = default; /// Destructor ~MemoryManager() = default; @@ -87,23 +80,17 @@ class MemoryManager { void release(AllocationType allocationType, void* pointer, size_t size); /// Return the pool allocator - MemoryAllocator& getPoolAllocator(); + PoolAllocator& getPoolAllocator(); /// Return the single frame stack allocator SingleFrameAllocator& getSingleFrameAllocator(); /// Return the base memory allocator static MemoryAllocator& getBaseAllocator(); - + /// Set the base memory allocator static void setBaseAllocator(MemoryAllocator* memoryAllocator); - /// Set the single frame memory allocator - void setSingleFrameAllocator(SingleFrameAllocator* singleFrameAllocator); - - /// Set the pool memory allocator - void setPoolAllocator(MemoryAllocator* poolAllocator); - /// Reset the single frame allocator void resetFrameAllocator(); }; @@ -113,8 +100,8 @@ inline void* MemoryManager::allocate(AllocationType allocationType, size_t size) switch (allocationType) { case AllocationType::Base: return mBaseAllocator->allocate(size); - case AllocationType::Pool: return mPoolAllocator->allocate(size); - case AllocationType::Frame: return mSingleFrameAllocator->allocate(size); + case AllocationType::Pool: return mPoolAllocator.allocate(size); + case AllocationType::Frame: return mSingleFrameAllocator.allocate(size); } return nullptr; @@ -125,19 +112,19 @@ 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::Frame: mSingleFrameAllocator->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 MemoryAllocator& MemoryManager::getPoolAllocator() { - return *mPoolAllocator; +inline PoolAllocator& MemoryManager::getPoolAllocator() { + return mPoolAllocator; } // Return the single frame stack allocator inline SingleFrameAllocator& MemoryManager::getSingleFrameAllocator() { - return *mSingleFrameAllocator; + return mSingleFrameAllocator; } // Return the base memory allocator @@ -150,19 +137,9 @@ inline void MemoryManager::setBaseAllocator(MemoryAllocator* baseAllocator) { mBaseAllocator = baseAllocator; } -// Set the base memory 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 inline void MemoryManager::resetFrameAllocator() { - mSingleFrameAllocator->reset(); + mSingleFrameAllocator.reset(); } } diff --git a/src/memory/DefaultPoolAllocator.cpp b/src/memory/PoolAllocator.cpp similarity index 94% rename from src/memory/DefaultPoolAllocator.cpp rename to src/memory/PoolAllocator.cpp index 31892902..3126ff9e 100644 --- a/src/memory/DefaultPoolAllocator.cpp +++ b/src/memory/PoolAllocator.cpp @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -24,7 +24,7 @@ ********************************************************************************/ // Libraries -#include "DefaultPoolAllocator.h" +#include "PoolAllocator.h" #include "MemoryManager.h" #include #include @@ -32,12 +32,12 @@ using namespace reactphysics3d; // Initialization of static variables -bool DefaultPoolAllocator::isMapSizeToHeadIndexInitialized = false; -size_t DefaultPoolAllocator::mUnitSizes[NB_HEAPS]; -int DefaultPoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1]; +bool PoolAllocator::isMapSizeToHeadIndexInitialized = false; +size_t PoolAllocator::mUnitSizes[NB_HEAPS]; +int PoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1]; // Constructor -DefaultPoolAllocator::DefaultPoolAllocator() { +PoolAllocator::PoolAllocator() { // Allocate some memory to manage the blocks mNbAllocatedMemoryBlocks = 64; @@ -79,7 +79,7 @@ DefaultPoolAllocator::DefaultPoolAllocator() { } // Destructor -DefaultPoolAllocator::~DefaultPoolAllocator() { +PoolAllocator::~PoolAllocator() { // Release the memory allocated for each block for (uint i=0; i 0); @@ -173,7 +173,7 @@ void* DefaultPoolAllocator::allocate(size_t size) { } // Release previously allocated memory. -void DefaultPoolAllocator::release(void* pointer, size_t size) { +void PoolAllocator::release(void* pointer, size_t size) { assert(size > 0); diff --git a/src/memory/DefaultPoolAllocator.h b/src/memory/PoolAllocator.h similarity index 94% rename from src/memory/DefaultPoolAllocator.h rename to src/memory/PoolAllocator.h index d1e93b91..27c4586d 100644 --- a/src/memory/DefaultPoolAllocator.h +++ b/src/memory/PoolAllocator.h @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -33,14 +33,14 @@ /// ReactPhysics3D namespace namespace reactphysics3d { -// Class DefaultPoolAllocator +// Class PoolAllocator /** * This class is used to efficiently allocate memory on the heap. * It allows us to allocate small blocks of memory (smaller or equal to 1024 bytes) * efficiently. This implementation is inspired by the small block allocator * described here : http://www.codeproject.com/useritems/Small_Block_Allocator.asp */ -class DefaultPoolAllocator : public MemoryAllocator { +class PoolAllocator : public MemoryAllocator { private : @@ -126,13 +126,13 @@ class DefaultPoolAllocator : public MemoryAllocator { // -------------------- Methods -------------------- // /// Constructor - DefaultPoolAllocator(); + PoolAllocator(); /// Destructor - virtual ~DefaultPoolAllocator() override; + virtual ~PoolAllocator() override; /// Assignment operator - DefaultPoolAllocator& operator=(DefaultPoolAllocator& allocator) = default; + PoolAllocator& operator=(PoolAllocator& allocator) = default; /// Allocate memory of a given size (in bytes) and return a pointer to the /// allocated memory. diff --git a/src/memory/DefaultSingleFrameAllocator.cpp b/src/memory/SingleFrameAllocator.cpp similarity index 79% rename from src/memory/DefaultSingleFrameAllocator.cpp rename to src/memory/SingleFrameAllocator.cpp index 8d1691e0..6ffe924b 100644 --- a/src/memory/DefaultSingleFrameAllocator.cpp +++ b/src/memory/SingleFrameAllocator.cpp @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -24,7 +24,7 @@ ********************************************************************************/ // Libraries -#include "DefaultSingleFrameAllocator.h" +#include "SingleFrameAllocator.h" #include "MemoryManager.h" #include #include @@ -32,27 +32,26 @@ using namespace reactphysics3d; // Constructor -DefaultSingleFrameAllocator::DefaultSingleFrameAllocator() - : mBaseMemoryAllocator(&MemoryManager::getBaseAllocator()), - mTotalSizeBytes(INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES), +SingleFrameAllocator::SingleFrameAllocator() + : mTotalSizeBytes(INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES), mCurrentOffset(0), mNbFramesTooMuchAllocated(0), mNeedToAllocatedMore(false) { // Allocate a whole block of memory at the beginning - mMemoryBufferStart = static_cast(mBaseMemoryAllocator->allocate(mTotalSizeBytes)); + mMemoryBufferStart = static_cast(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes)); assert(mMemoryBufferStart != nullptr); } // Destructor -DefaultSingleFrameAllocator::~DefaultSingleFrameAllocator() { +SingleFrameAllocator::~SingleFrameAllocator() { // Release the memory allocated at the beginning - mBaseMemoryAllocator->release(mMemoryBufferStart, mTotalSizeBytes); + MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes); } // Allocate memory of a given size (in bytes) and return a pointer to the // allocated memory. -void* DefaultSingleFrameAllocator::allocate(size_t size) { +void* SingleFrameAllocator::allocate(size_t size) { // Check that there is enough remaining memory in the buffer if (mCurrentOffset + size > mTotalSizeBytes) { @@ -61,7 +60,7 @@ void* DefaultSingleFrameAllocator::allocate(size_t size) { mNeedToAllocatedMore = true; // Return default memory allocation - return mBaseMemoryAllocator->allocate(size); + return MemoryManager::getBaseAllocator().allocate(size); } // Next available memory location @@ -75,19 +74,19 @@ void* DefaultSingleFrameAllocator::allocate(size_t size) { } // Release previously allocated memory. -void DefaultSingleFrameAllocator::release(void* pointer, size_t size) { +void SingleFrameAllocator::release(void* pointer, size_t size) { // If allocated memory is not within the single frame allocation range char* p = static_cast(pointer); if (p < mMemoryBufferStart || p > mMemoryBufferStart + mTotalSizeBytes) { // Use default deallocation - mBaseMemoryAllocator->release(pointer, size); + MemoryManager::getBaseAllocator().release(pointer, size); } } // Reset the marker of the current allocated memory -void DefaultSingleFrameAllocator::reset() { +void SingleFrameAllocator::reset() { // If too much memory is allocated if (mCurrentOffset < mTotalSizeBytes / 2) { @@ -97,14 +96,14 @@ void DefaultSingleFrameAllocator::reset() { if (mNbFramesTooMuchAllocated > NB_FRAMES_UNTIL_SHRINK) { // Release the memory allocated at the beginning - mBaseMemoryAllocator->release(mMemoryBufferStart, mTotalSizeBytes); + MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes); // Divide the total memory to allocate by two mTotalSizeBytes /= 2; if (mTotalSizeBytes == 0) mTotalSizeBytes = 1; // Allocate a whole block of memory at the beginning - mMemoryBufferStart = static_cast(mBaseMemoryAllocator->allocate(mTotalSizeBytes)); + mMemoryBufferStart = static_cast(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes)); assert(mMemoryBufferStart != nullptr); mNbFramesTooMuchAllocated = 0; @@ -118,13 +117,13 @@ void DefaultSingleFrameAllocator::reset() { if (mNeedToAllocatedMore) { // Release the memory allocated at the beginning - mBaseMemoryAllocator->release(mMemoryBufferStart, mTotalSizeBytes); + MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes); // Multiply the total memory to allocate by two mTotalSizeBytes *= 2; // Allocate a whole block of memory at the beginning - mMemoryBufferStart = static_cast(mBaseMemoryAllocator->allocate(mTotalSizeBytes)); + mMemoryBufferStart = static_cast(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes)); assert(mMemoryBufferStart != nullptr); mNeedToAllocatedMore = false; diff --git a/src/memory/DefaultSingleFrameAllocator.h b/src/memory/SingleFrameAllocator.h similarity index 86% rename from src/memory/DefaultSingleFrameAllocator.h rename to src/memory/SingleFrameAllocator.h index b519fe74..599c0b65 100644 --- a/src/memory/DefaultSingleFrameAllocator.h +++ b/src/memory/SingleFrameAllocator.h @@ -1,6 +1,6 @@ /******************************************************************************** * ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2019 Daniel Chappuis * +* Copyright (c) 2010-2018 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -33,12 +33,12 @@ /// ReactPhysics3D namespace namespace reactphysics3d { -// Class DefaultSingleFrameAllocator +// Class SingleFrameAllocator /** * This class represent a memory allocator used to efficiently allocate * memory on the heap that is used during a single frame. */ -class DefaultSingleFrameAllocator : public SingleFrameAllocator { +class SingleFrameAllocator : public MemoryAllocator { private : @@ -49,13 +49,10 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator { static const int NB_FRAMES_UNTIL_SHRINK = 120; /// Initial size (in bytes) of the single frame allocator - static const size_t INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES = 1048576; // 1Mb + size_t INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES = 1048576; // 1Mb // -------------------- Attributes -------------------- // - /// Cached memory allocator used on construction - MemoryAllocator* mBaseMemoryAllocator; - /// Total size (in bytes) of memory of the allocator size_t mTotalSizeBytes; @@ -77,13 +74,13 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator { // -------------------- Methods -------------------- // /// Constructor - DefaultSingleFrameAllocator(); + SingleFrameAllocator(); /// Destructor - virtual ~DefaultSingleFrameAllocator() override; + virtual ~SingleFrameAllocator() override; /// Assignment operator - DefaultSingleFrameAllocator& operator=(DefaultSingleFrameAllocator& allocator) = default; + SingleFrameAllocator& operator=(SingleFrameAllocator& allocator) = default; /// Allocate memory of a given size (in bytes) virtual void* allocate(size_t size) override; @@ -92,7 +89,7 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator { virtual void release(void* pointer, size_t size) override; /// Reset the marker of the current allocated memory - virtual void reset() override; + virtual void reset(); }; }