btAlignedAllocator.cpp 6.2 KB

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