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

Generated on Sat Mar 19 18:32:18 2005 for libstdc++-v3 Source by  doxygen 1.4.1