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