Merge master into develop and fix conflicts

This commit is contained in:
Daniel Chappuis 2018-09-26 19:50:44 +02:00
commit 198624b89c
13 changed files with 118 additions and 60 deletions

View File

@ -6,6 +6,7 @@
- Make possible for the user to get vertices, normals and triangle indices of a ConcaveMeshShape - Make possible for the user to get vertices, normals and triangle indices of a ConcaveMeshShape
- Make possible for the user to get vertices and height values of the HeightFieldShape - Make possible for the user to get vertices and height values of the HeightFieldShape
- Make possible for the user to use a custom single frame and pool memory allocator
### Fixed ### Fixed

View File

@ -148,8 +148,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/PoolAllocator.h" "src/memory/DefaultPoolAllocator.h"
"src/memory/SingleFrameAllocator.h" "src/memory/DefaultSingleFrameAllocator.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"
@ -226,8 +226,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/PoolAllocator.cpp" "src/memory/DefaultPoolAllocator.cpp"
"src/memory/SingleFrameAllocator.cpp" "src/memory/DefaultSingleFrameAllocator.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"

View File

@ -42,7 +42,7 @@ class ProxyShape;
class CollisionWorld; class CollisionWorld;
class CollisionShape; class CollisionShape;
struct RaycastInfo; struct RaycastInfo;
class PoolAllocator; class DefaultPoolAllocator;
class Profiler; class Profiler;
/// Enumeration for the type of a body /// Enumeration for the type of a body

View File

