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