btAlignedAllocator.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef BT_ALIGNED_ALLOCATOR
  14. #define BT_ALIGNED_ALLOCATOR
  15. ///we probably replace this with our own aligned memory allocator
  16. ///so we replace _aligned_malloc and _aligned_free with our own
  17. ///that is better portable and more predictable
  18. #include "btScalar.h"
  19. ///BT_DEBUG_MEMORY_ALLOCATIONS preprocessor can be set in build system
  20. ///for regression tests to detect memory leaks
  21. ///#define BT_DEBUG_MEMORY_ALLOCATIONS 1
  22. #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
  23. int btDumpMemoryLeaks();
  24. #define btAlignedAlloc(a, b) \
  25. btAlignedAllocInternal(a, b, __LINE__, __FILE__)
  26. #define btAlignedFree(ptr) \
  27. btAlignedFreeInternal(ptr, __LINE__, __FILE__)
  28. void* btAlignedAllocInternal(size_t size, int alignment, int line, const char* filename);
  29. void btAlignedFreeInternal(void* ptr, int line, const char* filename);
  30. #else
  31. void* btAlignedAllocInternal(size_t size, int alignment);
  32. void btAlignedFreeInternal(void* ptr);
  33. #define btAlignedAlloc(size, alignment) btAlignedAllocInternal(size, alignment)
  34. #define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
  35. #endif
  36. typedef int size_type;
  37. typedef void*(btAlignedAllocFunc)(size_t size, int alignment);
  38. typedef void(btAlignedFreeFunc)(void* memblock);
  39. typedef void*(btAllocFunc)(size_t size);
  40. typedef void(btFreeFunc)(void* memblock);
  41. ///The developer can let all Bullet memory allocations go through a custom memory allocator, using btAlignedAllocSetCustom
  42. void btAlignedAllocSetCustom(btAllocFunc* allocFunc, btFreeFunc* freeFunc);
  43. ///If the developer has already an custom aligned allocator, then btAlignedAllocSetCustomAligned can be used. The default aligned allocator pre-allocates extra memory using the non-aligned allocator, and instruments it.
  44. void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc);
  45. ///The btAlignedAllocator is a portable class for aligned memory allocations.
  46. ///Default implementations for unaligned and aligned allocations can be overridden by a custom allocator using btAlignedAllocSetCustom and btAlignedAllocSetCustomAligned.
  47. template <typename T, unsigned Alignment>
  48. class btAlignedAllocator
  49. {
  50. typedef btAlignedAllocator<T, Alignment> self_type;
  51. public:
  52. //just going down a list:
  53. btAlignedAllocator() {}
  54. /*
  55. btAlignedAllocator( const self_type & ) {}
  56. */
  57. template <typename Other>
  58. btAlignedAllocator(const btAlignedAllocator<Other, Alignment>&)
  59. {
  60. }
  61. typedef const T* const_pointer;
  62. typedef const T& const_reference;
  63. typedef T* pointer;
  64. typedef T& reference;
  65. typedef T value_type;
  66. pointer address(reference ref) const { return &ref; }
  67. const_pointer address(const_reference ref) const { return &ref; }
  68. pointer allocate(size_type n, const_pointer* hint = 0)
  69. {
  70. (void)hint;
  71. return reinterpret_cast<pointer>(btAlignedAlloc(sizeof(value_type) * n, Alignment));
  72. }
  73. void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); }
  74. void deallocate(pointer ptr)
  75. {
  76. btAlignedFree(reinterpret_cast<void*>(ptr));
  77. }
  78. void destroy(pointer ptr) { ptr->~value_type(); }
  79. template <typename O>
  80. struct rebind
  81. {
  82. typedef btAlignedAllocator<O, Alignment> other;
  83. };
  84. template <typename O>
  85. self_type& operator=(const btAlignedAllocator<O, Alignment>&)
  86. {
  87. return *this;
  88. }
  89. friend bool operator==(const self_type&, const self_type&) { return true; }
  90. };
  91. #endif //BT_ALIGNED_ALLOCATOR