1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include "btAlignedAllocator.h"
17
18// -- GODOT start --
19namespace VHACD {
20// -- GODOT end --
21
22#ifdef _MSC_VER
23#pragma warning(disable:4311 4302)
24#endif
25
26int32_t gNumAlignedAllocs = 0;
27int32_t gNumAlignedFree = 0;
28int32_t gTotalBytesAlignedAllocs = 0; //detect memory leaks
29
30static void* btAllocDefault(size_t size)
31{
32 return malloc(size);
33}
34
35static void btFreeDefault(void* ptr)
36{
37 free(ptr);
38}
39
40static btAllocFunc* sAllocFunc = btAllocDefault;
41static btFreeFunc* sFreeFunc = btFreeDefault;
42
43#if defined(BT_HAS_ALIGNED_ALLOCATOR)
44#include <malloc.h>
45static void* btAlignedAllocDefault(size_t size, int32_t alignment)
46{
47 return _aligned_malloc(size, (size_t)alignment);
48}
49
50static void btAlignedFreeDefault(void* ptr)
51{
52 _aligned_free(ptr);
53}
54#elif defined(__CELLOS_LV2__)
55#include <stdlib.h>
56
57static inline void* btAlignedAllocDefault(size_t size, int32_t alignment)
58{
59 return memalign(alignment, size);
60}
61
62static inline void btAlignedFreeDefault(void* ptr)
63{
64 free(ptr);
65}
66#else
67static 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
89static 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
100static btAlignedAllocFunc* sAlignedAllocFunc = btAlignedAllocDefault;
101static btAlignedFreeFunc* sAlignedFreeFunc = btAlignedFreeDefault;
102
103void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc)
104{
105 sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
106 sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
107}
108
109void 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
119void* 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
146void 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
168void* 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
177void 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