- keep track of all memory allocations (gNumAllignedAllocs/gNumAllignedFree)

All memory allocations in Bullet go through btAlignedAlloc/btAlignedFree
Fix in hinge constraint constructors, thanks Marcus Hennix!
This commit is contained in:
ejcoumans
2007-10-22 22:23:10 +00:00
parent 1b70c4e5c9
commit ec76f2e0a3
18 changed files with 210 additions and 113 deletions

View File

@@ -41,6 +41,9 @@ btCollisionShape* gShapePtr[maxNumObjects];//1 rigidbody has 1 shape (no re-use
#ifdef SHOW_NUM_DEEP_PENETRATIONS #ifdef SHOW_NUM_DEEP_PENETRATIONS
extern int gNumDeepPenetrationChecks; extern int gNumDeepPenetrationChecks;
extern int gNumGjkChecks; extern int gNumGjkChecks;
extern int gNumAlignedAllocs;
extern int gNumAlignedFree;
#endif // #endif //
@@ -885,6 +888,7 @@ void DemoApplication::renderme()
#ifdef SHOW_NUM_DEEP_PENETRATIONS #ifdef SHOW_NUM_DEEP_PENETRATIONS
glRasterPos3f(xOffset,yStart,0); glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"gNumDeepPenetrationChecks = %d",gNumDeepPenetrationChecks); sprintf(buf,"gNumDeepPenetrationChecks = %d",gNumDeepPenetrationChecks);
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
@@ -895,6 +899,22 @@ void DemoApplication::renderme()
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr; yStart += yIncr;
glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"gNumAlignedAllocs = %d",gNumAlignedAllocs);
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"gNumAlignedFree= %d",gNumAlignedFree);
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"# alloc-free = %d",gNumAlignedAllocs-gNumAlignedFree);
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
#endif //SHOW_NUM_DEEP_PENETRATIONS #endif //SHOW_NUM_DEEP_PENETRATIONS
resetPerspectiveProjection(); resetPerspectiveProjection();

View File

@@ -52,11 +52,12 @@ m_invalidPair(0)
} }
}; };
m_filterCallback = new btMultiSapOverlapFilterCallback(); void* mem = btAlignedAlloc(sizeof(btMultiSapOverlapFilterCallback),16);
m_filterCallback = new (mem)btMultiSapOverlapFilterCallback();
m_overlappingPairs->setOverlapFilterCallback(m_filterCallback); m_overlappingPairs->setOverlapFilterCallback(m_filterCallback);
mem = btAlignedAlloc(sizeof(btSimpleBroadphase),16);
m_simpleBroadphase = new btSimpleBroadphase(maxProxies,m_overlappingPairs); m_simpleBroadphase = new (mem) btSimpleBroadphase(maxProxies,m_overlappingPairs);
} }
btMultiSapBroadphase::~btMultiSapBroadphase() btMultiSapBroadphase::~btMultiSapBroadphase()
@@ -69,7 +70,8 @@ btMultiSapBroadphase::~btMultiSapBroadphase()
btBroadphaseProxy* btMultiSapBroadphase::createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr, short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher) btBroadphaseProxy* btMultiSapBroadphase::createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr, short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher)
{ {
btMultiSapProxy* proxy = new btMultiSapProxy(aabbMin, aabbMax,shapeType,userPtr, collisionFilterGroup,collisionFilterMask); void* mem = btAlignedAlloc(sizeof(btMultiSapProxy),16);
btMultiSapProxy* proxy = new (mem)btMultiSapProxy(aabbMin, aabbMax,shapeType,userPtr, collisionFilterGroup,collisionFilterMask);
m_multiSapProxies.push_back(proxy); m_multiSapProxies.push_back(proxy);
///we don't pass the userPtr but our multisap proxy. We need to patch this, before processing an actual collision ///we don't pass the userPtr but our multisap proxy. We need to patch this, before processing an actual collision
@@ -77,6 +79,7 @@ btBroadphaseProxy* btMultiSapBroadphase::createProxy( const btVector3& aabbMin,
btBroadphaseProxy* simpleProxy = m_simpleBroadphase->createProxy(aabbMin,aabbMax,shapeType,userPtr,collisionFilterGroup,collisionFilterMask, dispatcher); btBroadphaseProxy* simpleProxy = m_simpleBroadphase->createProxy(aabbMin,aabbMax,shapeType,userPtr,collisionFilterGroup,collisionFilterMask, dispatcher);
simpleProxy->m_multiSapParentProxy = proxy; simpleProxy->m_multiSapParentProxy = proxy;
mem = btAlignedAlloc(sizeof(btChildProxy),16);
btChildProxy* childProxyRef = new btChildProxy(); btChildProxy* childProxyRef = new btChildProxy();
childProxyRef->m_proxy = simpleProxy; childProxyRef->m_proxy = simpleProxy;
childProxyRef->m_childBroadphase = m_simpleBroadphase; childProxyRef->m_childBroadphase = m_simpleBroadphase;

View File

@@ -40,9 +40,7 @@ subject to the following restrictions:
btCollisionWorld::btCollisionWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache, btCollisionConfiguration* collisionConfiguration) btCollisionWorld::btCollisionWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache, btCollisionConfiguration* collisionConfiguration)
:m_dispatcher1(dispatcher), :m_dispatcher1(dispatcher),
m_broadphasePairCache(pairCache), m_broadphasePairCache(pairCache)
m_ownsDispatcher(false),
m_ownsBroadphasePairCache(false)
{ {
m_stackAlloc = collisionConfiguration->getStackAllocator(); m_stackAlloc = collisionConfiguration->getStackAllocator();
m_dispatchInfo.m_stackAllocator = m_stackAlloc; m_dispatchInfo.m_stackAllocator = m_stackAlloc;
@@ -69,10 +67,6 @@ btCollisionWorld::~btCollisionWorld()
} }
} }
if (m_ownsDispatcher)
delete m_dispatcher1;
if (m_ownsBroadphasePairCache)
delete m_broadphasePairCache;
} }

