Use memory allocators from ecs branch
This commit is contained in:
parent
815182aae9
commit
2dfe254c86
|
@ -166,8 +166,8 @@ SET (REACTPHYSICS3D_HEADERS
|
||||||
"src/mathematics/Vector3.h"
|
"src/mathematics/Vector3.h"
|
||||||
"src/mathematics/Ray.h"
|
"src/mathematics/Ray.h"
|
||||||
"src/memory/MemoryAllocator.h"
|
"src/memory/MemoryAllocator.h"
|
||||||
"src/memory/DefaultPoolAllocator.h"
|
"src/memory/PoolAllocator.h"
|
||||||
"src/memory/DefaultSingleFrameAllocator.h"
|
"src/memory/SingleFrameAllocator.h"
|
||||||
"src/memory/DefaultAllocator.h"
|
"src/memory/DefaultAllocator.h"
|
||||||
"src/memory/MemoryManager.h"
|
"src/memory/MemoryManager.h"
|
||||||
"src/containers/Stack.h"
|
"src/containers/Stack.h"
|
||||||
|
@ -263,8 +263,8 @@ SET (REACTPHYSICS3D_SOURCES
|
||||||
"src/mathematics/Transform.cpp"
|
"src/mathematics/Transform.cpp"
|
||||||
"src/mathematics/Vector2.cpp"
|
"src/mathematics/Vector2.cpp"
|
||||||
"src/mathematics/Vector3.cpp"
|
"src/mathematics/Vector3.cpp"
|
||||||
"src/memory/DefaultPoolAllocator.cpp"
|
"src/memory/PoolAllocator.cpp"
|
||||||
"src/memory/DefaultSingleFrameAllocator.cpp"
|
"src/memory/SingleFrameAllocator.cpp"
|
||||||
"src/memory/MemoryManager.cpp"
|
"src/memory/MemoryManager.cpp"
|
||||||
"src/utils/Profiler.cpp"
|
"src/utils/Profiler.cpp"
|
||||||
"src/utils/Logger.cpp"
|
"src/utils/Logger.cpp"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
@ -39,8 +39,6 @@ namespace reactphysics3d {
|
||||||
*/
|
*/
|
||||||
class DefaultAllocator : public MemoryAllocator {
|
class DefaultAllocator : public MemoryAllocator {
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
|
@ -61,7 +59,6 @@ class DefaultAllocator : public MemoryAllocator {
|
||||||
|
|
||||||
/// 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 {
|
||||||
|
|
||||||
free(pointer);
|
free(pointer);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* 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;
|
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
|
#endif
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
@ -32,9 +32,3 @@ using namespace reactphysics3d;
|
||||||
DefaultAllocator MemoryManager::mDefaultAllocator;
|
DefaultAllocator MemoryManager::mDefaultAllocator;
|
||||||
MemoryAllocator* MemoryManager::mBaseAllocator = &mDefaultAllocator;
|
MemoryAllocator* MemoryManager::mBaseAllocator = &mDefaultAllocator;
|
||||||
|
|
||||||
// Constructor
|
|
||||||
MemoryManager::MemoryManager() {
|
|
||||||
|
|
||||||
mSingleFrameAllocator = &mDefaultSingleFrameAllocator;
|
|
||||||
mPoolAllocator = &mDefaultPoolAllocator;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
@ -28,9 +28,8 @@
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "memory/DefaultAllocator.h"
|
#include "memory/DefaultAllocator.h"
|
||||||
#include "memory/DefaultPoolAllocator.h"
|
#include "memory/PoolAllocator.h"
|
||||||
#include "memory/MemoryAllocator.h"
|
#include "memory/SingleFrameAllocator.h"
|
||||||
#include "memory/DefaultSingleFrameAllocator.h"
|
|
||||||
|
|
||||||
/// Namespace ReactPhysics3D
|
/// Namespace ReactPhysics3D
|
||||||
namespace reactphysics3d {
|
namespace reactphysics3d {
|
||||||
|
@ -46,24 +45,18 @@ class MemoryAllocator;
|
||||||
class MemoryManager {
|
class MemoryManager {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/// Default malloc/free memory allocator
|
/// Default malloc/free memory allocator
|
||||||
static DefaultAllocator mDefaultAllocator;
|
static DefaultAllocator mDefaultAllocator;
|
||||||
|
|
||||||
/// Default single frame memory allocator
|
|
||||||
DefaultSingleFrameAllocator mDefaultSingleFrameAllocator;
|
|
||||||
|
|
||||||
/// Default pool memory allocator
|
|
||||||
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
|
|
||||||
SingleFrameAllocator* mSingleFrameAllocator;
|
|
||||||
|
|
||||||
/// Memory pool allocator
|
/// Memory pool allocator
|
||||||
MemoryAllocator* mPoolAllocator;
|
PoolAllocator mPoolAllocator;
|
||||||
|
|
||||||
|
/// Single frame stack allocator
|
||||||
|
SingleFrameAllocator mSingleFrameAllocator;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -75,7 +68,7 @@ class MemoryManager {
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
MemoryManager();
|
MemoryManager() = default;
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~MemoryManager() = default;
|
~MemoryManager() = default;
|
||||||
|
@ -87,23 +80,17 @@ class MemoryManager {
|
||||||
void release(AllocationType allocationType, void* pointer, size_t size);
|
void release(AllocationType allocationType, void* pointer, size_t size);
|
||||||
|
|
||||||
/// Return the pool allocator
|
/// Return the pool allocator
|
||||||
MemoryAllocator& getPoolAllocator();
|
PoolAllocator& getPoolAllocator();
|
||||||
|
|
||||||
/// Return the single frame stack allocator
|
/// Return the single frame stack allocator
|
||||||
SingleFrameAllocator& getSingleFrameAllocator();
|
SingleFrameAllocator& getSingleFrameAllocator();
|
||||||
|
|
||||||
/// Return the base memory allocator
|
/// Return the base memory allocator
|
||||||
static MemoryAllocator& getBaseAllocator();
|
static MemoryAllocator& getBaseAllocator();
|
||||||
|
|
||||||
/// Set the base memory allocator
|
/// Set the base memory allocator
|
||||||
static void setBaseAllocator(MemoryAllocator* memoryAllocator);
|
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
|
/// Reset the single frame allocator
|
||||||
void resetFrameAllocator();
|
void resetFrameAllocator();
|
||||||
};
|
};
|
||||||
|
@ -113,8 +100,8 @@ inline void* MemoryManager::allocate(AllocationType allocationType, size_t size)
|
||||||
|
|
||||||
switch (allocationType) {
|
switch (allocationType) {
|
||||||
case AllocationType::Base: return mBaseAllocator->allocate(size);
|
case AllocationType::Base: return mBaseAllocator->allocate(size);
|
||||||
case AllocationType::Pool: return mPoolAllocator->allocate(size);
|
case AllocationType::Pool: return mPoolAllocator.allocate(size);
|
||||||
case AllocationType::Frame: return mSingleFrameAllocator->allocate(size);
|
case AllocationType::Frame: return mSingleFrameAllocator.allocate(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -125,19 +112,19 @@ inline void MemoryManager::release(AllocationType allocationType, void* pointer,
|
||||||
|
|
||||||
switch (allocationType) {
|
switch (allocationType) {
|
||||||
case AllocationType::Base: mBaseAllocator->release(pointer, size); break;
|
case AllocationType::Base: mBaseAllocator->release(pointer, size); break;
|
||||||
case AllocationType::Pool: mPoolAllocator->release(pointer, size); break;
|
case AllocationType::Pool: mPoolAllocator.release(pointer, size); break;
|
||||||
case AllocationType::Frame: mSingleFrameAllocator->release(pointer, size); break;
|
case AllocationType::Frame: mSingleFrameAllocator.release(pointer, size); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the pool allocator
|
// Return the pool allocator
|
||||||
inline MemoryAllocator& MemoryManager::getPoolAllocator() {
|
inline PoolAllocator& MemoryManager::getPoolAllocator() {
|
||||||
return *mPoolAllocator;
|
return mPoolAllocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the single frame stack allocator
|
// Return the single frame stack allocator
|
||||||
inline SingleFrameAllocator& MemoryManager::getSingleFrameAllocator() {
|
inline SingleFrameAllocator& MemoryManager::getSingleFrameAllocator() {
|
||||||
return *mSingleFrameAllocator;
|
return mSingleFrameAllocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the base memory allocator
|
// Return the base memory allocator
|
||||||
|
@ -150,19 +137,9 @@ inline void MemoryManager::setBaseAllocator(MemoryAllocator* baseAllocator) {
|
||||||
mBaseAllocator = 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
|
// Reset the single frame allocator
|
||||||
inline void MemoryManager::resetFrameAllocator() {
|
inline void MemoryManager::resetFrameAllocator() {
|
||||||
mSingleFrameAllocator->reset();
|
mSingleFrameAllocator.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
********************************************************************************/
|
********************************************************************************/
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "DefaultPoolAllocator.h"
|
#include "PoolAllocator.h"
|
||||||
#include "MemoryManager.h"
|
#include "MemoryManager.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
@ -32,12 +32,12 @@
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
|
||||||
// Initialization of static variables
|
// Initialization of static variables
|
||||||
bool DefaultPoolAllocator::isMapSizeToHeadIndexInitialized = false;
|
bool PoolAllocator::isMapSizeToHeadIndexInitialized = false;
|
||||||
size_t DefaultPoolAllocator::mUnitSizes[NB_HEAPS];
|
size_t PoolAllocator::mUnitSizes[NB_HEAPS];
|
||||||
int DefaultPoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1];
|
int PoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1];
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
DefaultPoolAllocator::DefaultPoolAllocator() {
|
PoolAllocator::PoolAllocator() {
|
||||||
|
|
||||||
// Allocate some memory to manage the blocks
|
// Allocate some memory to manage the blocks
|
||||||
mNbAllocatedMemoryBlocks = 64;
|
mNbAllocatedMemoryBlocks = 64;
|
||||||
|
@ -79,7 +79,7 @@ DefaultPoolAllocator::DefaultPoolAllocator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
DefaultPoolAllocator::~DefaultPoolAllocator() {
|
PoolAllocator::~PoolAllocator() {
|
||||||
|
|
||||||
// Release the memory allocated for each block
|
// Release the memory allocated for each block
|
||||||
for (uint i=0; i<mNbCurrentMemoryBlocks; i++) {
|
for (uint i=0; i<mNbCurrentMemoryBlocks; i++) {
|
||||||
|
@ -98,7 +98,7 @@ DefaultPoolAllocator::~DefaultPoolAllocator() {
|
||||||
|
|
||||||
// Allocate memory of a given size (in bytes) and return a pointer to the
|
// Allocate memory of a given size (in bytes) and return a pointer to the
|
||||||
// allocated memory.
|
// allocated memory.
|
||||||
void* DefaultPoolAllocator::allocate(size_t size) {
|
void* PoolAllocator::allocate(size_t size) {
|
||||||
|
|
||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ void* DefaultPoolAllocator::allocate(size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release previously allocated memory.
|
// Release previously allocated memory.
|
||||||
void DefaultPoolAllocator::release(void* pointer, size_t size) {
|
void PoolAllocator::release(void* pointer, size_t size) {
|
||||||
|
|
||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
@ -33,14 +33,14 @@
|
||||||
/// ReactPhysics3D namespace
|
/// ReactPhysics3D namespace
|
||||||
namespace reactphysics3d {
|
namespace reactphysics3d {
|
||||||
|
|
||||||
// Class DefaultPoolAllocator
|
// Class PoolAllocator
|
||||||
/**
|
/**
|
||||||
* This class is used to efficiently allocate memory on the heap.
|
* 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)
|
* 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
|
* efficiently. This implementation is inspired by the small block allocator
|
||||||
* described here : http://www.codeproject.com/useritems/Small_Block_Allocator.asp
|
* described here : http://www.codeproject.com/useritems/Small_Block_Allocator.asp
|
||||||
*/
|
*/
|
||||||
class DefaultPoolAllocator : public MemoryAllocator {
|
class PoolAllocator : public MemoryAllocator {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -126,13 +126,13 @@ class DefaultPoolAllocator : public MemoryAllocator {
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
DefaultPoolAllocator();
|
PoolAllocator();
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
virtual ~DefaultPoolAllocator() override;
|
virtual ~PoolAllocator() override;
|
||||||
|
|
||||||
/// Assignment operator
|
/// 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
|
/// Allocate memory of a given size (in bytes) and return a pointer to the
|
||||||
/// allocated memory.
|
/// allocated memory.
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
********************************************************************************/
|
********************************************************************************/
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "DefaultSingleFrameAllocator.h"
|
#include "SingleFrameAllocator.h"
|
||||||
#include "MemoryManager.h"
|
#include "MemoryManager.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
@ -32,27 +32,26 @@
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
DefaultSingleFrameAllocator::DefaultSingleFrameAllocator()
|
SingleFrameAllocator::SingleFrameAllocator()
|
||||||
: mBaseMemoryAllocator(&MemoryManager::getBaseAllocator()),
|
: mTotalSizeBytes(INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES),
|
||||||
mTotalSizeBytes(INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES),
|
|
||||||
mCurrentOffset(0), mNbFramesTooMuchAllocated(0), mNeedToAllocatedMore(false) {
|
mCurrentOffset(0), mNbFramesTooMuchAllocated(0), mNeedToAllocatedMore(false) {
|
||||||
|
|
||||||
// Allocate a whole block of memory at the beginning
|
// Allocate a whole block of memory at the beginning
|
||||||
mMemoryBufferStart = static_cast<char*>(mBaseMemoryAllocator->allocate(mTotalSizeBytes));
|
mMemoryBufferStart = static_cast<char*>(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes));
|
||||||
assert(mMemoryBufferStart != nullptr);
|
assert(mMemoryBufferStart != nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
DefaultSingleFrameAllocator::~DefaultSingleFrameAllocator() {
|
SingleFrameAllocator::~SingleFrameAllocator() {
|
||||||
|
|
||||||
// Release the memory allocated at the beginning
|
// 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
|
// Allocate memory of a given size (in bytes) and return a pointer to the
|
||||||
// allocated memory.
|
// allocated memory.
|
||||||
void* DefaultSingleFrameAllocator::allocate(size_t size) {
|
void* SingleFrameAllocator::allocate(size_t size) {
|
||||||
|
|
||||||
// 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) {
|
||||||
|
@ -61,7 +60,7 @@ void* DefaultSingleFrameAllocator::allocate(size_t size) {
|
||||||
mNeedToAllocatedMore = true;
|
mNeedToAllocatedMore = true;
|
||||||
|
|
||||||
// Return default memory allocation
|
// Return default memory allocation
|
||||||
return mBaseMemoryAllocator->allocate(size);
|
return MemoryManager::getBaseAllocator().allocate(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next available memory location
|
// Next available memory location
|
||||||
|
@ -75,19 +74,19 @@ void* DefaultSingleFrameAllocator::allocate(size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release previously allocated memory.
|
// 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
|
// 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) {
|
||||||
|
|
||||||
// Use default deallocation
|
// Use default deallocation
|
||||||
mBaseMemoryAllocator->release(pointer, size);
|
MemoryManager::getBaseAllocator().release(pointer, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the marker of the current allocated memory
|
// Reset the marker of the current allocated memory
|
||||||
void DefaultSingleFrameAllocator::reset() {
|
void SingleFrameAllocator::reset() {
|
||||||
|
|
||||||
// If too much memory is allocated
|
// If too much memory is allocated
|
||||||
if (mCurrentOffset < mTotalSizeBytes / 2) {
|
if (mCurrentOffset < mTotalSizeBytes / 2) {
|
||||||
|
@ -97,14 +96,14 @@ void DefaultSingleFrameAllocator::reset() {
|
||||||
if (mNbFramesTooMuchAllocated > NB_FRAMES_UNTIL_SHRINK) {
|
if (mNbFramesTooMuchAllocated > NB_FRAMES_UNTIL_SHRINK) {
|
||||||
|
|
||||||
// Release the memory allocated at the beginning
|
// Release the memory allocated at the beginning
|
||||||
mBaseMemoryAllocator->release(mMemoryBufferStart, mTotalSizeBytes);
|
MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes);
|
||||||
|
|
||||||
// Divide the total memory to allocate by two
|
// Divide the total memory to allocate by two
|
||||||
mTotalSizeBytes /= 2;
|
mTotalSizeBytes /= 2;
|
||||||
if (mTotalSizeBytes == 0) mTotalSizeBytes = 1;
|
if (mTotalSizeBytes == 0) mTotalSizeBytes = 1;
|
||||||
|
|
||||||
// Allocate a whole block of memory at the beginning
|
// Allocate a whole block of memory at the beginning
|
||||||
mMemoryBufferStart = static_cast<char*>(mBaseMemoryAllocator->allocate(mTotalSizeBytes));
|
mMemoryBufferStart = static_cast<char*>(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes));
|
||||||
assert(mMemoryBufferStart != nullptr);
|
assert(mMemoryBufferStart != nullptr);
|
||||||
|
|
||||||
mNbFramesTooMuchAllocated = 0;
|
mNbFramesTooMuchAllocated = 0;
|
||||||
|
@ -118,13 +117,13 @@ void DefaultSingleFrameAllocator::reset() {
|
||||||
if (mNeedToAllocatedMore) {
|
if (mNeedToAllocatedMore) {
|
||||||
|
|
||||||
// Release the memory allocated at the beginning
|
// Release the memory allocated at the beginning
|
||||||
mBaseMemoryAllocator->release(mMemoryBufferStart, mTotalSizeBytes);
|
MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes);
|
||||||
|
|
||||||
// Multiply the total memory to allocate by two
|
// Multiply the total memory to allocate by two
|
||||||
mTotalSizeBytes *= 2;
|
mTotalSizeBytes *= 2;
|
||||||
|
|
||||||
// Allocate a whole block of memory at the beginning
|
// Allocate a whole block of memory at the beginning
|
||||||
mMemoryBufferStart = static_cast<char*>(mBaseMemoryAllocator->allocate(mTotalSizeBytes));
|
mMemoryBufferStart = static_cast<char*>(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes));
|
||||||
assert(mMemoryBufferStart != nullptr);
|
assert(mMemoryBufferStart != nullptr);
|
||||||
|
|
||||||
mNeedToAllocatedMore = false;
|
mNeedToAllocatedMore = false;
|
|
@ -1,6 +1,6 @@
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
* 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. *
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
@ -33,12 +33,12 @@
|
||||||
/// ReactPhysics3D namespace
|
/// ReactPhysics3D namespace
|
||||||
namespace reactphysics3d {
|
namespace reactphysics3d {
|
||||||
|
|
||||||
// Class DefaultSingleFrameAllocator
|
// Class SingleFrameAllocator
|
||||||
/**
|
/**
|
||||||
* This class represent a memory allocator used to efficiently allocate
|
* This class represent a memory allocator used to efficiently allocate
|
||||||
* memory on the heap that is used during a single frame.
|
* memory on the heap that is used during a single frame.
|
||||||
*/
|
*/
|
||||||
class DefaultSingleFrameAllocator : public SingleFrameAllocator {
|
class SingleFrameAllocator : public MemoryAllocator {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -49,13 +49,10 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator {
|
||||||
static const int NB_FRAMES_UNTIL_SHRINK = 120;
|
static const int NB_FRAMES_UNTIL_SHRINK = 120;
|
||||||
|
|
||||||
/// Initial size (in bytes) of the single frame allocator
|
/// 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 -------------------- //
|
// -------------------- Attributes -------------------- //
|
||||||
|
|
||||||
/// Cached memory allocator used on construction
|
|
||||||
MemoryAllocator* mBaseMemoryAllocator;
|
|
||||||
|
|
||||||
/// Total size (in bytes) of memory of the allocator
|
/// Total size (in bytes) of memory of the allocator
|
||||||
size_t mTotalSizeBytes;
|
size_t mTotalSizeBytes;
|
||||||
|
|
||||||
|
@ -77,13 +74,13 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator {
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
DefaultSingleFrameAllocator();
|
SingleFrameAllocator();
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
virtual ~DefaultSingleFrameAllocator() override;
|
virtual ~SingleFrameAllocator() override;
|
||||||
|
|
||||||
/// Assignment operator
|
/// Assignment operator
|
||||||
DefaultSingleFrameAllocator& operator=(DefaultSingleFrameAllocator& allocator) = default;
|
SingleFrameAllocator& operator=(SingleFrameAllocator& allocator) = default;
|
||||||
|
|
||||||
/// Allocate memory of a given size (in bytes)
|
/// Allocate memory of a given size (in bytes)
|
||||||
virtual void* allocate(size_t size) override;
|
virtual void* allocate(size_t size) override;
|
||||||
|
@ -92,7 +89,7 @@ class DefaultSingleFrameAllocator : public SingleFrameAllocator {
|
||||||
virtual void release(void* pointer, size_t size) override;
|
virtual void release(void* pointer, size_t size) override;
|
||||||
|
|
||||||
/// Reset the marker of the current allocated memory
|
/// Reset the marker of the current allocated memory
|
||||||
virtual void reset() override;
|
virtual void reset();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user