b3AlignedAllocator.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef B3_ALIGNED_ALLOCATOR
  14. #define B3_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 "b3Scalar.h"
  19. //#define B3_DEBUG_MEMORY_ALLOCATIONS 1
  20. #ifdef B3_DEBUG_MEMORY_ALLOCATIONS
  21. #define b3AlignedAlloc(a, b) \
  22. b3AlignedAllocInternal(a, b, __LINE__, __FILE__)
  23. #define b3AlignedFree(ptr) \
  24. b3AlignedFreeInternal(ptr, __LINE__, __FILE__)
  25. void* b3AlignedAllocInternal(size_t size, int alignment, int line, char* filename);
  26. void b3AlignedFreeInternal(void* ptr, int line, char* filename);
  27. #else
  28. void* b3AlignedAllocInternal(size_t size, int alignment);
  29. void b3AlignedFreeInternal(void* ptr);
  30. #define b3AlignedAlloc(size, alignment) b3AlignedAllocInternal(size, alignment)
  31. #define b3AlignedFree(ptr) b3AlignedFreeInternal(ptr)
  32. #endif
  33. typedef int btSizeType;
  34. typedef void*(b3AlignedAllocFunc)(size_t size, int alignment);
  35. typedef void(b3AlignedFreeFunc)(void* memblock);
  36. typedef void*(b3AllocFunc)(size_t size);
  37. typedef void(b3FreeFunc)(void* memblock);
  38. ///The developer can let all Bullet memory allocations go through a custom memory allocator, using b3AlignedAllocSetCustom
  39. void b3AlignedAllocSetCustom(b3AllocFunc* allocFunc, b3FreeFunc* freeFunc);
  40. ///If the developer has already an custom aligned allocator, then b3AlignedAllocSetCustomAligned can be used. The default aligned allocator pre-allocates extra memory using the non-aligned allocator, and instruments it.
  41. void b3AlignedAllocSetCustomAligned(b3AlignedAllocFunc* allocFunc, b3AlignedFreeFunc* freeFunc);
  42. ///The b3AlignedAllocator is a portable class for aligned memory allocations.
  43. ///Default implementations for unaligned and aligned allocations can be overridden by a custom allocator using b3AlignedAllocSetCustom and b3AlignedAllocSetCustomAligned.
  44. template <typename T, unsigned Alignment>
  45. class b3AlignedAllocator
  46. {
  47. typedef b3AlignedAllocator<T, Alignment> self_type;
  48. public:
  49. //just going down a list:
  50. b3AlignedAllocator() {}
  51. /*
  52. b3AlignedAllocator( const self_type & ) {}
  53. */
  54. template <typename Other>
  55. b3AlignedAllocator(const b3AlignedAllocator<Other, Alignment>&)
  56. {
  57. }
  58. typedef const T* const_pointer;
  59. typedef const T& const_reference;
  60. typedef T* pointer;
  61. typedef T& reference;
  62. typedef T value_type;
  63. pointer address(reference ref) const { return &ref; }
  64. const_pointer address(const_reference ref) const { return &ref; }
  65. pointer allocate(btSizeType n, const_pointer* hint = 0)
  66. {
  67. (void)hint;
  68. return reinterpret_cast<pointer>(b3AlignedAlloc(sizeof(value_type) * n, Alignment));
  69. }
  70. void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); }
  71. void deallocate(pointer ptr)
  72. {
  73. b3AlignedFree(reinterpret_cast<void*>(ptr));
  74. }
  75. void destroy(pointer ptr) { ptr->~value_type(); }
  76. template <typename O>
  77. struct rebind
  78. {
  79. typedef b3AlignedAllocator<O, Alignment> other;
  80. };
  81. template <typename O>
  82. self_type& operator=(const b3AlignedAllocator<O, Alignment>&)
  83. {
  84. return *this;
  85. }
  86. friend bool operator==(const self_type&, const self_type&) { return true; }
  87. };
  88. #endif //B3_ALIGNED_ALLOCATOR