[libcxx-commits] [libcxx] c99f5b2 - [libc++] Handle arrays in std::destroy_at
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jul 30 06:40:10 PDT 2021
Author: Louis Dionne
Date: 2021-07-30T09:39:36-04:00
New Revision: c99f5b2af1fce174822a40789ab4e305553f088a
URL: https://github.com/llvm/llvm-project/commit/c99f5b2af1fce174822a40789ab4e305553f088a
DIFF: https://github.com/llvm/llvm-project/commit/c99f5b2af1fce174822a40789ab4e305553f088a.diff
LOG: [libc++] Handle arrays in std::destroy_at
Also, improve tests for std::destroy and std::destroy_n so that they
check for array support.
These changes are part of http://wg21.link/p0896 (the One Ranges proposal).
Differential Revision: https://reviews.llvm.org/D106916
Added:
Modified:
libcxx/include/__memory/construct_at.h
libcxx/include/__memory/uninitialized_algorithms.h
libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy.pass.cpp
libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_at.pass.cpp
libcxx/test/std/utilities/memory/specialized.algorithms/specialized.destroy/destroy_n.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/__memory/construct_at.h b/libcxx/include/__memory/construct_at.h
index 7ab19315dcd8b..dfbdb295d9016 100644
--- 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 <type_traits>
#include <utility>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -43,13 +46,41 @@ constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
#if _LIBCPP_STD_VER > 14
-template <class _Tp>
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
+template <class _ForwardIterator>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
+void destroy(_ForwardIterator, _ForwardIterator);
+
+template <class _Tp, _EnableIf<!is_array_v<_Tp>, 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 <class _Tp, _EnableIf<is_array_v<_Tp>, 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 <class _ForwardIterator>
+_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 <class _ForwardIterator, class _Size>
+_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
index 39edabbd8e4f7..56c7bf6bedfea 100644
--- a/libcxx/include/__memory/uninitialized_algorithms.h
+++ b/libcxx/include/__memory/uninitialized_algorithms.h
@@ -122,21 +122,6 @@ uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
#if _LIBCPP_STD_VER > 14
-template <class _ForwardIterator>
-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 <class _ForwardIterator, class _Size>
-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 <class _ForwardIterator>
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
index d9998c7d0429d..294af4bbaf934 100644
--- 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 <memory>
#include <cassert>
+#include <type_traits>
#include "test_macros.h"
#include "test_iterators.h"
@@ -31,34 +32,89 @@ struct Counted {
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<Array>;
+ int counter = 0;
+ Alloc alloc;
+ Array* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
+
+ for (Array* p = pool; p != pool + 5; ++p) {
+ Array& arr = *p;
+ for (int i = 0; i != 3; ++i) {
+ std::allocator_traits<Alloc>::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<Alloc>::deallocate(alloc, pool, 5);
+ }
+ {
+ using Array = Counted[3][2];
+ using Alloc = std::allocator<Array>;
+ int counter = 0;
+ Alloc alloc;
+ Array* pool = std::allocator_traits<Alloc>::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<Alloc>::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<Alloc>::deallocate(alloc, pool, 5);
+ }
+
+ return true;
+}
+#endif
+
+template <class It>
+TEST_CONSTEXPR_CXX20 void test() {
using Alloc = std::allocator<Counted>;
int counter = 0;
- int const N = 5;
Alloc alloc;
- Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, N);
+ Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
- for (Counted* p = pool; p != pool + N; ++p)
+ for (Counted* p = pool; p != pool + 5; ++p)
std::allocator_traits<Alloc>::construct(alloc, p, &counter);
assert(counter == 5);
- std::destroy(pool, pool + 1);
- assert(counter == 4);
-
- std::destroy(forward_iterator<Counted*>(pool + 1), forward_iterator<Counted*>(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<Alloc>::deallocate(alloc, pool, N);
+ std::allocator_traits<Alloc>::deallocate(alloc, pool, 5);
+}
+TEST_CONSTEXPR_CXX20 bool tests() {
+ test<Counted*>();
+ test<forward_iterator<Counted*>>();
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
index 50c43a10dfbc3..3bc5dc0bbd19a 100644
--- 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 <memory>
#include <cassert>
+#include <type_traits>
#include "test_macros.h"
@@ -42,8 +43,50 @@ struct DerivedCounted : VirtualCounted {
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<Array>;
+ Alloc alloc;
+ Array* ptr = std::allocator_traits<Alloc>::allocate(alloc, 1);
+ Array& arr = *ptr;
+
+ int counter = 0;
+ for (int i = 0; i != 3; ++i)
+ std::allocator_traits<Alloc>::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<Alloc>::deallocate(alloc, ptr, 1);
+ }
+ {
+ using Array = Counted[3][2];
+ using Alloc = std::allocator<Array>;
+ Alloc alloc;
+ Array* ptr = std::allocator_traits<Alloc>::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<Alloc>::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<Alloc>::deallocate(alloc, ptr, 1);
+ }
+ return true;
+}
+#endif
+
+TEST_CONSTEXPR_CXX20 bool test() {
{
using Alloc = std::allocator<Counted>;
Alloc alloc;
@@ -56,6 +99,7 @@ TEST_CONSTEXPR_CXX20 bool test()
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 @@ TEST_CONSTEXPR_CXX20 bool test()
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 @@ TEST_CONSTEXPR_CXX20 bool test()
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
index 54b1633d096fe..a35088db7821e 100644
--- 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 <memory>
#include <cassert>
+#include <type_traits>
#include "test_macros.h"
#include "test_iterators.h"
@@ -31,36 +32,92 @@ struct Counted {
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<Array>;
+ int counter = 0;
+ Alloc alloc;
+ Array* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
+
+ for (Array* p = pool; p != pool + 5; ++p) {
+ Array& arr = *p;
+ for (int i = 0; i != 3; ++i) {
+ std::allocator_traits<Alloc>::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<Alloc>::deallocate(alloc, pool, 5);
+ }
+ {
+ using Array = Counted[3][2];
+ using Alloc = std::allocator<Array>;
+ int counter = 0;
+ Alloc alloc;
+ Array* pool = std::allocator_traits<Alloc>::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<Alloc>::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<Alloc>::deallocate(alloc, pool, 5);
+ }
+
+ return true;
+}
+#endif
+
+template <class It>
+TEST_CONSTEXPR_CXX20 void test() {
using Alloc = std::allocator<Counted>;
int counter = 0;
- int const N = 5;
Alloc alloc;
- Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, N);
+ Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
- for (Counted* p = pool; p != pool + N; ++p)
+ for (Counted* p = pool; p != pool + 5; ++p)
std::allocator_traits<Alloc>::construct(alloc, p, &counter);
assert(counter == 5);
- Counted* np = std::destroy_n(pool, 1);
- assert(np == pool + 1);
- assert(counter == 4);
-
- forward_iterator<Counted*> it = std::destroy_n(forward_iterator<Counted*>(pool + 1), 4);
- assert(it == forward_iterator<Counted*>(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<Alloc>::deallocate(alloc, pool, N);
+ std::allocator_traits<Alloc>::deallocate(alloc, pool, 5);
+}
+TEST_CONSTEXPR_CXX20 bool tests() {
+ test<Counted*>();
+ test<forward_iterator<Counted*>>();
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;
}
More information about the libcxx-commits
mailing list