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