📄 allocators.h
字号:
/*************************************************************************** * * Oxford Semiconductor Proprietary and Confidential Information * * Copyright: Oxford Semiconductor Ltd, 2003 * * Description: * * {{{ * $Log: Allocators.h,v $ * Revision 1.3 2004/11/09 10:20:22 ford * removed Syslib dependancy. * * Revision 1.2 2004/11/04 14:06:02 ford * removed rtl dependacny. * * Revision 1.1 2004/11/04 12:54:59 ford * unicode changes. * * * }}} * ***************************************************************************/#ifndef ALLOCATORS_H#define ALLOCATORS_H#include "PoolableMemory.h"#include <cstdlib>#include <memory>#include "Loki/TypeManip.h"//#include "SysLib/xdbg.h"extern "C" { void *malloc_aligned(size_t size, unsigned int powerOfTwo);}namespace oxsemi{ namespace allocator { /* template< typename T > class default_allocator { protected: public: static T* allocate(size_t iSize) { return new T[iSize]; } static void deallocate(T* p) { delete [] p; } }; */ // ===================================================================== // ===================================================================== // ===================================================================== // Default ============================================================= template< class T > class Default { public: typedef T value_type; typedef size_t size_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; static inline pointer allocate( size_type iNum ); static inline void deallocate( pointer pAlloc ); }; // ===================================================================== template<class T> inline typename Default<T>::pointer Default<T>::allocate( size_type iNum ) // {{{ { // I'm not sure how this guarentees correct alignment, but we'll let // that pass for now! //return static_cast<pointer>( std::malloc(sizeof(T) * iNum) ); //TRACE("Default<T>::allocate(%u x %u)", iNum, sizeof(T) ); //oxsemi::debug::backtrace(6); return static_cast<pointer>( ::operator new(sizeof(T)*iNum) ); //TRACE(".\n"); } // }}} template<class T> inline void Default<T>::deallocate( typename Default<T>::pointer pAlloc ) // {{{ { //TRACE("Default<T>::deallocate(? x %u)", sizeof(T) ); //std::free( pAlloc ); ::operator delete( static_cast< void* >(pAlloc) ); //TRACE(".\n"); } // }}} // ===================================================================== // ===================================================================== // ===================================================================== // DefaultTrace ======================================================== /* template< class T > class DefaultTrace // move out of utils. { public: typedef T value_type; typedef size_t size_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; static inline pointer allocate( size_type iNum ); static inline void deallocate( pointer pAlloc ); }; // ===================================================================== template<class T> inline typename DefaultTrace<T>::pointer DefaultTrace<T>::allocate( size_type iNum ) // {{{ { // I'm not sure how this guarentees correct alignment, but we'll let // that pass for now! //return static_cast<pointer>( std::malloc(sizeof(T) * iNum) ); T* pAlloc = static_cast<pointer>( ::operator new(sizeof(T)*iNum) ); TRACE("Default<T>::allocate(%u) @ 0x%08x", iNum, pAlloc ); oxsemi::debug::backtrace(4); return pAlloc; //TRACE(".\n"); } // }}} template<class T> inline void DefaultTrace<T>::deallocate( typename DefaultTrace<T>::pointer pAlloc ) // {{{ { TRACE("Default<T>::deallocate(? ) @ 0x%08x", pAlloc ); oxsemi::debug::backtrace(4); //std::free( pAlloc ); ::operator delete( static_cast< void* >(pAlloc) ); //TRACE(".\n"); } // }}} */ // ===================================================================== // ===================================================================== // ===================================================================== // DynamicallyAligned ================================================== template< class T > class DynamicallyAligned { public: typedef T value_type; typedef size_t size_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; static inline pointer allocate( size_type n, unsigned alignmentPow2 = 0 ); static inline void deallocate( pointer p, size_type n ); }; // ===================================================================== template<class T> inline typename DynamicallyAligned<T>::pointer DynamicallyAligned<T>::allocate( size_type n, unsigned alignmentPow2 ) // {{{ { return reinterpret_cast<pointer>(malloc_aligned(sizeof(T) * n, alignmentPow2)); } // }}} template<class T> inline void DynamicallyAligned<T>::deallocate( typename DynamicallyAligned<T>::pointer p, size_type n ) // {{{ { free(p); } // }}} // ===================================================================== // ===================================================================== // ===================================================================== // FixedPool =========================================================== /** * @class FixedPool * Implements static allocate and deallocate methods. On Comming into scope * the instance will allocate 'POOLSIZE' bytes on the heap, and limit all * transactions to space within that pool. * @param T is the type that is stored in the pool. * @param POOLSIZE is a value that determines the size of the initial * pool allocation, in terms of the number of 'T's that will fit into it. * @param Instance is an optional parameter that can be given a unique * type to force a new instantiation of the allocator, eg when two equaly * sized but distinct pools, both of type T are needed. */ template< class T, int POOLSIZE, typename Instance = Loki::Int2Type<0> > class FixedPool { public: typedef T value_type; typedef size_t size_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; static inline pointer allocate( size_type iNum ); static inline void deallocate( pointer pAlloc ); private: // pool instance... static void* mpMemoryPool_; }; // ===================================================================== // pool instance... template< class T, int POOLSIZE, typename Instance > void* FixedPool<T,POOLSIZE,Instance>::mpMemoryPool_ = static_cast< T* >( ::operator new(sizeof(T)*POOLSIZE) ); template< class T, int POOLSIZE, typename Instance > inline typename FixedPool<T,POOLSIZE,Instance>::pointer FixedPool<T,POOLSIZE,Instance>::allocate( size_type iNum ) // {{{ { return 0; } // }}} template< class T, int POOLSIZE, typename Instance > inline void FixedPool<T,POOLSIZE,Instance>::deallocate( typename FixedPool<T,POOLSIZE,Instance>::pointer pAlloc ) // {{{ { ; } // }}} } // end namespace allocator } // end namespace oxsemi#endif // ALLOCATORS_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -