btAlignedAllocator.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btAlignedAllocator.h"
  14. #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
  15. int gNumAlignedAllocs = 0;
  16. int gNumAlignedFree = 0;
  17. int gTotalBytesAlignedAllocs = 0; //detect memory leaks
  18. #endif //BT_DEBUG_MEMORY_ALLOCATIONST_DEBUG_ALLOCATIONS
  19. static void *btAllocDefault(size_t size)
  20. {
  21. return malloc(size);
  22. }
  23. static void btFreeDefault(void *ptr)
  24. {
  25. free(ptr);
  26. }
  27. static btAllocFunc *sAllocFunc = btAllocDefault;
  28. static btFreeFunc *sFreeFunc = btFreeDefault;
  29. #if defined(BT_HAS_ALIGNED_ALLOCATOR)
  30. #include <malloc.h>
  31. static void *btAlignedAllocDefault(size_t size, int alignment)
  32. {
  33. return _aligned_malloc(size, (size_t)alignment);
  34. }
  35. static void btAlignedFreeDefault(void *ptr)
  36. {
  37. _aligned_free(ptr);
  38. }
  39. #elif defined(__CELLOS_LV2__)
  40. #include <stdlib.h>
  41. static inline void *btAlignedAllocDefault(size_t size, int alignment)
  42. {
  43. return memalign(alignment, size);
  44. }
  45. static inline void btAlignedFreeDefault(void *ptr)
  46. {
  47. free(ptr);
  48. }
  49. #else
  50. static inline void *btAlignedAllocDefault(size_t size, int alignment)
  51. {
  52. void *ret;
  53. char *real;
  54. real = (char *)sAllocFunc(size + sizeof(void *) + (alignment - 1));
  55. if (real)
  56. {
  57. ret = btAlignPointer(real + sizeof(void *), alignment);
  58. *((void **)(ret)-1) = (void *)(real);
  59. }
  60. else
  61. {
  62. ret = (void *)(real);
  63. }
  64. return (ret);
  65. }
  66. static inline void btAlignedFreeDefault(void *ptr)
  67. {
  68. void *real;
  69. if (ptr)
  70. {
  71. real = *((void **)(ptr)-1);
  72. sFreeFunc(real);
  73. }
  74. }
  75. #endif
  76. static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
  77. static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
  78. void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
  79. {
  80. sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
  81. sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
  82. }
  83. void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
  84. {
  85. sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
  86. sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
  87. }
  88. #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
  89. static int allocations_id[10241024];
  90. static int allocations_bytes[10241024];
  91. static int mynumallocs = 0;
  92. #include <stdio.h>
  93. int btDumpMemoryLeaks()
  94. {
  95. int totalLeak = 0;
  96. for (int i = 0; i < mynumallocs; i++)
  97. {
  98. printf("Error: leaked memory of allocation #%d (%d bytes)\n", allocations_id[i], allocations_bytes[i]);
  99. totalLeak += allocations_bytes[i];
  100. }
  101. if (totalLeak)
  102. {
  103. printf("Error: memory leaks: %d allocations were not freed and leaked together %d bytes\n", mynumallocs, totalLeak);
  104. }
  105. return totalLeak;
  106. }
  107. //this generic allocator provides the total allocated number of bytes
  108. #include <stdio.h>
  109. struct btDebugPtrMagic
  110. {
  111. union {
  112. void **vptrptr;
  113. void *vptr;
  114. int *iptr;
  115. char *cptr;
  116. };
  117. };
  118. void *btAlignedAllocInternal(size_t size, int alignment, int line, const char *filename)
  119. {
  120. if (size == 0)
  121. {
  122. printf("Whaat? size==0");
  123. return 0;
  124. }
  125. static int allocId = 0;
  126. void *ret;
  127. char *real;
  128. // to find some particular memory leak, you could do something like this:
  129. // if (allocId==172)
  130. // {
  131. // printf("catch me!\n");
  132. // }
  133. // if (size>1024*1024)
  134. // {
  135. // printf("big alloc!%d\n", size);
  136. // }
  137. gTotalBytesAlignedAllocs += size;
  138. gNumAlignedAllocs++;
  139. int sz4prt = 4 * sizeof(void *);
  140. real = (char *)sAllocFunc(size + sz4prt + (alignment - 1));
  141. if (real)
  142. {
  143. ret = (void *)btAlignPointer(real + sz4prt, alignment);
  144. btDebugPtrMagic p;
  145. p.vptr = ret;
  146. p.cptr -= sizeof(void *);
  147. *p.vptrptr = (void *)real;
  148. p.cptr -= sizeof(void *);
  149. *p.iptr = size;
  150. p.cptr -= sizeof(void *);
  151. *p.iptr = allocId;
  152. allocations_id[mynumallocs] = allocId;
  153. allocations_bytes[mynumallocs] = size;
  154. mynumallocs++;
  155. }
  156. else
  157. {
  158. ret = (void *)(real); //??
  159. }
  160. printf("allocation %d at address %x, from %s,line %d, size %d (total allocated = %d)\n", allocId, real, filename, line, size, gTotalBytesAlignedAllocs);
  161. allocId++;
  162. int *ptr = (int *)ret;
  163. *ptr = 12;
  164. return (ret);
  165. }
  166. void btAlignedFreeInternal(void *ptr, int line, const char *filename)
  167. {
  168. void *real;
  169. if (ptr)
  170. {
  171. gNumAlignedFree++;
  172. btDebugPtrMagic p;
  173. p.vptr = ptr;
  174. p.cptr -= sizeof(void *);
  175. real = *p.vptrptr;
  176. p.cptr -= sizeof(void *);
  177. int size = *p.iptr;
  178. p.cptr -= sizeof(void *);
  179. int allocId = *p.iptr;
  180. bool found = false;
  181. for (int i = 0; i < mynumallocs; i++)
  182. {
  183. if (allocations_id[i] == allocId)
  184. {
  185. allocations_id[i] = allocations_id[mynumallocs - 1];
  186. allocations_bytes[i] = allocations_bytes[mynumallocs - 1];
  187. mynumallocs--;
  188. found = true;
  189. break;
  190. }
  191. }
  192. gTotalBytesAlignedAllocs -= size;
  193. int diff = gNumAlignedAllocs - gNumAlignedFree;
  194. printf("free %d at address %x, from %s,line %d, size %d (total remain = %d in %d non-freed allocations)\n", allocId, real, filename, line, size, gTotalBytesAlignedAllocs, diff);
  195. sFreeFunc(real);
  196. }
  197. else
  198. {
  199. //printf("deleting a NULL ptr, no effect\n");
  200. }
  201. }
  202. #else //BT_DEBUG_MEMORY_ALLOCATIONS
  203. void *btAlignedAllocInternal(size_t size, int alignment)
  204. {
  205. void *ptr;
  206. ptr = sAlignedAllocFunc(size, alignment);
  207. // printf("btAlignedAllocInternal %d, %x\n",size,ptr);
  208. return ptr;
  209. }
  210. void btAlignedFreeInternal(void *ptr)
  211. {
  212. if (!ptr)
  213. {
  214. return;
  215. }
  216. // printf("btAlignedFreeInternal %x\n",ptr);
  217. sAlignedFreeFunc(ptr);
  218. }
  219. #endif //BT_DEBUG_MEMORY_ALLOCATIONS