# 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 {
    00071   template<typename _Tp>
    00072     pair<_Tp*, ptrdiff_t>
    00073     __get_temporary_buffer(ptrdiff_t __len, _Tp*)
    00074     {
    00075       if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
    00076     __len = INT_MAX / sizeof(_Tp);
    00077
    00078       while (__len > 0)
    00079     {
    00080       _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
    00081       if (__tmp != 0)
    00082         return pair<_Tp*, ptrdiff_t>(__tmp, __len);
    00083       __len /= 2;
    00084     }
    00085       return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
    00086     }
    00087
    00103   template<typename _Tp>
    00104     inline pair<_Tp*,ptrdiff_t>
    00105     get_temporary_buffer(ptrdiff_t __len)
    00106     { return __get_temporary_buffer(__len, (_Tp*) 0); }
    00107
    00115   template<typename _Tp>
    00116     void
    00117     return_temporary_buffer(_Tp* __p)
    00118     { std::free(__p); }
    00119
    00127   template<typename _Tp1>
    00128     struct auto_ptr_ref
    00129     {
    00130       _Tp1* _M_ptr;
    00131
    00132       explicit
    00133       auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
    00134     };
    00135
    00136
    00166   template<typename _Tp>
    00167     class auto_ptr
    00168     {
    00169     private:
    00170       _Tp* _M_ptr;
    00171
    00172     public:
    00174       typedef _Tp element_type;
    00175
    00182       explicit
    00183       auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
    00184
    00192       auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
    00193
    00203       template<typename _Tp1>
    00204         auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
    00205
    00214       auto_ptr&
    00215       operator=(auto_ptr& __a) throw()
    00216       {
    00217     reset(__a.release());
    00218     return *this;
    00219       }
    00220
    00231       template<typename _Tp1>
    00232         auto_ptr&
    00233         operator=(auto_ptr<_Tp1>& __a) throw()
    00234         {
    00235       reset(__a.release());
    00236       return *this;
    00237     }
    00238
    00251       ~auto_ptr() { delete _M_ptr; }
    00252
    00261       element_type&
    00262       operator*() const throw() { return *_M_ptr; }
    00263
    00270       element_type*
    00271       operator->() const throw() { return _M_ptr; }
    00272
    00283       element_type*
    00284       get() const throw() { return _M_ptr; }
    00285
    00297       element_type*
    00298       release() throw()
    00299       {
    00300     element_type* __tmp = _M_ptr;
    00301     _M_ptr = 0;
    00302     return __tmp;
    00303       }
    00304
    00312       void
    00313       reset(element_type* __p = 0) throw()
    00314       {
    00315     if (__p != _M_ptr)
    00316       {
    00317         delete _M_ptr;
    00318         _M_ptr = __p;
    00319       }
    00320       }
    00321
    00333       auto_ptr(auto_ptr_ref<element_type> __ref) throw()
    00334       : _M_ptr(__ref._M_ptr) { }
    00335
    00336       auto_ptr&
    00337       operator=(auto_ptr_ref<element_type> __ref) throw()
    00338       {
    00339     if (__ref._M_ptr != this->get())
    00340       {
    00341         delete _M_ptr;
    00342         _M_ptr = __ref._M_ptr;
    00343       }
    00344     return *this;
    00345       }
    00346
    00347       template<typename _Tp1>
    00348         operator auto_ptr_ref<_Tp1>() throw()
    00349         { return auto_ptr_ref<_Tp1>(this->release()); }
    00350
    00351       template<typename _Tp1>
    00352         operator auto_ptr<_Tp1>() throw()
    00353         { return auto_ptr<_Tp1>(this->release()); }
    00355   };
    00356 } // namespace std
    00357
    00358 #endif