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 and height values of the HeightFieldShape
- Make possible for the user to use a custom single frame and pool memory allocator
### Fixed

View File

@ -148,8 +148,8 @@ SET (REACTPHYSICS3D_HEADERS
"src/mathematics/Vector3.h"
"src/mathematics/Ray.h"
"src/memory/MemoryAllocator.h"
"src/memory/PoolAllocator.h"
"src/memory/SingleFrameAllocator.h"
"src/memory/DefaultPoolAllocator.h"
"src/memory/DefaultSingleFrameAllocator.h"
"src/memory/DefaultAllocator.h"
"src/memory/MemoryManager.h"
"src/containers/Stack.h"
@ -226,8 +226,8 @@ SET (REACTPHYSICS3D_SOURCES
"src/mathematics/Transform.cpp"
"src/mathematics/Vector2.cpp"
"src/mathematics/Vector3.cpp"
"src/memory/PoolAllocator.cpp"
"src/memory/SingleFrameAllocator.cpp"
"src/memory/DefaultPoolAllocator.cpp"
"src/memory/DefaultSingleFrameAllocator.cpp"
"src/memory/MemoryManager.cpp"
"src/utils/Profiler.cpp"
"src/utils/Logger.cpp"

View File

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

View File

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

View File

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

View File

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

View File

@ -24,7 +24,7 @@
********************************************************************************/
// Libraries
#include "PoolAllocator.h"
#include "DefaultPoolAllocator.h"
#include "MemoryManager.h"
#include <cstdlib>
#include <cassert>
@ -32,12 +32,12 @@
using namespace reactphysics3d;
// Initialization of static variables
bool PoolAllocator::isMapSizeToHeadIndexInitialized = false;
size_t PoolAllocator::mUnitSizes[NB_HEAPS];
int PoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1];
bool DefaultPoolAllocator::isMapSizeToHeadIndexInitialized = false;
size_t DefaultPoolAllocator::mUnitSizes[NB_HEAPS];
int DefaultPoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1];
// Constructor
PoolAllocator::PoolAllocator() {
DefaultPoolAllocator::DefaultPoolAllocator() {
// Allocate some memory to manage the blocks
mNbAllocatedMemoryBlocks = 64;
@ -79,7 +79,7 @@ PoolAllocator::PoolAllocator() {
}
// Destructor
PoolAllocator::~PoolAllocator() {
DefaultPoolAllocator::~DefaultPoolAllocator() {
// Release the memory allocated for each block
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
// allocated memory.
void* PoolAllocator::allocate(size_t size) {
void* DefaultPoolAllocator::allocate(size_t size) {
// We cannot allocate zero bytes
if (size == 0) return nullptr;
@ -171,7 +171,7 @@ void* PoolAllocator::allocate(size_t size) {
}
// 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
if (size == 0) return;

View File

@ -33,14 +33,14 @@
/// ReactPhysics3D namespace
namespace reactphysics3d {
// Class PoolAllocator
// Class DefaultPoolAllocator
/**
* 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)
* efficiently. This implementation is inspired by the small block allocator
* described here : http://www.codeproject.com/useritems/Small_Block_Allocator.asp
*/
class PoolAllocator : public MemoryAllocator {
class DefaultPoolAllocator : public MemoryAllocator {
private :
@ -126,13 +126,13 @@ class PoolAllocator : public MemoryAllocator {
// -------------------- Methods -------------------- //
/// Constructor
PoolAllocator();
DefaultPoolAllocator();
/// Destructor
virtual ~PoolAllocator() override;
virtual ~DefaultPoolAllocator() override;
/// 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
/// allocated memory.

View File

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

View File

@ -33,12 +33,12 @@
/// ReactPhysics3D namespace
namespace reactphysics3d {
// Class SingleFrameAllocator
// Class DefaultSingleFrameAllocator
/**
* This class represent a memory allocator used to efficiently allocate
* memory on the heap that is used during a single frame.
*/
class SingleFrameAllocator : public MemoryAllocator {
class DefaultSingleFrameAllocator : public SingleFrameAllocator {
private :
@ -49,9 +49,11 @@ class SingleFrameAllocator : public MemoryAllocator {
static const int NB_FRAMES_UNTIL_SHRINK = 120;
/// 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 -------------------- //
/// Cached memory allocator used on construction
MemoryAllocator* mBaseMemoryAllocator;
/// Total size (in bytes) of memory of the allocator
size_t mTotalSizeBytes;
@ -74,13 +76,13 @@ class SingleFrameAllocator : public MemoryAllocator {
// -------------------- Methods -------------------- //
/// Constructor
SingleFrameAllocator();
DefaultSingleFrameAllocator();
/// Destructor
virtual ~SingleFrameAllocator() override;
virtual ~DefaultSingleFrameAllocator() override;
/// Assignment operator
SingleFrameAllocator& operator=(SingleFrameAllocator& allocator) = default;
DefaultSingleFrameAllocator& operator=(DefaultSingleFrameAllocator& allocator) = default;
/// Allocate memory of a given size (in bytes)
virtual void* allocate(size_t size) override;
@ -89,7 +91,7 @@ class SingleFrameAllocator : public MemoryAllocator {
virtual void release(void* pointer, size_t size) override;
/// 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;
};
/**
* 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

View File

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

View File

@ -28,8 +28,9 @@
// Libraries
#include "memory/DefaultAllocator.h"
#include "memory/PoolAllocator.h"
#include "memory/SingleFrameAllocator.h"
#include "memory/DefaultPoolAllocator.h"
#include "memory/MemoryAllocator.h"
#include "memory/DefaultSingleFrameAllocator.h"
/// Namespace ReactPhysics3D
namespace reactphysics3d {
@ -45,18 +46,24 @@ class MemoryAllocator;
class MemoryManager {
private:
/// Default malloc/free memory allocator
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
static MemoryAllocator* mBaseAllocator;
/// Memory pool allocator
PoolAllocator mPoolAllocator;
/// Single frame stack allocator
SingleFrameAllocator mSingleFrameAllocator;
static SingleFrameAllocator* mSingleFrameAllocator;
/// Memory pool allocator
static MemoryAllocator* mPoolAllocator;
public:
@ -80,17 +87,23 @@ class MemoryManager {
void release(AllocationType allocationType, void* pointer, size_t size);
/// Return the pool allocator
PoolAllocator& getPoolAllocator();
MemoryAllocator& getPoolAllocator();
/// Return the single frame stack allocator
SingleFrameAllocator& getSingleFrameAllocator();
/// Return the base memory allocator
static MemoryAllocator& getBaseAllocator();
/// Set the base memory allocator
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
void resetFrameAllocator();
};
@ -100,8 +113,8 @@ inline void* MemoryManager::allocate(AllocationType allocationType, size_t size)
switch (allocationType) {
case AllocationType::Base: return mBaseAllocator->allocate(size);
case AllocationType::Pool: return mPoolAllocator.allocate(size);
case AllocationType::Frame: return mSingleFrameAllocator.allocate(size);
case AllocationType::Pool: return mPoolAllocator->allocate(size);
case AllocationType::Frame: return mSingleFrameAllocator->allocate(size);
}
return nullptr;
@ -112,19 +125,19 @@ inline void MemoryManager::release(AllocationType allocationType, void* pointer,
switch (allocationType) {
case AllocationType::Base: mBaseAllocator->release(pointer, size); break;
case AllocationType::Pool: mPoolAllocator.release(pointer, size); break;
case AllocationType::Frame: mSingleFrameAllocator.release(pointer, size); break;
case AllocationType::Pool: mPoolAllocator->release(pointer, size); break;
case AllocationType::Frame: mSingleFrameAllocator->release(pointer, size); break;
}
}
// Return the pool allocator
inline PoolAllocator& MemoryManager::getPoolAllocator() {
return mPoolAllocator;
inline MemoryAllocator& MemoryManager::getPoolAllocator() {
return *mPoolAllocator;
}
// Return the single frame stack allocator
inline SingleFrameAllocator& MemoryManager::getSingleFrameAllocator() {
return mSingleFrameAllocator;
return *mSingleFrameAllocator;
}
// Return the base memory allocator
@ -137,9 +150,19 @@ inline void MemoryManager::setBaseAllocator(MemoryAllocator* 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
inline void MemoryManager::resetFrameAllocator() {
mSingleFrameAllocator.reset();
mSingleFrameAllocator->reset();
}
}