| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#ifndef _STL_TEMPBUF_H |
| 57 |
#define _STL_TEMPBUF_H 1 |
| 58 |
|
| 59 |
#include <new> |
| 60 |
#include <bits/exception_defines.h> |
| 61 |
#include <bits/stl_construct.h> |
| 62 |
#include <bits/stl_pair.h> |
| 63 |
#include <ext/numeric_traits.h> |
| 64 |
|
| 65 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 66 |
{ |
| 67 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 68 |
|
| 69 |
#if __has_builtin(__builtin_operator_new) >= 201802L |
| 70 |
# define _GLIBCXX_OPERATOR_NEW __builtin_operator_new |
| 71 |
# define _GLIBCXX_OPERATOR_DELETE __builtin_operator_delete |
| 72 |
#else |
| 73 |
# define _GLIBCXX_OPERATOR_NEW ::operator new |
| 74 |
# define _GLIBCXX_OPERATOR_DELETE ::operator delete |
| 75 |
#endif |
| 76 |
|
| 77 |
namespace __detail |
| 78 |
{ |
| 79 |
|
| 80 |
|
| 81 |
template<typename _Tp> |
| 82 |
inline _Tp* |
| 83 |
__get_temporary_buffer(ptrdiff_t __len) _GLIBCXX_NOTHROW |
| 84 |
{ |
| 85 |
if (__builtin_expect(size_t(__len) > (size_t(-1) / sizeof(_Tp)), 0)) |
| 86 |
return 0; |
| 87 |
|
| 88 |
#if __cpp_aligned_new && __cplusplus >= 201103L |
| 89 |
if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) |
| 90 |
return (_Tp*) _GLIBCXX_OPERATOR_NEW(__len * sizeof(_Tp), |
| 91 |
align_val_t(alignof(_Tp)), |
| 92 |
nothrow_t()); |
| 93 |
#endif |
| 94 |
return (_Tp*) _GLIBCXX_OPERATOR_NEW(__len * sizeof(_Tp), nothrow_t()); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
template<typename _Tp> |
| 100 |
inline void |
| 101 |
__return_temporary_buffer(_Tp* __p, |
| 102 |
size_t __len __attribute__((__unused__))) |
| 103 |
{ |
| 104 |
#if __cpp_sized_deallocation |
| 105 |
# define _GLIBCXX_SIZED_DEALLOC(T, p, n) (p), (n) * sizeof(T) |
| 106 |
#else |
| 107 |
# define _GLIBCXX_SIZED_DEALLOC(T, p, n) (p) |
| 108 |
#endif |
| 109 |
|
| 110 |
#if __cpp_aligned_new && __cplusplus >= 201103L |
| 111 |
if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) |
| 112 |
{ |
| 113 |
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(_Tp, __p, __len), |
| 114 |
align_val_t(alignof(_Tp))); |
| 115 |
return; |
| 116 |
} |
| 117 |
#endif |
| 118 |
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(_Tp, __p, __len)); |
| 119 |
} |
| 120 |
#undef _GLIBCXX_SIZED_DEALLOC |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
template<typename _Tp> |
| 141 |
_GLIBCXX17_DEPRECATED |
| 142 |
pair<_Tp*, ptrdiff_t> |
| 143 |
get_temporary_buffer(ptrdiff_t __len) _GLIBCXX_NOEXCEPT |
| 144 |
{ |
| 145 |
const ptrdiff_t __max = |
| 146 |
__gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp); |
| 147 |
if (__len > __max) |
| 148 |
__len = __max; |
| 149 |
|
| 150 |
while (__len > 0) |
| 151 |
{ |
| 152 |
if (_Tp* __tmp = __detail::__get_temporary_buffer<_Tp>(__len)) |
| 153 |
return pair<_Tp*, ptrdiff_t>(__tmp, __len); |
| 154 |
__len = __len == 1 ? 0 : ((__len + 1) / 2); |
| 155 |
} |
| 156 |
return pair<_Tp*, ptrdiff_t>(); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
template<typename _Tp> |
| 167 |
_GLIBCXX17_DEPRECATED |
| 168 |
inline void |
| 169 |
return_temporary_buffer(_Tp* __p) |
| 170 |
{ |
| 171 |
#if __cpp_aligned_new && __cplusplus >= 201103L |
| 172 |
if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) |
| 173 |
_GLIBCXX_OPERATOR_DELETE(__p, align_val_t(alignof(_Tp))); |
| 174 |
else |
| 175 |
#endif |
| 176 |
_GLIBCXX_OPERATOR_DELETE(__p); |
| 177 |
} |
| 178 |
|
| 179 |
#undef _GLIBCXX_OPERATOR_DELETE |
| 180 |
#undef _GLIBCXX_OPERATOR_NEW |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
template<typename _ForwardIterator, typename _Tp> |
| 188 |
class _Temporary_buffer |
| 189 |
{ |
| 190 |
|
| 191 |
__glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept) |
| 192 |
|
| 193 |
public: |
| 194 |
typedef _Tp value_type; |
| 195 |
typedef value_type* pointer; |
| 196 |
typedef pointer iterator; |
| 197 |
typedef ptrdiff_t size_type; |
| 198 |
|
| 199 |
protected: |
| 200 |
size_type _M_original_len; |
| 201 |
struct _Impl |
| 202 |
{ |
| 203 |
#pragma GCC diagnostic push |
| 204 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 205 |
explicit |
| 206 |
_Impl(ptrdiff_t __original_len) |
| 207 |
{ |
| 208 |
pair<pointer, size_type> __p( |
| 209 |
std::get_temporary_buffer<value_type>(__original_len)); |
| 210 |
_M_len = __p.second; |
| 211 |
_M_buffer = __p.first; |
| 212 |
} |
| 213 |
#pragma GCC diagnostic pop |
| 214 |
|
| 215 |
~_Impl() |
| 216 |
{ std::__detail::__return_temporary_buffer(_M_buffer, _M_len); } |
| 217 |
|
| 218 |
size_type _M_len; |
| 219 |
pointer _M_buffer; |
| 220 |
} _M_impl; |
| 221 |
|
| 222 |
public: |
| 223 |
|
| 224 |
size_type |
| 225 |
size() const |
| 226 |
{ return _M_impl._M_len; } |
| 227 |
|
| 228 |
|
| 229 |
size_type |
| 230 |
requested_size() const |
| 231 |
{ return _M_original_len; } |
| 232 |
|
| 233 |
|
| 234 |
iterator |
| 235 |
begin() |
| 236 |
{ return _M_impl._M_buffer; } |
| 237 |
|
| 238 |
|
| 239 |
iterator |
| 240 |
end() |
| 241 |
{ return _M_impl._M_buffer + _M_impl._M_len; } |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
_Temporary_buffer(_ForwardIterator __seed, size_type __original_len); |
| 248 |
|
| 249 |
~_Temporary_buffer() |
| 250 |
{ std::_Destroy(_M_impl._M_buffer, _M_impl._M_buffer + _M_impl._M_len); } |
| 251 |
|
| 252 |
private: |
| 253 |
|
| 254 |
_Temporary_buffer(const _Temporary_buffer&); |
| 255 |
|
| 256 |
void |
| 257 |
operator=(const _Temporary_buffer&); |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
template<bool> |
| 262 |
struct __uninitialized_construct_buf_dispatch |
| 263 |
{ |
| 264 |
template<typename _Pointer, typename _ForwardIterator> |
| 265 |
static void |
| 266 |
__ucr(_Pointer __first, _Pointer __last, |
| 267 |
_ForwardIterator __seed) |
| 268 |
{ |
| 269 |
if (__builtin_expect(__first == __last, 0)) |
| 270 |
return; |
| 271 |
|
| 272 |
_Pointer __cur = __first; |
| 273 |
__try |
| 274 |
{ |
| 275 |
std::_Construct(std::__addressof(*__first), |
| 276 |
_GLIBCXX_MOVE(*__seed)); |
| 277 |
_Pointer __prev = __cur; |
| 278 |
++__cur; |
| 279 |
for(; __cur != __last; ++__cur, ++__prev) |
| 280 |
std::_Construct(std::__addressof(*__cur), |
| 281 |
_GLIBCXX_MOVE(*__prev)); |
| 282 |
*__seed = _GLIBCXX_MOVE(*__prev); |
| 283 |
} |
| 284 |
__catch(...) |
| 285 |
{ |
| 286 |
std::_Destroy(__first, __cur); |
| 287 |
__throw_exception_again; |
| 288 |
} |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
template<> |
| 293 |
struct __uninitialized_construct_buf_dispatch<true> |
| 294 |
{ |
| 295 |
template<typename _Pointer, typename _ForwardIterator> |
| 296 |
static void |
| 297 |
__ucr(_Pointer, _Pointer, _ForwardIterator) { } |
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
template<typename _Tp, typename _ForwardIterator> |
| 312 |
inline void |
| 313 |
__uninitialized_construct_buf(_Tp* __first, _Tp* __last, |
| 314 |
_ForwardIterator __seed) |
| 315 |
{ |
| 316 |
std::__uninitialized_construct_buf_dispatch< |
| 317 |
__has_trivial_constructor(_Tp)>:: |
| 318 |
__ucr(__first, __last, __seed); |
| 319 |
} |
| 320 |
|
| 321 |
template<typename _ForwardIterator, typename _Tp> |
| 322 |
_Temporary_buffer<_ForwardIterator, _Tp>:: |
| 323 |
_Temporary_buffer(_ForwardIterator __seed, size_type __original_len) |
| 324 |
: _M_original_len(__original_len), _M_impl(__original_len) |
| 325 |
{ |
| 326 |
std::__uninitialized_construct_buf(begin(), end(), __seed); |
| 327 |
} |
| 328 |
|
| 329 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 330 |
} |
| 331 |
|
| 332 |
#endif |