16
16
#include "btAlignedAllocator.h"
19
#if defined (BT_HAS_ALIGNED_ALOCATOR)
18
int gNumAlignedAllocs = 0;
19
int gNumAlignedFree = 0;
20
int gTotalBytesAlignedAllocs = 0;//detect memory leaks
22
#if defined (BT_HAS_ALIGNED_ALLOCATOR)
21
23
#include <malloc.h>
22
void* btAlignedAlloc (int size, int alignment)
24
static void *btAlignedAllocDefault(size_t size, int alignment)
24
return _aligned_malloc(size,alignment);
26
return _aligned_malloc(size, (size_t)alignment);
27
void btAlignedFree (void* ptr)
29
static void btAlignedFreeDefault(void *ptr)
29
31
_aligned_free(ptr);
33
#elif defined(__CELLOS_LV2__)
36
34
#include <stdlib.h>
41
void* btAlignedAlloc (int size, int alignment)
36
static inline void *btAlignedAllocDefault(size_t size, int alignment)
44
38
return memalign(alignment, size);
47
void btAlignedFree (void* ptr)
41
static inline void btAlignedFreeDefault(void *ptr)
55
///will add some multi-platform version that works without _aligned_malloc/_aligned_free
57
void* btAlignedAlloc (int size, int alignment)
59
return new char[size];
62
void btAlignedFree (void* ptr)
64
delete [] (char*) ptr;
46
static inline void *btAlignedAllocDefault(size_t size, int alignment)
52
real = (char *)malloc(size + sizeof(void *) + (alignment-1));
54
offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
55
ret = (void *)((real + sizeof(void *)) + offset);
56
*((void **)(ret)-1) = (void *)(real);
63
static inline void btAlignedFreeDefault(void *ptr)
68
real = *((void **)(ptr)-1);
74
static void *btAllocDefault(size_t size)
79
static void btFreeDefault(void *ptr)
84
static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
85
static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
86
static btAllocFunc *sAllocFunc = btAllocDefault;
87
static btFreeFunc *sFreeFunc = btFreeDefault;
89
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
91
sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
92
sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
95
void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
97
sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
98
sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
101
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
102
//this generic allocator provides the total allocated number of bytes
105
void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename)
109
unsigned long offset;
111
gTotalBytesAlignedAllocs += size;
115
real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
117
offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) &
119
ret = (void *)((real + 2*sizeof(void *)) + offset);
120
*((void **)(ret)-1) = (void *)(real);
121
*((int*)(ret)-2) = size;
124
ret = (void *)(real);//??
127
printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
129
int* ptr = (int*)ret;
134
void btAlignedFreeInternal (void* ptr,int line,char* filename)
141
real = *((void **)(ptr)-1);
142
int size = *((int*)(ptr)-2);
143
gTotalBytesAlignedAllocs -= size;
145
printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
150
printf("NULL ptr\n");
154
#else //BT_DEBUG_MEMORY_ALLOCATIONS
156
void* btAlignedAllocInternal (size_t size, int alignment)
160
#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
161
ptr = sAlignedAllocFunc(size, alignment);
164
unsigned long offset;
166
real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
168
offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
169
ptr = (void *)((real + sizeof(void *)) + offset);
170
*((void **)(ptr)-1) = (void *)(real);
172
ptr = (void *)(real);
174
#endif // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
175
// printf("btAlignedAllocInternal %d, %x\n",size,ptr);
179
void btAlignedFreeInternal (void* ptr)
187
// printf("btAlignedFreeInternal %x\n",ptr);
188
#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
189
sAlignedFreeFunc(ptr);
194
real = *((void **)(ptr)-1);
197
#endif // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
200
#endif //BT_DEBUG_MEMORY_ALLOCATIONS