@ -38,7 +38,7 @@ class ContactManifoldInfo;
struct ContactPointInfo; struct ContactPointInfo;
class CollisionBody; class CollisionBody;
class ContactPoint; class ContactPoint;
class PoolAllocator; class DefaultPoolAllocator;
// Structure ContactManifoldListElement // Structure ContactManifoldListElement
/** /**

View File

@ -34,7 +34,7 @@ namespace reactphysics3d {
class CollisionDetection; class CollisionDetection;
class Body; class Body;
class ContactManifoldInfo; class ContactManifoldInfo;
class PoolAllocator; class DefaultPoolAllocator;
class OverlappingPair; class OverlappingPair;
struct NarrowPhaseInfo; struct NarrowPhaseInfo;
struct ContactPointInfo; struct ContactPointInfo;

View File

@ -35,7 +35,7 @@ namespace reactphysics3d {
// Declarations // Declarations
class Profiler; class Profiler;
class PoolAllocator; class DefaultPoolAllocator;
class AABB; class AABB;
class Transform; class Transform;
struct Ray; struct Ray;

View File

@ -24,7 +24,7 @@
********************************************************************************/ ********************************************************************************/
// Libraries // Libraries
#include "PoolAllocator.h" #include "DefaultPoolAllocator.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 PoolAllocator::isMapSizeToHeadIndexInitialized = false; bool DefaultPoolAllocator::isMapSizeToHeadIndexInitialized = false;
size_t PoolAllocator::mUnitSizes[NB_HEAPS]; size_t DefaultPoolAllocator::mUnitSizes[NB_HEAPS];
int PoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1]; int DefaultPoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1];
// Constructor // Constructor
PoolAllocator::PoolAllocator() { DefaultPoolAllocator::DefaultPoolAllocator() {
// Allocate some memory to manage the blocks // Allocate some memory to manage the blocks
mNbAllocatedMemoryBlocks = 64; mNbAllocatedMemoryBlocks = 64;
@ -79,7 +79,7 @@ PoolAllocator::PoolAllocator() {
} }
// Destructor // Destructor
PoolAllocator::~PoolAllocator() { DefaultPoolAllocator::~DefaultPoolAllocator() {
// 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 @@ PoolAllocator::~PoolAllocator() {
// 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* PoolAllocator::allocate(size_t size) { void* DefaultPoolAllocator::allocate(size_t size) {
// We cannot allocate zero bytes // We cannot allocate zero bytes
if (size == 0) return nullptr; if (size == 0) return nullptr;
@ -171,7 +171,7 @@ void* PoolAllocator::allocate(size_t size) {
} }
// Release previously allocated memory. // Release previously allocated memory.
void PoolAllocator::release(void* pointer, size_t size) { void DefaultPoolAllocator::release(void* pointer, size_t size) {
// Cannot release a 0-byte allocated memory // Cannot release a 0-byte allocated memory
if (size == 0) return; if (size == 0) return;

View File

@ -33,14 +33,14 @@
/// ReactPhysics3D namespace /// ReactPhysics3D namespace
namespace reactphysics3d { namespace reactphysics3d {
// Class PoolAllocator // Class DefaultPoolAllocator
/** /**
* 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 PoolAllocator : public MemoryAllocator { class DefaultPoolAllocator : public MemoryAllocator {
private : private :
@ -126,13 +126,13 @@ class PoolAllocator : public MemoryAllocator {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
/// Constructor /// Constructor
PoolAllocator(); DefaultPoolAllocator();
/// Destructor /// Destructor
virtual ~PoolAllocator() override; virtual ~DefaultPoolAllocator() override;
/// Assignment operator /// Assignment operator
PoolAllocator& operator=(PoolAllocator& allocator) = default; DefaultPoolAllocator& operator=(DefaultPoolAllocator& 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.

View File

@ -24,7 +24,7 @@
********************************************************************************/ ********************************************************************************/
// Libraries // Libraries
#include "SingleFrameAllocator.h" #include "DefaultSingleFrameAllocator.h"
#include "MemoryManager.h" #include "MemoryManager.h"
#include <cstdlib> #include <cstdlib>
#include <cassert> #include <cassert>
@ -32,26 +32,27 @@
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor // Constructor
SingleFrameAllocator::SingleFrameAllocator() DefaultSingleFrameAllocator::DefaultSingleFrameAllocator()
: mTotalSizeBytes(INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES), : mBaseMemoryAllocator(&MemoryManager::getBaseAllocator()),
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*>(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes)); mMemoryBufferStart = static_cast<char*>(mBaseMemoryAllocator->allocate(mTotalSizeBytes));
assert(mMemoryBufferStart != nullptr); assert(mMemoryBufferStart != nullptr);
} }
// Destructor // Destructor
SingleFrameAllocator::~SingleFrameAllocator() { DefaultSingleFrameAllocator::~DefaultSingleFrameAllocator() {
// Release the memory allocated at the beginning // Release the memory allocated at the beginning
MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes); mBaseMemoryAllocator->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* SingleFrameAllocator::allocate(size_t size) { void* DefaultSingleFrameAllocator::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) {
@ -60,7 +61,7 @@ void* SingleFrameAllocator::allocate(size_t size) {
mNeedToAllocatedMore = true; mNeedToAllocatedMore = true;
// Return default memory allocation // Return default memory allocation
return MemoryManager::getBaseAllocator().allocate(size); return mBaseMemoryAllocator->allocate(size);
} }
// Next available memory location // Next available memory location
@ -74,19 +75,19 @@ void* SingleFrameAllocator::allocate(size_t size) {
} }
// Release previously allocated memory. // Release previously allocated memory.
void SingleFrameAllocator::release(void* pointer, size_t size) { void DefaultSingleFrameAllocator::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
MemoryManager::getBaseAllocator().release(pointer, size); mBaseMemoryAllocator->release(pointer, size);
} }
} }
// Reset the marker of the current allocated memory // Reset the marker of the current allocated memory
void SingleFrameAllocator::reset() { void DefaultSingleFrameAllocator::reset() {
// If too much memory is allocated // If too much memory is allocated
if (mCurrentOffset < mTotalSizeBytes / 2) { if (mCurrentOffset < mTotalSizeBytes / 2) {
@ -96,14 +97,14 @@ void SingleFrameAllocator::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
MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes); mBaseMemoryAllocator->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*>(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes)); mMemoryBufferStart = static_cast<char*>(mBaseMemoryAllocator->allocate(mTotalSizeBytes));
assert(mMemoryBufferStart != nullptr); assert(mMemoryBufferStart != nullptr);
mNbFramesTooMuchAllocated = 0; mNbFramesTooMuchAllocated = 0;
@ -117,13 +118,13 @@ void SingleFrameAllocator::reset() {
if (mNeedToAllocatedMore) { if (mNeedToAllocatedMore) {
// Release the memory allocated at the beginning // Release the memory allocated at the beginning
MemoryManager::getBaseAllocator().release(mMemoryBufferStart, mTotalSizeBytes); mBaseMemoryAllocator->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*>(MemoryManager::getBaseAllocator().allocate(mTotalSizeBytes)); mMemoryBufferStart = static_cast<char*>(mBaseMemoryAllocator->allocate(mTotalSizeBytes));
assert(mMemoryBufferStart != nullptr); assert(mMemoryBufferStart != nullptr);
mNeedToAllocatedMore = false; mNeedToAllocatedMore = false;

View File

@ -33,12 +33,12 @@
/// ReactPhysics3D namespace /// ReactPhysics3D namespace
namespace reactphysics3d { namespace reactphysics3d {
// Class SingleFrameAllocator // Class DefaultSingleFrameAllocator
/** /**
* 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 SingleFrameAllocator : public MemoryAllocator { class DefaultSingleFrameAllocator : public SingleFrameAllocator {
private : private :
@ -49,9 +49,11 @@ class SingleFrameAllocator : public MemoryAllocator {
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
size_t INIT_SINGLE_FRAME_ALLOCATOR_NB_BYTES = 1048576; // 1Mb static const 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;
@ -74,13 +76,13 @@ class SingleFrameAllocator : public MemoryAllocator {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
/// Constructor /// Constructor
SingleFrameAllocator(); DefaultSingleFrameAllocator();
/// Destructor /// Destructor
virtual ~SingleFrameAllocator() override; virtual ~DefaultSingleFrameAllocator() override;
/// Assignment operator /// Assignment operator
SingleFrameAllocator& operator=(SingleFrameAllocator& allocator) = default; DefaultSingleFrameAllocator& operator=(DefaultSingleFrameAllocator& 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;
@ -89,7 +91,7 @@ class SingleFrameAllocator : public MemoryAllocator {
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(); virtual void reset() override;
}; };
} }

View File

@ -57,6 +57,33 @@ 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

View File

@ -30,5 +30,9 @@ 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;

View File

@ -28,8 +28,9 @@
// Libraries // Libraries
#include "memory/DefaultAllocator.h" #include "memory/DefaultAllocator.h"
#include "memory/PoolAllocator.h" #include "memory/DefaultPoolAllocator.h"
#include "memory/SingleFrameAllocator.h" #include "memory/MemoryAllocator.h"
#include "memory/DefaultSingleFrameAllocator.h"
/// Namespace ReactPhysics3D /// Namespace ReactPhysics3D
namespace reactphysics3d { namespace reactphysics3d {
@ -49,14 +50,20 @@ class MemoryManager {
/// Default malloc/free memory allocator /// Default malloc/free memory allocator
static DefaultAllocator mDefaultAllocator; static DefaultAllocator mDefaultAllocator;
/// Default single frame memory allocator
static DefaultSingleFrameAllocator mDefaultSingleFrameAllocator;
/// Default pool memory allocator
static DefaultPoolAllocator mDefaultPoolAllocator;
/// Pointer to the base memory allocator to use /// Pointer to the base memory allocator to use
static MemoryAllocator* mBaseAllocator; static MemoryAllocator* mBaseAllocator;
/// Memory pool allocator
PoolAllocator mPoolAllocator;
/// Single frame stack allocator /// Single frame stack allocator
SingleFrameAllocator mSingleFrameAllocator; static SingleFrameAllocator* mSingleFrameAllocator;
/// Memory pool allocator
static MemoryAllocator* mPoolAllocator;
public: public:
@ -80,7 +87,7 @@ 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
PoolAllocator& getPoolAllocator(); MemoryAllocator& getPoolAllocator();
/// Return the single frame stack allocator /// Return the single frame stack allocator
SingleFrameAllocator& getSingleFrameAllocator(); SingleFrameAllocator& getSingleFrameAllocator();
@ -91,6 +98,12 @@ class MemoryManager {
/// 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
static void setSingleFrameAllocator(SingleFrameAllocator* singleFrameAllocator);
/// Set the pool memory allocator
static void setPoolAllocator(MemoryAllocator* poolAllocator);
/// Reset the single frame allocator /// Reset the single frame allocator
void resetFrameAllocator(); void resetFrameAllocator();
}; };
@ -100,8 +113,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;
@ -112,19 +125,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 PoolAllocator& MemoryManager::getPoolAllocator() { inline MemoryAllocator& 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
@ -137,9 +150,19 @@ 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();
} }
} }