00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00049 #ifndef _CPP_MEMORY
00050 #define _CPP_MEMORY 1
00051 
00052 #pragma GCC system_header
00053 
00054 #include <bits/stl_algobase.h>
00055 #include <bits/stl_alloc.h>
00056 #include <bits/stl_construct.h>
00057 #include <bits/stl_iterator_base_types.h> 
00058 #include <bits/stl_uninitialized.h>
00059 #include <bits/stl_raw_storage_iter.h>
00060 
00061 namespace std
00062 {
00063 
00070   template <class _Tp>
00071   pair<_Tp*, ptrdiff_t> 
00072   __get_temporary_buffer(ptrdiff_t __len, _Tp*)
00073   {
00074     if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
00075       __len = INT_MAX / sizeof(_Tp);
00076 
00077     while (__len > 0) {
00078       _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
00079       if (__tmp != 0)
00080     return pair<_Tp*, ptrdiff_t>(__tmp, __len);
00081       __len /= 2;
00082     }
00083 
00084     return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
00085   }
00086 
00102   template <class _Tp>
00103   inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) {
00104     return __get_temporary_buffer(__len, (_Tp*) 0);
00105   }
00106 
00114   template <class _Tp>
00115   void return_temporary_buffer(_Tp* __p) {
00116     std::free(__p);
00117   }
00118 
00119 
00120 template <class _Tp1>
00121   struct auto_ptr_ref
00122 {
00123    _Tp1* _M_ptr;
00124    auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
00125 };
00126 
00130 template <class _Tp>
00131   class auto_ptr
00132 {
00133 private:
00134   _Tp* _M_ptr;
00135 
00136 public:
00137   typedef _Tp element_type;
00138 
00139   explicit auto_ptr(_Tp* __p = 0) throw() : _M_ptr(__p) {}
00140   auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) {}
00141 
00142   template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) throw()
00143     : _M_ptr(__a.release()) {}
00144 
00145   auto_ptr& operator=(auto_ptr& __a) throw() {
00146     reset(__a.release());
00147     return *this;
00148   }
00149 
00150   template <class _Tp1>
00151   auto_ptr& operator=(auto_ptr<_Tp1>& __a) throw() {
00152     reset(__a.release());
00153     return *this;
00154   }
00155   
00156   
00157   
00158   
00159   
00160   ~auto_ptr() { delete _M_ptr; }
00161  
00162   _Tp& operator*() const throw() {
00163     return *_M_ptr;
00164   }
00165   _Tp* operator->() const throw() {
00166     return _M_ptr;
00167   }
00168   _Tp* get() const throw() {
00169     return _M_ptr;
00170   }
00171   _Tp* release() throw() {
00172     _Tp* __tmp = _M_ptr;
00173     _M_ptr = 0;
00174     return __tmp;
00175   }
00176   void reset(_Tp* __p = 0) throw() {
00177     if (__p != _M_ptr) {
00178       delete _M_ptr;
00179       _M_ptr = __p;
00180     }    
00181   }
00182 
00183 public:
00184   auto_ptr(auto_ptr_ref<_Tp> __ref) throw()
00185     : _M_ptr(__ref._M_ptr) {}
00186 
00187   auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) throw() {
00188     if (__ref._M_ptr != this->get()) {
00189       delete _M_ptr;
00190       _M_ptr = __ref._M_ptr;
00191     }
00192     return *this;
00193   }
00194 
00195   template <class _Tp1> operator auto_ptr_ref<_Tp1>() throw() 
00196     { return auto_ptr_ref<_Tp>(this->release()); }
00197   template <class _Tp1> operator auto_ptr<_Tp1>() throw()
00198     { return auto_ptr<_Tp1>(this->release()); }
00199 };
00200 
00201 } 
00202 
00203 #endif 
00204