memory

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

Generated on Fri May 6 01:09:02 2005 for libstdc++-v3 Source by  doxygen 1.4.2