libstdc++
stl_tempbuf.h
Go to the documentation of this file.
00001 // Temporary buffer implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  *
00027  * Copyright (c) 1994
00028  * Hewlett-Packard Company
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Hewlett-Packard Company makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  *
00039  * Copyright (c) 1996,1997
00040  * Silicon Graphics Computer Systems, Inc.
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Silicon Graphics makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  */
00050 
00051 /** @file bits/stl_tempbuf.h
00052  *  This is an internal header file, included by other library headers.
00053  *  Do not attempt to use it directly. @headername{memory}
00054  */
00055 
00056 #ifndef _STL_TEMPBUF_H
00057 #define _STL_TEMPBUF_H 1
00058 
00059 #include <bits/stl_algobase.h>
00060 #include <bits/stl_construct.h>
00061 
00062 namespace std _GLIBCXX_VISIBILITY(default)
00063 {
00064 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00065 
00066   /**
00067    *  @brief Allocates a temporary buffer.
00068    *  @param  __len  The number of objects of type Tp.
00069    *  @return See full description.
00070    *
00071    *  Reinventing the wheel, but this time with prettier spokes!
00072    *
00073    *  This function tries to obtain storage for @c __len adjacent Tp
00074    *  objects.  The objects themselves are not constructed, of course.
00075    *  A pair<> is returned containing <em>the buffer s address and
00076    *  capacity (in the units of sizeof(_Tp)), or a pair of 0 values if
00077    *  no storage can be obtained.</em>  Note that the capacity obtained
00078    *  may be less than that requested if the memory is unavailable;
00079    *  you should compare len with the .second return value.
00080    *
00081    * Provides the nothrow exception guarantee.
00082    */
00083   template<typename _Tp>
00084     pair<_Tp*, ptrdiff_t>
00085     get_temporary_buffer(ptrdiff_t __len) _GLIBCXX_NOEXCEPT
00086     {
00087       const ptrdiff_t __max =
00088         __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
00089       if (__len > __max)
00090         __len = __max;
00091       
00092       while (__len > 0) 
00093         {
00094           _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), 
00095                                                         std::nothrow));
00096           if (__tmp != 0)
00097             return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
00098           __len /= 2;
00099         }
00100       return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
00101     }
00102 
00103   /**
00104    *  @brief The companion to get_temporary_buffer().
00105    *  @param  __p  A buffer previously allocated by get_temporary_buffer.
00106    *  @return   None.
00107    *
00108    *  Frees the memory pointed to by __p.
00109    */
00110   template<typename _Tp>
00111     inline void
00112     return_temporary_buffer(_Tp* __p)
00113     { ::operator delete(__p, std::nothrow); }
00114 
00115 
00116   /**
00117    *  This class is used in two places: stl_algo.h and ext/memory,
00118    *  where it is wrapped as the temporary_buffer class.  See
00119    *  temporary_buffer docs for more notes.
00120    */
00121   template<typename _ForwardIterator, typename _Tp>
00122     class _Temporary_buffer
00123     {
00124       // concept requirements
00125       __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
00126 
00127     public:
00128       typedef _Tp         value_type;
00129       typedef value_type* pointer;
00130       typedef pointer     iterator;
00131       typedef ptrdiff_t   size_type;
00132 
00133     protected:
00134       size_type  _M_original_len;
00135       size_type  _M_len;
00136       pointer    _M_buffer;
00137 
00138     public:
00139       /// As per Table mumble.
00140       size_type
00141       size() const
00142       { return _M_len; }
00143 
00144       /// Returns the size requested by the constructor; may be >size().
00145       size_type
00146       requested_size() const
00147       { return _M_original_len; }
00148 
00149       /// As per Table mumble.
00150       iterator
00151       begin()
00152       { return _M_buffer; }
00153 
00154       /// As per Table mumble.
00155       iterator
00156       end()
00157       { return _M_buffer + _M_len; }
00158 
00159       /**
00160        * Constructs a temporary buffer of a size somewhere between
00161        * zero and the size of the given range.
00162        */
00163       _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last);
00164 
00165       ~_Temporary_buffer()
00166       {
00167         std::_Destroy(_M_buffer, _M_buffer + _M_len);
00168         std::return_temporary_buffer(_M_buffer);
00169       }
00170 
00171     private:
00172       // Disable copy constructor and assignment operator.
00173       _Temporary_buffer(const _Temporary_buffer&);
00174 
00175       void
00176       operator=(const _Temporary_buffer&);
00177     };
00178 
00179 
00180   template<bool>
00181     struct __uninitialized_construct_buf_dispatch
00182     {
00183       template<typename _Pointer, typename _ForwardIterator>
00184         static void
00185         __ucr(_Pointer __first, _Pointer __last,
00186               _ForwardIterator __seed)
00187         {
00188           if(__first == __last)
00189             return;
00190 
00191           _Pointer __cur = __first;
00192           __try
00193             {
00194               std::_Construct(std::__addressof(*__first),
00195                               _GLIBCXX_MOVE(*__seed));
00196               _Pointer __prev = __cur;
00197               ++__cur;
00198               for(; __cur != __last; ++__cur, ++__prev)
00199                 std::_Construct(std::__addressof(*__cur),
00200                                 _GLIBCXX_MOVE(*__prev));
00201               *__seed = _GLIBCXX_MOVE(*__prev);
00202             }
00203           __catch(...)
00204             {
00205               std::_Destroy(__first, __cur);
00206               __throw_exception_again;
00207             }
00208         }
00209     };
00210 
00211   template<>
00212     struct __uninitialized_construct_buf_dispatch<true>
00213     {
00214       template<typename _Pointer, typename _ForwardIterator>
00215         static void
00216         __ucr(_Pointer, _Pointer, _ForwardIterator) { }
00217     };
00218 
00219   // Constructs objects in the range [first, last).
00220   // Note that while these new objects will take valid values,
00221   // their exact value is not defined. In particular they may
00222   // be 'moved from'.
00223   //
00224   // While *__seed may be altered during this algorithm, it will have
00225   // the same value when the algorithm finishes, unless one of the
00226   // constructions throws.
00227   //
00228   // Requirements: _Pointer::value_type(_Tp&&) is valid.
00229   template<typename _Pointer, typename _ForwardIterator>
00230     inline void
00231     __uninitialized_construct_buf(_Pointer __first, _Pointer __last,
00232                                   _ForwardIterator __seed)
00233     {
00234       typedef typename std::iterator_traits<_Pointer>::value_type
00235         _ValueType;
00236 
00237       std::__uninitialized_construct_buf_dispatch<
00238         __has_trivial_constructor(_ValueType)>::
00239           __ucr(__first, __last, __seed);
00240     }
00241 
00242   template<typename _ForwardIterator, typename _Tp>
00243     _Temporary_buffer<_ForwardIterator, _Tp>::
00244     _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00245     : _M_original_len(std::distance(__first, __last)),
00246       _M_len(0), _M_buffer(0)
00247     {
00248       __try
00249         {
00250           std::pair<pointer, size_type> __p(std::get_temporary_buffer<
00251                                             value_type>(_M_original_len));
00252           _M_buffer = __p.first;
00253           _M_len = __p.second;
00254           if (_M_buffer)
00255             std::__uninitialized_construct_buf(_M_buffer, _M_buffer + _M_len,
00256                                                __first);
00257         }
00258       __catch(...)
00259         {
00260           std::return_temporary_buffer(_M_buffer);
00261           _M_buffer = 0;
00262           _M_len = 0;
00263           __throw_exception_again;
00264         }
00265     }
00266 
00267 _GLIBCXX_END_NAMESPACE_VERSION
00268 } // namespace
00269 
00270 #endif /* _STL_TEMPBUF_H */
00271