diff --git a/src/memory/DefaultAllocator.h b/src/memory/DefaultAllocator.h index 5ff94895..c8432438 100644 --- a/src/memory/DefaultAllocator.h +++ b/src/memory/DefaultAllocator.h @@ -29,6 +29,7 @@ // Libraries #include "memory/MemoryAllocator.h" #include +#include /// ReactPhysics3D namespace namespace reactphysics3d { @@ -39,6 +40,11 @@ namespace reactphysics3d { */ class DefaultAllocator : public MemoryAllocator { + protected: + + /// Mutex + std::mutex mMutex; + public: /// Destructor @@ -50,11 +56,19 @@ class DefaultAllocator : public MemoryAllocator { /// Allocate memory of a given size (in bytes) and return a pointer to the /// 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 39f84eee..fddcc2f8 100644 --- a/src/memory/DefaultPoolAllocator.cpp +++ b/src/memory/DefaultPoolAllocator.cpp @@ -100,6 +100,9 @@ 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; @@ -173,6 +176,9 @@ 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 172a3c6c..8610da0c 100644 --- a/src/memory/DefaultPoolAllocator.h +++ b/src/memory/DefaultPoolAllocator.h @@ -29,6 +29,7 @@ // Libraries #include "configuration.h" #include "MemoryAllocator.h" +#include /// ReactPhysics3D namespace namespace reactphysics3d { @@ -101,6 +102,9 @@ 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 de76ce57..3037488e 100644 --- a/src/memory/DefaultSingleFrameAllocator.cpp +++ b/src/memory/DefaultSingleFrameAllocator.cpp @@ -54,6 +54,9 @@ 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) { @@ -77,6 +80,9 @@ 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 65641129..1ad98769 100644 --- a/src/memory/DefaultSingleFrameAllocator.h +++ b/src/memory/DefaultSingleFrameAllocator.h @@ -29,6 +29,7 @@ // Libraries #include "MemoryAllocator.h" #include "configuration.h" +#include /// ReactPhysics3D namespace namespace reactphysics3d { @@ -52,7 +53,11 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator { static const size_t INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES = 1048576; // 1Mb // -------------------- Attributes -------------------- // - /// Cached memory allocator used on construction + + /// Mutex + std::mutex mMutex; + + /// Cached memory allocator used on construction MemoryAllocator* mBaseMemoryAllocator; /// Total size (in bytes) of memory of the allocator