- 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

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

View File

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

View File

@@ -22,6 +22,7 @@ subject to the following restrictions:
#include "Bullet-C-Api.h"
#include "btBulletDynamicsCommon.h"
#include "LinearMath/btAlignedAllocator.h"
/*
Create and Delete a Physics SDK
@@ -51,13 +52,14 @@ struct btPhysicsSdk
plPhysicsSdkHandle plNewBulletSdk()
{
return (plPhysicsSdkHandle)new btPhysicsSdk;
void* mem = btAlignedAlloc(sizeof(btPhysicsSdk),16);
return (plPhysicsSdkHandle)new (mem)btPhysicsSdk;
}
void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk)
{
btPhysicsSdk* phys = reinterpret_cast<btPhysicsSdk*>(physicsSdk);
delete phys;
btAlignedFree(phys);
}
@@ -65,17 +67,23 @@ void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk)
plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdkHandle)
{
btPhysicsSdk* physicsSdk = reinterpret_cast<btPhysicsSdk*>(physicsSdkHandle);
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btBroadphaseInterface* pairCache = new btAxisSweep3(physicsSdk->m_worldAabbMin,physicsSdk->m_worldAabbMax);
btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver();
void* mem = btAlignedAlloc(sizeof(btDefaultCollisionConfiguration),16);
btDefaultCollisionConfiguration* collisionConfiguration = new (mem)btDefaultCollisionConfiguration();
mem = btAlignedAlloc(sizeof(btCollisionDispatcher),16);
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)
{
//todo: also clean up the other allocations, axisSweep, pairCache,dispatcher,constraintSolver,collisionConfiguration
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
delete dynamicsWorld;
btAlignedFree(dynamicsWorld);
}
void plStepSimulation(plDynamicsWorldHandle world, plReal timeStep)
@@ -118,7 +126,8 @@ plRigidBodyHandle plCreateRigidBody( void* user_data, float mass, plCollisionSh
{
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->setUserPointer(user_data);
return (plRigidBodyHandle) body;
@@ -128,7 +137,7 @@ void plDeleteRigidBody(plRigidBodyHandle cbody)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody);
assert(body);
delete body;
btAlignedFree( body);
}
@@ -136,13 +145,15 @@ void plDeleteRigidBody(plRigidBodyHandle cbody)
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)
{
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)
@@ -152,22 +163,26 @@ plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height)
const int numSpheres = 2;
btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)};
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)
{
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)
{
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 */
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()
{
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)
@@ -224,7 +240,7 @@ void plDeleteShape(plCollisionShapeHandle cshape)
{
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape);
delete shape;
btAlignedFree(shape);
}
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)
:btDynamicsWorld(dispatcher,pairCache,collisionConfiguration),
m_constraintSolver(constraintSolver? constraintSolver: new btSequentialImpulseConstraintSolver),
m_constraintSolver(constraintSolver),
m_debugDrawer(0),
m_gravity(0,-10,0),
m_localTime(btScalar(1.)/btScalar(60.)),
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_ownsConstraintSolver = (constraintSolver==0);
}
@@ -75,9 +88,9 @@ btDiscreteDynamicsWorld::~btDiscreteDynamicsWorld()
{
//only delete it when we created it
if (m_ownsIslandManager)
delete m_islandManager;
btAlignedFree( m_islandManager);
if (m_ownsConstraintSolver)
delete m_constraintSolver;
btAlignedFree(m_constraintSolver);
}
void btDiscreteDynamicsWorld::saveKinematicState(btScalar timeStep)
@@ -958,7 +971,7 @@ void btDiscreteDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
{
if (m_ownsConstraintSolver)
{
delete m_constraintSolver;
btAlignedFree( m_constraintSolver);
}
m_ownsConstraintSolver = false;
m_constraintSolver = solver;

View File

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

View File

@@ -527,10 +527,14 @@ void btRaycastVehicle::updateFriction(btScalar timeStep)
return;
btVector3* forwardWS = new btVector3[numWheel];
btVector3* axle = new btVector3[numWheel];
btScalar* forwardImpulse = new btScalar[numWheel];
btScalar* sideImpulse = new btScalar[numWheel];
void* mem = btAlignedAlloc(numWheel*sizeof(btVector3),16);
btVector3* forwardWS = new (mem)btVector3[numWheel];
mem = btAlignedAlloc(numWheel*sizeof(btVector3),16);
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;
@@ -702,10 +706,10 @@ void btRaycastVehicle::updateFriction(btScalar timeStep)
}
}
delete []forwardWS;
delete [] axle;
delete[]forwardImpulse;
delete[] sideImpulse;
btAlignedFree(forwardWS);
btAlignedFree(axle);
btAlignedFree(forwardImpulse);
btAlignedFree(sideImpulse);
}