View File

@@ -92,9 +92,6 @@ protected:
btBroadphaseInterface* m_broadphasePairCache; btBroadphaseInterface* m_broadphasePairCache;
bool m_ownsDispatcher;
bool m_ownsBroadphasePairCache;
public: public:
//this constructor doesn't own the dispatcher and paircache/broadphase //this constructor doesn't own the dispatcher and paircache/broadphase

View File

@@ -48,26 +48,16 @@ subject to the following restrictions:
btConvexConvexAlgorithm::CreateFunc::CreateFunc()
{
m_ownsSolvers = true;
m_simplexSolver = new btVoronoiSimplexSolver();
m_pdSolver = new btGjkEpaPenetrationDepthSolver;
}
btConvexConvexAlgorithm::CreateFunc::CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver) btConvexConvexAlgorithm::CreateFunc::CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver)
{ {
m_ownsSolvers = false;
m_simplexSolver = simplexSolver; m_simplexSolver = simplexSolver;
m_pdSolver = pdSolver; m_pdSolver = pdSolver;
} }
btConvexConvexAlgorithm::CreateFunc::~CreateFunc() btConvexConvexAlgorithm::CreateFunc::~CreateFunc()
{ {
if (m_ownsSolvers){
delete m_simplexSolver;
delete m_pdSolver;
}
} }
btConvexConvexAlgorithm::btConvexConvexAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1,btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver) btConvexConvexAlgorithm::btConvexConvexAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1,btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver)

View File

@@ -59,10 +59,9 @@ public:
{ {
btConvexPenetrationDepthSolver* m_pdSolver; btConvexPenetrationDepthSolver* m_pdSolver;
btSimplexSolverInterface* m_simplexSolver; btSimplexSolverInterface* m_simplexSolver;
bool m_ownsSolvers;
CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver); CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver);
CreateFunc();
virtual ~CreateFunc(); virtual ~CreateFunc();
virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1) virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1)

View File

