# memory

    00001 // <memory> -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /*
    00031  * Copyright (c) 1997-1999
    00032  * Silicon Graphics Computer Systems, Inc.
    00033  *
    00034  * Permission to use, copy, modify, distribute and sell this software
    00035  * and its documentation for any purpose is hereby granted without fee,
    00036  * provided that the above copyright notice appear in all copies and
    00037  * that both that copyright notice and this permission notice appear
    00038  * in supporting documentation.  Silicon Graphics makes no
    00039  * representations about the suitability of this software for any
    00040  * purpose.  It is provided "as is" without express or implied warranty.
    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> //for iterator_traits
    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   // Note: The C++ standard says there is supposed to be an empty throw
    00157   // specification here, but omitting it is standard conforming.  Its
    00158   // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
    00159   // this is prohibited.
    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 } // namespace std
    00202
    00203 #endif /* _CPP_MEMORY */
    00204