2
Bullet Continuous Collision Detection and Physics Library
3
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5
This software is provided 'as-is', without any express or implied warranty.
6
In no event will the authors be held liable for any damages arising from the use of this software.
7
Permission is granted to anyone to use this software for any purpose,
8
including commercial applications, and to alter it and redistribute it freely,
9
subject to the following restrictions:
11
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13
3. This notice may not be removed or altered from any source distribution.
16
#include "btAlignedAllocator.h"
18
int gNumAlignedAllocs = 0;
19
int gNumAlignedFree = 0;
20
int gTotalBytesAlignedAllocs = 0;//detect memory leaks
22
static void *btAllocDefault(size_t size)
27
static void btFreeDefault(void *ptr)
32
static btAllocFunc *sAllocFunc = btAllocDefault;
33
static btFreeFunc *sFreeFunc = btFreeDefault;
37
#if defined (BT_HAS_ALIGNED_ALLOCATOR)
39
static void *btAlignedAllocDefault(size_t size, int alignment)
41
return _aligned_malloc(size, (size_t)alignment);
44
static void btAlignedFreeDefault(void *ptr)
48
#elif defined(__CELLOS_LV2__)
51
static inline void *btAlignedAllocDefault(size_t size, int alignment)
53
return memalign(alignment, size);
56
static inline void btAlignedFreeDefault(void *ptr)
61
static inline void *btAlignedAllocDefault(size_t size, int alignment)
67
real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
69
offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
70
ret = (void *)((real + sizeof(void *)) + offset);
71
*((void **)(ret)-1) = (void *)(real);
78
static inline void btAlignedFreeDefault(void *ptr)
83
real = *((void **)(ptr)-1);
90
static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
91
static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
93
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
95
sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
96
sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
99
void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
101
sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
102
sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
105
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
106
//this generic allocator provides the total allocated number of bytes
109
void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename)
113
unsigned long offset;
115
gTotalBytesAlignedAllocs += size;
119
real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
121
offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) &
123
ret = (void *)((real + 2*sizeof(void *)) + offset);
124
*((void **)(ret)-1) = (void *)(real);
125
*((int*)(ret)-2) = size;
128
ret = (void *)(real);//??
131
printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
133
int* ptr = (int*)ret;
138
void btAlignedFreeInternal (void* ptr,int line,char* filename)
145
real = *((void **)(ptr)-1);
146
int size = *((int*)(ptr)-2);
147
gTotalBytesAlignedAllocs -= size;
149
printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
154
printf("NULL ptr\n");
158
#else //BT_DEBUG_MEMORY_ALLOCATIONS
160
void* btAlignedAllocInternal (size_t size, int alignment)
164
ptr = sAlignedAllocFunc(size, alignment);
165
// printf("btAlignedAllocInternal %d, %x\n",size,ptr);
169
void btAlignedFreeInternal (void* ptr)
177
// printf("btAlignedFreeInternal %x\n",ptr);
178
sAlignedFreeFunc(ptr);
181
#endif //BT_DEBUG_MEMORY_ALLOCATIONS