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