@@ -23,6 +23,10 @@ subject to the following restrictions:
#include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h" #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btSphereBoxCollisionAlgorithm.h" #include "BulletCollision/CollisionDispatch/btSphereBoxCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btSphereTriangleCollisionAlgorithm.h" #include "BulletCollision/CollisionDispatch/btSphereTriangleCollisionAlgorithm.h"
#include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
#include "LinearMath/btStackAlloc.h" #include "LinearMath/btStackAlloc.h"
#include "LinearMath/btPoolAllocator.h" #include "LinearMath/btPoolAllocator.h"
@@ -36,20 +40,36 @@ subject to the following restrictions:
btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btStackAlloc* stackAlloc,btPoolAllocator* persistentManifoldPool,btPoolAllocator* collisionAlgorithmPool) btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btStackAlloc* stackAlloc,btPoolAllocator* persistentManifoldPool,btPoolAllocator* collisionAlgorithmPool)
{ {
void* mem = btAlignedAlloc(sizeof(btVoronoiSimplexSolver),16);
m_simplexSolver = new (mem)btVoronoiSimplexSolver();
mem = btAlignedAlloc(sizeof(btGjkEpaPenetrationDepthSolver),16);
m_pdSolver = new (mem)btGjkEpaPenetrationDepthSolver;
//default CreationFunctions, filling the m_doubleDispatch table //default CreationFunctions, filling the m_doubleDispatch table
m_convexConvexCreateFunc = new btConvexConvexAlgorithm::CreateFunc; mem = btAlignedAlloc(sizeof(btConvexConvexAlgorithm::CreateFunc),16);
m_convexConcaveCreateFunc = new btConvexConcaveCollisionAlgorithm::CreateFunc; m_convexConvexCreateFunc = new(mem) btConvexConvexAlgorithm::CreateFunc(m_simplexSolver,m_pdSolver);
m_swappedConvexConcaveCreateFunc = new btConvexConcaveCollisionAlgorithm::SwappedCreateFunc; mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
m_compoundCreateFunc = new btCompoundCollisionAlgorithm::CreateFunc; m_convexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::CreateFunc;
m_swappedCompoundCreateFunc = new btCompoundCollisionAlgorithm::SwappedCreateFunc; mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
m_emptyCreateFunc = new btEmptyAlgorithm::CreateFunc; m_swappedConvexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::SwappedCreateFunc;
m_sphereSphereCF = new btSphereSphereCollisionAlgorithm::CreateFunc; mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::CreateFunc),16);
m_sphereBoxCF = new btSphereBoxCollisionAlgorithm::CreateFunc; m_compoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::CreateFunc;
m_boxSphereCF = new btSphereBoxCollisionAlgorithm::CreateFunc; mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::SwappedCreateFunc),16);
m_swappedCompoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::SwappedCreateFunc;
mem = btAlignedAlloc(sizeof(btEmptyAlgorithm::CreateFunc),16);
m_emptyCreateFunc = new(mem) btEmptyAlgorithm::CreateFunc;
mem = btAlignedAlloc(sizeof(btSphereSphereCollisionAlgorithm::CreateFunc),16);
m_sphereSphereCF = new(mem) btSphereSphereCollisionAlgorithm::CreateFunc;
mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
m_sphereBoxCF = new(mem) btSphereBoxCollisionAlgorithm::CreateFunc;
mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
m_boxSphereCF = new (mem)btSphereBoxCollisionAlgorithm::CreateFunc;
m_boxSphereCF->m_swapped = true; m_boxSphereCF->m_swapped = true;
mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
m_sphereTriangleCF = new btSphereTriangleCollisionAlgorithm::CreateFunc; m_sphereTriangleCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
m_triangleSphereCF = new btSphereTriangleCollisionAlgorithm::CreateFunc; mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
m_triangleSphereCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
m_triangleSphereCF->m_swapped = true; m_triangleSphereCF->m_swapped = true;
@@ -115,17 +135,20 @@ btDefaultCollisionConfiguration::~btDefaultCollisionConfiguration()
btAlignedFree(m_persistentManifoldPool); btAlignedFree(m_persistentManifoldPool);
} }
delete m_convexConvexCreateFunc; btAlignedFree( m_convexConvexCreateFunc);
delete m_convexConcaveCreateFunc; btAlignedFree( m_convexConcaveCreateFunc);
delete m_swappedConvexConcaveCreateFunc; btAlignedFree( m_swappedConvexConcaveCreateFunc);
delete m_compoundCreateFunc; btAlignedFree( m_compoundCreateFunc);
delete m_swappedCompoundCreateFunc; btAlignedFree( m_swappedCompoundCreateFunc);
delete m_emptyCreateFunc; btAlignedFree( m_emptyCreateFunc);
delete m_sphereSphereCF; btAlignedFree( m_sphereSphereCF);
delete m_sphereBoxCF; btAlignedFree( m_sphereBoxCF);
delete m_boxSphereCF; btAlignedFree( m_boxSphereCF);
delete m_sphereTriangleCF; btAlignedFree( m_sphereTriangleCF);
delete m_triangleSphereCF; btAlignedFree( m_triangleSphereCF);
btAlignedFree(m_simplexSolver);
btAlignedFree(m_pdSolver);
} }

View File

@@ -17,6 +17,9 @@ subject to the following restrictions:
#define BT_DEFAULT_COLLISION_CONFIGURATION #define BT_DEFAULT_COLLISION_CONFIGURATION
#include "btCollisionConfiguration.h" #include "btCollisionConfiguration.h"
class btVoronoiSimplexSolver;
class btGjkEpaPenetrationDepthSolver;
///btCollisionConfiguration allows to configure Bullet collision detection ///btCollisionConfiguration allows to configure Bullet collision detection
///stack allocator, pool memory allocators ///stack allocator, pool memory allocators
@@ -35,6 +38,9 @@ class btDefaultCollisionConfiguration : public btCollisionConfiguration
btPoolAllocator* m_collisionAlgorithmPool; btPoolAllocator* m_collisionAlgorithmPool;
bool m_ownsCollisionAlgorithmPool; bool m_ownsCollisionAlgorithmPool;
//default simplex/penetration depth solvers
btVoronoiSimplexSolver* m_simplexSolver;
btGjkEpaPenetrationDepthSolver* m_pdSolver;
//default CreationFunctions, filling the m_doubleDispatch table //default CreationFunctions, filling the m_doubleDispatch table
btCollisionAlgorithmCreateFunc* m_convexConvexCreateFunc; btCollisionAlgorithmCreateFunc* m_convexConvexCreateFunc;

View File

