|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef _LIBCPP___MEMORY_UNINITIALIZED_RELOCATE_H |
| 10 | +#define _LIBCPP___MEMORY_UNINITIALIZED_RELOCATE_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__iterator/iterator_traits.h> |
| 14 | +#include <__memory/allocator_traits.h> |
| 15 | +#include <__memory/is_trivially_allocator_relocatable.h> |
| 16 | +#include <__memory/pointer_traits.h> |
| 17 | +#include <__memory/relocate_at.h> |
| 18 | +#include <__type_traits/is_constant_evaluated.h> |
| 19 | +#include <__type_traits/is_nothrow_constructible.h> |
| 20 | +#include <__type_traits/is_trivially_relocatable.h> |
| 21 | +#include <__utility/move.h> |
| 22 | + |
| 23 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 24 | +# pragma GCC system_header |
| 25 | +#endif |
| 26 | + |
| 27 | +_LIBCPP_PUSH_MACROS |
| 28 | +#include <__undef_macros> |
| 29 | + |
| 30 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 31 | + |
| 32 | +// __uninitialized_relocate relocates the objects in [__first, __last) into __result. |
| 33 | +// |
| 34 | +// Relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and destroy, |
| 35 | +// except that the move constructor and destructor may never be called if they are known to be equivalent to a memcpy. |
| 36 | +// |
| 37 | +// This algorithm works even if part of the resulting range overlaps with [__first, __last), as long as __result itself |
| 38 | +// is not in [__first, last). |
| 39 | +// |
| 40 | +// This algorithm doesn't throw any exceptions. However, it requires the types in the range to be nothrow |
| 41 | +// move-constructible and the iterator operations not to throw any exceptions. |
| 42 | +// |
| 43 | +// Preconditions: |
| 44 | +// - __result doesn't contain any objects and [__first, __last) contains objects |
| 45 | +// - __result is not in [__first, __last) |
| 46 | +// Postconditions: __result contains the objects from [__first, __last) and |
| 47 | +// [__first, __last) doesn't contain any objects |
| 48 | +template <class _ContiguousIterator> |
| 49 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ContiguousIterator __uninitialized_relocate( |
| 50 | + _ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) _NOEXCEPT { |
| 51 | + using _ValueType = typename iterator_traits<_ContiguousIterator>::value_type; |
| 52 | + static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, ""); |
| 53 | + static_assert(is_nothrow_move_constructible<_ValueType>::value, ""); |
| 54 | + if (!__libcpp_is_constant_evaluated() && __libcpp_is_trivially_relocatable<_ValueType>::value) { |
| 55 | + auto const __n = __last - __first; |
| 56 | + // Casting to void* to suppress clang complaining that this is technically UB. |
| 57 | + __builtin_memmove( |
| 58 | + static_cast<void*>(std::__to_address(__result)), std::__to_address(__first), sizeof(_ValueType) * __n); |
| 59 | + return __result + __n; |
| 60 | + } else { |
| 61 | + while (__first != __last) { |
| 62 | + std::__relocate_at(std::__to_address(__first), std::__to_address(__result)); |
| 63 | + ++__first; |
| 64 | + ++__result; |
| 65 | + } |
| 66 | + return __result; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Equivalent to __uninitialized_relocate, but uses the provided allocator's construct() and destroy() methods. |
| 71 | +template <class _Alloc, class _ContiguousIterator> |
| 72 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ContiguousIterator __uninitialized_allocator_relocate( |
| 73 | + _Alloc& __alloc, _ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) _NOEXCEPT { |
| 74 | + using _ValueType = typename iterator_traits<_ContiguousIterator>::value_type; |
| 75 | + static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, ""); |
| 76 | + static_assert(is_nothrow_move_constructible<_ValueType>::value, ""); |
| 77 | + if (!__libcpp_is_constant_evaluated() && __is_trivially_allocator_relocatable<_Alloc, _ValueType>::value) { |
| 78 | + (void)__alloc; // ignore the allocator |
| 79 | + return std::__uninitialized_relocate(std::move(__first), std::move(__last), std::move(__result)); |
| 80 | + } else { |
| 81 | + while (__first != __last) { |
| 82 | + std::__allocator_relocate_at(__alloc, std::__to_address(__first), std::__to_address(__result)); |
| 83 | + ++__first; |
| 84 | + ++__result; |
| 85 | + } |
| 86 | + return __result; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +_LIBCPP_END_NAMESPACE_STD |
| 91 | + |
| 92 | +_LIBCPP_POP_MACROS |
| 93 | + |
| 94 | +#endif // _LIBCPP___MEMORY_UNINITIALIZED_RELOCATE_H |
0 commit comments