| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H |
| 11 | #define _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H |
| 12 | |
| 13 | #include <__algorithm/copy.h> |
| 14 | #include <__algorithm/in_out_result.h> |
| 15 | #include <__algorithm/unwrap_iter.h> |
| 16 | #include <__algorithm/unwrap_range.h> |
| 17 | #include <__config> |
| 18 | #include <__fwd/memory.h> |
| 19 | #include <__iterator/iterator_traits.h> |
| 20 | #include <__iterator/reverse_iterator.h> |
| 21 | #include <__memory/addressof.h> |
| 22 | #include <__memory/allocator_traits.h> |
| 23 | #include <__memory/construct_at.h> |
| 24 | #include <__memory/destroy.h> |
| 25 | #include <__memory/pointer_traits.h> |
| 26 | #include <__type_traits/enable_if.h> |
| 27 | #include <__type_traits/is_constant_evaluated.h> |
| 28 | #include <__type_traits/is_nothrow_constructible.h> |
| 29 | #include <__type_traits/is_reference.h> |
| 30 | #include <__type_traits/is_relocatable.h> |
| 31 | #include <__type_traits/is_same.h> |
| 32 | #include <__type_traits/is_trivially_assignable.h> |
| 33 | #include <__type_traits/is_trivially_constructible.h> |
| 34 | #include <__type_traits/remove_const.h> |
| 35 | #include <__utility/exception_guard.h> |
| 36 | #include <__utility/move.h> |
| 37 | #include <__utility/pair.h> |
| 38 | |
| 39 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 40 | # pragma GCC system_header |
| 41 | #endif |
| 42 | |
| 43 | _LIBCPP_PUSH_MACROS |
| 44 | #include <__undef_macros> |
| 45 | |
| 46 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 47 | |
| 48 | struct __always_false { |
| 49 | template <class... _Args> |
| 50 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(_Args&&...) const _NOEXCEPT { |
| 51 | return false; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | // uninitialized_copy |
| 56 | |
| 57 | template <class _ValueType, class _InputIterator, class _Sentinel1, class _ForwardIterator, class _EndPredicate> |
| 58 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator> |
| 59 | __uninitialized_copy( |
| 60 | _InputIterator __ifirst, _Sentinel1 __ilast, _ForwardIterator __ofirst, _EndPredicate __stop_copying) { |
| 61 | _ForwardIterator __idx = __ofirst; |
| 62 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); |
| 63 | for (; __ifirst != __ilast && !__stop_copying(__idx); ++__ifirst, (void)++__idx) |
| 64 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(*__ifirst); |
| 65 | __guard.__complete(); |
| 66 | |
| 67 | return {std::move(__ifirst), std::move(__idx)}; |
| 68 | } |
| 69 | |
| 70 | template <class _InputIterator, class _ForwardIterator> |
| 71 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 72 | uninitialized_copy(_InputIterator __ifirst, _InputIterator __ilast, _ForwardIterator __ofirst) { |
| 73 | typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; |
| 74 | auto __result = std::__uninitialized_copy<_ValueType>( |
| 75 | std::move(__ifirst), std::move(__ilast), std::move(__ofirst), __always_false()); |
| 76 | return std::move(__result.__out_); |
| 77 | } |
| 78 | |
| 79 | // uninitialized_copy_n |
| 80 | |
| 81 | template <class _ValueType, class _InputIterator, class _Size, class _ForwardIterator, class _EndPredicate> |
| 82 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator> |
| 83 | __uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_copying) { |
| 84 | _ForwardIterator __idx = __ofirst; |
| 85 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); |
| 86 | for (; __n > 0 && !__stop_copying(__idx); ++__ifirst, (void)++__idx, (void)--__n) |
| 87 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(*__ifirst); |
| 88 | __guard.__complete(); |
| 89 | |
| 90 | return {std::move(__ifirst), std::move(__idx)}; |
| 91 | } |
| 92 | |
| 93 | template <class _InputIterator, class _Size, class _ForwardIterator> |
| 94 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 95 | uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst) { |
| 96 | typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; |
| 97 | auto __result = |
| 98 | std::__uninitialized_copy_n<_ValueType>(std::move(__ifirst), __n, std::move(__ofirst), __always_false()); |
| 99 | return std::move(__result.__out_); |
| 100 | } |
| 101 | |
| 102 | // uninitialized_fill |
| 103 | |
| 104 | template <class _ValueType, class _ForwardIterator, class _Sentinel, class _Tp> |
| 105 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 106 | __uninitialized_fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __x) { |
| 107 | _ForwardIterator __idx = __first; |
| 108 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); |
| 109 | for (; __idx != __last; ++__idx) |
| 110 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__x); |
| 111 | __guard.__complete(); |
| 112 | |
| 113 | return __idx; |
| 114 | } |
| 115 | |
| 116 | template <class _ForwardIterator, class _Tp> |
| 117 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 118 | uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x) { |
| 119 | typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; |
| 120 | (void)std::__uninitialized_fill<_ValueType>(__first, __last, __x); |
| 121 | } |
| 122 | |
| 123 | // uninitialized_fill_n |
| 124 | |
| 125 | template <class _ValueType, class _ForwardIterator, class _Size, class _Tp> |
| 126 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 127 | __uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) { |
| 128 | _ForwardIterator __idx = __first; |
| 129 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); |
| 130 | for (; __n > 0; ++__idx, (void)--__n) |
| 131 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__x); |
| 132 | __guard.__complete(); |
| 133 | |
| 134 | return __idx; |
| 135 | } |
| 136 | |
| 137 | template <class _ForwardIterator, class _Size, class _Tp> |
| 138 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 139 | uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) { |
| 140 | typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; |
| 141 | return std::__uninitialized_fill_n<_ValueType>(__first, __n, __x); |
| 142 | } |
| 143 | |
| 144 | #if _LIBCPP_STD_VER >= 17 |
| 145 | |
| 146 | template <class _Iter> |
| 147 | _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) __deref_move(_Iter& __it) { |
| 148 | if constexpr (is_lvalue_reference_v<decltype(*__it)>) |
| 149 | return std::move(*__it); |
| 150 | else |
| 151 | return *__it; |
| 152 | } |
| 153 | |
| 154 | // uninitialized_default_construct |
| 155 | |
| 156 | template <class _ValueType, class _ForwardIterator, class _Sentinel> |
| 157 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 158 | __uninitialized_default_construct(_ForwardIterator __first, _Sentinel __last) { |
| 159 | auto __idx = __first; |
| 160 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); |
| 161 | for (; __idx != __last; ++__idx) |
| 162 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType; |
| 163 | __guard.__complete(); |
| 164 | |
| 165 | return __idx; |
| 166 | } |
| 167 | |
| 168 | template <class _ForwardIterator> |
| 169 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 170 | uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) { |
| 171 | using _ValueType = typename iterator_traits<_ForwardIterator>::value_type; |
| 172 | (void)std::__uninitialized_default_construct<_ValueType>(std::move(__first), std::move(__last)); |
| 173 | } |
| 174 | |
| 175 | // uninitialized_default_construct_n |
| 176 | |
| 177 | template <class _ValueType, class _ForwardIterator, class _Size> |
| 178 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 179 | __uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) { |
| 180 | auto __idx = __first; |
| 181 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); |
| 182 | for (; __n > 0; ++__idx, (void)--__n) |
| 183 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType; |
| 184 | __guard.__complete(); |
| 185 | |
| 186 | return __idx; |
| 187 | } |
| 188 | |
| 189 | template <class _ForwardIterator, class _Size> |
| 190 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 191 | uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) { |
| 192 | using _ValueType = typename iterator_traits<_ForwardIterator>::value_type; |
| 193 | return std::__uninitialized_default_construct_n<_ValueType>(std::move(__first), __n); |
| 194 | } |
| 195 | |
| 196 | // uninitialized_value_construct |
| 197 | |
| 198 | template <class _ValueType, class _ForwardIterator, class _Sentinel> |
| 199 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 200 | __uninitialized_value_construct(_ForwardIterator __first, _Sentinel __last) { |
| 201 | auto __idx = __first; |
| 202 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); |
| 203 | for (; __idx != __last; ++__idx) |
| 204 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(); |
| 205 | __guard.__complete(); |
| 206 | |
| 207 | return __idx; |
| 208 | } |
| 209 | |
| 210 | template <class _ForwardIterator> |
| 211 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 212 | uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) { |
| 213 | using _ValueType = typename iterator_traits<_ForwardIterator>::value_type; |
| 214 | (void)std::__uninitialized_value_construct<_ValueType>(std::move(__first), std::move(__last)); |
| 215 | } |
| 216 | |
| 217 | // uninitialized_value_construct_n |
| 218 | |
| 219 | template <class _ValueType, class _ForwardIterator, class _Size> |
| 220 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 221 | __uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) { |
| 222 | auto __idx = __first; |
| 223 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); }); |
| 224 | for (; __n > 0; ++__idx, (void)--__n) |
| 225 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(); |
| 226 | __guard.__complete(); |
| 227 | |
| 228 | return __idx; |
| 229 | } |
| 230 | |
| 231 | template <class _ForwardIterator, class _Size> |
| 232 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 233 | uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) { |
| 234 | using _ValueType = typename iterator_traits<_ForwardIterator>::value_type; |
| 235 | return std::__uninitialized_value_construct_n<_ValueType>(std::move(__first), __n); |
| 236 | } |
| 237 | |
| 238 | // uninitialized_move |
| 239 | |
| 240 | template <class _ValueType, |
| 241 | class _InputIterator, |
| 242 | class _Sentinel1, |
| 243 | class _ForwardIterator, |
| 244 | class _EndPredicate, |
| 245 | class _IterMove> |
| 246 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator> |
| 247 | __uninitialized_move(_InputIterator __ifirst, |
| 248 | _Sentinel1 __ilast, |
| 249 | _ForwardIterator __ofirst, |
| 250 | _EndPredicate __stop_moving, |
| 251 | _IterMove __iter_move) { |
| 252 | auto __idx = __ofirst; |
| 253 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); |
| 254 | for (; __ifirst != __ilast && !__stop_moving(__idx); ++__idx, (void)++__ifirst) { |
| 255 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); |
| 256 | } |
| 257 | __guard.__complete(); |
| 258 | |
| 259 | return {std::move(__ifirst), std::move(__idx)}; |
| 260 | } |
| 261 | |
| 262 | template <class _InputIterator, class _ForwardIterator> |
| 263 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator |
| 264 | uninitialized_move(_InputIterator __ifirst, _InputIterator __ilast, _ForwardIterator __ofirst) { |
| 265 | using _ValueType = typename iterator_traits<_ForwardIterator>::value_type; |
| 266 | auto __iter_move = [](auto&& __iter) -> decltype(auto) { return std::__deref_move(__iter); }; |
| 267 | |
| 268 | auto __result = std::__uninitialized_move<_ValueType>( |
| 269 | std::move(__ifirst), std::move(__ilast), std::move(__ofirst), __always_false(), __iter_move); |
| 270 | return std::move(__result.__out_); |
| 271 | } |
| 272 | |
| 273 | // uninitialized_move_n |
| 274 | |
| 275 | template <class _ValueType, |
| 276 | class _InputIterator, |
| 277 | class _Size, |
| 278 | class _ForwardIterator, |
| 279 | class _EndPredicate, |
| 280 | class _IterMove> |
| 281 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator> |
| 282 | __uninitialized_move_n( |
| 283 | _InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_moving, _IterMove __iter_move) { |
| 284 | auto __idx = __ofirst; |
| 285 | auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); }); |
| 286 | for (; __n > 0 && !__stop_moving(__idx); ++__idx, (void)++__ifirst, --__n) |
| 287 | ::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); |
| 288 | __guard.__complete(); |
| 289 | |
| 290 | return {std::move(__ifirst), std::move(__idx)}; |
| 291 | } |
| 292 | |
| 293 | template <class _InputIterator, class _Size, class _ForwardIterator> |
| 294 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<_InputIterator, _ForwardIterator> |
| 295 | uninitialized_move_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst) { |
| 296 | using _ValueType = typename iterator_traits<_ForwardIterator>::value_type; |
| 297 | auto __iter_move = [](auto&& __iter) -> decltype(auto) { return std::__deref_move(__iter); }; |
| 298 | |
| 299 | auto __result = std::__uninitialized_move_n<_ValueType>( |
| 300 | std::move(__ifirst), __n, std::move(__ofirst), __always_false(), __iter_move); |
| 301 | return {std::move(__result.__in_), std::move(__result.__out_)}; |
| 302 | } |
| 303 | |
| 304 | #endif // _LIBCPP_STD_VER >= 17 |
| 305 | |
| 306 | // __uninitialized_relocate relocates the objects in [__first, __last) into __result element-by-element from __first to |
| 307 | // __last. Relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and |
| 308 | // destroy, except that the move constructor and destructor may never be called if they are known to be equivalent to a |
| 309 | // memcpy. |
| 310 | // |
| 311 | // Preconditions: At __result there is no object and [__first, __last) contains objects |
| 312 | // Postconditions: If no exceptions were thrown, __result contains the objects from [__first, __last), otherwise it |
| 313 | // doesn't contain any objects. |
| 314 | // [__first, __last) doesn't contain any objects. |
| 315 | template <class _ContiguousIterator> |
| 316 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void |
| 317 | __uninitialized_relocate(_ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) { |
| 318 | static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, "" ); |
| 319 | using _ValueType = typename iterator_traits<_ContiguousIterator>::value_type; |
| 320 | |
| 321 | // TODO: Accept non-trivially relocatable types |
| 322 | static_assert(__is_trivially_relocatable_v<_ValueType>); |
| 323 | |
| 324 | if (__libcpp_is_constant_evaluated()) { |
| 325 | auto __iter = __first; |
| 326 | while (__iter != __last) { |
| 327 | std::__construct_at(std::__to_address(__result), std::move(*__iter)); |
| 328 | std::__destroy_at(std::__to_address(__iter)); |
| 329 | ++__iter; |
| 330 | ++__result; |
| 331 | } |
| 332 | } else { |
| 333 | // Casting to void* to suppress clang complaining that this is technically UB. |
| 334 | __builtin_memmove(static_cast<void*>(std::__to_address(__result)), |
| 335 | std::__to_address(__first), |
| 336 | sizeof(_ValueType) * (__last - __first)); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | template <class _Alloc, class _Iter> |
| 341 | class _AllocatorDestroyRangeReverse { |
| 342 | public: |
| 343 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 |
| 344 | _AllocatorDestroyRangeReverse(_Alloc& __alloc, _Iter& __first, _Iter& __last) |
| 345 | : __alloc_(__alloc), __first_(__first), __last_(__last) {} |
| 346 | |
| 347 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void operator()() const { |
| 348 | std::__allocator_destroy(__alloc_, std::reverse_iterator<_Iter>(__last_), std::reverse_iterator<_Iter>(__first_)); |
| 349 | } |
| 350 | |
| 351 | private: |
| 352 | _Alloc& __alloc_; |
| 353 | _Iter& __first_; |
| 354 | _Iter& __last_; |
| 355 | }; |
| 356 | |
| 357 | // Copy-construct [__first1, __last1) in [__first2, __first2 + N), where N is distance(__first1, __last1). |
| 358 | // |
| 359 | // The caller has to ensure that __first2 can hold at least N uninitialized elements. If an exception is thrown the |
| 360 | // already copied elements are destroyed in reverse order of their construction. |
| 361 | template <class _Alloc, class _Iter1, class _Sent1, class _Iter2> |
| 362 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2 |
| 363 | __uninitialized_allocator_copy_impl(_Alloc& __alloc, _Iter1 __first1, _Sent1 __last1, _Iter2 __first2) { |
| 364 | auto __destruct_first = __first2; |
| 365 | auto __guard = |
| 366 | std::__make_exception_guard(_AllocatorDestroyRangeReverse<_Alloc, _Iter2>(__alloc, __destruct_first, __first2)); |
| 367 | while (__first1 != __last1) { |
| 368 | allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__first2), *__first1); |
| 369 | ++__first1; |
| 370 | ++__first2; |
| 371 | } |
| 372 | __guard.__complete(); |
| 373 | return __first2; |
| 374 | } |
| 375 | |
| 376 | template <class _Alloc, class _Type> |
| 377 | inline const bool __allocator_has_trivial_copy_construct_v = !__has_construct_v<_Alloc, _Type*, const _Type&>; |
| 378 | |
| 379 | template <class _Type> |
| 380 | inline const bool __allocator_has_trivial_copy_construct_v<allocator<_Type>, _Type> = true; |
| 381 | |
| 382 | template <class _Alloc, |
| 383 | class _In, |
| 384 | class _Out, |
| 385 | __enable_if_t<is_trivially_copy_constructible<_In>::value && is_trivially_assignable<_Out&, _In&>::value && |
| 386 | is_same<__remove_const_t<_In>, __remove_const_t<_Out> >::value && |
| 387 | __allocator_has_trivial_copy_construct_v<_Alloc, _In>, |
| 388 | int> = 0> |
| 389 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Out* |
| 390 | __uninitialized_allocator_copy_impl(_Alloc&, _In* __first1, _In* __last1, _Out* __first2) { |
| 391 | if (__libcpp_is_constant_evaluated()) { |
| 392 | while (__first1 != __last1) { |
| 393 | std::__construct_at(std::__to_address(__first2), *__first1); |
| 394 | ++__first1; |
| 395 | ++__first2; |
| 396 | } |
| 397 | return __first2; |
| 398 | } else { |
| 399 | return std::copy(__first1, __last1, __first2); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | template <class _Alloc, class _Iter1, class _Sent1, class _Iter2> |
| 404 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2 |
| 405 | __uninitialized_allocator_copy(_Alloc& __alloc, _Iter1 __first1, _Sent1 __last1, _Iter2 __first2) { |
| 406 | auto __unwrapped_range = std::__unwrap_range(std::move(__first1), std::move(__last1)); |
| 407 | auto __result = std::__uninitialized_allocator_copy_impl( |
| 408 | __alloc, std::move(__unwrapped_range.first), std::move(__unwrapped_range.second), std::__unwrap_iter(__first2)); |
| 409 | return std::__rewrap_iter(__first2, __result); |
| 410 | } |
| 411 | |
| 412 | template <class _Alloc, class _Type> |
| 413 | inline const bool __allocator_has_trivial_move_construct_v = !__has_construct_v<_Alloc, _Type*, _Type&&>; |
| 414 | |
| 415 | template <class _Type> |
| 416 | inline const bool __allocator_has_trivial_move_construct_v<allocator<_Type>, _Type> = true; |
| 417 | |
| 418 | template <class _Alloc, class _Tp> |
| 419 | inline const bool __allocator_has_trivial_destroy_v = !__has_destroy_v<_Alloc, _Tp*>; |
| 420 | |
| 421 | template <class _Tp, class _Up> |
| 422 | inline const bool __allocator_has_trivial_destroy_v<allocator<_Tp>, _Up> = true; |
| 423 | |
| 424 | // __uninitialized_allocator_relocate relocates the objects in [__first, __last) into __result. |
| 425 | // Relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and destroy, |
| 426 | // except that the move constructor and destructor may never be called if they are known to be equivalent to a memcpy. |
| 427 | // |
| 428 | // Preconditions: __result doesn't contain any objects and [__first, __last) contains objects |
| 429 | // Postconditions: __result contains the objects from [__first, __last) and |
| 430 | // [__first, __last) doesn't contain any objects |
| 431 | // |
| 432 | // The strong exception guarantee is provided if any of the following are true: |
| 433 | // - is_nothrow_move_constructible<_ValueType> |
| 434 | // - is_copy_constructible<_ValueType> |
| 435 | // - __is_trivially_relocatable_v<_ValueType> |
| 436 | template <class _Alloc, class _ContiguousIterator> |
| 437 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __uninitialized_allocator_relocate( |
| 438 | _Alloc& __alloc, _ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) { |
| 439 | static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, "" ); |
| 440 | static_assert( |
| 441 | __is_cpp17_move_insertable_v<_Alloc>, "The specified type does not meet the requirements of Cpp17MoveInsertable" ); |
| 442 | |
| 443 | using __value_type = typename iterator_traits<_ContiguousIterator>::value_type; |
| 444 | |
| 445 | if _LIBCPP_CONSTEXPR (__allocator_has_trivial_move_construct_v<_Alloc, __value_type> && |
| 446 | __allocator_has_trivial_destroy_v<_Alloc, __value_type> && |
| 447 | __is_trivially_relocatable_v<__value_type>) { |
| 448 | if (!__libcpp_is_constant_evaluated()) { |
| 449 | // Casting to void* to suppress clang complaining that this is technically UB. |
| 450 | __builtin_memcpy(static_cast<void*>(std::__to_address(__result)), |
| 451 | std::__to_address(__first), |
| 452 | sizeof(__value_type) * (__last - __first)); |
| 453 | return; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | using __alloc_traits = allocator_traits<_Alloc>; |
| 458 | #ifndef _LIBCPP_CXX03_LANG |
| 459 | if constexpr (!_LIBCPP_HAS_EXCEPTIONS || is_nothrow_move_constructible<__value_type>::value) { |
| 460 | while (__first != __last) { |
| 461 | __alloc_traits::construct(__alloc, std::__to_address(__result), std::move(*__first)); |
| 462 | __alloc_traits::destroy(__alloc, std::__to_address(__first)); |
| 463 | ++__first; |
| 464 | ++__result; |
| 465 | } |
| 466 | } else |
| 467 | #endif |
| 468 | { |
| 469 | auto __destruct_first = __result; |
| 470 | auto __guard = std::__make_exception_guard( |
| 471 | _AllocatorDestroyRangeReverse<_Alloc, _ContiguousIterator>(__alloc, __destruct_first, __result)); |
| 472 | auto __iter = __first; |
| 473 | while (__iter != __last) { |
| 474 | allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move_if_noexcept(*__iter)); |
| 475 | ++__iter; |
| 476 | ++__result; |
| 477 | } |
| 478 | __guard.__complete(); |
| 479 | std::__allocator_destroy(__alloc, __first, __last); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | _LIBCPP_END_NAMESPACE_STD |
| 484 | |
| 485 | _LIBCPP_POP_MACROS |
| 486 | |
| 487 | #endif // _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H |
| 488 | |