Remove mutexes and do not use static pool and single frame memory allocators

This commit is contained in:
Daniel Chappuis 2019-02-28 17:25:37 +01:00
parent 2ce0f8d76f
commit ca87fb624d
8 changed files with 21 additions and 41 deletions

View File

@ -165,6 +165,9 @@ class CollisionWorld {
/// Test and report collisions between all shapes of the world /// Test and report collisions between all shapes of the world
void testCollision(CollisionCallback* callback); void testCollision(CollisionCallback* callback);
/// Return a reference to the memory manager of the world
MemoryManager& getMemoryManager();
#ifdef IS_PROFILING_ACTIVE #ifdef IS_PROFILING_ACTIVE
/// Return a reference to the profiler /// Return a reference to the profiler
@ -255,6 +258,11 @@ inline void CollisionWorld::testOverlap(CollisionBody* body, OverlapCallback* ov
mCollisionDetection.testOverlap(body, overlapCallback, categoryMaskBits); 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 the name of the world
/** /**
* @return Name of the world * @return Name of the world

View File

@ -29,7 +29,6 @@
// Libraries // Libraries
#include "memory/MemoryAllocator.h" #include "memory/MemoryAllocator.h"
#include <cstdlib> #include <cstdlib>
#include <mutex>
/// ReactPhysics3D namespace /// ReactPhysics3D namespace
namespace reactphysics3d { namespace reactphysics3d {
@ -42,9 +41,6 @@ class DefaultAllocator : public MemoryAllocator {
protected: protected:
/// Mutex
std::mutex mMutex;
public: public:
/// Destructor /// Destructor
@ -57,18 +53,12 @@ class DefaultAllocator : public MemoryAllocator {
/// allocated memory. /// allocated memory.
virtual void* allocate(size_t size) override { virtual void* allocate(size_t size) override {
// Lock the method with a mutex
std::lock_guard<std::mutex> lock(mMutex);
return malloc(size); return malloc(size);
} }
/// Release previously allocated memory. /// Release previously allocated memory.
virtual void release(void* pointer, size_t size) override { virtual void release(void* pointer, size_t size) override {
// Lock the method with a mutex
std::lock_guard<std::mutex> lock(mMutex);
free(pointer); free(pointer);
} }
}; };

View File

@ -100,9 +100,6 @@ DefaultPoolAllocator::~DefaultPoolAllocator() {
// allocated memory. // allocated memory.
void* DefaultPoolAllocator::allocate(size_t size) { void* DefaultPoolAllocator::allocate(size_t size) {
// Lock the method with a mutex
std::lock_guard<std::mutex> lock(mMutex);
// We cannot allocate zero bytes // We cannot allocate zero bytes
if (size == 0) return nullptr; if (size == 0) return nullptr;
@ -176,9 +173,6 @@ void* DefaultPoolAllocator::allocate(size_t size) {
// Release previously allocated memory. // Release previously allocated memory.
void DefaultPoolAllocator::release(void* pointer, size_t size) { void DefaultPoolAllocator::release(void* pointer, size_t size) {
// Lock the method with a mutex
std::lock_guard<std::mutex> lock(mMutex);
// Cannot release a 0-byte allocated memory // Cannot release a 0-byte allocated memory
if (size == 0) return; if (size == 0) return;

View File

@ -29,7 +29,6 @@
// Libraries // Libraries
#include "configuration.h" #include "configuration.h"
#include "MemoryAllocator.h" #include "MemoryAllocator.h"
#include <mutex>
/// ReactPhysics3D namespace /// ReactPhysics3D namespace
namespace reactphysics3d { namespace reactphysics3d {
@ -102,9 +101,6 @@ class DefaultPoolAllocator : public MemoryAllocator {
/// True if the mMapSizeToHeapIndex array has already been initialized /// True if the mMapSizeToHeapIndex array has already been initialized
static bool isMapSizeToHeadIndexInitialized; static bool isMapSizeToHeadIndexInitialized;
/// Mutex
std::mutex mMutex;
/// Pointers to the first free memory unit for each heap /// Pointers to the first free memory unit for each heap
MemoryUnit* mFreeMemoryUnits[NB_HEAPS]; MemoryUnit* mFreeMemoryUnits[NB_HEAPS];

View File

@ -54,9 +54,6 @@ DefaultSingleFrameAllocator::~DefaultSingleFrameAllocator() {
// allocated memory. // allocated memory.
void* DefaultSingleFrameAllocator::allocate(size_t size) { void* DefaultSingleFrameAllocator::allocate(size_t size) {
// Lock the method with a mutex
std::lock_guard<std::mutex> lock(mMutex);
// Check that there is enough remaining memory in the buffer // Check that there is enough remaining memory in the buffer
if (mCurrentOffset + size > mTotalSizeBytes) { if (mCurrentOffset + size > mTotalSizeBytes) {
@ -80,9 +77,6 @@ void* DefaultSingleFrameAllocator::allocate(size_t size) {
// Release previously allocated memory. // Release previously allocated memory.
void DefaultSingleFrameAllocator::release(void* pointer, size_t size) { void DefaultSingleFrameAllocator::release(void* pointer, size_t size) {
// Lock the method with a mutex
std::lock_guard<std::mutex> lock(mMutex);
// If allocated memory is not within the single frame allocation range // If allocated memory is not within the single frame allocation range
char* p = static_cast<char*>(pointer); char* p = static_cast<char*>(pointer);
if (p < mMemoryBufferStart || p > mMemoryBufferStart + mTotalSizeBytes) { if (p < mMemoryBufferStart || p > mMemoryBufferStart + mTotalSizeBytes) {

View File

@ -29,7 +29,6 @@
// Libraries // Libraries
#include "MemoryAllocator.h" #include "MemoryAllocator.h"
#include "configuration.h" #include "configuration.h"
#include <mutex>
/// ReactPhysics3D namespace /// ReactPhysics3D namespace
namespace reactphysics3d { namespace reactphysics3d {
@ -54,9 +53,6 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator {
// -------------------- Attributes -------------------- // // -------------------- Attributes -------------------- //
/// Mutex
std::mutex mMutex;
/// Cached memory allocator used on construction /// Cached memory allocator used on construction
MemoryAllocator* mBaseMemoryAllocator; MemoryAllocator* mBaseMemoryAllocator;

View File

@ -30,9 +30,11 @@ using namespace reactphysics3d;
// Static variables // Static variables
DefaultAllocator MemoryManager::mDefaultAllocator; DefaultAllocator MemoryManager::mDefaultAllocator;
DefaultSingleFrameAllocator MemoryManager::mDefaultSingleFrameAllocator;
DefaultPoolAllocator MemoryManager::mDefaultPoolAllocator;
MemoryAllocator* MemoryManager::mBaseAllocator = &mDefaultAllocator; MemoryAllocator* MemoryManager::mBaseAllocator = &mDefaultAllocator;
MemoryAllocator* MemoryManager::mPoolAllocator = &mDefaultPoolAllocator;
SingleFrameAllocator* MemoryManager::mSingleFrameAllocator = &mDefaultSingleFrameAllocator;
// Constructor
MemoryManager::MemoryManager() {
mSingleFrameAllocator = &mDefaultSingleFrameAllocator;
mPoolAllocator = &mDefaultPoolAllocator;
}

View File

@ -51,19 +51,19 @@ class MemoryManager {
static DefaultAllocator mDefaultAllocator; static DefaultAllocator mDefaultAllocator;
/// Default single frame memory allocator /// Default single frame memory allocator
static DefaultSingleFrameAllocator mDefaultSingleFrameAllocator; DefaultSingleFrameAllocator mDefaultSingleFrameAllocator;
/// Default pool memory allocator /// Default pool memory allocator
static DefaultPoolAllocator mDefaultPoolAllocator; DefaultPoolAllocator mDefaultPoolAllocator;
/// Pointer to the base memory allocator to use /// Pointer to the base memory allocator to use
static MemoryAllocator* mBaseAllocator; static MemoryAllocator* mBaseAllocator;
/// Single frame stack allocator /// Single frame stack allocator
static SingleFrameAllocator* mSingleFrameAllocator; SingleFrameAllocator* mSingleFrameAllocator;
/// Memory pool allocator /// Memory pool allocator
static MemoryAllocator* mPoolAllocator; MemoryAllocator* mPoolAllocator;
public: public:
@ -75,7 +75,7 @@ class MemoryManager {
}; };
/// Constructor /// Constructor
MemoryManager() = default; MemoryManager();
/// Destructor /// Destructor
~MemoryManager() = default; ~MemoryManager() = default;
@ -99,10 +99,10 @@ class MemoryManager {
static void setBaseAllocator(MemoryAllocator* memoryAllocator); static void setBaseAllocator(MemoryAllocator* memoryAllocator);
/// Set the single frame memory allocator /// Set the single frame memory allocator
static void setSingleFrameAllocator(SingleFrameAllocator* singleFrameAllocator); void setSingleFrameAllocator(SingleFrameAllocator* singleFrameAllocator);
/// Set the pool memory allocator /// Set the pool memory allocator
static void setPoolAllocator(MemoryAllocator* poolAllocator); void setPoolAllocator(MemoryAllocator* poolAllocator);
/// Reset the single frame allocator /// Reset the single frame allocator
void resetFrameAllocator(); void resetFrameAllocator();