libstdc++
|
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 bits/stl_tempbuf.h 00053 * This is an internal header file, included by other library headers. 00054 * Do not attempt to use it directly. @headername{memory} 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 00063 namespace std _GLIBCXX_VISIBILITY(default) 00064 { 00065 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00066 00067 /** 00068 * @brief Allocates a temporary buffer. 00069 * @param len The number of objects of type Tp. 00070 * @return See full description. 00071 * 00072 * Reinventing the wheel, but this time with prettier spokes! 00073 * 00074 * This function tries to obtain storage for @c len adjacent Tp 00075 * objects. The objects themselves are not constructed, of course. 00076 * A pair<> is returned containing <em>the buffer s address and 00077 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if 00078 * no storage can be obtained.</em> Note that the capacity obtained 00079 * may be less than that requested if the memory is unavailable; 00080 * you should compare len with the .second return value. 00081 * 00082 * Provides the nothrow exception guarantee. 00083 */ 00084 template<typename _Tp> 00085 pair<_Tp*, ptrdiff_t> 00086 get_temporary_buffer(ptrdiff_t __len) 00087 { 00088 const ptrdiff_t __max = 00089 __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp); 00090 if (__len > __max) 00091 __len = __max; 00092 00093 while (__len > 0) 00094 { 00095 _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), 00096 std::nothrow)); 00097 if (__tmp != 0) 00098 return std::pair<_Tp*, ptrdiff_t>(__tmp, __len); 00099 __len /= 2; 00100 } 00101 return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0); 00102 } 00103 00104 /** 00105 * @brief The companion to get_temporary_buffer(). 00106 * @param p A buffer previously allocated by get_temporary_buffer. 00107 * @return None. 00108 * 00109 * Frees the memory pointed to by p. 00110 */ 00111 template<typename _Tp> 00112 inline void 00113 return_temporary_buffer(_Tp* __p) 00114 { ::operator delete(__p, std::nothrow); } 00115 00116 00117 /** 00118 * This class is used in two places: stl_algo.h and ext/memory, 00119 * where it is wrapped as the temporary_buffer class. See 00120 * temporary_buffer docs for more notes. 00121 */ 00122 template<typename _ForwardIterator, typename _Tp> 00123 class _Temporary_buffer 00124 { 00125 // concept requirements 00126 __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept) 00127 00128 public: 00129 typedef _Tp value_type; 00130 typedef value_type* pointer; 00131 typedef pointer iterator; 00132 typedef ptrdiff_t size_type; 00133 00134 protected: 00135 size_type _M_original_len; 00136 size_type _M_len; 00137 pointer _M_buffer; 00138 00139 public: 00140 /// As per Table mumble. 00141 size_type 00142 size() const 00143 { return _M_len; } 00144 00145 /// Returns the size requested by the constructor; may be >size(). 00146 size_type 00147 requested_size() const 00148 { return _M_original_len; } 00149 00150 /// As per Table mumble. 00151 iterator 00152 begin() 00153 { return _M_buffer; } 00154 00155 /// As per Table mumble. 00156 iterator 00157 end() 00158 { return _M_buffer + _M_len; } 00159 00160 /** 00161 * Constructs a temporary buffer of a size somewhere between 00162 * zero and the size of the given range. 00163 */ 00164 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last); 00165 00166 ~_Temporary_buffer() 00167 { 00168 std::_Destroy(_M_buffer, _M_buffer + _M_len); 00169 std::return_temporary_buffer(_M_buffer); 00170 } 00171 00172 private: 00173 // Disable copy constructor and assignment operator. 00174 _Temporary_buffer(const _Temporary_buffer&); 00175 00176 void 00177 operator=(const _Temporary_buffer&); 00178 }; 00179 00180 00181 template<bool> 00182 struct __uninitialized_construct_buf_dispatch 00183 { 00184 template<typename _ForwardIterator, typename _Tp> 00185 static void 00186 __ucr(_ForwardIterator __first, _ForwardIterator __last, 00187 _Tp& __value) 00188 { 00189 if(__first == __last) 00190 return; 00191 00192 _ForwardIterator __cur = __first; 00193 __try 00194 { 00195 std::_Construct(std::__addressof(*__first), 00196 _GLIBCXX_MOVE(__value)); 00197 _ForwardIterator __prev = __cur; 00198 ++__cur; 00199 for(; __cur != __last; ++__cur, ++__prev) 00200 std::_Construct(std::__addressof(*__cur), 00201 _GLIBCXX_MOVE(*__prev)); 00202 __value = _GLIBCXX_MOVE(*__prev); 00203 } 00204 __catch(...) 00205 { 00206 std::_Destroy(__first, __cur); 00207 __throw_exception_again; 00208 } 00209 } 00210 }; 00211 00212 template<> 00213 struct __uninitialized_construct_buf_dispatch<true> 00214 { 00215 template<typename _ForwardIterator, typename _Tp> 00216 static void 00217 __ucr(_ForwardIterator, _ForwardIterator, _Tp&) { } 00218 }; 00219 00220 // Constructs objects in the range [first, last). 00221 // Note that while these new objects will take valid values, 00222 // their exact value is not defined. In particular they may 00223 // be 'moved from'. 00224 // 00225 // While __value may altered during this algorithm, it will have 00226 // the same value when the algorithm finishes, unless one of the 00227 // constructions throws. 00228 // 00229 // Requirements: _ForwardIterator::value_type(_Tp&&) is valid. 00230 template<typename _ForwardIterator, typename _Tp> 00231 inline void 00232 __uninitialized_construct_buf(_ForwardIterator __first, 00233 _ForwardIterator __last, 00234 _Tp& __value) 00235 { 00236 typedef typename std::iterator_traits<_ForwardIterator>::value_type 00237 _ValueType; 00238 00239 std::__uninitialized_construct_buf_dispatch< 00240 __has_trivial_constructor(_ValueType)>:: 00241 __ucr(__first, __last, __value); 00242 } 00243 00244 template<typename _ForwardIterator, typename _Tp> 00245 _Temporary_buffer<_ForwardIterator, _Tp>:: 00246 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) 00247 : _M_original_len(std::distance(__first, __last)), 00248 _M_len(0), _M_buffer(0) 00249 { 00250 __try 00251 { 00252 std::pair<pointer, size_type> __p(std::get_temporary_buffer< 00253 value_type>(_M_original_len)); 00254 _M_buffer = __p.first; 00255 _M_len = __p.second; 00256 if(_M_buffer) 00257 std::__uninitialized_construct_buf(_M_buffer, _M_buffer + _M_len, 00258 *__first); 00259 } 00260 __catch(...) 00261 { 00262 std::return_temporary_buffer(_M_buffer); 00263 _M_buffer = 0; 00264 _M_len = 0; 00265 __throw_exception_again; 00266 } 00267 } 00268 00269 _GLIBCXX_END_NAMESPACE_VERSION 00270 } // namespace 00271 00272 #endif /* _STL_TEMPBUF_H */ 00273