# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

stl_block_allocator.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_STL_BLOCK_ALLOCATOR_H
00027 #define NL_STL_BLOCK_ALLOCATOR_H
00028 
00029 
00030 #include "nel/misc/types_nl.h"
00031 #include "nel/misc/block_memory.h"
00032 
00033 
00034 namespace NLMISC {
00035 
00036 
00037 // ***************************************************************************
00067 #ifdef NL_OS_WINDOWS
00068 
00069 template<class T>
00070 class CSTLBlockAllocator
00071 {
00072 public:
00073 
00075         // @{
00076 
00077         typedef T                       value_type;
00078         typedef value_type *pointer;
00079         typedef const T*        const_pointer;
00080         typedef T&                      reference;
00081         typedef const T&        const_reference;
00082         typedef size_t          size_type;
00083         typedef ptrdiff_t       difference_type;
00084 
00085 
00086         pointer adress(reference x) const
00087         {
00088                 return &x;
00089         }
00090 
00091         const_pointer   adress(const_reference x) const
00092         {
00093                 return &x;
00094         }
00095 
00096         void    construct(pointer p, const T &val)
00097         {
00098                 new (p) T(val);
00099         }
00100 
00101         void    destroy(pointer p)
00102         {
00103                 p->T::~T();
00104         }
00105 
00106         // @}
00107 
00108 public:
00109 
00111         CSTLBlockAllocator(CBlockMemory<T, false> *bm) : _BlockMemory(bm)
00112         {
00113         }
00115         CSTLBlockAllocator(const CSTLBlockAllocator<T> &other) : _BlockMemory(other._BlockMemory)
00116         {
00117                 // just copy the block memory from the other allocator.
00118         }
00120         ~CSTLBlockAllocator()
00121         {
00122                 _BlockMemory= NULL;
00123         }
00124 
00125 
00126         pointer allocate(size_type n, const_pointer hint= NULL)
00127         {
00128                 if(n==0)
00129                         return NULL;
00130                 // If sizeof 1, use CBlockMemory allocation
00131                 if(n==1)
00132                 {
00133 #ifdef NL_DEBUG
00134                         // verify that we allocate with good size!! (verify __stl_alloc_rebind scheme).
00135                         // ie an allocator can be used only to allocate a kind of element
00136                         uint    eltSize= std::max(sizeof(T), sizeof(void*));
00137                         nlassert( eltSize == _BlockMemory->__stl_alloc_getEltSize() );
00138 #endif
00139                         // and allocate.
00140                         return _BlockMemory->allocate();
00141                 }
00142                 // else use std malloc
00143                 else
00144                         return (T*)new uint8[n*sizeof(T)];
00145         }
00146 
00147         void    deallocate(void *p, size_type n)
00148         {
00149                 if(n==0)
00150                         return;
00151                 // If sizeof 1, use CBlockMemory allocation
00152                 if(n==1)
00153                         _BlockMemory->free((T*)p);
00154                 // else use std free
00155                 else
00156                         delete [] ((uint8*)p);
00157         }
00158 
00159 
00160         template <class _Tp, class U>
00161         CSTLBlockAllocator<U>& __stl_alloc_rebind(CSTLBlockAllocator<_Tp>& __a, const U*)
00162         {
00163                 // must change the internal eltSize of __a.
00164                 __a._BlockMemory->__stl_alloc_changeEltSize(sizeof(U));
00165                 // and just typecast/return him
00166                 return (CSTLBlockAllocator<U>&)(__a); 
00167         }
00168 
00169         template <class _Tp, class U>
00170         CSTLBlockAllocator<U> __stl_alloc_create(const CSTLBlockAllocator<_Tp>&, const U*)
00171         {
00172                 return CSTLBlockAllocator<U>();
00173         }
00174 
00175 
00176 
00177 // *******************
00178 private:
00179 
00180         // The blockMemory used to allocate elements
00181         CBlockMemory<T, false>          *_BlockMemory;
00182 
00183 };
00184 
00185 
00186 #else // NL_OS_WINDOWS
00187 
00189 
00190 # if !defined (__STL_USE_SGI_ALLOCATORS)
00191 template<class T>
00192 class CSTLBlockAllocator : public  std::allocator< T >
00193 {
00194 public:
00195 
00197         CSTLBlockAllocator(CBlockMemory<T, false> *bm)
00198         {
00199         }
00201         CSTLBlockAllocator(const CSTLBlockAllocator<T> &other) : std::allocator<T>(other)
00202         {
00203         }
00205         ~CSTLBlockAllocator()
00206         {
00207         }
00208 
00209 };
00210 # else  // !defined (__STL_USE_SGI_ALLOCATORS)
00211 class CSTLBlockAllocator : public  __sgi_alloc
00212 {
00213 public:
00214 
00216         CSTLBlockAllocator(CBlockMemory<T, false> *bm)
00217         {
00218         }
00220         CSTLBlockAllocator(const CSTLBlockAllocator<T> &other) : __sgi_alloc(other)
00221         {
00222         }
00224         ~CSTLBlockAllocator()
00225         {
00226         }
00227 
00228 };
00229 # endif // !defined (__STL_USE_SGI_ALLOCATORS)
00230 
00231 
00232 #endif // NL_OS_WINDOWS
00233 
00234 
00235 
00236 } // NLMISC
00237 
00238 
00239 #endif // NL_STL_BLOCK_ALLOCATOR_H
00240 
00241 /* End of stl_block_allocator.h */