btAlignedAllocator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #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. // -- GODOT start --
  20. namespace VHACD {
  21. // -- GODOT end --
  22. //#define BT_DEBUG_MEMORY_ALLOCATIONS 1
  23. #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
  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, int32_t alignment, int32_t line, char* filename);
  29. void btAlignedFreeInternal(void* ptr, int32_t line, char* filename);
  30. #else
  31. void* btAlignedAllocInternal(size_t size, int32_t alignment);
  32. void btAlignedFreeInternal(void* ptr);
  33. #define btAlignedAlloc(size, alignment) btAlignedAllocInternal(size, alignment)
  34. #define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
  35. #endif
  36. typedef int32_t size_type;
  37. typedef void*(btAlignedAllocFunc)(size_t size, int32_t 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. typedef btAlignedAllocator<T, Alignment> self_type;
  50. public:
  51. //just going down a list:
  52. btAlignedAllocator() {}
  53. /*
  54. btAlignedAllocator( const self_type & ) {}
  55. */
  56. template <typename Other>
  57. btAlignedAllocator(const btAlignedAllocator<Other, Alignment>&) {}
  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(size_type n, const_pointer* hint = 0)
  66. {
  67. (void)hint;
  68. return reinterpret_cast<pointer>(btAlignedAlloc(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. btAlignedFree(reinterpret_cast<void*>(ptr));
  74. }
  75. void destroy(pointer ptr) { ptr->~value_type(); }
  76. template <typename O>
  77. struct rebind {
  78. typedef btAlignedAllocator<O, Alignment> other;
  79. };
  80. template <typename O>
  81. self_type& operator=(const btAlignedAllocator<O, Alignment>&) { return *this; }
  82. friend bool operator==(const self_type&, const self_type&) { return true; }
  83. };
  84. // -- GODOT start --
  85. }; // namespace VHACD
  86. // -- GODOT end --
  87. #endif //BT_ALIGNED_ALLOCATOR