b3AlignedAllocator.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://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 "b3AlignedAllocator.h"
  14. #ifdef B3_ALLOCATOR_STATISTICS
  15. int b3g_numAlignedAllocs = 0;
  16. int b3g_numAlignedFree = 0;
  17. int b3g_totalBytesAlignedAllocs = 0; //detect memory leaks
  18. #endif
  19. static void *b3AllocDefault(size_t size)
  20. {
  21. return malloc(size);
  22. }
  23. static void b3FreeDefault(void *ptr)
  24. {
  25. free(ptr);
  26. }
  27. static b3AllocFunc *b3s_allocFunc = b3AllocDefault;
  28. static b3FreeFunc *b3s_freeFunc = b3FreeDefault;
  29. #if defined(B3_HAS_ALIGNED_ALLOCATOR)
  30. #include <malloc.h>
  31. static void *b3AlignedAllocDefault(size_t size, int alignment)
  32. {
  33. return _aligned_malloc(size, (size_t)alignment);
  34. }
  35. static void b3AlignedFreeDefault(void *ptr)
  36. {
  37. _aligned_free(ptr);
  38. }
  39. #elif defined(__CELLOS_LV2__)
  40. #include <stdlib.h>
  41. static inline void *b3AlignedAllocDefault(size_t size, int alignment)
  42. {
  43. return memalign(alignment, size);
  44. }
  45. static inline void b3AlignedFreeDefault(void *ptr)
  46. {
  47. free(ptr);
  48. }
  49. #else
  50. static inline void *b3AlignedAllocDefault(size_t size, int alignment)
  51. {
  52. void *ret;
  53. char *real;
  54. real = (char *)b3s_allocFunc(size + sizeof(void *) + (alignment - 1));
  55. if (real)
  56. {
  57. ret = b3AlignPointer(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 b3AlignedFreeDefault(void *ptr)
  67. {
  68. void *real;
  69. if (ptr)
  70. {
  71. real = *((void **)(ptr)-1);
  72. b3s_freeFunc(real);
  73. }
  74. }
  75. #endif
  76. static b3AlignedAllocFunc *b3s_alignedAllocFunc = b3AlignedAllocDefault;
  77. static b3AlignedFreeFunc *b3s_alignedFreeFunc = b3AlignedFreeDefault;
  78. void b3AlignedAllocSetCustomAligned(b3AlignedAllocFunc *allocFunc, b3AlignedFreeFunc *freeFunc)
  79. {
  80. b3s_alignedAllocFunc = allocFunc ? allocFunc : b3AlignedAllocDefault;
  81. b3s_alignedFreeFunc = freeFunc ? freeFunc : b3AlignedFreeDefault;
  82. }
  83. void b3AlignedAllocSetCustom(b3AllocFunc *allocFunc, b3FreeFunc *freeFunc)
  84. {
  85. b3s_allocFunc = allocFunc ? allocFunc : b3AllocDefault;
  86. b3s_freeFunc = freeFunc ? freeFunc : b3FreeDefault;
  87. }
  88. #ifdef B3_DEBUG_MEMORY_ALLOCATIONS
  89. //this generic allocator provides the total allocated number of bytes
  90. #include <stdio.h>
  91. void *b3AlignedAllocInternal(size_t size, int alignment, int line, char *filename)
  92. {
  93. void *ret;
  94. char *real;
  95. #ifdef B3_ALLOCATOR_STATISTICS
  96. b3g_totalBytesAlignedAllocs += size;
  97. b3g_numAlignedAllocs++;
  98. #endif
  99. real = (char *)b3s_allocFunc(size + 2 * sizeof(void *) + (alignment - 1));
  100. if (real)
  101. {
  102. ret = (void *)b3AlignPointer(real + 2 * sizeof(void *), alignment);
  103. *((void **)(ret)-1) = (void *)(real);
  104. *((int *)(ret)-2) = size;
  105. }
  106. else
  107. {
  108. ret = (void *)(real); //??
  109. }
  110. b3Printf("allocation#%d at address %x, from %s,line %d, size %d\n", b3g_numAlignedAllocs, real, filename, line, size);
  111. int *ptr = (int *)ret;
  112. *ptr = 12;
  113. return (ret);
  114. }
  115. void b3AlignedFreeInternal(void *ptr, int line, char *filename)
  116. {
  117. void *real;
  118. #ifdef B3_ALLOCATOR_STATISTICS
  119. b3g_numAlignedFree++;
  120. #endif
  121. if (ptr)
  122. {
  123. real = *((void **)(ptr)-1);
  124. int size = *((int *)(ptr)-2);
  125. #ifdef B3_ALLOCATOR_STATISTICS
  126. b3g_totalBytesAlignedAllocs -= size;
  127. #endif
  128. b3Printf("free #%d at address %x, from %s,line %d, size %d\n", b3g_numAlignedFree, real, filename, line, size);
  129. b3s_freeFunc(real);
  130. }
  131. else
  132. {
  133. b3Printf("NULL ptr\n");
  134. }
  135. }
  136. #else //B3_DEBUG_MEMORY_ALLOCATIONS
  137. void *b3AlignedAllocInternal(size_t size, int alignment)
  138. {
  139. #ifdef B3_ALLOCATOR_STATISTICS
  140. b3g_numAlignedAllocs++;
  141. #endif
  142. void *ptr;
  143. ptr = b3s_alignedAllocFunc(size, alignment);
  144. // b3Printf("b3AlignedAllocInternal %d, %x\n",size,ptr);
  145. return ptr;
  146. }
  147. void b3AlignedFreeInternal(void *ptr)
  148. {
  149. if (!ptr)
  150. {
  151. return;
  152. }
  153. #ifdef B3_ALLOCATOR_STATISTICS
  154. b3g_numAlignedFree++;
  155. #endif
  156. // b3Printf("b3AlignedFreeInternal %x\n",ptr);
  157. b3s_alignedFreeFunc(ptr);
  158. }
  159. #endif //B3_DEBUG_MEMORY_ALLOCATIONS