diff --git a/libcxx/include/__memory/construct_at.h b/libcxx/include/__memory/construct_at.h --- a/libcxx/include/__memory/construct_at.h +++ b/libcxx/include/__memory/construct_at.h @@ -12,7 +12,10 @@ #include <__config> #include <__debug> +#include <__iterator/access.h> +#include <__memory/addressof.h> #include <__utility/forward.h> +#include #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) @@ -43,13 +46,41 @@ #if _LIBCPP_STD_VER > 14 -template -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 +template +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 +void destroy(_ForwardIterator, _ForwardIterator); + +template , int> = 0> +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void destroy_at(_Tp* __loc) { _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at"); __loc->~_Tp(); } +#if _LIBCPP_STD_VER > 17 +template , int> = 0> +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 +void destroy_at(_Tp* __loc) { + _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at"); + _VSTD::destroy(_VSTD::begin(*__loc), _VSTD::end(*__loc)); +} +#endif + +template +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 +void destroy(_ForwardIterator __first, _ForwardIterator __last) { + for (; __first != __last; ++__first) + _VSTD::destroy_at(_VSTD::addressof(*__first)); +} + +template +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 +_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) { + for (; __n > 0; (void)++__first, --__n) + _VSTD::destroy_at(_VSTD::addressof(*__first)); + return __first; +} + #endif _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__memory/uninitialized_algorithms.h b/libcxx/include/__memory/uninitialized_algorithms.h --- a/libcxx/include/__memory/uninitialized_algorithms.h +++ b/libcxx/include/__memory/uninitialized_algorithms.h @@ -122,21 +122,6 @@ #if _LIBCPP_STD_VER > 14 -template -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 -void destroy(_ForwardIterator __first, _ForwardIterator __last) { - for (; __first != __last; ++__first) - _VSTD::destroy_at(_VSTD::addressof(*__first)); -} - -template -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 -_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) { - for (; __n > 0; (void)++__first, --__n) - _VSTD::destroy_at(_VSTD::addressof(*__first)); - return __first; -} - template inline _LIBCPP_INLINE_VISIBILITY void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) { diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy.pass.cpp --- a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy.pass.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "test_macros.h" #include "test_iterators.h" @@ -31,34 +32,89 @@ friend void operator&(Counted) = delete; }; -TEST_CONSTEXPR_CXX20 bool test() -{ +#if TEST_STD_VER > 17 +constexpr bool test_arrays() { + { + using Array = Counted[3]; + using Alloc = std::allocator; + int counter = 0; + Alloc alloc; + Array* pool = std::allocator_traits::allocate(alloc, 5); + + for (Array* p = pool; p != pool + 5; ++p) { + Array& arr = *p; + for (int i = 0; i != 3; ++i) { + std::allocator_traits::construct(alloc, std::addressof(arr[i]), &counter); + } + } + assert(counter == 5 * 3); + + std::destroy(pool, pool + 5); + ASSERT_SAME_TYPE(decltype(std::destroy(pool, pool + 5)), void); + assert(counter == 0); + + std::allocator_traits::deallocate(alloc, pool, 5); + } + { + using Array = Counted[3][2]; + using Alloc = std::allocator; + int counter = 0; + Alloc alloc; + Array* pool = std::allocator_traits::allocate(alloc, 5); + + for (Array* p = pool; p != pool + 5; ++p) { + Array& arr = *p; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != 2; ++j) { + std::allocator_traits::construct(alloc, std::addressof(arr[i][j]), &counter); + } + } + } + assert(counter == 5 * 3 * 2); + + std::destroy(pool, pool + 5); + ASSERT_SAME_TYPE(decltype(std::destroy(pool, pool + 5)), void); + assert(counter == 0); + + std::allocator_traits::deallocate(alloc, pool, 5); + } + + return true; +} +#endif + +template +TEST_CONSTEXPR_CXX20 void test() { using Alloc = std::allocator; int counter = 0; - int const N = 5; Alloc alloc; - Counted* pool = std::allocator_traits::allocate(alloc, N); + Counted* pool = std::allocator_traits::allocate(alloc, 5); - for (Counted* p = pool; p != pool + N; ++p) + for (Counted* p = pool; p != pool + 5; ++p) std::allocator_traits::construct(alloc, p, &counter); assert(counter == 5); - std::destroy(pool, pool + 1); - assert(counter == 4); - - std::destroy(forward_iterator(pool + 1), forward_iterator(pool + 5)); + std::destroy(It(pool), It(pool + 5)); + ASSERT_SAME_TYPE(decltype(std::destroy(It(pool), It(pool + 5))), void); assert(counter == 0); - std::allocator_traits::deallocate(alloc, pool, N); + std::allocator_traits::deallocate(alloc, pool, 5); +} +TEST_CONSTEXPR_CXX20 bool tests() { + test(); + test>(); return true; } -int main(int, char**) -{ - test(); +int main(int, char**) { + tests(); #if TEST_STD_VER > 17 - static_assert(test()); + test_arrays(); + static_assert(tests()); + // TODO: Until std::construct_at has support for arrays, it's impossible to test this + // in a constexpr context. + // static_assert(test_arrays()); #endif return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_at.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_at.pass.cpp --- a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_at.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_at.pass.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "test_macros.h" @@ -42,8 +43,50 @@ friend void operator&(DerivedCounted) = delete; }; -TEST_CONSTEXPR_CXX20 bool test() -{ +#if TEST_STD_VER > 17 +constexpr bool test_arrays() { + { + using Array = Counted[3]; + using Alloc = std::allocator; + Alloc alloc; + Array* ptr = std::allocator_traits::allocate(alloc, 1); + Array& arr = *ptr; + + int counter = 0; + for (int i = 0; i != 3; ++i) + std::allocator_traits::construct(alloc, std::addressof(arr[i]), &counter); + assert(counter == 3); + + std::destroy_at(ptr); + ASSERT_SAME_TYPE(decltype(std::destroy_at(ptr)), void); + assert(counter == 0); + + std::allocator_traits::deallocate(alloc, ptr, 1); + } + { + using Array = Counted[3][2]; + using Alloc = std::allocator; + Alloc alloc; + Array* ptr = std::allocator_traits::allocate(alloc, 1); + Array& arr = *ptr; + + int counter = 0; + for (int i = 0; i != 3; ++i) + for (int j = 0; j != 2; ++j) + std::allocator_traits::construct(alloc, std::addressof(arr[i][j]), &counter); + assert(counter == 3 * 2); + + std::destroy_at(ptr); + ASSERT_SAME_TYPE(decltype(std::destroy_at(ptr)), void); + assert(counter == 0); + + std::allocator_traits::deallocate(alloc, ptr, 1); + } + return true; +} +#endif + +TEST_CONSTEXPR_CXX20 bool test() { { using Alloc = std::allocator; Alloc alloc; @@ -56,6 +99,7 @@ assert(counter == 2); std::destroy_at(ptr1); + ASSERT_SAME_TYPE(decltype(std::destroy_at(ptr1)), void); assert(counter == 1); std::destroy_at(ptr2); @@ -76,6 +120,7 @@ assert(counter == 2); std::destroy_at(ptr1); + ASSERT_SAME_TYPE(decltype(std::destroy_at(ptr1)), void); assert(counter == 1); std::destroy_at(ptr2); @@ -88,11 +133,14 @@ return true; } -int main(int, char**) -{ +int main(int, char**) { test(); #if TEST_STD_VER > 17 + test_arrays(); static_assert(test()); + // TODO: Until std::construct_at has support for arrays, it's impossible to test this + // in a constexpr context. + // static_assert(test_arrays()); #endif return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_n.pass.cpp --- a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_n.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_n.pass.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "test_macros.h" #include "test_iterators.h" @@ -31,36 +32,92 @@ friend void operator&(Counted) = delete; }; -TEST_CONSTEXPR_CXX20 bool test() -{ +#if TEST_STD_VER > 17 +constexpr bool test_arrays() { + { + using Array = Counted[3]; + using Alloc = std::allocator; + int counter = 0; + Alloc alloc; + Array* pool = std::allocator_traits::allocate(alloc, 5); + + for (Array* p = pool; p != pool + 5; ++p) { + Array& arr = *p; + for (int i = 0; i != 3; ++i) { + std::allocator_traits::construct(alloc, std::addressof(arr[i]), &counter); + } + } + assert(counter == 5 * 3); + + Array* p = std::destroy_n(pool, 5); + ASSERT_SAME_TYPE(decltype(std::destroy_n(pool, 5)), Array*); + assert(p == pool + 5); + assert(counter == 0); + + std::allocator_traits::deallocate(alloc, pool, 5); + } + { + using Array = Counted[3][2]; + using Alloc = std::allocator; + int counter = 0; + Alloc alloc; + Array* pool = std::allocator_traits::allocate(alloc, 5); + + for (Array* p = pool; p != pool + 5; ++p) { + Array& arr = *p; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != 2; ++j) { + std::allocator_traits::construct(alloc, std::addressof(arr[i][j]), &counter); + } + } + } + assert(counter == 5 * 3 * 2); + + Array* p = std::destroy_n(pool, 5); + ASSERT_SAME_TYPE(decltype(std::destroy_n(pool, 5)), Array*); + assert(p == pool + 5); + assert(counter == 0); + + std::allocator_traits::deallocate(alloc, pool, 5); + } + + return true; +} +#endif + +template +TEST_CONSTEXPR_CXX20 void test() { using Alloc = std::allocator; int counter = 0; - int const N = 5; Alloc alloc; - Counted* pool = std::allocator_traits::allocate(alloc, N); + Counted* pool = std::allocator_traits::allocate(alloc, 5); - for (Counted* p = pool; p != pool + N; ++p) + for (Counted* p = pool; p != pool + 5; ++p) std::allocator_traits::construct(alloc, p, &counter); assert(counter == 5); - Counted* np = std::destroy_n(pool, 1); - assert(np == pool + 1); - assert(counter == 4); - - forward_iterator it = std::destroy_n(forward_iterator(pool + 1), 4); - assert(it == forward_iterator(pool + 5)); + It it = std::destroy_n(It(pool), 5); + ASSERT_SAME_TYPE(decltype(std::destroy_n(It(pool), 5)), It); + assert(it == It(pool + 5)); assert(counter == 0); - std::allocator_traits::deallocate(alloc, pool, N); + std::allocator_traits::deallocate(alloc, pool, 5); +} +TEST_CONSTEXPR_CXX20 bool tests() { + test(); + test>(); return true; } -int main(int, char**) -{ - test(); +int main(int, char**) { + tests(); #if TEST_STD_VER > 17 - static_assert(test()); + test_arrays(); + static_assert(tests()); + // TODO: Until std::construct_at has support for arrays, it's impossible to test this + // in a constexpr context. + // static_assert(test_arrays()); #endif return 0; }