/home/ntakagi/work/STLport-5.1.5/stlport/stl/_alloc_old.h

Go to the documentation of this file.
00001 template<class _Tp, class _Alloc>
00002 class __simple_alloc {
00003   typedef _Alloc __alloc_type;
00004 public:
00005   typedef typename _Alloc::value_type __alloc_value_type;
00006   typedef _Tp value_type;
00007   static size_t  _STLP_CALL __chunk(size_t __n) {
00008     return (sizeof(__alloc_value_type)==sizeof(value_type)) ? __n :
00009       ((__n*sizeof(value_type)+sizeof(__alloc_value_type)-1)/sizeof(__alloc_value_type));
00010   }
00011   static _Tp*  _STLP_CALL allocate(size_t __n) { return 0 == __n ? 0 : (_Tp*) __alloc_type::allocate(__chunk(__n)); }
00012   static void  _STLP_CALL deallocate(_Tp * __p, size_t __n) {
00013     __alloc_type::deallocate((__alloc_value_type*)__p, __chunk(__n)); }
00014 };
00015 
00016 // Allocator adaptor to turn an SGI-style allocator (e.g. alloc, malloc_alloc)
00017 // into a standard-conforming allocator.   Note that this adaptor does
00018 // *not* assume that all objects of the underlying alloc class are
00019 // identical, nor does it assume that all of the underlying alloc's
00020 // member functions are static member functions.  Note, also, that
00021 // __allocator<_Tp, alloc> is essentially the same thing as allocator<_Tp>.
00022 
00023 template <class _Tp, class _Alloc>
00024 struct __allocator : public _Alloc {
00025   typedef _Alloc __underlying_alloc;
00026 
00027   typedef size_t    size_type;
00028   typedef ptrdiff_t difference_type;
00029   typedef _Tp*       pointer;
00030   typedef const _Tp* const_pointer;
00031   typedef _Tp&       reference;
00032   typedef const _Tp& const_reference;
00033   typedef _Tp        value_type;
00034 
00035 # if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
00036   template <class _Tp1> struct rebind {
00037     typedef __allocator<_Tp1, _Alloc> other;
00038   };
00039 # endif
00040   __allocator() _STLP_NOTHROW {}
00041   __allocator(const _Alloc& ) _STLP_NOTHROW {}
00042   __allocator(const __allocator<_Tp, _Alloc>& __a) _STLP_NOTHROW
00043     : _Alloc(__a) {}
00044 # if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
00045   template <class _Tp1>
00046   __allocator(const __allocator<_Tp1, _Alloc>& __a) _STLP_NOTHROW
00047     : _Alloc(__a) {}
00048 # endif
00049 # ifdef _STLP_TRIVIAL_DESTRUCTOR_BUG
00050   ~__allocator() _STLP_NOTHROW {}
00051 # endif
00052   pointer address(reference __x) const { return &__x; }
00053 
00054 # if !defined (__WATCOM_CPLUSPLUS__)
00055   const_pointer address(const_reference __x) const { return &__x; }
00056 # endif
00057 
00058   // __n is permitted to be 0.
00059   _Tp* allocate(size_type __n, const void* = 0) {
00060     if (__n > max_size())
00061       __THROW_BAD_ALLOC;
00062     return __n != 0
00063         ? __STATIC_CAST(_Tp*,__underlying_alloc::allocate(__n * sizeof(_Tp)))
00064         : 0;
00065   }
00066 
00067   // __p is not permitted to be a null pointer.
00068   void deallocate(pointer __p, size_type __n)
00069     { if (__p) __underlying_alloc::deallocate(__p, __n * sizeof(_Tp)); }
00070 
00071   size_type max_size() const _STLP_NOTHROW
00072     { return size_t(-1) / sizeof(_Tp); }
00073 
00074   void construct(pointer __p, const_reference __val) { _STLP_STD::_Copy_Construct(__p, __val); }
00075   void destroy(pointer __p) { _STLP_STD::_Destroy(__p); }
00076 
00077   const __underlying_alloc& __get_underlying_alloc() const { return *this; }
00078 };
00079 
00080 #ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
00081 template <class _Alloc>
00082 class __allocator<void, _Alloc> {
00083   typedef size_t      size_type;
00084   typedef ptrdiff_t   difference_type;
00085   typedef void*       pointer;
00086   typedef const void* const_pointer;
00087   typedef void        value_type;
00088 #ifdef _STLP_MEMBER_TEMPLATE_CLASSES
00089   template <class _Tp1> struct rebind {
00090     typedef __allocator<_Tp1, _Alloc> other;
00091   };
00092 #endif
00093 };
00094 #endif
00095 
00096 template <class _Tp, class _Alloc>
00097 inline bool  _STLP_CALL operator==(const __allocator<_Tp, _Alloc>& __a1,
00098                                    const __allocator<_Tp, _Alloc>& __a2)
00099 {
00100   return __a1.__get_underlying_alloc() == __a2.__get_underlying_alloc();
00101 }
00102 
00103 #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
00104 template <class _Tp, class _Alloc>
00105 inline bool  _STLP_CALL operator!=(const __allocator<_Tp, _Alloc>& __a1,
00106                                    const __allocator<_Tp, _Alloc>& __a2)
00107 {
00108   return __a1.__get_underlying_alloc() != __a2.__get_underlying_alloc();
00109 }
00110 #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
00111 
00112 
00113 // Comparison operators for all of the predifined SGI-style allocators.
00114 // This ensures that __allocator<malloc_alloc> (for example) will
00115 // work correctly.
00116 
00117 #ifndef _STLP_NON_TYPE_TMPL_PARAM_BUG
00118 inline bool  _STLP_CALL operator==(const __malloc_alloc&, const __malloc_alloc&)
00119 { return true; }
00120 
00121 #  ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
00122 inline bool  _STLP_CALL operator!=(const __malloc_alloc&, const __malloc_alloc&)
00123 { return false; }
00124 #  endif
00125 
00126 inline bool _STLP_CALL operator==(const __new_alloc&, const __new_alloc&) { return true; }
00127 
00128 #  ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
00129 inline bool _STLP_CALL operator!=(const __new_alloc&, const __new_alloc&) { return false; }
00130 #  endif
00131 
00132 #  if !defined (_STLP_USE_NO_IOSTREAMS)
00133 inline bool  _STLP_CALL operator==(const __node_alloc&,
00134                                    const __node_alloc&)
00135 { return true; }
00136 
00137 #    if defined( _STLP_FUNCTION_TMPL_PARTIAL_ORDER )
00138 
00139 inline bool  _STLP_CALL operator!=(const __node_alloc&,
00140                                    const __node_alloc&)
00141 { return false; }
00142 #    endif
00143 #  endif
00144 
00145 #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
00146 
00147 template <class _Alloc>
00148 inline bool  _STLP_CALL operator==(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&) {  return true; }
00149 # ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
00150 template <class _Alloc>
00151 inline bool  _STLP_CALL operator!=(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&) {  return false; }
00152 # endif
00153 
00154 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
00155 
00156 // Versions for the predefined SGI-style allocators.
00157 template <class _Tp>
00158 struct _Alloc_traits<_Tp, __malloc_alloc> {
00159   typedef __allocator<_Tp, __malloc_alloc> allocator_type;
00160 };
00161 
00162 #  if !defined (_STLP_USE_NO_IOSTREAMS)
00163 template <class _Tp>
00164 struct _Alloc_traits<_Tp, __node_alloc> {
00165   typedef __allocator<_Tp, __node_alloc> allocator_type;
00166 };
00167 #  endif
00168 
00169 template <class _Tp, class _Alloc>
00170 struct _Alloc_traits<_Tp, __debug_alloc<_Alloc> > {
00171   typedef __allocator<_Tp, __debug_alloc<_Alloc> > allocator_type;
00172 };
00173 
00174 // Versions for the __allocator adaptor used with the predefined
00175 // SGI-style allocators.
00176 
00177 template <class _Tp, class _Tp1, class _Alloc>
00178 struct _Alloc_traits<_Tp, __allocator<_Tp1, _Alloc > > {
00179   typedef __allocator<_Tp, _Alloc > allocator_type;
00180 };
00181 
00182 #endif
00183 
00184 #if defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE)
00185 
00186 // Versions for the predefined SGI-style allocators.
00187 
00188 
00189 #  if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
00190 
00191 typedef __malloc_alloc __malloc_alloc_dfl;
00192 
00193 template <class _Tp>
00194 inline __allocator<_Tp, __malloc_alloc_dfl >& _STLP_CALL
00195 __stl_alloc_rebind(__malloc_alloc_dfl& __a, const _Tp*) {
00196   return (__allocator<_Tp, __malloc_alloc_dfl >&)__a;
00197 }
00198 
00199 #    if !defined (_STLP_USE_NO_IOSTREAMS)
00200 template <class _Tp>
00201 inline __allocator<_Tp, __node_alloc>& _STLP_CALL
00202 __stl_alloc_rebind(__node_alloc& __a, const _Tp*) {
00203   return (__allocator<_Tp, __node_alloc>&)__a;
00204 }
00205 #    endif
00206 
00207 template <class _Tp>
00208 inline __allocator<_Tp, __malloc_alloc_dfl > _STLP_CALL
00209 __stl_alloc_create(const __malloc_alloc_dfl&, const _Tp*) {
00210   return __allocator<_Tp, __malloc_alloc_dfl > ();
00211 }
00212 
00213 #    if !defined (_STLP_USE_NO_IOSTREAMS)
00214 template <class _Tp>
00215 inline __allocator<_Tp, __node_alloc> _STLP_CALL
00216 __stl_alloc_create(const __node_alloc&, const _Tp*) {
00217   return __allocator<_Tp, __node_alloc>();
00218 }
00219 
00220 #    endif
00221 
00222 #  else
00223 
00224 template <class _Tp>
00225 inline __allocator<_Tp, __malloc_alloc>& _STLP_CALL
00226 __stl_alloc_rebind(__malloc_alloc& __a, const _Tp*) {
00227   return (__allocator<_Tp, __malloc_alloc>&)__a;
00228 }
00229 
00230 #    if !defined (_STLP_USE_NO_IOSTREAMS)
00231 template <class _Tp>
00232 inline __allocator<_Tp, __node_alloc>& _STLP_CALL
00233 __stl_alloc_rebind(__node_alloc& __a, const _Tp*) {
00234   return (__allocator<_Tp, __node_alloc>&)__a;
00235 }
00236 #    endif
00237 
00238 template <class _Tp>
00239 inline __allocator<_Tp, __malloc_alloc> _STLP_CALL
00240 __stl_alloc_create(const __malloc_alloc&, const _Tp*) {
00241   return __allocator<_Tp, __malloc_alloc>();
00242 }
00243 
00244 #    if !defined (_STLP_USE_NO_IOSTREAMS)
00245 template <class _Tp>
00246 inline __allocator<_Tp, __node_alloc> _STLP_CALL
00247 __stl_alloc_create(const __node_alloc&, const _Tp*) {
00248   return __allocator<_Tp, __node_alloc>();
00249 }
00250 #    endif
00251 
00252 #  endif
00253 
00254 template <class _Tp, class _Alloc>
00255 inline __allocator<_Tp, __debug_alloc<_Alloc> > _STLP_CALL
00256 __stl_alloc_create(const __debug_alloc<_Alloc>&, const _Tp*) {
00257   return __allocator<_Tp, __debug_alloc<_Alloc> >();
00258 }
00259 template <class _Tp, class _Alloc>
00260 inline __allocator<_Tp, __debug_alloc<_Alloc> >& _STLP_CALL
00261 __stl_alloc_rebind(__debug_alloc<_Alloc>& __a, const _Tp*) {
00262   return (__allocator<_Tp, __debug_alloc<_Alloc> >&)__a;
00263 }
00264 
00265 template <class _Tp>
00266 inline __allocator<_Tp, __new_alloc > _STLP_CALL
00267 __stl_alloc_create(const __new_alloc&, const _Tp*) {
00268   return __allocator<_Tp, __new_alloc >();
00269 }
00270 template <class _Tp>
00271 inline __allocator<_Tp, __new_alloc >&  _STLP_CALL
00272 __stl_alloc_rebind(__new_alloc& __a, const _Tp*) {
00273   return (__allocator<_Tp, __new_alloc >&)__a;
00274 }
00275 
00276 template <class _Tp1, class _Alloc, class _Tp2>
00277 inline __allocator<_Tp2, _Alloc>& _STLP_CALL
00278 __stl_alloc_rebind(__allocator<_Tp1, _Alloc>& __a, const _Tp2*) {
00279   return (__allocator<_Tp2, _Alloc>&)__a;
00280 }
00281 
00282 template <class _Tp1, class _Alloc, class _Tp2>
00283 inline __allocator<_Tp2, _Alloc> _STLP_CALL
00284 __stl_alloc_create(const __allocator<_Tp1, _Alloc>&, const _Tp2*) {
00285   return __allocator<_Tp2, _Alloc>();
00286 }
00287 #endif



Generated on Mon Mar 10 15:32:18 2008 by  doxygen 1.5.1