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
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

View File

@ -29,7 +29,6 @@
// Libraries
#include "memory/MemoryAllocator.h"
#include <cstdlib>
#include <mutex>
/// 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<std::mutex> 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<std::mutex> lock(mMutex);
free(pointer);
}
};

View File

@ -100,9 +100,6 @@ DefaultPoolAllocator::~DefaultPoolAllocator() {
// allocated memory.
void* DefaultPoolAllocator::allocate(size_t size) {
// Lock the method with a mutex
std::lock_guard<std::mutex> 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<std::mutex> lock(mMutex);
// Cannot release a 0-byte allocated memory
if (size == 0) return;

View File

@ -29,7 +29,6 @@
// Libraries
#include "configuration.h"
#include "MemoryAllocator.h"
#include <mutex>
/// 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];

View File

@ -54,9 +54,6 @@ DefaultSingleFrameAllocator::~DefaultSingleFrameAllocator() {
// allocated memory.
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
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<std::mutex> lock(mMutex);
// If allocated memory is not within the single frame allocation range
char* p = static_cast<char*>(pointer);
if (p < mMemoryBufferStart || p > mMemoryBufferStart + mTotalSizeBytes) {

View File

@ -29,7 +29,6 @@
// Libraries
#include "MemoryAllocator.h"
#include "configuration.h"
#include <mutex>
/// 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;

View File

@ -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;
}

View File

@ -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();