| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef BT_ALIGNED_ALLOCATOR |
| 17 |
#define BT_ALIGNED_ALLOCATOR |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "btScalar.h" |
| 24 |
|
| 25 |
namespace VHACD { |
| 26 |
|
| 27 |
|
| 28 |
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS |
| 29 |
|
| 30 |
#define btAlignedAlloc(a, b) \ |
| 31 |
btAlignedAllocInternal(a, b, __LINE__, __FILE__) |
| 32 |
|
| 33 |
#define btAlignedFree(ptr) \ |
| 34 |
btAlignedFreeInternal(ptr, __LINE__, __FILE__) |
| 35 |
|
| 36 |
void* btAlignedAllocInternal(size_t size, int32_t alignment, int32_t line, char* filename); |
| 37 |
|
| 38 |
void btAlignedFreeInternal(void* ptr, int32_t line, char* filename); |
| 39 |
|
| 40 |
#else |
| 41 |
void* btAlignedAllocInternal(size_t size, int32_t alignment); |
| 42 |
void btAlignedFreeInternal(void* ptr); |
| 43 |
|
| 44 |
#define btAlignedAlloc(size, alignment) btAlignedAllocInternal(size, alignment) |
| 45 |
#define btAlignedFree(ptr) btAlignedFreeInternal(ptr) |
| 46 |
|
| 47 |
#endif |
| 48 |
typedef int32_t size_type; |
| 49 |
|
| 50 |
typedef void*(btAlignedAllocFunc)(size_t size, int32_t alignment); |
| 51 |
typedef void(btAlignedFreeFunc)(void* memblock); |
| 52 |
typedef void*(btAllocFunc)(size_t size); |
| 53 |
typedef void(btFreeFunc)(void* memblock); |
| 54 |
|
| 55 |
|
| 56 |
void btAlignedAllocSetCustom(btAllocFunc* allocFunc, btFreeFunc* freeFunc); |
| 57 |
|
| 58 |
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
template <typename T, unsigned Alignment> |
| 63 |
class btAlignedAllocator { |
| 64 |
|
| 65 |
typedef btAlignedAllocator<T, Alignment> self_type; |
| 66 |
|
| 67 |
public: |
| 68 |
|
| 69 |
btAlignedAllocator() {} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
template <typename Other> |
| 75 |
btAlignedAllocator(const btAlignedAllocator<Other, Alignment>&) {} |
| 76 |
|
| 77 |
typedef const T* const_pointer; |
| 78 |
typedef const T& const_reference; |
| 79 |
typedef T* pointer; |
| 80 |
typedef T& reference; |
| 81 |
typedef T value_type; |
| 82 |
|
| 83 |
pointer address(reference ref) const { return &ref; } |
| 84 |
const_pointer address(const_reference ref) const { return &ref; } |
| 85 |
pointer allocate(size_type n, const_pointer* hint = 0) |
| 86 |
{ |
| 87 |
(void)hint; |
| 88 |
return reinterpret_cast<pointer>(btAlignedAlloc(sizeof(value_type) * n, Alignment)); |
| 89 |
} |
| 90 |
void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); } |
| 91 |
void deallocate(pointer ptr) |
| 92 |
{ |
| 93 |
btAlignedFree(reinterpret_cast<void*>(ptr)); |
| 94 |
} |
| 95 |
void destroy(pointer ptr) { ptr->~value_type(); } |
| 96 |
|
| 97 |
template <typename O> |
| 98 |
struct rebind { |
| 99 |
typedef btAlignedAllocator<O, Alignment> other; |
| 100 |
}; |
| 101 |
template <typename O> |
| 102 |
self_type& operator=(const btAlignedAllocator<O, Alignment>&) { return *this; } |
| 103 |
|
| 104 |
friend bool operator==(const self_type&, const self_type&) { return true; } |
| 105 |
}; |
| 106 |
|
| 107 |
}; |
| 108 |
|
| 109 |
#endif |