SourceForge.jp

allocator.h

説明を見る。
00001 
00009 #ifndef _dkutil_allocator__
00010 #define _dkutil_allocator__
00011 
00012 
00013 
00014 
00015 #define DKUTIL_BAKAMON "ERROR:バカモン!NULLのポインタを渡して開放しようとするな!"
00016 
00017 
00018 
00019 
00020 namespace dkutil{
00021 
00022 template<class T>
00023 struct dkutil_allocator_base{
00024     typedef size_t    size_type;
00025   typedef ptrdiff_t difference_type;
00026   typedef T*        pointer;
00027   typedef const T*  const_pointer;
00028   typedef T&        reference;
00029   typedef const T&  const_reference;
00030   typedef T         value_type;
00031 
00032     template<class Tt_> 
00033     Tt_ * _Allocate( size_type size, Tt_ * )
00034     {
00035         //if( size < 0 ) size = 0;
00036         Tt_ * pT = ( Tt_ * ) operator new( size * sizeof( Tt_ ) );
00037         return pT;
00038     }
00039     void _Deallocate( pointer p, size_type n = 0)
00040     {
00041         operator delete(p);
00042     }
00043 
00044     T *allocate_base(size_type n, const void *hint)
00045     {
00046         return (pointer)(operator new(n * sizeof(T)));
00047     }
00048     
00049 
00050     template<class T1, class T2>
00051     void _Construct( T1 * pT1, const T2 & rT2 )
00052     {
00053         new ( ( void * ) pT1 ) T1( rT2 );
00054     }
00055 
00056     char * _Charalloc( size_type size )
00057     {
00058         T * pT = _Allocate( size, ( char * ) 0 );
00059         return pT;
00060     }
00061 
00062     template<class Ty_>
00063     void _Destroy( Ty_ *p ) {       p->~Ty_();  }
00064 
00065 };
00066 
00067 
00077 template <class T > 
00078 class dkutil_allocator : public dkutil_allocator_base<T>{
00079 public:
00080 
00081   template <class U>
00082         struct rebind { typedef dkutil_allocator<U> other; };
00083     
00084         pointer       address(reference x) const
00085     { return &x; }
00086   const_pointer address(const_reference x) const
00087     { return &x; }
00088   pointer       allocate(size_type n, const void* hint = 0) {
00089     //cout << "allocate " << n << " of " << typeid(T).name() << endl;
00090     //return a.allocate(n * sizeof(value_type));//new(n * sizeof(value_type));
00091         
00092         //dkutil_allocator_base
00093         return _Allocate(n, ( pointer ) 0 );
00094         
00095     }
00096   void deallocate(pointer p, size_type n = 0) {
00097     //cout << "deallocate " << n << " of " << typeid(T).name() << endl;
00098     //if( (p == 0) == (n == 0) ) MB("WHAT!!! deallocate error");
00099         if(p==NULL){
00100             
00101             //DEBUGASSERT(!DKUTIL_BAKAMON);
00102             //DEBUGMB("WHAT!!! deallocate error please break.!!");
00103         }
00104         _Deallocate(p);
00105     //a.deallocate(p);
00106   }
00107   size_type max_size() const
00108     { return (size_t)-1 / sizeof(T); }
00109   void construct(pointer p, const T& val) {
00110     //cout << "construct " << typeid(T).name()<< '(' << val << ')' << endl;
00111     new ((void*)p) T(val);
00112   }
00113   void destroy(pointer p) {
00114    // cout << "destroy " << typeid(T).name() << endl;
00115         ((T*)p)->~T();
00116   }
00117 
00118 
00119     //STLPort用
00120 
00122     template <class _Tp1, class _Tp2>
00123         inline dkutil_allocator<_Tp2>& __stl_alloc_rebind(dkutil_allocator<_Tp1>& __a, const _Tp2*) 
00124     {
00125         return (dkutil_allocator<_Tp2>&)(__a);
00126     }
00128     template <class _Tp1, class _Tp2>
00129     inline dkutil_allocator<_Tp2> __stl_alloc_create(
00130             const dkutil_allocator<_Tp1>&, const _Tp2*
00131     ){ 
00132         return dkutil_allocator<_Tp2>(); 
00133     }
00134 
00135 
00136 };
00137 
00138 
00139 
00140 //template <class T, class U>
00141 //bool operator!=(const my_allocator<T>& a,const my_allocator<U>& b);
00142 
00143 
00144 template<class T>
00145 class throw_allocator {
00146 public:
00147      typedef size_t    size_type;
00148   typedef ptrdiff_t difference_type;
00149   typedef T*        pointer;
00150   typedef const T*  const_pointer;
00151   typedef T&        reference;
00152   typedef const T&  const_reference;
00153   typedef T         value_type;
00154 
00155     template <class U>
00156         struct rebind { typedef throw_allocator<U> other; };
00157 
00158     pointer address(reference x) const{ return &x; }
00159     const_pointer address(const_reference x) const{ return &x; }
00160 
00161     throw_allocator(){}
00162     throw_allocator(const throw_allocator<T> &){}
00163     template<class T2> throw_allocator<T>& operator=(const throw_allocator<T2> &){}
00164     pointer allocate(size_type n, const void *hint){
00165         pointer p = (pointer)(operator new(n * sizeof(T)));
00166         if(!p){ throw std::bad_alloc();}
00167     }
00168     char* _Charalloc(size_type n) {
00169     return allocate(n);
00170   }
00171     void deallocate(pointer p, size_type n){
00172         if(!p){DEBUGMB(DKUTIL_BAKAMON );}
00173         operator delete(p); 
00174     }
00175     void construct(pointer p, const T& val){
00176         new ((void *)p) T(val); 
00177     }
00178     void destroy(pointer p){
00179         p->~T(); 
00180     }
00181     size_type max_size() const{
00182         size_type c = (size_type)(-1) / sizeof(T);
00183         return (0<c ? c : 1);
00184     }
00185 
00186 
00188     template <class _Tp1, class _Tp2>
00189         inline throw_allocator<_Tp2>& __stl_alloc_rebind(throw_allocator<_Tp1>& __a, const _Tp2*) 
00190     {
00191         return (throw_allocator<_Tp2>&)(__a);
00192     }
00194     template <class _Tp1, class _Tp2>
00195     inline throw_allocator<_Tp2> __stl_alloc_create(
00196             const throw_allocator<_Tp1>&, const _Tp2*
00197     ){ 
00198         return throw_allocator<_Tp2>(); 
00199     }
00200 };
00201 
00202 
00203 }//end of dkutil namespace
00204 
00205 #undef DKUTIL_BAKAMON 
00206 
00207 #endif//end of include once

dkutil 1.02リリース前 d金魚専用マニュアルバージョンに対してSun Dec 28 21:23:07 2003に生成されました。 doxygen 1.3.5