libstdc++
memory
Go to the documentation of this file.
00001 // Memory extensions -*- C++ -*-
00002 
00003 // Copyright (C) 2002-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
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 ext/memory
00052  *  This file is a GNU extension to the Standard C++ Library (possibly
00053  *  containing extensions from the HP/SGI STL subset).
00054  */
00055 
00056 #ifndef _EXT_MEMORY
00057 #define _EXT_MEMORY 1
00058 
00059 #pragma GCC system_header
00060 
00061 #include <memory>
00062 #include <bits/stl_tempbuf.h>
00063 
00064 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00065 {
00066 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00067 
00068   using std::ptrdiff_t;
00069   using std::pair;
00070   using std::__iterator_category;
00071   using std::_Temporary_buffer;
00072 
00073   template<typename _InputIter, typename _Size, typename _ForwardIter>
00074     pair<_InputIter, _ForwardIter>
00075     __uninitialized_copy_n(_InputIter __first, _Size __count,
00076                            _ForwardIter __result, std::input_iterator_tag)
00077     {
00078       _ForwardIter __cur = __result;
00079       __try
00080         {
00081           for (; __count > 0 ; --__count, ++__first, ++__cur)
00082             std::_Construct(&*__cur, *__first);
00083           return pair<_InputIter, _ForwardIter>(__first, __cur);
00084         }
00085       __catch(...)
00086         {
00087           std::_Destroy(__result, __cur);
00088           __throw_exception_again;
00089         }
00090     }
00091 
00092   template<typename _RandomAccessIter, typename _Size, typename _ForwardIter>
00093     inline pair<_RandomAccessIter, _ForwardIter>
00094     __uninitialized_copy_n(_RandomAccessIter __first, _Size __count,
00095                            _ForwardIter __result,
00096                            std::random_access_iterator_tag)
00097     {
00098       _RandomAccessIter __last = __first + __count;
00099       return (pair<_RandomAccessIter, _ForwardIter>
00100               (__last, std::uninitialized_copy(__first, __last, __result)));
00101     }
00102 
00103   template<typename _InputIter, typename _Size, typename _ForwardIter>
00104     inline pair<_InputIter, _ForwardIter>
00105     __uninitialized_copy_n(_InputIter __first, _Size __count,
00106                            _ForwardIter __result)
00107     { return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
00108                                                __iterator_category(__first)); }
00109 
00110   /**
00111    *  @brief Copies the range [first,last) into result.
00112    *  @param  __first  An input iterator.
00113    *  @param  __count  Length
00114    *  @param  __result An output iterator.
00115    *  @return   __result + (__first + __count)
00116    *  @ingroup SGIextensions
00117    *
00118    *  Like copy(), but does not require an initialized output range.
00119   */
00120   template<typename _InputIter, typename _Size, typename _ForwardIter>
00121     inline pair<_InputIter, _ForwardIter>
00122     uninitialized_copy_n(_InputIter __first, _Size __count,
00123                          _ForwardIter __result)
00124     { return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
00125                                                __iterator_category(__first)); }
00126 
00127 
00128   // An alternative version of uninitialized_copy_n that constructs
00129   // and destroys objects with a user-provided allocator.
00130   template<typename _InputIter, typename _Size, typename _ForwardIter,
00131            typename _Allocator>
00132     pair<_InputIter, _ForwardIter>
00133     __uninitialized_copy_n_a(_InputIter __first, _Size __count,
00134                              _ForwardIter __result,
00135                              _Allocator __alloc)
00136     {
00137       _ForwardIter __cur = __result;
00138       __try
00139         {
00140           for (; __count > 0 ; --__count, ++__first, ++__cur)
00141             __alloc.construct(&*__cur, *__first);
00142           return pair<_InputIter, _ForwardIter>(__first, __cur);
00143         }
00144       __catch(...)
00145         {
00146           std::_Destroy(__result, __cur, __alloc);
00147           __throw_exception_again;
00148         }
00149     }
00150 
00151   template<typename _InputIter, typename _Size, typename _ForwardIter,
00152            typename _Tp>
00153     inline pair<_InputIter, _ForwardIter>
00154     __uninitialized_copy_n_a(_InputIter __first, _Size __count,
00155                              _ForwardIter __result,
00156                              std::allocator<_Tp>)
00157     {
00158       return __gnu_cxx::uninitialized_copy_n(__first, __count, __result);
00159     }
00160 
00161   /**
00162    *  This class provides similar behavior and semantics of the standard
00163    *  functions get_temporary_buffer() and return_temporary_buffer(), but
00164    *  encapsulated in a type vaguely resembling a standard container.
00165    *
00166    *  By default, a temporary_buffer<Iter> stores space for objects of
00167    *  whatever type the Iter iterator points to.  It is constructed from a
00168    *  typical [first,last) range, and provides the begin(), end(), size()
00169    *  functions, as well as requested_size().  For non-trivial types, copies
00170    *  of *first will be used to initialize the storage.
00171    *
00172    *  @c malloc is used to obtain underlying storage.
00173    *
00174    *  Like get_temporary_buffer(), not all the requested memory may be
00175    *  available.  Ideally, the created buffer will be large enough to hold a
00176    *  copy of [first,last), but if size() is less than requested_size(),
00177    *  then this didn't happen.
00178    *
00179    *  @ingroup SGIextensions
00180   */
00181   template <class _ForwardIterator, class _Tp
00182             = typename std::iterator_traits<_ForwardIterator>::value_type >
00183     struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
00184     {
00185       /// Requests storage large enough to hold a copy of [first,last).
00186       temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00187       : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) { }
00188       
00189       /// Destroys objects and frees storage.
00190       ~temporary_buffer() { }
00191     };
00192 
00193 _GLIBCXX_END_NAMESPACE_VERSION
00194 } // namespace
00195 
00196 #endif
00197