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