From d9a4f936d05c1e8740f5f73da1b149c36d25d02c Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Tue, 3 Nov 2020 12:05:55 -0500 Subject: [PATCH] [libc++] Move helpers outside of std::allocator_traits They don't really belong as members of allocator_traits. --- libcxx/include/memory | 198 ++++++++++++++++++++++---------------------------- libcxx/include/vector | 11 +-- 2 files changed, 91 insertions(+), 118 deletions(-) diff --git a/libcxx/include/memory b/libcxx/include/memory index f3b0b04..25f940a 100644 --- a/libcxx/include/memory +++ b/libcxx/include/memory @@ -1458,118 +1458,7 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits __has_select_on_container_copy_construction(), __a);} - template - _LIBCPP_INLINE_VISIBILITY - static - void - __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) - { - static_assert(__is_cpp17_move_insertable::value, - "The specified type does not meet the requirements of Cpp17MoveInsertible"); - for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) - construct(__a, _VSTD::__to_address(__begin2), -#ifdef _LIBCPP_NO_EXCEPTIONS - _VSTD::move(*__begin1) -#else - _VSTD::move_if_noexcept(*__begin1) -#endif - ); - } - - template - _LIBCPP_INLINE_VISIBILITY - static - typename enable_if - < - (__is_default_allocator::value - || !__has_construct::value) && - is_trivially_move_constructible<_Tp>::value, - void - >::type - __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) - { - ptrdiff_t _Np = __end1 - __begin1; - if (_Np > 0) - { - _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp)); - __begin2 += _Np; - } - } - - template - _LIBCPP_INLINE_VISIBILITY - static - void - __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) - { - for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) - construct(__a, _VSTD::__to_address(__begin2), *__begin1); - } - - template ::type, - class _RawDestTp = typename remove_const<_DestTp>::type> - _LIBCPP_INLINE_VISIBILITY - static - typename enable_if - < - is_trivially_copy_constructible<_DestTp>::value && - is_same<_RawSourceTp, _RawDestTp>::value && - (__is_default_allocator::value || - !__has_construct::value), - void - >::type - __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2) - { - ptrdiff_t _Np = __end1 - __begin1; - if (_Np > 0) - { - _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp)); - __begin2 += _Np; - } - } - - template - _LIBCPP_INLINE_VISIBILITY - static - void - __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) - { - static_assert(__is_cpp17_move_insertable::value, - "The specified type does not meet the requirements of Cpp17MoveInsertable"); - while (__end1 != __begin1) - { - construct(__a, _VSTD::__to_address(__end2 - 1), -#ifdef _LIBCPP_NO_EXCEPTIONS - _VSTD::move(*--__end1) -#else - _VSTD::move_if_noexcept(*--__end1) -#endif - ); - --__end2; - } - } - - template - _LIBCPP_INLINE_VISIBILITY - static - typename enable_if - < - (__is_default_allocator::value - || !__has_construct::value) && - is_trivially_move_constructible<_Tp>::value, - void - >::type - __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) - { - ptrdiff_t _Np = __end1 - __begin1; - __end2 -= _Np; - if (_Np > 0) - _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp)); - } - private: - _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 static pointer __allocate(allocator_type& __a, size_type __n, const_void_pointer __hint, true_type) @@ -1822,6 +1711,93 @@ template inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;} +template +_LIBCPP_INLINE_VISIBILITY +void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) { + static_assert(__is_cpp17_move_insertable<_Alloc>::value, + "The specified type does not meet the requirements of Cpp17MoveInsertible"); + typedef allocator_traits<_Alloc> _Traits; + for (; __begin1 != __end1; ++__begin1, (void)++__begin2) { + _Traits::construct(__a, _VSTD::__to_address(__begin2), +#ifdef _LIBCPP_NO_EXCEPTIONS + _VSTD::move(*__begin1) +#else + _VSTD::move_if_noexcept(*__begin1) +#endif + ); + } +} + +template ::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && + is_trivially_move_constructible<_Tp>::value +>::type> +_LIBCPP_INLINE_VISIBILITY +void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) { + ptrdiff_t _Np = __end1 - __begin1; + if (_Np > 0) { + _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp)); + __begin2 += _Np; + } +} + +template +_LIBCPP_INLINE_VISIBILITY +void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) { + typedef allocator_traits<_Alloc> _Traits; + for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) { + _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1); + } +} + +template ::type, + class _RawDest = typename remove_const<_Dest>::type, + class = + typename enable_if< + is_trivially_copy_constructible<_Dest>::value && + is_same<_RawSource, _RawDest>::value && + (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value) + >::type> +_LIBCPP_INLINE_VISIBILITY +void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) { + ptrdiff_t _Np = __end1 - __begin1; + if (_Np > 0) { + _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest)); + __begin2 += _Np; + } +} + +template +_LIBCPP_INLINE_VISIBILITY +void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) { + static_assert(__is_cpp17_move_insertable<_Alloc>::value, + "The specified type does not meet the requirements of Cpp17MoveInsertable"); + typedef allocator_traits<_Alloc> _Traits; + while (__end1 != __begin1) { + _Traits::construct(__a, _VSTD::__to_address(__end2 - 1), +#ifdef _LIBCPP_NO_EXCEPTIONS + _VSTD::move(*--__end1) +#else + _VSTD::move_if_noexcept(*--__end1) +#endif + ); + --__end2; + } +} + +template ::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && + is_trivially_move_constructible<_Tp>::value +>::type> +_LIBCPP_INLINE_VISIBILITY +void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) { + ptrdiff_t _Np = __end1 - __begin1; + __end2 -= _Np; + if (_Np > 0) + _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp)); +} + template class _LIBCPP_TEMPLATE_VIS raw_storage_iterator : public iterator::__swap_out_circular_buffer(__split_buffer__alloc(), this->__begin_, this->__end_, __v.__begin_); + __construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_); _VSTD::swap(this->__begin_, __v.__begin_); _VSTD::swap(this->__end_, __v.__end_); _VSTD::swap(this->__end_cap(), __v.__end_cap()); @@ -967,10 +966,8 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer__alloc(), this->__begin_, __p, __v.__begin_); - __alloc_traits::__construct_forward_with_exception_guarantees( - this->__alloc(), __p, this->__end_, __v.__end_); + __construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_); + __construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_); _VSTD::swap(this->__begin_, __v.__begin_); _VSTD::swap(this->__end_, __v.__end_); _VSTD::swap(this->__end_cap(), __v.__end_cap()); @@ -1077,7 +1074,7 @@ typename enable_if vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n) { _ConstructTransaction __tx(*this, __n); - __alloc_traits::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_); + __construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_); } // Default constructs __n objects starting at __end_ -- 2.7.4