00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef BT_ALIGNED_ALLOCATOR
00017 #define BT_ALIGNED_ALLOCATOR
00018
00022
00023 #include "btScalar.h"
00024
00025 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
00026
00027 #define btAlignedAlloc(a, b) \
00028 btAlignedAllocInternal(a, b, __LINE__, __FILE__)
00029
00030 #define btAlignedFree(ptr) \
00031 btAlignedFreeInternal(ptr, __LINE__, __FILE__)
00032
00033 void* btAlignedAllocInternal(size_t size, int32_t alignment, int32_t line, char* filename);
00034
00035 void btAlignedFreeInternal(void* ptr, int32_t line, char* filename);
00036
00037 #else
00038 void* btAlignedAllocInternal(size_t size, int32_t alignment);
00039 void btAlignedFreeInternal(void* ptr);
00040
00041 #define btAlignedAlloc(size, alignment) btAlignedAllocInternal(size, alignment)
00042 #define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
00043
00044 #endif
00045 typedef int32_t size_type;
00046
00047 typedef void*(btAlignedAllocFunc)(size_t size, int32_t alignment);
00048 typedef void(btAlignedFreeFunc)(void* memblock);
00049 typedef void*(btAllocFunc)(size_t size);
00050 typedef void(btFreeFunc)(void* memblock);
00051
00053 void btAlignedAllocSetCustom(btAllocFunc* allocFunc, btFreeFunc* freeFunc);
00055 void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc);
00056
00059 template <typename T, unsigned Alignment>
00060 class btAlignedAllocator {
00061
00062 typedef btAlignedAllocator<T, Alignment> self_type;
00063
00064 public:
00065
00066 btAlignedAllocator() {}
00067
00068
00069
00070
00071 template <typename Other>
00072 btAlignedAllocator(const btAlignedAllocator<Other, Alignment>&) {}
00073
00074 typedef const T* const_pointer;
00075 typedef const T& const_reference;
00076 typedef T* pointer;
00077 typedef T& reference;
00078 typedef T value_type;
00079
00080 pointer address(reference ref) const { return &ref; }
00081 const_pointer address(const_reference ref) const { return &ref; }
00082 pointer allocate(size_type n, const_pointer* hint = 0)
00083 {
00084 (void)hint;
00085 return reinterpret_cast<pointer>(btAlignedAlloc(sizeof(value_type) * n, Alignment));
00086 }
00087 void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); }
00088 void deallocate(pointer ptr)
00089 {
00090 btAlignedFree(reinterpret_cast<void*>(ptr));
00091 }
00092 void destroy(pointer ptr) { ptr->~value_type(); }
00093
00094 template <typename O>
00095 struct rebind {
00096 typedef btAlignedAllocator<O, Alignment> other;
00097 };
00098 template <typename O>
00099 self_type& operator=(const btAlignedAllocator<O, Alignment>&) { return *this; }
00100
00101 friend bool operator==(const self_type&, const self_type&) { return true; }
00102 };
00103
00104 #endif //BT_ALIGNED_ALLOCATOR