# memory

Go to the documentation of this file.

    00001 // <memory> -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2004 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
    00044 /** @file
    00045  *  This is a Standard C++ Library header.
    00046  */
    00047
    00048 #ifndef _GLIBCXX_MEMORY
    00049 #define _GLIBCXX_MEMORY 1
    00050
    00051 #pragma GCC system_header
    00052
    00053 #include <bits/stl_algobase.h>
    00054 #include <bits/allocator.h>
    00055 #include <bits/stl_construct.h>
    00056 #include <bits/stl_iterator_base_types.h> //for iterator_traits
    00057 #include <bits/stl_uninitialized.h>
    00058 #include <bits/stl_raw_storage_iter.h>
    00059 #include <debug/debug.h>
    00060 #include <limits>
    00061
    00062 namespace std
    00063 {
    00064   /**
    00065    *  @if maint
    00066    *  This is a helper function.  The unused second parameter exists to
    00067    *  permit the real get_temporary_buffer to use template parameter deduction.
    00068    *
    00069    *  XXX This should perhaps use the pool.
    00070    *  @endif
    00071    */
    00072   template<typename _Tp>
    00073     pair<_Tp*, ptrdiff_t>
    00074     __get_temporary_buffer(ptrdiff_t __len, _Tp*)
    00075     {
    00076       const ptrdiff_t __max = numeric_limits<ptrdiff_t>::max() / sizeof(_Tp);
    00077       if (__len > __max)
    00078     __len = __max;
    00079
    00080       while (__len > 0)
    00081     {
    00082       _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
    00083                             nothrow));
    00084       if (__tmp != 0)
    00085         return pair<_Tp*, ptrdiff_t>(__tmp, __len);
    00086       __len /= 2;
    00087     }
    00088       return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
    00089     }
    00090
    00091   /**
    00092    *  @brief Allocates a temporary buffer.
    00093    *  @param  len  The number of objects of type Tp.
    00094    *  @return See full description.
    00095    *
    00096    *  Reinventing the wheel, but this time with prettier spokes!
    00097    *
    00098    *  This function tries to obtain storage for @c len adjacent Tp
    00099    *  objects.  The objects themselves are not constructed, of course.
    00100    *  A pair<> is returned containing "the buffer s address and
    00101    *  capacity (in the units of sizeof(Tp)), or a pair of 0 values if
    00102    *  no storage can be obtained."  Note that the capacity obtained
    00103    *  may be less than that requested if the memory is unavailable;
    00104    *  you should compare len with the .second return value.
    00105    *
    00106    * Provides the nothrow exception guarantee.
    00107    */
    00108   template<typename _Tp>
    00109     inline pair<_Tp*, ptrdiff_t>
    00110     get_temporary_buffer(ptrdiff_t __len)
    00111     { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); }
    00112
    00113   /**
    00114    *  @brief The companion to get_temporary_buffer().
    00115    *  @param  p  A buffer previously allocated by get_temporary_buffer.
    00116    *  @return   None.
    00117    *
    00118    *  Frees the memory pointed to by p.
    00119    */
    00120   template<typename _Tp>
    00121     void
    00122     return_temporary_buffer(_Tp* __p)
    00123     { ::operator delete(__p, nothrow); }
    00124
    00125   /**
    00126    *  A wrapper class to provide auto_ptr with reference semantics.
    00127    *  For example, an auto_ptr can be assigned (or constructed from)
    00128    *  the result of a function which returns an auto_ptr by value.
    00129    *
    00130    *  All the auto_ptr_ref stuff should happen behind the scenes.
    00131    */
    00132   template<typename _Tp1>
    00133     struct auto_ptr_ref
    00134     {
    00135       _Tp1* _M_ptr;
    00136
    00137       explicit
    00138       auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
    00139     };
    00140
    00141
    00142   /**
    00143    *  @brief  A simple smart pointer providing strict ownership semantics.
    00144    *
    00145    *  The Standard says:
    00146    *  <pre>
    00147    *  An @c auto_ptr owns the object it holds a pointer to.  Copying
    00148    *  an @c auto_ptr copies the pointer and transfers ownership to the
    00149    *  destination.  If more than one @c auto_ptr owns the same object
    00150    *  at the same time the behavior of the program is undefined.
    00151    *
    00152    *  The uses of @c auto_ptr include providing temporary
    00153    *  exception-safety for dynamically allocated memory, passing
    00154    *  ownership of dynamically allocated memory to a function, and
    00155    *  returning dynamically allocated memory from a function.  @c
    00156    *  auto_ptr does not meet the CopyConstructible and Assignable
    00157    *  requirements for Standard Library <a
    00158    *  href="tables.html#65">container</a> elements and thus
    00159    *  instantiating a Standard Library container with an @c auto_ptr
    00160    *  results in undefined behavior.
    00161    *  </pre>
    00162    *  Quoted from [20.4.5]/3.
    00163    *
    00164    *  Good examples of what can and cannot be done with auto_ptr can
    00165    *  be found in the libstdc++ testsuite.
    00166    *
    00167    *  @if maint
    00168    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
    00169    *  127.  auto_ptr<> conversion issues
    00170    *  These resolutions have all been incorporated.
    00171    *  @endif
    00172    */
    00173   template<typename _Tp>
    00174     class auto_ptr
    00175     {
    00176     private:
    00177       _Tp* _M_ptr;
    00178
    00179     public:
    00180       /// The pointed-to type.
    00181       typedef _Tp element_type;
    00182
    00183       /**
    00184        *  @brief  An %auto_ptr is usually constructed from a raw pointer.
    00185        *  @param  p  A pointer (defaults to NULL).
    00186        *
    00187        *  This object now @e owns the object pointed to by @a p.
    00188        */
    00189       explicit
    00190       auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
    00191
    00192       /**
    00193        *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
    00194        *  @param  a  Another %auto_ptr of the same type.
    00195        *
    00196        *  This object now @e owns the object previously owned by @a a,
    00197        *  which has given up ownsership.
    00198        */
    00199       auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
    00200
    00201       /**
    00202        *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
    00203        *  @param  a  Another %auto_ptr of a different but related type.
    00204        *
    00205        *  A pointer-to-Tp1 must be convertible to a
    00206        *  pointer-to-Tp/element_type.
    00207        *
    00208        *  This object now @e owns the object previously owned by @a a,
    00209        *  which has given up ownsership.
    00210        */
    00211       template<typename _Tp1>
    00212         auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
    00213
    00214       /**
    00215        *  @brief  %auto_ptr assignment operator.
    00216        *  @param  a  Another %auto_ptr of the same type.
    00217        *
    00218        *  This object now @e owns the object previously owned by @a a,
    00219        *  which has given up ownsership.  The object that this one @e
    00220        *  used to own and track has been deleted.
    00221        */
    00222       auto_ptr&
    00223       operator=(auto_ptr& __a) throw()
    00224       {
    00225     reset(__a.release());
    00226     return *this;
    00227       }
    00228
    00229       /**
    00230        *  @brief  %auto_ptr assignment operator.
    00231        *  @param  a  Another %auto_ptr of a different but related type.
    00232        *
    00233        *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
    00234        *
    00235        *  This object now @e owns the object previously owned by @a a,
    00236        *  which has given up ownsership.  The object that this one @e
    00237        *  used to own and track has been deleted.
    00238        */
    00239       template<typename _Tp1>
    00240         auto_ptr&
    00241         operator=(auto_ptr<_Tp1>& __a) throw()
    00242         {
    00243       reset(__a.release());
    00244       return *this;
    00245     }
    00246
    00247       /**
    00248        *  When the %auto_ptr goes out of scope, the object it owns is
    00249        *  deleted.  If it no longer owns anything (i.e., @c get() is
    00250        *  @c NULL), then this has no effect.
    00251        *
    00252        *  @if maint
    00253        *  The C++ standard says there is supposed to be an empty throw
    00254        *  specification here, but omitting it is standard conforming.  Its
    00255        *  presence can be detected only if _Tp::~_Tp() throws, but this is
    00256        *  prohibited.  [17.4.3.6]/2
    00257        *  @endif
    00258        */
    00259       ~auto_ptr() { delete _M_ptr; }
    00260
    00261       /**
    00262        *  @brief  Smart pointer dereferencing.
    00263        *
    00264        *  If this %auto_ptr no longer owns anything, then this
    00265        *  operation will crash.  (For a smart pointer, "no longer owns
    00266        *  anything" is the same as being a null pointer, and you know
    00267        *  what happens when you dereference one of those...)
    00268        */
    00269       element_type&
    00270       operator*() const throw()
    00271       {
    00272     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
    00273     return *_M_ptr;
    00274       }
    00275
    00276       /**
    00277        *  @brief  Smart pointer dereferencing.
    00278        *
    00279        *  This returns the pointer itself, which the language then will
    00280        *  automatically cause to be dereferenced.
    00281        */
    00282       element_type*
    00283       operator->() const throw()
    00284       {
    00285     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
    00286     return _M_ptr;
    00287       }
    00288
    00289       /**
    00290        *  @brief  Bypassing the smart pointer.
    00291        *  @return  The raw pointer being managed.
    00292        *
    00293        *  You can get a copy of the pointer that this object owns, for
    00294        *  situations such as passing to a function which only accepts
    00295        *  a raw pointer.
    00296        *
    00297        *  @note  This %auto_ptr still owns the memory.
    00298        */
    00299       element_type*
    00300       get() const throw() { return _M_ptr; }
    00301
    00302       /**
    00303        *  @brief  Bypassing the smart pointer.
    00304        *  @return  The raw pointer being managed.
    00305        *
    00306        *  You can get a copy of the pointer that this object owns, for
    00307        *  situations such as passing to a function which only accepts
    00308        *  a raw pointer.
    00309        *
    00310        *  @note  This %auto_ptr no longer owns the memory.  When this object
    00311        *  goes out of scope, nothing will happen.
    00312        */
    00313       element_type*
    00314       release() throw()
    00315       {
    00316     element_type* __tmp = _M_ptr;
    00317     _M_ptr = 0;
    00318     return __tmp;
    00319       }
    00320
    00321       /**
    00322        *  @brief  Forcibly deletes the managed object.
    00323        *  @param  p  A pointer (defaults to NULL).
    00324        *
    00325        *  This object now @e owns the object pointed to by @a p.  The
    00326        *  previous object has been deleted.
    00327        */
    00328       void
    00329       reset(element_type* __p = 0) throw()
    00330       {
    00331     if (__p != _M_ptr)
    00332       {
    00333         delete _M_ptr;
    00334         _M_ptr = __p;
    00335       }
    00336       }
    00337
    00338       /**
    00339        *  @brief  Automatic conversions
    00340        *
    00341        *  These operations convert an %auto_ptr into and from an auto_ptr_ref
    00342        *  automatically as needed.  This allows constructs such as
    00343        *  @code
    00344        *    auto_ptr<Derived>  func_returning_auto_ptr(.....);
    00345        *    ...
    00346        *    auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
    00347        *  @endcode
    00348        */
    00349       auto_ptr(auto_ptr_ref<element_type> __ref) throw()
    00350       : _M_ptr(__ref._M_ptr) { }
    00351
    00352       auto_ptr&
    00353       operator=(auto_ptr_ref<element_type> __ref) throw()
    00354       {
    00355     if (__ref._M_ptr != this->get())
    00356       {
    00357         delete _M_ptr;
    00358         _M_ptr = __ref._M_ptr;
    00359       }
    00360     return *this;
    00361       }
    00362
    00363       template<typename _Tp1>
    00364         operator auto_ptr_ref<_Tp1>() throw()
    00365         { return auto_ptr_ref<_Tp1>(this->release()); }
    00366
    00367       template<typename _Tp1>
    00368         operator auto_ptr<_Tp1>() throw()
    00369         { return auto_ptr<_Tp1>(this->release()); }
    00370   };
    00371 } // namespace std
    00372
    00373 #endif /* _GLIBCXX_MEMORY */