2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://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 "btAlignedAllocator.h"
19 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
20 int gNumAlignedAllocs = 0;
21 int gNumAlignedFree = 0;
22 int gTotalBytesAlignedAllocs = 0; //detect memory leaks
23 #endif //BT_DEBUG_MEMORY_ALLOCATIONST_DEBUG_ALLOCATIONS
25 static void *btAllocDefault(size_t size)
27 char* data = (char*) malloc(size);
28 memset(data,0,size);//keep msan happy
32 static void btFreeDefault(void *ptr)
37 static btAllocFunc *sAllocFunc = btAllocDefault;
38 static btFreeFunc *sFreeFunc = btFreeDefault;
40 #if defined(BT_HAS_ALIGNED_ALLOCATOR)
42 static void *btAlignedAllocDefault(size_t size, int alignment)
44 return _aligned_malloc(size, (size_t)alignment);
47 static void btAlignedFreeDefault(void *ptr)
51 #elif defined(__CELLOS_LV2__)
54 static inline void *btAlignedAllocDefault(size_t size, int alignment)
56 return memalign(alignment, size);
59 static inline void btAlignedFreeDefault(void *ptr)
65 static inline void *btAlignedAllocDefault(size_t size, int alignment)
69 real = (char *)sAllocFunc(size + sizeof(void *) + (alignment - 1));
72 ret = btAlignPointer(real + sizeof(void *), alignment);
73 *((void **)(ret)-1) = (void *)(real);
80 memset((char*) ret, 0, size);
84 static inline void btAlignedFreeDefault(void *ptr)
90 real = *((void **)(ptr)-1);
96 static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
97 static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
99 void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
101 sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
102 sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
105 void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
107 sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
108 sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
111 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
113 static int allocations_id[10241024];
114 static int allocations_bytes[10241024];
115 static int mynumallocs = 0;
118 int btDumpMemoryLeaks()
122 for (int i = 0; i < mynumallocs; i++)
124 printf("Error: leaked memory of allocation #%d (%d bytes)\n", allocations_id[i], allocations_bytes[i]);
125 totalLeak += allocations_bytes[i];
129 printf("Error: memory leaks: %d allocations were not freed and leaked together %d bytes\n", mynumallocs, totalLeak);
133 //this generic allocator provides the total allocated number of bytes
136 struct btDebugPtrMagic
146 void *btAlignedAllocInternal(size_t size, int alignment, int line, const char *filename)
150 printf("Whaat? size==0");
153 static int allocId = 0;
158 // to find some particular memory leak, you could do something like this:
161 // printf("catch me!\n");
163 // if (size>1024*1024)
165 // printf("big alloc!%d\n", size);
168 gTotalBytesAlignedAllocs += size;
171 int sz4prt = 4 * sizeof(void *);
173 real = (char *)sAllocFunc(size + sz4prt + (alignment - 1));
176 ret = (void *)btAlignPointer(real + sz4prt, alignment);
179 p.cptr -= sizeof(void *);
180 *p.vptrptr = (void *)real;
181 p.cptr -= sizeof(void *);
183 p.cptr -= sizeof(void *);
186 allocations_id[mynumallocs] = allocId;
187 allocations_bytes[mynumallocs] = size;
192 ret = (void *)(real); //??
195 printf("allocation %d at address %x, from %s,line %d, size %d (total allocated = %d)\n", allocId, real, filename, line, size, gTotalBytesAlignedAllocs);
198 int *ptr = (int *)ret;
203 void btAlignedFreeInternal(void *ptr, int line, const char *filename)
213 p.cptr -= sizeof(void *);
215 p.cptr -= sizeof(void *);
217 p.cptr -= sizeof(void *);
218 int allocId = *p.iptr;
222 for (int i = 0; i < mynumallocs; i++)
224 if (allocations_id[i] == allocId)
226 allocations_id[i] = allocations_id[mynumallocs - 1];
227 allocations_bytes[i] = allocations_bytes[mynumallocs - 1];
234 gTotalBytesAlignedAllocs -= size;
236 int diff = gNumAlignedAllocs - gNumAlignedFree;
237 printf("free %d at address %x, from %s,line %d, size %d (total remain = %d in %d non-freed allocations)\n", allocId, real, filename, line, size, gTotalBytesAlignedAllocs, diff);
243 //printf("deleting a NULL ptr, no effect\n");
247 #else //BT_DEBUG_MEMORY_ALLOCATIONS
249 void *btAlignedAllocInternal(size_t size, int alignment)
252 ptr = sAlignedAllocFunc(size, alignment);
253 // printf("btAlignedAllocInternal %d, %x\n",size,ptr);
257 void btAlignedFreeInternal(void *ptr)
264 // printf("btAlignedFreeInternal %x\n",ptr);
265 sAlignedFreeFunc(ptr);
268 #endif //BT_DEBUG_MEMORY_ALLOCATIONS