blob: e16331f3d21317b0b888697426b6cb6993f63bcc [file]
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
#define _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
#include <__algorithm/in_out_result.h>
#include <__algorithm/unwrap_iter.h>
#include <__config>
#include <__fwd/memory.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/reverse_iterator.h>
#include <__memory/addressof.h>
#include <__memory/allocator_traits.h>
#include <__memory/construct_at.h>
#include <__memory/destroy.h>
#include <__memory/pointer_traits.h>
#include <__string/constexpr_c_functions.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_always_bitcastable.h>
#include <__type_traits/is_constant_evaluated.h>
#include <__type_traits/is_nothrow_constructible.h>
#include <__type_traits/is_reference.h>
#include <__type_traits/is_relocatable.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_trivially_assignable.h>
#include <__type_traits/is_trivially_constructible.h>
#include <__type_traits/remove_const.h>
#include <__utility/exception_guard.h>
#include <__utility/move.h>
#include <__utility/pair.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
struct __always_false {
template <class... _Args>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(_Args&&...) const _NOEXCEPT {
return false;
}
};
// uninitialized_copy
template <class _ValueType, class _InputIterator, class _Sentinel1, class _ForwardIterator, class _EndPredicate>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator>
__uninitialized_copy(
_InputIterator __ifirst, _Sentinel1 __ilast, _ForwardIterator __ofirst, _EndPredicate __stop_copying) {
_ForwardIterator __idx = __ofirst;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); });
for (; __ifirst != __ilast && !__stop_copying(__idx); ++__ifirst, (void)++__idx)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(*__ifirst);
__guard.__complete();
return {std::move(__ifirst), std::move(__idx)};
}
template <class _InputIterator, class _ForwardIterator>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
uninitialized_copy(_InputIterator __ifirst, _InputIterator __ilast, _ForwardIterator __ofirst) {
typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
auto __result = std::__uninitialized_copy<_ValueType>(
std::move(__ifirst), std::move(__ilast), std::move(__ofirst), __always_false());
return std::move(__result.__out_);
}
// uninitialized_copy_n
template <class _ValueType, class _InputIterator, class _Size, class _ForwardIterator, class _EndPredicate>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator>
__uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_copying) {
_ForwardIterator __idx = __ofirst;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); });
for (; __n > 0 && !__stop_copying(__idx); ++__ifirst, (void)++__idx, (void)--__n)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(*__ifirst);
__guard.__complete();
return {std::move(__ifirst), std::move(__idx)};
}
template <class _InputIterator, class _Size, class _ForwardIterator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst) {
typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
auto __result =
std::__uninitialized_copy_n<_ValueType>(std::move(__ifirst), __n, std::move(__ofirst), __always_false());
return std::move(__result.__out_);
}
// uninitialized_fill
template <class _ValueType, class _ForwardIterator, class _Sentinel, class _Tp>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
__uninitialized_fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __x) {
_ForwardIterator __idx = __first;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
for (; __idx != __last; ++__idx)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__x);
__guard.__complete();
return __idx;
}
template <class _ForwardIterator, class _Tp>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void
uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x) {
typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
(void)std::__uninitialized_fill<_ValueType>(__first, __last, __x);
}
// uninitialized_fill_n
template <class _ValueType, class _ForwardIterator, class _Size, class _Tp>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
__uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) {
_ForwardIterator __idx = __first;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
for (; __n > 0; ++__idx, (void)--__n)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__x);
__guard.__complete();
return __idx;
}
template <class _ForwardIterator, class _Size, class _Tp>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) {
typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
return std::__uninitialized_fill_n<_ValueType>(__first, __n, __x);
}
#if _LIBCPP_STD_VER >= 17
template <class _Iter>
_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) __deref_move(_Iter& __it) {
if constexpr (is_lvalue_reference_v<decltype(*__it)>)
return std::move(*__it);
else
return *__it;
}
// uninitialized_default_construct
template <class _ValueType, class _ForwardIterator, class _Sentinel>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
__uninitialized_default_construct(_ForwardIterator __first, _Sentinel __last) {
auto __idx = __first;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
for (; __idx != __last; ++__idx)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType;
__guard.__complete();
return __idx;
}
template <class _ForwardIterator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void
uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
(void)std::__uninitialized_default_construct<_ValueType>(std::move(__first), std::move(__last));
}
// uninitialized_default_construct_n
template <class _ValueType, class _ForwardIterator, class _Size>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
__uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
auto __idx = __first;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
for (; __n > 0; ++__idx, (void)--__n)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType;
__guard.__complete();
return __idx;
}
template <class _ForwardIterator, class _Size>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
return std::__uninitialized_default_construct_n<_ValueType>(std::move(__first), __n);
}
// uninitialized_value_construct
template <class _ValueType, class _ForwardIterator, class _Sentinel>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
__uninitialized_value_construct(_ForwardIterator __first, _Sentinel __last) {
auto __idx = __first;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
for (; __idx != __last; ++__idx)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType();
__guard.__complete();
return __idx;
}
template <class _ForwardIterator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void
uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
(void)std::__uninitialized_value_construct<_ValueType>(std::move(__first), std::move(__last));
}
// uninitialized_value_construct_n
template <class _ValueType, class _ForwardIterator, class _Size>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
__uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
auto __idx = __first;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
for (; __n > 0; ++__idx, (void)--__n)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType();
__guard.__complete();
return __idx;
}
template <class _ForwardIterator, class _Size>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
return std::__uninitialized_value_construct_n<_ValueType>(std::move(__first), __n);
}
// uninitialized_move
template <class _ValueType,
class _InputIterator,
class _Sentinel1,
class _ForwardIterator,
class _EndPredicate,
class _IterMove>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator>
__uninitialized_move(_InputIterator __ifirst,
_Sentinel1 __ilast,
_ForwardIterator __ofirst,
_EndPredicate __stop_moving,
_IterMove __iter_move) {
auto __idx = __ofirst;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); });
for (; __ifirst != __ilast && !__stop_moving(__idx); ++__idx, (void)++__ifirst) {
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst));
}
__guard.__complete();
return {std::move(__ifirst), std::move(__idx)};
}
template <class _InputIterator, class _ForwardIterator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _ForwardIterator
uninitialized_move(_InputIterator __ifirst, _InputIterator __ilast, _ForwardIterator __ofirst) {
using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
auto __iter_move = [](auto&& __iter) -> decltype(auto) { return std::__deref_move(__iter); };
auto __result = std::__uninitialized_move<_ValueType>(
std::move(__ifirst), std::move(__ilast), std::move(__ofirst), __always_false(), __iter_move);
return std::move(__result.__out_);
}
// uninitialized_move_n
template <class _ValueType,
class _InputIterator,
class _Size,
class _ForwardIterator,
class _EndPredicate,
class _IterMove>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __in_out_result<_InputIterator, _ForwardIterator>
__uninitialized_move_n(
_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst, _EndPredicate __stop_moving, _IterMove __iter_move) {
auto __idx = __ofirst;
auto __guard = std::__make_exception_guard([&] { std::__destroy(__ofirst, __idx); });
for (; __n > 0 && !__stop_moving(__idx); ++__idx, (void)++__ifirst, --__n)
::new (static_cast<void*>(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst));
__guard.__complete();
return {std::move(__ifirst), std::move(__idx)};
}
template <class _InputIterator, class _Size, class _ForwardIterator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<_InputIterator, _ForwardIterator>
uninitialized_move_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofirst) {
using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
auto __iter_move = [](auto&& __iter) -> decltype(auto) { return std::__deref_move(__iter); };
auto __result = std::__uninitialized_move_n<_ValueType>(
std::move(__ifirst), __n, std::move(__ofirst), __always_false(), __iter_move);
return {std::move(__result.__in_), std::move(__result.__out_)};
}
#endif // _LIBCPP_STD_VER >= 17
// __uninitialized_relocate relocates the objects in [__first, __last) into __result element-by-element from __first to
// __last. Relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and
// destroy, except that the move constructor and destructor may never be called if they are known to be equivalent to a
// memcpy.
//
// Preconditions: At __result there is no object and [__first, __last) contains objects
// Postconditions: If no exceptions were thrown, __result contains the objects from [__first, __last), otherwise it
// doesn't contain any objects.
// [__first, __last) doesn't contain any objects.
template <class _ContiguousIterator>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
__uninitialized_relocate(_ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) {
static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, "");
using _ValueType = typename iterator_traits<_ContiguousIterator>::value_type;
// TODO: Accept non-trivially relocatable types
static_assert(__is_trivially_relocatable_v<_ValueType>);
if (__libcpp_is_constant_evaluated()) {
auto __iter = __first;
while (__iter != __last) {
std::__construct_at(std::__to_address(__result), std::move(*__iter));
std::__destroy_at(std::__to_address(__iter));
++__iter;
++__result;
}
} else {
// Casting to void* to suppress clang complaining that this is technically UB.
__builtin_memmove(static_cast<void*>(std::__to_address(__result)),
std::__to_address(__first),
sizeof(_ValueType) * (__last - __first));
}
}
template <class _Alloc, class _Iter>
class _AllocatorDestroyRangeReverse {
public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
_AllocatorDestroyRangeReverse(_Alloc& __alloc, _Iter& __first, _Iter& __last)
: __alloc_(__alloc), __first_(__first), __last_(__last) {}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void operator()() const {
std::__allocator_destroy(__alloc_, std::reverse_iterator<_Iter>(__last_), std::reverse_iterator<_Iter>(__first_));
}
private:
_Alloc& __alloc_;
_Iter& __first_;
_Iter& __last_;
};
// Copy-construct [__first1, __last1) in [__first2, __first2 + N), where N is distance(__first1, __last1).
//
// The caller has to ensure that __first2 can hold at least N uninitialized elements. If an exception is thrown the
// already copied elements are destroyed in reverse order of their construction.
template <class _Alloc, class _Iter1, class _Size, class _Iter2>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2
__uninitialized_allocator_copy_n_impl(_Alloc& __alloc, _Iter1 __first1, _Size __count, _Iter2 __first2) {
auto __destruct_first = __first2;
auto __guard =
std::__make_exception_guard(_AllocatorDestroyRangeReverse<_Alloc, _Iter2>(__alloc, __destruct_first, __first2));
for (_Size __i = 0; __i != __count; ++__i) {
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__first2), *__first1);
++__first1;
++__first2;
}
__guard.__complete();
return __first2;
}
template <class _Alloc, class _Type>
inline const bool __allocator_has_trivial_copy_construct_v = !__has_construct_v<_Alloc, _Type*, const _Type&>;
template <class _Type>
inline const bool __allocator_has_trivial_copy_construct_v<allocator<_Type>, _Type> = true;
template <class _Alloc,
class _In,
class _Size,
class _Out,
__enable_if_t<__is_always_bitcastable<_In, _Out>::value && is_trivially_constructible<_Out, _In>::value &&
is_trivially_assignable<_Out&, _In&>::value &&
is_same<__remove_const_t<_In>, __remove_const_t<_Out> >::value &&
__allocator_has_trivial_copy_construct_v<_Alloc, _In>,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Out*
__uninitialized_allocator_copy_n_impl(_Alloc&, _In* __first1, _Size __count, _Out* __first2) {
if (__libcpp_is_constant_evaluated()) {
for (_Size __i = 0; __i != __count; ++__i) {
std::__construct_at(std::__to_address(__first2), *__first1);
++__first1;
++__first2;
}
return __first2;
} else {
std::__constexpr_memmove(__first2, __first1, __element_count(__count));
return __first2 + __count;
}
}
template <class _Alloc, class _Iter1, class _Size, class _Iter2>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2
__uninitialized_allocator_copy_n(_Alloc& __alloc, _Iter1 __first1, _Size __count, _Iter2 __first2) {
auto __result = std::__uninitialized_allocator_copy_n_impl(
__alloc, std::__unwrap_iter(std::move(__first1)), __count, std::__unwrap_iter(__first2));
return std::__rewrap_iter(__first2, __result);
}
template <class _Alloc, class _Type>
inline const bool __allocator_has_trivial_move_construct_v = !__has_construct_v<_Alloc, _Type*, _Type&&>;
template <class _Type>
inline const bool __allocator_has_trivial_move_construct_v<allocator<_Type>, _Type> = true;
template <class _Alloc, class _Tp>
inline const bool __allocator_has_trivial_destroy_v = !__has_destroy_v<_Alloc, _Tp*>;
template <class _Tp, class _Up>
inline const bool __allocator_has_trivial_destroy_v<allocator<_Tp>, _Up> = true;
// __uninitialized_allocator_relocate relocates the objects in [__first, __last) into __result.
// Relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and destroy,
// except that the move constructor and destructor may never be called if they are known to be equivalent to a memcpy.
//
// Preconditions: __result doesn't contain any objects and [__first, __last) contains objects
// Postconditions: __result contains the objects from [__first, __last) and
// [__first, __last) doesn't contain any objects
//
// The strong exception guarantee is provided if any of the following are true:
// - is_nothrow_move_constructible<_ValueType>
// - is_copy_constructible<_ValueType>
// - __is_trivially_relocatable_v<_ValueType>
template <class _Alloc, class _ContiguousIterator>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __uninitialized_allocator_relocate(
_Alloc& __alloc, _ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) {
static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, "");
static_assert(
__is_cpp17_move_insertable_v<_Alloc>, "The specified type does not meet the requirements of Cpp17MoveInsertable");
using __value_type = typename iterator_traits<_ContiguousIterator>::value_type;
if _LIBCPP_CONSTEXPR (__allocator_has_trivial_move_construct_v<_Alloc, __value_type> &&
__allocator_has_trivial_destroy_v<_Alloc, __value_type> &&
__is_trivially_relocatable_v<__value_type>) {
if (!__libcpp_is_constant_evaluated()) {
// Casting to void* to suppress clang complaining that this is technically UB.
__builtin_memcpy(static_cast<void*>(std::__to_address(__result)),
std::__to_address(__first),
sizeof(__value_type) * (__last - __first));
return;
}
}
using __alloc_traits = allocator_traits<_Alloc>;
#ifndef _LIBCPP_CXX03_LANG
if constexpr (!_LIBCPP_HAS_EXCEPTIONS || is_nothrow_move_constructible<__value_type>::value) {
while (__first != __last) {
__alloc_traits::construct(__alloc, std::__to_address(__result), std::move(*__first));
__alloc_traits::destroy(__alloc, std::__to_address(__first));
++__first;
++__result;
}
} else
#endif
{
auto __destruct_first = __result;
auto __guard = std::__make_exception_guard(
_AllocatorDestroyRangeReverse<_Alloc, _ContiguousIterator>(__alloc, __destruct_first, __result));
auto __iter = __first;
while (__iter != __last) {
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move_if_noexcept(*__iter));
++__iter;
++__result;
}
__guard.__complete();
std::__allocator_destroy(__alloc, __first, __last);
}
}
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H