|
| 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_RELOCATE_AT_H |
| 10 | +#define _LIBCPP___MEMORY_RELOCATE_AT_H |
| 11 | + |
| 12 | +#include <__memory/allocator_traits.h> |
| 13 | +#include <__memory/construct_at.h> |
| 14 | +#include <__memory/is_trivially_allocator_relocatable.h> |
| 15 | +#include <__type_traits/enable_if.h> |
| 16 | +#include <__type_traits/is_constant_evaluated.h> |
| 17 | +#include <__type_traits/is_trivially_relocatable.h> |
| 18 | +#include <__utility/move.h> |
| 19 | +#include <__utility/scope_guard.h> |
| 20 | + |
| 21 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 22 | +# pragma GCC system_header |
| 23 | +#endif |
| 24 | + |
| 25 | +_LIBCPP_PUSH_MACROS |
| 26 | +#include <__undef_macros> |
| 27 | + |
| 28 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | + |
| 30 | +template <class _Tp> |
| 31 | +struct __destroy_object { |
| 32 | + _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()() const { std::__destroy_at(__obj_); } |
| 33 | + _Tp* __obj_; |
| 34 | +}; |
| 35 | + |
| 36 | +template <class _Alloc, class _Tp> |
| 37 | +struct __allocator_destroy_object { |
| 38 | + _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()() const { allocator_traits<_Alloc>::destroy(__alloc_, __obj_); } |
| 39 | + _Alloc& __alloc_; |
| 40 | + _Tp* __obj_; |
| 41 | +}; |
| 42 | + |
| 43 | +template <class _Tp> |
| 44 | +_LIBCPP_HIDE_FROM_ABI _Tp* __libcpp_builtin_trivially_relocate_at(_Tp* __source, _Tp* __dest) _NOEXCEPT { |
| 45 | + static_assert(__libcpp_is_trivially_relocatable<_Tp>::value, ""); |
| 46 | + // Casting to void* to suppress clang complaining that this is technically UB. |
| 47 | + __builtin_memcpy(static_cast<void*>(__dest), __source, sizeof(_Tp)); |
| 48 | + return __dest; |
| 49 | +} |
| 50 | + |
| 51 | +template <class _Tp> |
| 52 | +_LIBCPP_HIDE_FROM_ABI _Tp* __libcpp_builtin_trivially_relocate_at(_Tp* __first, Tp* __last, _Tp* __dest) _NOEXCEPT { |
| 53 | + static_assert(__libcpp_is_trivially_relocatable<_Tp>::value, ""); |
| 54 | + // Casting to void* to suppress clang complaining that this is technically UB. |
| 55 | + __builtin_memmove(static_cast<void*>(__dest), __first, (__last - __first) * sizeof(_Tp)); |
| 56 | + return __dest; |
| 57 | +} |
| 58 | + |
| 59 | +template <class _Tp> |
| 60 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __relocate_at(_Tp* __source, _Tp* __dest) { |
| 61 | + if constexpr (__libcpp_is_trivially_relocatable<_Tp>::value) { |
| 62 | + if (!__libcpp_is_constant_evaluated()) { |
| 63 | + return std::__libcpp_builtin_trivially_relocate_at(__source, __dest); |
| 64 | + } |
| 65 | + } |
| 66 | + auto __guard = std::__make_scope_guard(__destroy_object<_Tp>{__source}); |
| 67 | + return std::__construct_at(__dest, std::move(*__source)); |
| 68 | +} |
| 69 | + |
| 70 | +template <class _Alloc, class _Tp> |
| 71 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* |
| 72 | +__allocator_relocate_at(_Alloc& __alloc, _Tp* __source, _Tp* __dest) { |
| 73 | + if constexpr (__allocator_has_trivial_move_construct<_Alloc, _Tp>::value && |
| 74 | + __allocator_has_trivial_destroy<_Alloc, _Tp>::value) { |
| 75 | + (void)__alloc; // ignore the allocator |
| 76 | + return std::__relocate_at(__source, __dest); |
| 77 | + } else { |
| 78 | + auto __guard = std::__make_scope_guard(__allocator_destroy_object<_Alloc, _Tp>{__alloc, __source}); |
| 79 | + allocator_traits<_Alloc>::construct(__alloc, __dest, std::move(*__source)); |
| 80 | + return __dest; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +_LIBCPP_END_NAMESPACE_STD |
| 85 | + |
| 86 | +_LIBCPP_POP_MACROS |
| 87 | + |
| 88 | +#endif // _LIBCPP___MEMORY_RELOCATE_AT_H |
0 commit comments