Back to index

supertuxkart  0.5+dfsg1
btAlignedAllocator.h
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose,
00008 including commercial applications, and to alter it and redistribute it freely,
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #ifndef BT_ALIGNED_ALLOCATOR
00017 #define BT_ALIGNED_ALLOCATOR
00018 
00022 
00023 #include "btScalar.h"
00024 //#define BT_DEBUG_MEMORY_ALLOCATIONS 1
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(a,b) btAlignedAllocInternal(a,b)
00042        #define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
00043 #endif
00044 typedef int   size_type;
00045 
00046 
00047 template < typename T , unsigned Alignment >
00048 class btAlignedAllocator {
00049        
00050        typedef btAlignedAllocator< T , Alignment > self_type;
00051        
00052 public:
00053 
00054        //just going down a list:
00055        btAlignedAllocator() {}
00056        /*
00057        btAlignedAllocator( const self_type & ) {}
00058        */
00059 
00060        template < typename Other >
00061        btAlignedAllocator( const btAlignedAllocator< Other , Alignment > & ) {}
00062 
00063        typedef const T*         const_pointer;
00064        typedef const T&         const_reference;
00065        typedef T*               pointer;
00066        typedef T&               reference;
00067        typedef T                value_type;
00068 
00069        pointer       address   ( reference        ref ) const                           { return &ref; }
00070        const_pointer address   ( const_reference  ref ) const                           { return &ref; }
00071        pointer       allocate  ( size_type        n   , const_pointer *      hint = 0 ) {
00072               (void)hint;
00073               return reinterpret_cast< pointer >(btAlignedAlloc( sizeof(value_type) * n , Alignment ));
00074        }
00075        void          construct ( pointer          ptr , const value_type &   value    ) { new (ptr) value_type( value ); }
00076        void          deallocate( pointer          ptr ) {
00077               btAlignedFree( reinterpret_cast< void * >( ptr ) );
00078        }
00079        void          destroy   ( pointer          ptr )                                 { ptr->~value_type(); }
00080        
00081 
00082        template < typename O > struct rebind {
00083               typedef btAlignedAllocator< O , Alignment > other;
00084        };
00085        template < typename O >
00086        self_type & operator=( const btAlignedAllocator< O , Alignment > & ) { return *this; }
00087 
00088        friend bool operator==( const self_type & , const self_type & ) { return true; }
00089 };
00090 
00091 
00092 
00093 #endif //BT_ALIGNED_ALLOCATOR
00094