1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4
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:
10
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.
14 */
15
16 #include "btAlignedAllocator.h"
17
18 int gNumAlignedAllocs = 0;
19 int gNumAlignedFree = 0;
20 int gTotalBytesAlignedAllocs = 0;//detect memory leaks
21
btAllocDefault(size_t size)22 static void *btAllocDefault(size_t size)
23 {
24 return malloc(size);
25 }
26
btFreeDefault(void * ptr)27 static void btFreeDefault(void *ptr)
28 {
29 free(ptr);
30 }
31
32 static btAllocFunc *sAllocFunc = btAllocDefault;
33 static btFreeFunc *sFreeFunc = btFreeDefault;
34
35
36
37 #if defined (BT_HAS_ALIGNED_ALLOCATOR)
38 #include <malloc.h>
btAlignedAllocDefault(size_t size,int alignment)39 static void *btAlignedAllocDefault(size_t size, int alignment)
40 {
41 return _aligned_malloc(size, (size_t)alignment);
42 }
43
btAlignedFreeDefault(void * ptr)44 static void btAlignedFreeDefault(void *ptr)
45 {
46 _aligned_free(ptr);
47 }
48 #elif defined(__CELLOS_LV2__)
49 #include <stdlib.h>
50
btAlignedAllocDefault(size_t size,int alignment)51 static inline void *btAlignedAllocDefault(size_t size, int alignment)
52 {
53 return memalign(alignment, size);
54 }
55
btAlignedFreeDefault(void * ptr)56 static inline void btAlignedFreeDefault(void *ptr)
57 {
58 free(ptr);
59 }
60 #else
61
62
63
64
65
btAlignedAllocDefault(size_t size,int alignment)66 static inline void *btAlignedAllocDefault(size_t size, int alignment)
67 {
68 void *ret;
69 char *real;
70 real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
71 if (real) {
72 ret = btAlignPointer(real + sizeof(void *),alignment);
73 *((void **)(ret)-1) = (void *)(real);
74 } else {
75 ret = (void *)(real);
76 }
77 return (ret);
78 }
79
btAlignedFreeDefault(void * ptr)80 static inline void btAlignedFreeDefault(void *ptr)
81 {
82 void* real;
83
84 if (ptr) {
85 real = *((void **)(ptr)-1);
86 sFreeFunc(real);
87 }
88 }
89 #endif
90
91
92 static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
93 static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
94
btAlignedAllocSetCustomAligned(btAlignedAllocFunc * allocFunc,btAlignedFreeFunc * freeFunc)95 void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
96 {
97 sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
98 sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
99 }
100
btAlignedAllocSetCustom(btAllocFunc * allocFunc,btFreeFunc * freeFunc)101 void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
102 {
103 sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
104 sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
105 }
106
107 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
108 //this generic allocator provides the total allocated number of bytes
109 #include <stdio.h>
110
btAlignedAllocInternal(size_t size,int alignment,int line,char * filename)111 void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename)
112 {
113 void *ret;
114 char *real;
115
116 gTotalBytesAlignedAllocs += size;
117 gNumAlignedAllocs++;
118
119
120 real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
121 if (real) {
122 ret = (void*) btAlignPointer(real + 2*sizeof(void *), alignment);
123 *((void **)(ret)-1) = (void *)(real);
124 *((int*)(ret)-2) = size;
125
126 } else {
127 ret = (void *)(real);//??
128 }
129
130 printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
131
132 int* ptr = (int*)ret;
133 *ptr = 12;
134 return (ret);
135 }
136
btAlignedFreeInternal(void * ptr,int line,char * filename)137 void btAlignedFreeInternal (void* ptr,int line,char* filename)
138 {
139
140 void* real;
141 gNumAlignedFree++;
142
143 if (ptr) {
144 real = *((void **)(ptr)-1);
145 int size = *((int*)(ptr)-2);
146 gTotalBytesAlignedAllocs -= size;
147
148 printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
149
150 sFreeFunc(real);
151 } else
152 {
153 printf("NULL ptr\n");
154 }
155 }
156
157 #else //BT_DEBUG_MEMORY_ALLOCATIONS
158
btAlignedAllocInternal(size_t size,int alignment)159 void* btAlignedAllocInternal (size_t size, int alignment)
160 {
161 gNumAlignedAllocs++;
162 void* ptr;
163 ptr = sAlignedAllocFunc(size, alignment);
164 // printf("btAlignedAllocInternal %d, %x\n",size,ptr);
165 return ptr;
166 }
167
btAlignedFreeInternal(void * ptr)168 void btAlignedFreeInternal (void* ptr)
169 {
170 if (!ptr)
171 {
172 return;
173 }
174
175 gNumAlignedFree++;
176 // printf("btAlignedFreeInternal %x\n",ptr);
177 sAlignedFreeFunc(ptr);
178 }
179
180 #endif //BT_DEBUG_MEMORY_ALLOCATIONS
181
182