added custom memory allocator registration, thanks to Sly.

call CProfileManager::CleanupMemory(), to cleanup btQuickprof memory.
changed include from #include <new.h> to #include <new> in btOdeQuickstepConstraintSolver.cpp
This commit is contained in:
erwin.coumans
2008-05-24 06:10:09 +00:00
parent 3a94e70370
commit 88ee734bfb
6 changed files with 147 additions and 80 deletions

View File

@@ -51,6 +51,7 @@ public:
virtual void clientMoveAndDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float xOffset = 10.f;

View File

@@ -25,7 +25,7 @@ subject to the following restrictions:
#include "btOdeContactJoint.h"
#include "btOdeTypedJoint.h"
#include "btOdeSolverBody.h"
#include <new.h>
#include <new>
#include "LinearMath/btQuickprof.h"
#include "LinearMath/btIDebugDraw.h"

View File

@@ -19,6 +19,85 @@ int gNumAlignedAllocs = 0;
int gNumAlignedFree = 0;
int gTotalBytesAlignedAllocs = 0;//detect memory leaks
#if defined (BT_HAS_ALIGNED_ALLOCATOR)
#include <malloc.h>
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 <stdlib.h>
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 <stdio.h>
@@ -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 <stdio.h>
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 <malloc.h>
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
#ifdef __CELLOS_LV2__
#include <stdlib.h>
void* btAlignedAllocInternal (size_t size, int alignment)
{
gNumAlignedAllocs++;
return memalign(alignment, size);
}
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)
{
if (ptr) {
real = *((void **)(ptr)-1);
//::operator delete(real);
free(real);
sFreeFunc(real);
}
#endif // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
}
#endif //
#endif
#endif //BT_DEBUG_MEMORY_ALLOCATIONS

View File

@@ -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 {

View File

@@ -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;

View File

@@ -14,7 +14,8 @@
#define QUICK_PROF_H
#include "btScalar.h"
#include "LinearMath/btAlignedAllocator.h"
#include <new>
//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;