Go to the documentation of this file.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, int alignment,int line,char* filename);
00034
00035 void btAlignedFreeInternal (void* ptr,int line,char* filename);
00036
00037 #else
00038 void* btAlignedAllocInternal (size_t size, int 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 int size_type;
00046
00047 typedef void *(btAlignedAllocFunc)(size_t size, int 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
00057
00060 template < typename T , unsigned Alignment >
00061 class btAlignedAllocator {
00062
00063 typedef btAlignedAllocator< T , Alignment > self_type;
00064
00065 public:
00066
00067
00068 btAlignedAllocator() {}
00069
00070
00071
00072
00073 template < typename Other >
00074 btAlignedAllocator( const btAlignedAllocator< Other , Alignment > & ) {}
00075
00076 typedef const T* const_pointer;
00077 typedef const T& const_reference;
00078 typedef T* pointer;
00079 typedef T& reference;
00080 typedef T value_type;
00081
00082 pointer address ( reference ref ) const { return &ref; }
00083 const_pointer address ( const_reference ref ) const { return &ref; }
00084 pointer allocate ( size_type n , const_pointer * hint = 0 ) {
00085 (void)hint;
00086 return reinterpret_cast< pointer >(btAlignedAlloc( sizeof(value_type) * n , Alignment ));
00087 }
00088 void construct ( pointer ptr , const value_type & value ) { new (ptr) value_type( value ); }
00089 void deallocate( pointer ptr ) {
00090 btAlignedFree( reinterpret_cast< void * >( ptr ) );
00091 }
00092 void destroy ( pointer ptr ) { ptr->~value_type(); }
00093
00094
00095 template < typename O > 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
00105
00106 #endif //BT_ALIGNED_ALLOCATOR
00107