diff --git a/Demos/AllBulletDemos/DemoEntries.cpp b/Demos/AllBulletDemos/DemoEntries.cpp index 8df60b8bc..276fbaf8a 100644 --- a/Demos/AllBulletDemos/DemoEntries.cpp +++ b/Demos/AllBulletDemos/DemoEntries.cpp @@ -51,6 +51,7 @@ public: virtual void clientMoveAndDisplay() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float xOffset = 10.f; diff --git a/src/BulletDynamics/ConstraintSolver/btOdeQuickstepConstraintSolver.cpp b/src/BulletDynamics/ConstraintSolver/btOdeQuickstepConstraintSolver.cpp index 9cf2d90bb..eb8052c25 100644 --- a/src/BulletDynamics/ConstraintSolver/btOdeQuickstepConstraintSolver.cpp +++ b/src/BulletDynamics/ConstraintSolver/btOdeQuickstepConstraintSolver.cpp @@ -25,7 +25,7 @@ subject to the following restrictions: #include "btOdeContactJoint.h" #include "btOdeTypedJoint.h" #include "btOdeSolverBody.h" -#include +#include #include "LinearMath/btQuickprof.h" #include "LinearMath/btIDebugDraw.h" diff --git a/src/LinearMath/btAlignedAllocator.cpp b/src/LinearMath/btAlignedAllocator.cpp index 4d8167c3d..6fa94b6d2 100644 --- a/src/LinearMath/btAlignedAllocator.cpp +++ b/src/LinearMath/btAlignedAllocator.cpp @@ -19,6 +19,85 @@ int gNumAlignedAllocs = 0; int gNumAlignedFree = 0; int gTotalBytesAlignedAllocs = 0;//detect memory leaks +#if defined (BT_HAS_ALIGNED_ALLOCATOR) +#include +static void *btAlignedAllocDefault(size_t size, int alignment) +{ + return _aligned_malloc(size, (size_t)alignment); +} + +static void btAlignedFreeDefault(void *ptr) +{ + _aligned_free(ptr); +} +#elif defined(__CELLOS_LV2__) +#include + +static inline void *btAlignedAllocDefault(size_t size, int alignment) +{ + return memalign(alignment, size); +} + +static inline void btAlignedFreeDefault(void *ptr) +{ + free(ptr); +} +#else +static inline void *btAlignedAllocDefault(size_t size, int alignment) +{ + void *ret; + char *real; + unsigned long offset; + + real = (char *)malloc(size + sizeof(void *) + (alignment-1)); + if (real) { + offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1); + ret = (void *)((real + sizeof(void *)) + offset); + *((void **)(ret)-1) = (void *)(real); + } else { + ret = (void *)(real); + } + return (ret); +} + +static inline void btAlignedFreeDefault(void *ptr) +{ + void* real; + + if (ptr) { + real = *((void **)(ptr)-1); + free(real); + } +} +#endif + +static void *btAllocDefault(size_t size) +{ + return malloc(size); +} + +static void btFreeDefault(void *ptr) +{ + free(ptr); +} + +static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault; +static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault; +static btAllocFunc *sAllocFunc = btAllocDefault; +static btFreeFunc *sFreeFunc = btFreeDefault; + +void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc) +{ + sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault; + sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault; +} + +void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc) +{ + sAllocFunc = allocFunc ? allocFunc : btAllocDefault; + sFreeFunc = freeFunc ? freeFunc : btFreeDefault; +} + #ifdef BT_DEBUG_MEMORY_ALLOCATIONS //this generic allocator provides the total allocated number of bytes #include @@ -33,7 +112,7 @@ void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filen gNumAlignedAllocs++; - real = (char *)malloc(size + 2*sizeof(void *) + (alignment-1)); + real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1)); if (real) { offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) & (alignment-1); @@ -51,7 +130,7 @@ void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filen *ptr = 12; return (ret); } -#include + void btAlignedFreeInternal (void* ptr,int line,char* filename) { @@ -65,7 +144,7 @@ void btAlignedFreeInternal (void* ptr,int line,char* filename) printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size); - free(real); + sFreeFunc(real); } else { printf("NULL ptr\n"); @@ -74,91 +153,49 @@ void btAlignedFreeInternal (void* ptr,int line,char* filename) #else //BT_DEBUG_MEMORY_ALLOCATIONS - -#if defined (BT_HAS_ALIGNED_ALLOCATOR) - - - - - -#include void* btAlignedAllocInternal (size_t size, int alignment) { gNumAlignedAllocs++; + void* ptr; +#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__) + ptr = sAlignedAllocFunc(size, alignment); +#else + char *real; + unsigned long offset; - void* ptr = _aligned_malloc(size,alignment); + real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1)); + if (real) { + offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1); + ptr = (void *)((real + sizeof(void *)) + offset); + *((void **)(ptr)-1) = (void *)(real); + } else { + ptr = (void *)(real); + } +#endif // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__) // printf("btAlignedAllocInternal %d, %x\n",size,ptr); return ptr; } void btAlignedFreeInternal (void* ptr) { + if (!ptr) + { + return; + } + gNumAlignedFree++; // printf("btAlignedFreeInternal %x\n",ptr); - _aligned_free(ptr); -} - +#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__) + sAlignedFreeFunc(ptr); #else + void* real; -#ifdef __CELLOS_LV2__ - -#include - - - -void* btAlignedAllocInternal (size_t size, int alignment) -{ - gNumAlignedAllocs++; - return memalign(alignment, size); + if (ptr) { + real = *((void **)(ptr)-1); + sFreeFunc(real); + } +#endif // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__) } -void btAlignedFreeInternal (void* ptr) -{ - gNumAlignedFree++; - free(ptr); -} - -#else - -void* btAlignedAllocInternal (std::size_t size, int alignment); - -void* btAlignedAllocInternal (std::size_t size, int alignment) -{ - void *ret; - char *real; - unsigned long offset; - - gNumAlignedAllocs++; - - real = (char*) malloc(size + sizeof(void *) + (alignment-1)); - if (real != 0) - { - offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1); - ret = (void *)((real + sizeof(void *)) + offset); - *((void **)(ret)-1) = (void *)(real); - } - else - { - ret = (void *)(real); - } - return (ret); -} - -void btAlignedFreeInternal (void* ptr) -{ - void* real; - gNumAlignedFree++; - - if (ptr != 0) - { - real = *((void **)(ptr)-1); - //::operator delete(real); - free(real); - } -} -#endif // - -#endif - #endif //BT_DEBUG_MEMORY_ALLOCATIONS diff --git a/src/LinearMath/btAlignedAllocator.h b/src/LinearMath/btAlignedAllocator.h index aab9c136e..0f6da30a5 100644 --- a/src/LinearMath/btAlignedAllocator.h +++ b/src/LinearMath/btAlignedAllocator.h @@ -35,14 +35,22 @@ void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename void btAlignedFreeInternal (void* ptr,int line,char* filename); #else -void* btAlignedAllocInternal (std::size_t size, int alignment); + void* btAlignedAllocInternal (size_t size, int alignment); void btAlignedFreeInternal (void* ptr); #define btAlignedAlloc(a,b) btAlignedAllocInternal(a,b) #define btAlignedFree(ptr) btAlignedFreeInternal(ptr) + #endif typedef int size_type; +typedef void *(btAlignedAllocFunc)(size_t size, int alignment); +typedef void (btAlignedFreeFunc)(void *memblock); +typedef void *(btAllocFunc)(size_t size); +typedef void (btFreeFunc)(void *memblock); + +void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc); +void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc); template < typename T , unsigned Alignment > class btAlignedAllocator { diff --git a/src/LinearMath/btQuickprof.cpp b/src/LinearMath/btQuickprof.cpp index de38bcd63..e5b119614 100644 --- a/src/LinearMath/btQuickprof.cpp +++ b/src/LinearMath/btQuickprof.cpp @@ -15,6 +15,7 @@ #include "LinearMath/btQuickprof.h" + #ifdef USE_BT_CLOCK static btClock gProfileClock; @@ -62,10 +63,18 @@ CProfileNode::CProfileNode( const char * name, CProfileNode * parent ) : } +void CProfileNode::CleanupMemory() +{ + delete ( Child); + Child = NULL; + delete ( Sibling); + Sibling = NULL; +} + CProfileNode::~CProfileNode( void ) { - delete Child; - delete Sibling; + delete ( Child); + delete ( Sibling); } @@ -89,6 +98,7 @@ CProfileNode * CProfileNode::Get_Sub_Node( const char * name ) } // We didn't find it, so add it + CProfileNode * node = new CProfileNode( name, this ); node->Sibling = Child; Child = node; diff --git a/src/LinearMath/btQuickprof.h b/src/LinearMath/btQuickprof.h index de5dcba0e..3ac628ce1 100644 --- a/src/LinearMath/btQuickprof.h +++ b/src/LinearMath/btQuickprof.h @@ -14,7 +14,8 @@ #define QUICK_PROF_H #include "btScalar.h" - +#include "LinearMath/btAlignedAllocator.h" +#include //To disable built-in profiling, please comment out next line //#define BT_NO_PROFILE 1 @@ -247,6 +248,7 @@ public: CProfileNode * Get_Sibling( void ) { return Sibling; } CProfileNode * Get_Child( void ) { return Child; } + void CleanupMemory(); void Reset( void ); void Call( void ); bool Return( void ); @@ -312,13 +314,22 @@ public: static void Start_Profile( const char * name ); static void Stop_Profile( void ); + static void CleanupMemory(void) + { + Root.CleanupMemory(); + } + static void Reset( void ); static void Increment_Frame_Counter( void ); static int Get_Frame_Count_Since_Reset( void ) { return FrameCounter; } static float Get_Time_Since_Reset( void ); - static CProfileIterator * Get_Iterator( void ) { return new CProfileIterator( &Root ); } - static void Release_Iterator( CProfileIterator * iterator ) { delete iterator; } + static CProfileIterator * Get_Iterator( void ) + { + + return new CProfileIterator( &Root ); + } + static void Release_Iterator( CProfileIterator * iterator ) { delete ( iterator); } private: static CProfileNode Root;