Fix issue and make sure we use the correct memory allocators

This commit is contained in:
Daniel Chappuis 2020-01-15 20:18:11 +01:00
parent 77940a43f7
commit eb270af309
14 changed files with 50 additions and 91 deletions

View File

@ -35,7 +35,6 @@ using namespace reactphysics3d;
// Constructor // Constructor
ConcaveMeshShape::ConcaveMeshShape(TriangleMesh* triangleMesh, MemoryAllocator& allocator, const Vector3& scaling) ConcaveMeshShape::ConcaveMeshShape(TriangleMesh* triangleMesh, MemoryAllocator& allocator, const Vector3& scaling)
// TODO : Do not use the default base allocator here
: ConcaveShape(CollisionShapeName::TRIANGLE_MESH), mDynamicAABBTree(allocator), : ConcaveShape(CollisionShapeName::TRIANGLE_MESH), mDynamicAABBTree(allocator),
mScaling(scaling) { mScaling(scaling) {
mTriangleMesh = triangleMesh; mTriangleMesh = triangleMesh;

View File

@ -36,15 +36,15 @@ using namespace std;
uint CollisionWorld::mNbWorlds = 0; uint CollisionWorld::mNbWorlds = 0;
// Constructor // Constructor
CollisionWorld::CollisionWorld(const WorldSettings& worldSettings, Logger* logger, Profiler* profiler, MemoryAllocator* baseMemoryAllocator) CollisionWorld::CollisionWorld(MemoryManager& memoryManager, const WorldSettings& worldSettings, Logger* logger, Profiler* profiler)
: mMemoryManager(baseMemoryAllocator), mConfig(worldSettings), mEntityManager(mMemoryManager.getPoolAllocator()), : mMemoryManager(memoryManager), mConfig(worldSettings), mEntityManager(mMemoryManager.getHeapAllocator()),
mCollisionBodyComponents(mMemoryManager.getBaseAllocator()), mRigidBodyComponents(mMemoryManager.getBaseAllocator()), mCollisionBodyComponents(mMemoryManager.getHeapAllocator()), mRigidBodyComponents(mMemoryManager.getHeapAllocator()),
mTransformComponents(mMemoryManager.getBaseAllocator()), mProxyShapesComponents(mMemoryManager.getBaseAllocator()), mTransformComponents(mMemoryManager.getHeapAllocator()), mProxyShapesComponents(mMemoryManager.getHeapAllocator()),
mJointsComponents(mMemoryManager.getBaseAllocator()), mBallAndSocketJointsComponents(mMemoryManager.getBaseAllocator()), mJointsComponents(mMemoryManager.getHeapAllocator()), mBallAndSocketJointsComponents(mMemoryManager.getHeapAllocator()),
mFixedJointsComponents(mMemoryManager.getBaseAllocator()), mHingeJointsComponents(mMemoryManager.getBaseAllocator()), mFixedJointsComponents(mMemoryManager.getHeapAllocator()), mHingeJointsComponents(mMemoryManager.getHeapAllocator()),
mSliderJointsComponents(mMemoryManager.getBaseAllocator()), mSliderJointsComponents(mMemoryManager.getHeapAllocator()),
mCollisionDetection(this, mProxyShapesComponents, mTransformComponents, mCollisionBodyComponents, mRigidBodyComponents, mMemoryManager), mCollisionDetection(this, mProxyShapesComponents, mTransformComponents, mCollisionBodyComponents, mRigidBodyComponents, mMemoryManager),
mBodies(mMemoryManager.getPoolAllocator()), mEventListener(nullptr), mBodies(mMemoryManager.getHeapAllocator()), mEventListener(nullptr),
mName(worldSettings.worldName), mIsProfilerCreatedByUser(profiler != nullptr), mName(worldSettings.worldName), mIsProfilerCreatedByUser(profiler != nullptr),
mIsLoggerCreatedByUser(logger != nullptr) { mIsLoggerCreatedByUser(logger != nullptr) {

View File

@ -73,7 +73,7 @@ class CollisionWorld {
// -------------------- Attributes -------------------- // // -------------------- Attributes -------------------- //
/// Memory manager /// Memory manager
MemoryManager mMemoryManager; MemoryManager& mMemoryManager;
/// Configuration of the physics world /// Configuration of the physics world
WorldSettings mConfig; WorldSettings mConfig;
@ -144,8 +144,8 @@ class CollisionWorld {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
/// Constructor /// Constructor
CollisionWorld(const WorldSettings& worldSettings = WorldSettings(), Logger* logger = nullptr, CollisionWorld(MemoryManager& memoryManager, const WorldSettings& worldSettings = WorldSettings(), Logger* logger = nullptr,
Profiler* profiler = nullptr, MemoryAllocator* baseMemoryAllocator = nullptr); Profiler* profiler = nullptr);
/// Notify the world if a body is disabled (slepping or inactive) or not /// Notify the world if a body is disabled (slepping or inactive) or not
void setBodyDisabled(Entity entity, bool isDisabled); void setBodyDisabled(Entity entity, bool isDisabled);

View File

@ -47,8 +47,8 @@ using namespace std;
* @param logger Pointer to the logger * @param logger Pointer to the logger
* @param profiler Pointer to the profiler * @param profiler Pointer to the profiler
*/ */
DynamicsWorld::DynamicsWorld(const Vector3& gravity, const WorldSettings& worldSettings, Logger* logger, Profiler* profiler) DynamicsWorld::DynamicsWorld(const Vector3& gravity, MemoryManager& memoryManager, const WorldSettings& worldSettings, Logger* logger, Profiler* profiler)
: CollisionWorld(worldSettings, logger, profiler), : CollisionWorld(memoryManager, worldSettings, logger, profiler),
mIslands(mMemoryManager.getSingleFrameAllocator()), mIslands(mMemoryManager.getSingleFrameAllocator()),
mContactSolverSystem(mMemoryManager, *this, mIslands, mCollisionBodyComponents, mRigidBodyComponents, mContactSolverSystem(mMemoryManager, *this, mIslands, mCollisionBodyComponents, mRigidBodyComponents,
mProxyShapesComponents, mConfig), mProxyShapesComponents, mConfig),

View File

@ -101,7 +101,7 @@ class DynamicsWorld : public CollisionWorld {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
/// Constructor /// Constructor
DynamicsWorld(const Vector3& mGravity, const WorldSettings& worldSettings = WorldSettings(), DynamicsWorld(const Vector3& mGravity, MemoryManager& memoryManager, const WorldSettings& worldSettings = WorldSettings(),
Logger* logger = nullptr, Profiler* profiler = nullptr); Logger* logger = nullptr, Profiler* profiler = nullptr);
/// Solve the contacts and constraints /// Solve the contacts and constraints

View File

@ -30,13 +30,12 @@ using namespace reactphysics3d;
/// Constructor /// Constructor
PhysicsCommon::PhysicsCommon(MemoryAllocator* baseMemoryAllocator) PhysicsCommon::PhysicsCommon(MemoryAllocator* baseMemoryAllocator)
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator : mMemoryManager(baseMemoryAllocator), mCollisionWorlds(mMemoryManager.getHeapAllocator()),
: mMemoryManager(baseMemoryAllocator), mCollisionWorlds(mMemoryManager.getPoolAllocator()), mDynamicsWorlds(mMemoryManager.getHeapAllocator()), mSphereShapes(mMemoryManager.getHeapAllocator()),
mDynamicsWorlds(mMemoryManager.getPoolAllocator()), mSphereShapes(mMemoryManager.getPoolAllocator()), mBoxShapes(mMemoryManager.getHeapAllocator()), mCapsuleShapes(mMemoryManager.getHeapAllocator()),
mBoxShapes(mMemoryManager.getPoolAllocator()), mCapsuleShapes(mMemoryManager.getPoolAllocator()), mConvexMeshShapes(mMemoryManager.getHeapAllocator()), mConcaveMeshShapes(mMemoryManager.getHeapAllocator()),
mConvexMeshShapes(mMemoryManager.getPoolAllocator()), mConcaveMeshShapes(mMemoryManager.getPoolAllocator()), mHeightFieldShapes(mMemoryManager.getHeapAllocator()), mPolyhedronMeshes(mMemoryManager.getHeapAllocator()),
mHeightFieldShapes(mMemoryManager.getPoolAllocator()), mPolyhedronMeshes(mMemoryManager.getPoolAllocator()), mTriangleMeshes(mMemoryManager.getHeapAllocator()) {
mTriangleMeshes(mMemoryManager.getPoolAllocator()) {
} }
@ -104,8 +103,7 @@ void PhysicsCommon::release() {
// Create and return an instance of CollisionWorld // Create and return an instance of CollisionWorld
CollisionWorld* PhysicsCommon::createCollisionWorld(const WorldSettings& worldSettings, Logger* logger, Profiler* profiler) { CollisionWorld* PhysicsCommon::createCollisionWorld(const WorldSettings& worldSettings, Logger* logger, Profiler* profiler) {
// TODO : Allocate in heap allocator here instead of pool CollisionWorld* world = new(mMemoryManager.allocate(MemoryManager::AllocationType::Heap, sizeof(CollisionWorld))) CollisionWorld(mMemoryManager, worldSettings, logger, profiler);
CollisionWorld* world = new(mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(CollisionWorld))) CollisionWorld(worldSettings, logger, profiler);
mCollisionWorlds.add(world); mCollisionWorlds.add(world);
return world; return world;
@ -118,8 +116,7 @@ void PhysicsCommon::destroyCollisionWorld(CollisionWorld* world) {
world->~CollisionWorld(); world->~CollisionWorld();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator mMemoryManager.release(MemoryManager::AllocationType::Heap, world, sizeof(CollisionWorld));
mMemoryManager.release(MemoryManager::AllocationType::Pool, world, sizeof(CollisionWorld));
mCollisionWorlds.remove(world); mCollisionWorlds.remove(world);
} }
@ -128,8 +125,7 @@ void PhysicsCommon::destroyCollisionWorld(CollisionWorld* world) {
DynamicsWorld* PhysicsCommon::createDynamicsWorld(const Vector3& gravity, const WorldSettings& worldSettings, DynamicsWorld* PhysicsCommon::createDynamicsWorld(const Vector3& gravity, const WorldSettings& worldSettings,
Logger* logger, Profiler* profiler) { Logger* logger, Profiler* profiler) {
// TODO : Allocate in heap allocator here instead of pool DynamicsWorld* world = new(mMemoryManager.allocate(MemoryManager::AllocationType::Heap, sizeof(DynamicsWorld))) DynamicsWorld(gravity, mMemoryManager, worldSettings, logger, profiler);
DynamicsWorld* world = new(mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(DynamicsWorld))) DynamicsWorld(gravity, worldSettings, logger, profiler);
mDynamicsWorlds.add(world); mDynamicsWorlds.add(world);
@ -143,8 +139,7 @@ DynamicsWorld* PhysicsCommon::destroyDynamicsWorld(DynamicsWorld* world) {
world->~DynamicsWorld(); world->~DynamicsWorld();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator mMemoryManager.release(MemoryManager::AllocationType::Heap, world, sizeof(DynamicsWorld));
mMemoryManager.release(MemoryManager::AllocationType::Pool, world, sizeof(DynamicsWorld));
mDynamicsWorlds.remove(world); mDynamicsWorlds.remove(world);
} }
@ -165,7 +160,6 @@ void PhysicsCommon::destroySphereShape(SphereShape* sphereShape) {
sphereShape->~SphereShape(); sphereShape->~SphereShape();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, sphereShape, sizeof(SphereShape)); mMemoryManager.release(MemoryManager::AllocationType::Pool, sphereShape, sizeof(SphereShape));
mSphereShapes.remove(sphereShape); mSphereShapes.remove(sphereShape);
@ -174,8 +168,7 @@ void PhysicsCommon::destroySphereShape(SphereShape* sphereShape) {
// Create and return a box collision shape // Create and return a box collision shape
BoxShape* PhysicsCommon::createBoxShape(const Vector3& extent) { BoxShape* PhysicsCommon::createBoxShape(const Vector3& extent) {
// TODO : Pass heap allocator here in parameter BoxShape* shape = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(BoxShape))) BoxShape(extent, mMemoryManager.getHeapAllocator());
BoxShape* shape = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(BoxShape))) BoxShape(extent, mMemoryManager.getPoolAllocator());
mBoxShapes.add(shape); mBoxShapes.add(shape);
@ -189,7 +182,6 @@ void PhysicsCommon::destroyBoxShape(BoxShape* boxShape) {
boxShape->~BoxShape(); boxShape->~BoxShape();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, boxShape, sizeof(BoxShape)); mMemoryManager.release(MemoryManager::AllocationType::Pool, boxShape, sizeof(BoxShape));
mBoxShapes.remove(boxShape); mBoxShapes.remove(boxShape);
@ -212,7 +204,6 @@ void PhysicsCommon::destroyCapsuleShape(CapsuleShape* capsuleShape) {
capsuleShape->~CapsuleShape(); capsuleShape->~CapsuleShape();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, capsuleShape, sizeof(CapsuleShape)); mMemoryManager.release(MemoryManager::AllocationType::Pool, capsuleShape, sizeof(CapsuleShape));
mCapsuleShapes.remove(capsuleShape); mCapsuleShapes.remove(capsuleShape);
@ -235,7 +226,6 @@ void PhysicsCommon::destroyConvexMeshShape(ConvexMeshShape* convexMeshShape) {
convexMeshShape->~ConvexMeshShape(); convexMeshShape->~ConvexMeshShape();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, convexMeshShape, sizeof(ConvexMeshShape)); mMemoryManager.release(MemoryManager::AllocationType::Pool, convexMeshShape, sizeof(ConvexMeshShape));
mConvexMeshShapes.remove(convexMeshShape); mConvexMeshShapes.remove(convexMeshShape);
@ -261,7 +251,6 @@ void PhysicsCommon::destroyHeightFieldShape(HeightFieldShape* heightFieldShape)
heightFieldShape->~HeightFieldShape(); heightFieldShape->~HeightFieldShape();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, heightFieldShape, sizeof(HeightFieldShape)); mMemoryManager.release(MemoryManager::AllocationType::Pool, heightFieldShape, sizeof(HeightFieldShape));
mHeightFieldShapes.remove(heightFieldShape); mHeightFieldShapes.remove(heightFieldShape);
@ -270,8 +259,7 @@ void PhysicsCommon::destroyHeightFieldShape(HeightFieldShape* heightFieldShape)
// Create and return a concave mesh shape // Create and return a concave mesh shape
ConcaveMeshShape* PhysicsCommon::createConcaveMeshShape(TriangleMesh* triangleMesh, const Vector3& scaling) { ConcaveMeshShape* PhysicsCommon::createConcaveMeshShape(TriangleMesh* triangleMesh, const Vector3& scaling) {
// TODO : Pass heap allocator here in parameter ConcaveMeshShape* shape = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(ConcaveMeshShape))) ConcaveMeshShape(triangleMesh, mMemoryManager.getHeapAllocator(), scaling);
ConcaveMeshShape* shape = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(ConcaveMeshShape))) ConcaveMeshShape(triangleMesh, mMemoryManager.getPoolAllocator(), scaling);
mConcaveMeshShapes.add(shape); mConcaveMeshShapes.add(shape);
@ -285,7 +273,6 @@ void PhysicsCommon::destroyConcaveMeshShape(ConcaveMeshShape* concaveMeshShape)
concaveMeshShape->~ConcaveMeshShape(); concaveMeshShape->~ConcaveMeshShape();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, concaveMeshShape, sizeof(ConcaveMeshShape)); mMemoryManager.release(MemoryManager::AllocationType::Pool, concaveMeshShape, sizeof(ConcaveMeshShape));
mConcaveMeshShapes.remove(concaveMeshShape); mConcaveMeshShapes.remove(concaveMeshShape);
@ -294,8 +281,7 @@ void PhysicsCommon::destroyConcaveMeshShape(ConcaveMeshShape* concaveMeshShape)
// Create a polyhedron mesh // Create a polyhedron mesh
PolyhedronMesh* PhysicsCommon::createPolyhedronMesh(PolygonVertexArray* polygonVertexArray) { PolyhedronMesh* PhysicsCommon::createPolyhedronMesh(PolygonVertexArray* polygonVertexArray) {
// TODO : Pass heap allocator here in parameter PolyhedronMesh* mesh = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(PolyhedronMesh))) PolyhedronMesh(polygonVertexArray, mMemoryManager.getHeapAllocator());
PolyhedronMesh* mesh = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(PolyhedronMesh))) PolyhedronMesh(polygonVertexArray, mMemoryManager.getPoolAllocator());
mPolyhedronMeshes.add(mesh); mPolyhedronMeshes.add(mesh);
@ -309,7 +295,6 @@ void PhysicsCommon::destroyPolyhedronMesh(PolyhedronMesh* polyhedronMesh) {
polyhedronMesh->~PolyhedronMesh(); polyhedronMesh->~PolyhedronMesh();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, polyhedronMesh, sizeof(PolyhedronMesh)); mMemoryManager.release(MemoryManager::AllocationType::Pool, polyhedronMesh, sizeof(PolyhedronMesh));
mPolyhedronMeshes.remove(polyhedronMesh); mPolyhedronMeshes.remove(polyhedronMesh);
@ -318,8 +303,7 @@ void PhysicsCommon::destroyPolyhedronMesh(PolyhedronMesh* polyhedronMesh) {
// Create a triangle mesh // Create a triangle mesh
TriangleMesh* PhysicsCommon::createTriangleMesh() { TriangleMesh* PhysicsCommon::createTriangleMesh() {
// TODO : Pass heap allocator here in parameter TriangleMesh* mesh = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(TriangleMesh))) TriangleMesh(mMemoryManager.getHeapAllocator());
TriangleMesh* mesh = new (mMemoryManager.allocate(MemoryManager::AllocationType::Pool, sizeof(TriangleMesh))) TriangleMesh(mMemoryManager.getPoolAllocator());
mTriangleMeshes.add(mesh); mTriangleMeshes.add(mesh);
@ -333,7 +317,6 @@ void PhysicsCommon::destroyTriangleMesh(TriangleMesh* triangleMesh) {
triangleMesh->~TriangleMesh(); triangleMesh->~TriangleMesh();
// Release allocated memory // Release allocated memory
// TODO : Use Heap allocator for internal Sets<> here instead of Pool allocator
mMemoryManager.release(MemoryManager::AllocationType::Pool, triangleMesh, sizeof(TriangleMesh)); mMemoryManager.release(MemoryManager::AllocationType::Pool, triangleMesh, sizeof(TriangleMesh));
mTriangleMeshes.remove(triangleMesh); mTriangleMeshes.remove(triangleMesh);

View File

@ -29,6 +29,7 @@
// Libraries // Libraries
#include "memory/MemoryAllocator.h" #include "memory/MemoryAllocator.h"
#include <cstdlib> #include <cstdlib>
#include <iostream>
/// ReactPhysics3D namespace /// ReactPhysics3D namespace
namespace reactphysics3d { namespace reactphysics3d {
@ -51,9 +52,6 @@ class DefaultAllocator : public MemoryAllocator {
/// allocated memory. /// allocated memory.
virtual void* allocate(size_t size) override { virtual void* allocate(size_t size) override {
// TODO : Make sure to reduce the calls to default allocator is not called within a frame. For instance by a call
// to a pool allocator with size too large
return malloc(size); return malloc(size);
} }

View File

@ -32,23 +32,19 @@
using namespace reactphysics3d; using namespace reactphysics3d;
size_t HeapAllocator::INIT_ALLOCATED_SIZE = 1024; size_t HeapAllocator::INIT_ALLOCATED_SIZE = 5 * 1048576; // 5 Mb
// Constructor // Constructor
HeapAllocator::HeapAllocator(MemoryAllocator& baseAllocator) HeapAllocator::HeapAllocator(MemoryAllocator& baseAllocator, size_t initAllocatedMemory)
: mBaseAllocator(baseAllocator), mAllocatedMemory(0), mMemoryUnits(nullptr), mCachedFreeUnit(nullptr), : mBaseAllocator(baseAllocator), mAllocatedMemory(0), mMemoryUnits(nullptr), mCachedFreeUnit(nullptr),
mNbTimesAllocateMethodCalled(0), mDebug(baseAllocator) { mNbTimesAllocateMethodCalled(0) {
reserve(INIT_ALLOCATED_SIZE); reserve(initAllocatedMemory == 0 ? INIT_ALLOCATED_SIZE : initAllocatedMemory);
} }
// Destructor // Destructor
HeapAllocator::~HeapAllocator() { HeapAllocator::~HeapAllocator() {
for (auto it = mDebug.begin(); it != mDebug.end(); ++it) {
std::cout << "Size: " << (*it).first << " -> " << (*it).second << std::endl;
}
#ifndef NDEBUG #ifndef NDEBUG
// Check that the allocate() and release() methods have been called the same // Check that the allocate() and release() methods have been called the same
// number of times to avoid memory leaks. // number of times to avoid memory leaks.
@ -113,13 +109,6 @@ void* HeapAllocator::allocate(size_t size) {
// We cannot allocate zero bytes // We cannot allocate zero bytes
if (size == 0) return nullptr; if (size == 0) return nullptr;
if (mDebug.containsKey(size)) {
mDebug[size]++;
}
else {
mDebug.add(Pair<size_t, int>(size, 1));
}
#ifndef NDEBUG #ifndef NDEBUG
mNbTimesAllocateMethodCalled++; mNbTimesAllocateMethodCalled++;
#endif #endif
@ -189,11 +178,6 @@ void HeapAllocator::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;
mDebug[size]--;
if (mDebug[size] == 0) {
mDebug.remove(size);
}
#ifndef NDEBUG #ifndef NDEBUG
mNbTimesAllocateMethodCalled--; mNbTimesAllocateMethodCalled--;
#endif #endif

View File

@ -100,9 +100,6 @@ class HeapAllocator : public MemoryAllocator {
/// Pointer to a cached free memory unit /// Pointer to a cached free memory unit
MemoryUnitHeader* mCachedFreeUnit; MemoryUnitHeader* mCachedFreeUnit;
// TODO : REMOVE THIS
Map<size_t, int> mDebug;
#ifndef NDEBUG #ifndef NDEBUG
/// This variable is incremented by one when the allocate() method has been /// This variable is incremented by one when the allocate() method has been
/// called and decreased by one when the release() method has been called. /// called and decreased by one when the release() method has been called.
@ -125,7 +122,7 @@ class HeapAllocator : public MemoryAllocator {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
/// Constructor /// Constructor
HeapAllocator(MemoryAllocator& baseAllocator); HeapAllocator(MemoryAllocator& baseAllocator, size_t initAllocatedMemory = 0);
/// Destructor /// Destructor
virtual ~HeapAllocator() override; virtual ~HeapAllocator() override;

View File

@ -29,9 +29,9 @@
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor // Constructor
MemoryManager::MemoryManager(MemoryAllocator* baseAllocator) : MemoryManager::MemoryManager(MemoryAllocator* baseAllocator, size_t initAllocatedMemory) :
mBaseAllocator(baseAllocator == nullptr ? &mDefaultAllocator : baseAllocator), mBaseAllocator(baseAllocator == nullptr ? &mDefaultAllocator : baseAllocator),
mHeapAllocator(*mBaseAllocator), mHeapAllocator(*mBaseAllocator, initAllocatedMemory),
mPoolAllocator(mHeapAllocator), mPoolAllocator(mHeapAllocator),
mSingleFrameAllocator(mHeapAllocator) { mSingleFrameAllocator(mHeapAllocator) {

View File

@ -72,11 +72,12 @@ class MemoryManager {
enum class AllocationType { enum class AllocationType {
Base, // Base memory allocator Base, // Base memory allocator
Pool, // Memory pool allocator Pool, // Memory pool allocator
Heap, // Memory pool allocator
Frame, // Single frame memory allocator Frame, // Single frame memory allocator
}; };
/// Constructor /// Constructor
MemoryManager(MemoryAllocator* baseAllocator); MemoryManager(MemoryAllocator* baseAllocator, size_t initAllocatedMemory = 0);
/// Destructor /// Destructor
~MemoryManager() = default; ~MemoryManager() = default;
@ -93,8 +94,8 @@ class MemoryManager {
/// Return the single frame stack allocator /// Return the single frame stack allocator
SingleFrameAllocator& getSingleFrameAllocator(); SingleFrameAllocator& getSingleFrameAllocator();
/// Return the base memory allocator /// Return the heap allocator
MemoryAllocator& getBaseAllocator(); HeapAllocator& getHeapAllocator();
/// Reset the single frame allocator /// Reset the single frame allocator
void resetFrameAllocator(); void resetFrameAllocator();
@ -106,6 +107,7 @@ 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::Heap: return mHeapAllocator.allocate(size);
case AllocationType::Frame: return mSingleFrameAllocator.allocate(size); case AllocationType::Frame: return mSingleFrameAllocator.allocate(size);
} }
@ -118,6 +120,7 @@ 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::Heap: mHeapAllocator.release(pointer, size); break;
case AllocationType::Frame: mSingleFrameAllocator.release(pointer, size); break; case AllocationType::Frame: mSingleFrameAllocator.release(pointer, size); break;
} }
} }
@ -132,9 +135,9 @@ inline SingleFrameAllocator& MemoryManager::getSingleFrameAllocator() {
return mSingleFrameAllocator; return mSingleFrameAllocator;
} }
// Return the base memory allocator // Return the heap allocator
inline MemoryAllocator& MemoryManager::getBaseAllocator() { inline HeapAllocator& MemoryManager::getHeapAllocator() {
return *mBaseAllocator; return mHeapAllocator;
} }
// Reset the single frame allocator // Reset the single frame allocator

View File

@ -61,7 +61,6 @@ CollisionDetectionSystem::CollisionDetectionSystem(CollisionWorld* world, ProxyS
mBroadPhaseSystem(*this, mProxyShapesComponents, transformComponents, rigidBodyComponents), mBroadPhaseSystem(*this, mProxyShapesComponents, transformComponents, rigidBodyComponents),
mMapBroadPhaseIdToProxyShapeEntity(memoryManager.getPoolAllocator()), mMapBroadPhaseIdToProxyShapeEntity(memoryManager.getPoolAllocator()),
mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator(), mOverlappingPairs), mPotentialContactPoints(mMemoryManager.getSingleFrameAllocator()), mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator(), mOverlappingPairs), mPotentialContactPoints(mMemoryManager.getSingleFrameAllocator()),
// TODO : We should probably use single frame allocator for mPotentialContactPoints, mPotentialContactManifolds, mMapPairIdToOverlappingPairContacts
mPotentialContactManifolds(mMemoryManager.getSingleFrameAllocator()), mContactPairs1(mMemoryManager.getPoolAllocator()), mPotentialContactManifolds(mMemoryManager.getSingleFrameAllocator()), mContactPairs1(mMemoryManager.getPoolAllocator()),
mContactPairs2(mMemoryManager.getPoolAllocator()), mPreviousContactPairs(&mContactPairs1), mCurrentContactPairs(&mContactPairs2), mContactPairs2(mMemoryManager.getPoolAllocator()), mPreviousContactPairs(&mContactPairs1), mCurrentContactPairs(&mContactPairs2),
mMapPairIdToContactPairIndex1(mMemoryManager.getPoolAllocator()), mMapPairIdToContactPairIndex2(mMemoryManager.getPoolAllocator()), mMapPairIdToContactPairIndex1(mMemoryManager.getPoolAllocator()), mMapPairIdToContactPairIndex2(mMemoryManager.getPoolAllocator()),
@ -582,7 +581,7 @@ bool CollisionDetectionSystem::computeNarrowPhaseCollisionSnapshot(NarrowPhaseIn
RP3D_PROFILE("CollisionDetectionSystem::computeNarrowPhaseCollisionSnapshot()", mProfiler); RP3D_PROFILE("CollisionDetectionSystem::computeNarrowPhaseCollisionSnapshot()", mProfiler);
MemoryAllocator& allocator = mMemoryManager.getPoolAllocator(); MemoryAllocator& allocator = mMemoryManager.getHeapAllocator();
// Test the narrow-phase collision detection on the batches to be tested // Test the narrow-phase collision detection on the batches to be tested
bool collisionFound = testNarrowPhaseCollision(narrowPhaseInput, true, false, allocator); bool collisionFound = testNarrowPhaseCollision(narrowPhaseInput, true, false, allocator);
@ -928,8 +927,6 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
// Add the contact point to the list of potential contact points // Add the contact point to the list of potential contact points
const uint contactPointIndex = static_cast<uint>(potentialContactPoints.size()); const uint contactPointIndex = static_cast<uint>(potentialContactPoints.size());
// TODO : We should probably use single frame allocator here for potentialContactPoints
// If so, do not forget to call potentialContactPoints.clear(true) at the end of frame
potentialContactPoints.add(contactPoint); potentialContactPoints.add(contactPoint);
bool similarManifoldFound = false; bool similarManifoldFound = false;
@ -975,8 +972,6 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
if (!similarManifoldFound) { if (!similarManifoldFound) {
// Create a new contact manifold for the overlapping pair // Create a new contact manifold for the overlapping pair
// TODO : We should probably use single frame allocator here
// If so, do not forget to call potentialContactPoints.clear(true) at the end of frame
ContactManifoldInfo contactManifoldInfo(pairId, mMemoryManager.getPoolAllocator()); ContactManifoldInfo contactManifoldInfo(pairId, mMemoryManager.getPoolAllocator());
// Add the contact point to the manifold // Add the contact point to the manifold
@ -993,11 +988,10 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
assert(!mWorld->mCollisionBodyComponents.getIsEntityDisabled(body1Entity) || !mWorld->mCollisionBodyComponents.getIsEntityDisabled(body2Entity)); assert(!mWorld->mCollisionBodyComponents.getIsEntityDisabled(body1Entity) || !mWorld->mCollisionBodyComponents.getIsEntityDisabled(body2Entity));
// TODO : We should probably use a single frame allocator here
const uint newContactPairIndex = contactPairs->size(); const uint newContactPairIndex = contactPairs->size();
ContactPair overlappingPairContact(pairId, body1Entity, body2Entity, ContactPair overlappingPairContact(pairId, body1Entity, body2Entity,
proxyShape1Entity, proxyShape2Entity, proxyShape1Entity, proxyShape2Entity,
newContactPairIndex, mMemoryManager.getPoolAllocator()); newContactPairIndex, mMemoryManager.getHeapAllocator());
contactPairs->add(overlappingPairContact); contactPairs->add(overlappingPairContact);
pairContact = &((*contactPairs)[newContactPairIndex]); pairContact = &((*contactPairs)[newContactPairIndex]);
mapPairIdToContactPairIndex->add(Pair<uint64, uint>(pairId, newContactPairIndex)); mapPairIdToContactPairIndex->add(Pair<uint64, uint>(pairId, newContactPairIndex));

View File

@ -181,9 +181,9 @@ class TestPointInside : public Test {
mPhysicsCommon.destroyCapsuleShape(mCapsuleShape); mPhysicsCommon.destroyCapsuleShape(mCapsuleShape);
mPhysicsCommon.destroyConvexMeshShape(mConvexMeshShape); mPhysicsCommon.destroyConvexMeshShape(mConvexMeshShape);
mPhysicsCommon.destroyCollisionWorld(mWorld); mPhysicsCommon.destroyCollisionWorld(mWorld);
mPhysicsCommon.destroyPolyhedronMesh(mConvexMeshPolyhedronMesh);
delete[] mConvexMeshPolygonFaces; delete[] mConvexMeshPolygonFaces;
delete mConvexMeshPolygonVertexArray; delete mConvexMeshPolygonVertexArray;
delete mConvexMeshPolyhedronMesh;
} }
/// Run the tests /// Run the tests

View File

@ -279,6 +279,7 @@ class TestRaycast : public Test {
vertexType, TriangleVertexArray::IndexDataType::INDEX_INTEGER_TYPE); vertexType, TriangleVertexArray::IndexDataType::INDEX_INTEGER_TYPE);
// Add the triangle vertex array of the subpart to the triangle mesh // Add the triangle vertex array of the subpart to the triangle mesh
mConcaveTriangleMesh = mPhysicsCommon.createTriangleMesh();
mConcaveTriangleMesh->addSubpart(mConcaveMeshVertexArray); mConcaveTriangleMesh->addSubpart(mConcaveMeshVertexArray);
mConcaveMeshShape = mPhysicsCommon.createConcaveMeshShape(mConcaveTriangleMesh); mConcaveMeshShape = mPhysicsCommon.createConcaveMeshShape(mConcaveTriangleMesh);
mConcaveMeshProxyShape = mConcaveMeshBody->addCollisionShape(mConcaveMeshShape, mShapeTransform); mConcaveMeshProxyShape = mConcaveMeshBody->addCollisionShape(mConcaveMeshShape, mShapeTransform);
@ -314,7 +315,7 @@ class TestRaycast : public Test {
delete mConcaveMeshVertexArray; delete mConcaveMeshVertexArray;
delete mPolygonVertexArray; delete mPolygonVertexArray;
delete mPolyhedronMesh; mPhysicsCommon.destroyPolyhedronMesh(mPolyhedronMesh);
} }
/// Run the tests /// Run the tests