Make memory allocators thread-safe
This commit is contained in:
parent
22109d02ff
commit
2ce0f8d76f
|
@ -29,6 +29,7 @@
|
|||
// Libraries
|
||||
#include "memory/MemoryAllocator.h"
|
||||
#include <cstdlib>
|
||||
#include <mutex>
|
||||
|
||||
/// 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<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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -100,6 +100,9 @@ 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;
|
||||
|
||||
|
@ -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<std::mutex> lock(mMutex);
|
||||
|
||||
// Cannot release a 0-byte allocated memory
|
||||
if (size == 0) return;
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
// Libraries
|
||||
#include "configuration.h"
|
||||
#include "MemoryAllocator.h"
|
||||
#include <mutex>
|
||||
|
||||
/// 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];
|
||||
|
||||
|
|
|
@ -54,6 +54,9 @@ 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) {
|
||||
|
||||
|
@ -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<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) {
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
// Libraries
|
||||
#include "MemoryAllocator.h"
|
||||
#include "configuration.h"
|
||||
#include <mutex>
|
||||
|
||||
/// 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
|
||||
|
|
Loading…
Reference in New Issue
Block a user