# stl_tempbuf.h

Go to the documentation of this file.

    00001 // Temporary buffer implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 3, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // Under Section 7 of GPL version 3, you are granted additional
    00018 // permissions described in the GCC Runtime Library Exception, version
    00019 // 3.1, as published by the Free Software Foundation.
    00020
    00021 // You should have received a copy of the GNU General Public License and
    00022 // a copy of the GCC Runtime Library Exception along with this program;
    00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00024 // <http://www.gnu.org/licenses/>.
    00025
    00026 /*
    00027  *
    00028  * Copyright (c) 1994
    00029  * Hewlett-Packard Company
    00030  *
    00031  * Permission to use, copy, modify, distribute and sell this software
    00032  * and its documentation for any purpose is hereby granted without fee,
    00033  * provided that the above copyright notice appear in all copies and
    00034  * that both that copyright notice and this permission notice appear
    00035  * in supporting documentation.  Hewlett-Packard Company makes no
    00036  * representations about the suitability of this software for any
    00037  * purpose.  It is provided "as is" without express or implied warranty.
    00038  *
    00039  *
    00040  * Copyright (c) 1996,1997
    00041  * Silicon Graphics Computer Systems, Inc.
    00042  *
    00043  * Permission to use, copy, modify, distribute and sell this software
    00044  * and its documentation for any purpose is hereby granted without fee,
    00045  * provided that the above copyright notice appear in all copies and
    00046  * that both that copyright notice and this permission notice appear
    00047  * in supporting documentation.  Silicon Graphics makes no
    00048  * representations about the suitability of this software for any
    00049  * purpose.  It is provided "as is" without express or implied warranty.
    00050  */
    00051
    00052 /** @file stl_tempbuf.h
    00053  *  This is an internal header file, included by other library headers.
    00054  *  You should not attempt to use it directly.
    00055  */
    00056
    00057 #ifndef _STL_TEMPBUF_H
    00058 #define _STL_TEMPBUF_H 1
    00059
    00060 #include <bits/stl_algobase.h>
    00061 #include <bits/stl_construct.h>
    00062 #include <bits/stl_uninitialized.h>
    00063
    00064 _GLIBCXX_BEGIN_NAMESPACE(std)
    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)
    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   template<typename _ForwardIterator, typename _Tp>
    00180     _Temporary_buffer<_ForwardIterator, _Tp>::
    00181     _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
    00182     : _M_original_len(std::distance(__first, __last)),
    00183       _M_len(0), _M_buffer(0)
    00184     {
    00185       __try
    00186     {
    00187       std::pair<pointer, size_type> __p(std::get_temporary_buffer<
    00188                         value_type>(_M_original_len));
    00189       _M_buffer = __p.first;
    00190       _M_len = __p.second;
    00191       if(_M_buffer)
    00192         std::__uninitialized_construct_range(_M_buffer, _M_buffer + _M_len,
    00193                          *__first);
    00194     }
    00195       __catch(...)
    00196     {
    00197       std::return_temporary_buffer(_M_buffer);
    00198       _M_buffer = 0;
    00199       _M_len = 0;
    00200       __throw_exception_again;
    00201     }
    00202     }
    00203
    00204 _GLIBCXX_END_NAMESPACE
    00205
    00206 #endif /* _STL_TEMPBUF_H */
    00207