2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
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 "b3AlignedAllocator.h"
18 #ifdef B3_ALLOCATOR_STATISTICS
19 int b3g_numAlignedAllocs = 0;
20 int b3g_numAlignedFree = 0;
21 int b3g_totalBytesAlignedAllocs = 0; //detect memory leaks
24 static void *b3AllocDefault(size_t size)
29 static void b3FreeDefault(void *ptr)
34 static b3AllocFunc *b3s_allocFunc = b3AllocDefault;
35 static b3FreeFunc *b3s_freeFunc = b3FreeDefault;
37 #if defined(B3_HAS_ALIGNED_ALLOCATOR)
39 static void *b3AlignedAllocDefault(size_t size, int alignment)
41 return _aligned_malloc(size, (size_t)alignment);
44 static void b3AlignedFreeDefault(void *ptr)
48 #elif defined(__CELLOS_LV2__)
51 static inline void *b3AlignedAllocDefault(size_t size, int alignment)
53 return memalign(alignment, size);
56 static inline void b3AlignedFreeDefault(void *ptr)
62 static inline void *b3AlignedAllocDefault(size_t size, int alignment)
66 real = (char *)b3s_allocFunc(size + sizeof(void *) + (alignment - 1));
69 ret = b3AlignPointer(real + sizeof(void *), alignment);
70 *((void **)(ret)-1) = (void *)(real);
79 static inline void b3AlignedFreeDefault(void *ptr)
85 real = *((void **)(ptr)-1);
91 static b3AlignedAllocFunc *b3s_alignedAllocFunc = b3AlignedAllocDefault;
92 static b3AlignedFreeFunc *b3s_alignedFreeFunc = b3AlignedFreeDefault;
94 void b3AlignedAllocSetCustomAligned(b3AlignedAllocFunc *allocFunc, b3AlignedFreeFunc *freeFunc)
96 b3s_alignedAllocFunc = allocFunc ? allocFunc : b3AlignedAllocDefault;
97 b3s_alignedFreeFunc = freeFunc ? freeFunc : b3AlignedFreeDefault;
100 void b3AlignedAllocSetCustom(b3AllocFunc *allocFunc, b3FreeFunc *freeFunc)
102 b3s_allocFunc = allocFunc ? allocFunc : b3AllocDefault;
103 b3s_freeFunc = freeFunc ? freeFunc : b3FreeDefault;
106 #ifdef B3_DEBUG_MEMORY_ALLOCATIONS
107 //this generic allocator provides the total allocated number of bytes
110 void *b3AlignedAllocInternal(size_t size, int alignment, int line, char *filename)
114 #ifdef B3_ALLOCATOR_STATISTICS
115 b3g_totalBytesAlignedAllocs += size;
116 b3g_numAlignedAllocs++;
118 real = (char *)b3s_allocFunc(size + 2 * sizeof(void *) + (alignment - 1));
121 ret = (void *)b3AlignPointer(real + 2 * sizeof(void *), alignment);
122 *((void **)(ret)-1) = (void *)(real);
123 *((int *)(ret)-2) = size;
127 ret = (void *)(real); //??
130 b3Printf("allocation#%d at address %x, from %s,line %d, size %d\n", b3g_numAlignedAllocs, real, filename, line, size);
132 int *ptr = (int *)ret;
137 void b3AlignedFreeInternal(void *ptr, int line, char *filename)
140 #ifdef B3_ALLOCATOR_STATISTICS
141 b3g_numAlignedFree++;
145 real = *((void **)(ptr)-1);
146 int size = *((int *)(ptr)-2);
147 #ifdef B3_ALLOCATOR_STATISTICS
148 b3g_totalBytesAlignedAllocs -= size;
150 b3Printf("free #%d at address %x, from %s,line %d, size %d\n", b3g_numAlignedFree, real, filename, line, size);
156 b3Printf("NULL ptr\n");
160 #else //B3_DEBUG_MEMORY_ALLOCATIONS
162 void *b3AlignedAllocInternal(size_t size, int alignment)
164 #ifdef B3_ALLOCATOR_STATISTICS
165 b3g_numAlignedAllocs++;
168 ptr = b3s_alignedAllocFunc(size, alignment);
169 // b3Printf("b3AlignedAllocInternal %d, %x\n",size,ptr);
173 void b3AlignedFreeInternal(void *ptr)
179 #ifdef B3_ALLOCATOR_STATISTICS
180 b3g_numAlignedFree++;
182 // b3Printf("b3AlignedFreeInternal %x\n",ptr);
183 b3s_alignedFreeFunc(ptr);
186 #endif //B3_DEBUG_MEMORY_ALLOCATIONS