@@ -241,8 +241,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
#define SPLIT_ISLANDS 1 #define SPLIT_ISLANDS 1
#ifdef SPLIT_ISLANDS #ifdef SPLIT_ISLANDS
btAlignedObjectArray<btPersistentManifold*> islandmanifold;
islandmanifold.reserve(maxNumManifolds);
#endif //SPLIT_ISLANDS #endif //SPLIT_ISLANDS
@@ -270,7 +269,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
#ifdef SPLIT_ISLANDS #ifdef SPLIT_ISLANDS
// //filtering for response // //filtering for response
if (dispatcher->needsResponse(colObj0,colObj1)) if (dispatcher->needsResponse(colObj0,colObj1))
islandmanifold.push_back(manifold); m_islandmanifold.push_back(manifold);
#endif //SPLIT_ISLANDS #endif //SPLIT_ISLANDS
} }
} }
@@ -284,10 +283,10 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
// Sort the vector using predicate and std::sort // Sort the vector using predicate and std::sort
//std::sort(islandmanifold.begin(), islandmanifold.end(), btPersistentManifoldSortPredicate); //std::sort(islandmanifold.begin(), islandmanifold.end(), btPersistentManifoldSortPredicate);
int numManifolds = int (islandmanifold.size()); int numManifolds = int (m_islandmanifold.size());
//we should do radix sort, it it much faster (O(n) instead of O (n log2(n)) //we should do radix sort, it it much faster (O(n) instead of O (n log2(n))
islandmanifold.heapSort(btPersistentManifoldSortPredicate()); m_islandmanifold.heapSort(btPersistentManifoldSortPredicate());
//now process all active islands (sets of manifolds for now) //now process all active islands (sets of manifolds for now)
@@ -298,7 +297,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
END_PROFILE("islandUnionFindAndHeapSort"); END_PROFILE("islandUnionFindAndHeapSort");
btAlignedObjectArray<btCollisionObject*> islandBodies;
// printf("Start Islands\n"); // printf("Start Islands\n");
@@ -314,7 +313,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
{ {
int i = getUnionFind().getElement(endIslandIndex).m_sz; int i = getUnionFind().getElement(endIslandIndex).m_sz;
btCollisionObject* colObj0 = collisionObjects[i]; btCollisionObject* colObj0 = collisionObjects[i];
islandBodies.push_back(colObj0); m_islandBodies.push_back(colObj0);
if (!colObj0->isActive()) if (!colObj0->isActive())
islandSleeping = true; islandSleeping = true;
} }
@@ -326,12 +325,12 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
if (startManifoldIndex<numManifolds) if (startManifoldIndex<numManifolds)
{ {
int curIslandId = getIslandId(islandmanifold[startManifoldIndex]); int curIslandId = getIslandId(m_islandmanifold[startManifoldIndex]);
if (curIslandId == islandId) if (curIslandId == islandId)
{ {
startManifold = &islandmanifold[startManifoldIndex]; startManifold = &m_islandmanifold[startManifoldIndex];
for (endManifoldIndex = startManifoldIndex+1;(endManifoldIndex<numManifolds) && (islandId == getIslandId(islandmanifold[endManifoldIndex]));endManifoldIndex++) for (endManifoldIndex = startManifoldIndex+1;(endManifoldIndex<numManifolds) && (islandId == getIslandId(m_islandmanifold[endManifoldIndex]));endManifoldIndex++)
{ {
} }
@@ -343,7 +342,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
if (!islandSleeping) if (!islandSleeping)
{ {
callback->ProcessIsland(&islandBodies[0],islandBodies.size(),startManifold,numIslandManifolds, islandId); callback->ProcessIsland(&m_islandBodies[0],m_islandBodies.size(),startManifold,numIslandManifolds, islandId);
// printf("Island callback of size:%d bodies, %d manifolds\n",islandBodies.size(),numIslandManifolds); // printf("Island callback of size:%d bodies, %d manifolds\n",islandBodies.size(),numIslandManifolds);
} }
@@ -352,8 +351,9 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher,
startManifoldIndex = endManifoldIndex; startManifoldIndex = endManifoldIndex;
} }
islandBodies.resize(0); m_islandBodies.resize(0);
} }
#endif //SPLIT_ISLANDS #endif //SPLIT_ISLANDS
m_islandmanifold.resize(0);
} }

View File

@@ -18,16 +18,24 @@ subject to the following restrictions:
#include "BulletCollision/CollisionDispatch/btUnionFind.h" #include "BulletCollision/CollisionDispatch/btUnionFind.h"
#include "btCollisionCreateFunc.h" #include "btCollisionCreateFunc.h"
#include "LinearMath/btAlignedObjectArray.h"
class btCollisionObject; class btCollisionObject;
class btCollisionWorld; class btCollisionWorld;
class btDispatcher; class btDispatcher;
class btPersistentManifold;
///SimulationIslandManager creates and handles simulation islands, using btUnionFind ///SimulationIslandManager creates and handles simulation islands, using btUnionFind
class btSimulationIslandManager class btSimulationIslandManager
{ {
btUnionFind m_unionFind; btUnionFind m_unionFind;
btAlignedObjectArray<btPersistentManifold*> m_islandmanifold;
btAlignedObjectArray<btCollisionObject* > m_islandBodies;
public: public:
btSimulationIslandManager(); btSimulationIslandManager();
virtual ~btSimulationIslandManager(); virtual ~btSimulationIslandManager();

View File

@@ -35,7 +35,8 @@ m_ownsBvh(false)
if (buildBvh) if (buildBvh)
{ {
m_bvh = new btOptimizedBvh(); void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16);
m_bvh = new (mem) btOptimizedBvh();
m_bvh->build(meshInterface,m_useQuantizedAabbCompression,bvhAabbMin,bvhAabbMax); m_bvh->build(meshInterface,m_useQuantizedAabbCompression,bvhAabbMin,bvhAabbMax);
m_ownsBvh = true; m_ownsBvh = true;
} }
@@ -55,7 +56,9 @@ m_ownsBvh(false)
if (buildBvh) if (buildBvh)
{ {
m_bvh = new btOptimizedBvh(); void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16);
m_bvh = new (mem) btOptimizedBvh();
m_bvh->build(meshInterface,m_useQuantizedAabbCompression,bvhAabbMin,bvhAabbMax); m_bvh->build(meshInterface,m_useQuantizedAabbCompression,bvhAabbMin,bvhAabbMax);
m_ownsBvh = true; m_ownsBvh = true;
} }
@@ -83,7 +86,9 @@ void btBvhTriangleMeshShape::refitTree()
btBvhTriangleMeshShape::~btBvhTriangleMeshShape() btBvhTriangleMeshShape::~btBvhTriangleMeshShape()
{ {
if (m_ownsBvh) if (m_ownsBvh)
delete m_bvh; {
btAlignedFree(m_bvh);
}
} }
//perform bvh tree traversal and report overlapping triangles to 'callback' //perform bvh tree traversal and report overlapping triangles to 'callback'
@@ -180,9 +185,12 @@ void btBvhTriangleMeshShape::setLocalScaling(const btVector3& scaling)
{ {
btTriangleMeshShape::setLocalScaling(scaling); btTriangleMeshShape::setLocalScaling(scaling);
if (m_ownsBvh) if (m_ownsBvh)
delete m_bvh; {
btAlignedFree(m_bvh);
}
///m_localAabbMin/m_localAabbMax is already re-calculated in btTriangleMeshShape. We could just scale aabb, but this needs some more work ///m_localAabbMin/m_localAabbMax is already re-calculated in btTriangleMeshShape. We could just scale aabb, but this needs some more work
m_bvh = new btOptimizedBvh(); void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16);
m_bvh = new(mem) btOptimizedBvh();
//rebuild the bvh... //rebuild the bvh...
m_bvh->build(m_meshInterface,m_useQuantizedAabbCompression,m_localAabbMin,m_localAabbMax); m_bvh->build(m_meshInterface,m_useQuantizedAabbCompression,m_localAabbMin,m_localAabbMax);

View File

@@ -139,11 +139,14 @@ btHingeConstraint::btHingeConstraint(btRigidBody& rbA, const btTransform& rbAFra
m_angularOnly(false), m_angularOnly(false),
m_enableAngularMotor(false) m_enableAngularMotor(false)
{ {
///not providing rigidbody B means implicitly using worldspace for body B
// flip axis // flip axis
m_rbBFrame.getBasis()[0][2] *= btScalar(-1.); m_rbBFrame.getBasis()[0][2] *= btScalar(-1.);
m_rbBFrame.getBasis()[1][2] *= btScalar(-1.); m_rbBFrame.getBasis()[1][2] *= btScalar(-1.);
m_rbBFrame.getBasis()[2][2] *= btScalar(-1.); m_rbBFrame.getBasis()[2][2] *= btScalar(-1.);
m_rbBFrame.getOrigin() = m_rbA.getCenterOfMassTransform()(m_rbAFrame.getOrigin());
//start with free //start with free
m_lowerLimit = btScalar(1e30); m_lowerLimit = btScalar(1e30);

View File

@@ -99,7 +99,7 @@ bool MyContactDestroyedCallback(void* userPersistentData)
{ {
assert (userPersistentData); assert (userPersistentData);
btConstraintPersistentData* cpd = (btConstraintPersistentData*)userPersistentData; btConstraintPersistentData* cpd = (btConstraintPersistentData*)userPersistentData;
delete cpd; btAlignedFree(cpd);
totalCpd--; totalCpd--;
//printf("totalCpd = %i. DELETED Ptr %x\n",totalCpd,userPersistentData); //printf("totalCpd = %i. DELETED Ptr %x\n",totalCpd,userPersistentData);
return true; return true;
@@ -469,7 +469,7 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
//todo: use stack allocator for this temp memory //todo: use stack allocator for this temp memory
int minReservation = numManifolds*2; int minReservation = numManifolds*2;
tmpSolverBodyPool.reserve(minReservation); //tmpSolverBodyPool.reserve(minReservation);
//don't convert all bodies, only the one we need so solver the constraints //don't convert all bodies, only the one we need so solver the constraints
/* /*
@@ -489,8 +489,9 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
} }
*/ */
tmpSolverConstraintPool.reserve(minReservation); //tmpSolverConstraintPool.reserve(minReservation);
tmpSolverFrictionConstraintPool.reserve(minReservation); //tmpSolverFrictionConstraintPool.reserve(minReservation);
{ {
int i; int i;
@@ -1024,7 +1025,9 @@ void btSequentialImpulseConstraintSolver::prepareConstraints(btPersistentManifol
} else } else
{ {
cpd = new btConstraintPersistentData; //todo: should this be in a pool?
void* mem = btAlignedAlloc(sizeof(btConstraintPersistentData),16);
cpd = new (mem)btConstraintPersistentData;
assert(cpd); assert(cpd);
totalCpd ++; totalCpd ++;
@@ -1071,7 +1074,6 @@ void btSequentialImpulseConstraintSolver::prepareConstraints(btPersistentManifol
cpd->m_penetration = btScalar(0.); cpd->m_penetration = btScalar(0.);
} }
btScalar relaxation = info.m_damping; btScalar relaxation = info.m_damping;
if (m_solverMode & SOLVER_USE_WARMSTARTING) if (m_solverMode & SOLVER_USE_WARMSTARTING)

View File

@@ -22,6 +22,7 @@ subject to the following restrictions:
#include "Bullet-C-Api.h" #include "Bullet-C-Api.h"
#include "btBulletDynamicsCommon.h" #include "btBulletDynamicsCommon.h"
#include "LinearMath/btAlignedAllocator.h"
/* /*
Create and Delete a Physics SDK Create and Delete a Physics SDK
@@ -51,13 +52,14 @@ struct btPhysicsSdk
plPhysicsSdkHandle plNewBulletSdk() plPhysicsSdkHandle plNewBulletSdk()
{ {
return (plPhysicsSdkHandle)new btPhysicsSdk; void* mem = btAlignedAlloc(sizeof(btPhysicsSdk),16);
return (plPhysicsSdkHandle)new (mem)btPhysicsSdk;
} }
void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk) void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk)
{ {
btPhysicsSdk* phys = reinterpret_cast<btPhysicsSdk*>(physicsSdk); btPhysicsSdk* phys = reinterpret_cast<btPhysicsSdk*>(physicsSdk);
delete phys; btAlignedFree(phys);
} }
@@ -65,17 +67,23 @@ void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk)
plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdkHandle) plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdkHandle)
{ {
btPhysicsSdk* physicsSdk = reinterpret_cast<btPhysicsSdk*>(physicsSdkHandle); btPhysicsSdk* physicsSdk = reinterpret_cast<btPhysicsSdk*>(physicsSdkHandle);
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); void* mem = btAlignedAlloc(sizeof(btDefaultCollisionConfiguration),16);
btDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); btDefaultCollisionConfiguration* collisionConfiguration = new (mem)btDefaultCollisionConfiguration();
btBroadphaseInterface* pairCache = new btAxisSweep3(physicsSdk->m_worldAabbMin,physicsSdk->m_worldAabbMax); mem = btAlignedAlloc(sizeof(btCollisionDispatcher),16);
btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver(); btDispatcher* dispatcher = new (mem)btCollisionDispatcher(collisionConfiguration);
mem = btAlignedAlloc(sizeof(btAxisSweep3),16);
btBroadphaseInterface* pairCache = new (mem)btAxisSweep3(physicsSdk->m_worldAabbMin,physicsSdk->m_worldAabbMax);
mem = btAlignedAlloc(sizeof(btSequentialImpulseConstraintSolver),16);
btConstraintSolver* constraintSolver = new(mem) btSequentialImpulseConstraintSolver();
return (plDynamicsWorldHandle) new btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration); mem = btAlignedAlloc(sizeof(btDiscreteDynamicsWorld),16);
return (plDynamicsWorldHandle) new (mem)btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration);
} }
void plDeleteDynamicsWorld(plDynamicsWorldHandle world) void plDeleteDynamicsWorld(plDynamicsWorldHandle world)
{ {
//todo: also clean up the other allocations, axisSweep, pairCache,dispatcher,constraintSolver,collisionConfiguration
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world); btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
delete dynamicsWorld; btAlignedFree(dynamicsWorld);
} }
void plStepSimulation(plDynamicsWorldHandle world, plReal timeStep) void plStepSimulation(plDynamicsWorldHandle world, plReal timeStep)
@@ -118,7 +126,8 @@ plRigidBodyHandle plCreateRigidBody( void* user_data, float mass, plCollisionSh
{ {
shape->calculateLocalInertia(mass,localInertia); shape->calculateLocalInertia(mass,localInertia);
} }
btRigidBody* body = new btRigidBody(mass, 0,shape,localInertia); void* mem = btAlignedAlloc(sizeof(btRigidBody),16);
btRigidBody* body = new (mem)btRigidBody(mass, 0,shape,localInertia);
body->setWorldTransform(trans); body->setWorldTransform(trans);
body->setUserPointer(user_data); body->setUserPointer(user_data);
return (plRigidBodyHandle) body; return (plRigidBodyHandle) body;
@@ -128,7 +137,7 @@ void plDeleteRigidBody(plRigidBodyHandle cbody)
{ {
btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody); btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody);
assert(body); assert(body);
delete body; btAlignedFree( body);
} }
@@ -136,13 +145,15 @@ void plDeleteRigidBody(plRigidBodyHandle cbody)
plCollisionShapeHandle plNewSphereShape(plReal radius) plCollisionShapeHandle plNewSphereShape(plReal radius)
{ {
return (plCollisionShapeHandle) new btSphereShape(radius); void* mem = btAlignedAlloc(sizeof(btSphereShape),16);
return (plCollisionShapeHandle) new (mem)btSphereShape(radius);
} }
plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z) plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z)
{ {
return (plCollisionShapeHandle) new btBoxShape(btVector3(x,y,z)); void* mem = btAlignedAlloc(sizeof(btBoxShape),16);
return (plCollisionShapeHandle) new (mem)btBoxShape(btVector3(x,y,z));
} }
plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height) plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height)
@@ -152,22 +163,26 @@ plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height)
const int numSpheres = 2; const int numSpheres = 2;
btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)}; btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)};
btScalar radi[numSpheres] = {radius,radius}; btScalar radi[numSpheres] = {radius,radius};
return (plCollisionShapeHandle) new btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres); void* mem = btAlignedAlloc(sizeof(btMultiSphereShape),16);
return (plCollisionShapeHandle) new (mem)btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres);
} }
plCollisionShapeHandle plNewConeShape(plReal radius, plReal height) plCollisionShapeHandle plNewConeShape(plReal radius, plReal height)
{ {
return (plCollisionShapeHandle) new btConeShape(radius,height); void* mem = btAlignedAlloc(sizeof(btConeShape),16);
return (plCollisionShapeHandle) new (mem)btConeShape(radius,height);
} }
plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height) plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height)
{ {
return (plCollisionShapeHandle) new btCylinderShape(btVector3(radius,height,radius)); void* mem = btAlignedAlloc(sizeof(btCylinderShape),16);
return (plCollisionShapeHandle) new (mem)btCylinderShape(btVector3(radius,height,radius));
} }
/* Convex Meshes */ /* Convex Meshes */
plCollisionShapeHandle plNewConvexHullShape() plCollisionShapeHandle plNewConvexHullShape()
{ {
return (plCollisionShapeHandle) new btConvexHullShape(); void* mem = btAlignedAlloc(sizeof(btConvexHullShape),16);
return (plCollisionShapeHandle) new (mem)btConvexHullShape();
} }
@@ -179,7 +194,8 @@ plMeshInterfaceHandle plNewMeshInterface()
plCollisionShapeHandle plNewCompoundShape() plCollisionShapeHandle plNewCompoundShape()
{ {
return (plCollisionShapeHandle) new btCompoundShape(); void* mem = btAlignedAlloc(sizeof(btCompoundShape),16);
return (plCollisionShapeHandle) new (mem)btCompoundShape();
} }
void plAddChildShape(plCollisionShapeHandle compoundShapeHandle,plCollisionShapeHandle childShapeHandle, plVector3 childPos,plQuaternion childOrn) void plAddChildShape(plCollisionShapeHandle compoundShapeHandle,plCollisionShapeHandle childShapeHandle, plVector3 childPos,plQuaternion childOrn)
@@ -224,7 +240,7 @@ void plDeleteShape(plCollisionShapeHandle cshape)
{ {
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape); btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape); assert(shape);
delete shape; btAlignedFree(shape);
} }
void plSetScaling(plCollisionShapeHandle cshape, plVector3 cscaling) void plSetScaling(plCollisionShapeHandle cshape, plVector3 cscaling)
{ {

View File

@@ -59,15 +59,28 @@ subject to the following restrictions:
btDiscreteDynamicsWorld::btDiscreteDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver, btCollisionConfiguration* collisionConfiguration) btDiscreteDynamicsWorld::btDiscreteDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver, btCollisionConfiguration* collisionConfiguration)
:btDynamicsWorld(dispatcher,pairCache,collisionConfiguration), :btDynamicsWorld(dispatcher,pairCache,collisionConfiguration),
m_constraintSolver(constraintSolver? constraintSolver: new btSequentialImpulseConstraintSolver), m_constraintSolver(constraintSolver),
m_debugDrawer(0), m_debugDrawer(0),
m_gravity(0,-10,0), m_gravity(0,-10,0),
m_localTime(btScalar(1.)/btScalar(60.)), m_localTime(btScalar(1.)/btScalar(60.)),
m_profileTimings(0) m_profileTimings(0)
{ {
m_islandManager = new btSimulationIslandManager(); if (m_constraintSolver)
{
void* mem = btAlignedAlloc(sizeof(btSequentialImpulseConstraintSolver),16);
m_constraintSolver = new (mem) btSequentialImpulseConstraintSolver;
m_ownsConstraintSolver = true;
} else
{
m_ownsConstraintSolver = false;
}
{
void* mem = btAlignedAlloc(sizeof(btSimulationIslandManager),16);
m_islandManager = new (mem) btSimulationIslandManager();
}
m_ownsIslandManager = true; m_ownsIslandManager = true;
m_ownsConstraintSolver = (constraintSolver==0);
} }
@@ -75,9 +88,9 @@ btDiscreteDynamicsWorld::~btDiscreteDynamicsWorld()
{ {
//only delete it when we created it //only delete it when we created it
if (m_ownsIslandManager) if (m_ownsIslandManager)
delete m_islandManager; btAlignedFree( m_islandManager);
if (m_ownsConstraintSolver) if (m_ownsConstraintSolver)
delete m_constraintSolver; btAlignedFree(m_constraintSolver);
} }
void btDiscreteDynamicsWorld::saveKinematicState(btScalar timeStep) void btDiscreteDynamicsWorld::saveKinematicState(btScalar timeStep)
@@ -958,7 +971,7 @@ void btDiscreteDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
{ {
if (m_ownsConstraintSolver) if (m_ownsConstraintSolver)
{ {
delete m_constraintSolver; btAlignedFree( m_constraintSolver);
} }
m_ownsConstraintSolver = false; m_ownsConstraintSolver = false;
m_constraintSolver = solver; m_constraintSolver = solver;

View File

@@ -46,7 +46,7 @@ m_gravity(0,0,-10)
btSimpleDynamicsWorld::~btSimpleDynamicsWorld() btSimpleDynamicsWorld::~btSimpleDynamicsWorld()
{ {
if (m_ownsConstraintSolver) if (m_ownsConstraintSolver)
delete m_constraintSolver; btAlignedFree( m_constraintSolver);
} }
int btSimpleDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep) int btSimpleDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep)
@@ -205,7 +205,7 @@ void btSimpleDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
{ {
if (m_ownsConstraintSolver) if (m_ownsConstraintSolver)
{ {
delete m_constraintSolver; btAlignedFree(m_constraintSolver);
} }
m_ownsConstraintSolver = false; m_ownsConstraintSolver = false;
m_constraintSolver = solver; m_constraintSolver = solver;

View File

@@ -527,10 +527,14 @@ void btRaycastVehicle::updateFriction(btScalar timeStep)
return; return;
btVector3* forwardWS = new btVector3[numWheel]; void* mem = btAlignedAlloc(numWheel*sizeof(btVector3),16);
btVector3* axle = new btVector3[numWheel]; btVector3* forwardWS = new (mem)btVector3[numWheel];
btScalar* forwardImpulse = new btScalar[numWheel]; mem = btAlignedAlloc(numWheel*sizeof(btVector3),16);
btScalar* sideImpulse = new btScalar[numWheel]; btVector3* axle = new (mem)btVector3[numWheel];
mem = btAlignedAlloc(numWheel*sizeof(btScalar),16);
btScalar* forwardImpulse = new (mem)btScalar[numWheel];
mem = btAlignedAlloc(numWheel*sizeof(btScalar),16);
btScalar* sideImpulse = new(mem) btScalar[numWheel];
int numWheelsOnGround = 0; int numWheelsOnGround = 0;
@@ -702,10 +706,10 @@ void btRaycastVehicle::updateFriction(btScalar timeStep)
} }
} }
delete []forwardWS; btAlignedFree(forwardWS);
delete [] axle; btAlignedFree(axle);
delete[]forwardImpulse; btAlignedFree(forwardImpulse);
delete[] sideImpulse; btAlignedFree(sideImpulse);
} }

View File

@@ -15,12 +15,20 @@ subject to the following restrictions:
#include "btAlignedAllocator.h" #include "btAlignedAllocator.h"
int gNumAlignedAllocs = 0;
int gNumAlignedFree = 0;
#if defined (BT_HAS_ALIGNED_ALLOCATOR) #if defined (BT_HAS_ALIGNED_ALLOCATOR)
#include <malloc.h> #include <malloc.h>
void* btAlignedAlloc (size_t size, int alignment) void* btAlignedAlloc (size_t size, int alignment)
{ {
gNumAlignedAllocs++;
void* ptr = _aligned_malloc(size,alignment); void* ptr = _aligned_malloc(size,alignment);
// printf("btAlignedAlloc %d, %x\n",size,ptr); // printf("btAlignedAlloc %d, %x\n",size,ptr);
return ptr; return ptr;
@@ -28,6 +36,7 @@ void* btAlignedAlloc (size_t size, int alignment)
void btAlignedFree (void* ptr) void btAlignedFree (void* ptr)
{ {
gNumAlignedFree++;
// printf("btAlignedFree %x\n",ptr); // printf("btAlignedFree %x\n",ptr);
_aligned_free(ptr); _aligned_free(ptr);
} }
@@ -38,18 +47,17 @@ void btAlignedFree (void* ptr)
#include <stdlib.h> #include <stdlib.h>
int numAllocs = 0;
int numFree = 0;
void* btAlignedAlloc (size_t size, int alignment) void* btAlignedAlloc (size_t size, int alignment)
{ {
numAllocs++; gNumAlignedAllocs++;
return memalign(alignment, size); return memalign(alignment, size);
} }
void btAlignedFree (void* ptr) void btAlignedFree (void* ptr)
{ {
numFree++; gNumAlignedFree++;
free(ptr); free(ptr);
} }
@@ -60,7 +68,9 @@ void* btAlignedAlloc (size_t size, int alignment)
void *ret; void *ret;
char *real; char *real;
unsigned long offset; unsigned long offset;
gNumAlignedAllocs++;
real = (char *)malloc(size + sizeof(void *) + (alignment-1)); real = (char *)malloc(size + sizeof(void *) + (alignment-1));
if (real) { if (real) {
offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1); offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
@@ -76,6 +86,7 @@ void btAlignedFree (void* ptr)
{ {
void* real; void* real;
gNumAlignedFree++;
if (ptr) { if (ptr) {
real = *((void **)(ptr)-1); real = *((void **)(ptr)-1);