[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
This commit is contained in:
Louis Dionne 2021-07-27 15:45:09 -04:00
parent 8070bf8c6e
commit c99f5b2af1
5 changed files with 238 additions and 61 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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()
{
using Alloc = std::allocator<Counted>;
int counter = 0;
int const N = 5;
Alloc alloc;
Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, N);
#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 (Counted* p = pool; p != pool + N; ++p)
std::allocator_traits<Alloc>::construct(alloc, p, &counter);
assert(counter == 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 + 1);
assert(counter == 4);
std::destroy(pool, pool + 5);
ASSERT_SAME_TYPE(decltype(std::destroy(pool, pool + 5)), void);
assert(counter == 0);
std::destroy(forward_iterator<Counted*>(pool + 1), forward_iterator<Counted*>(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);
std::allocator_traits<Alloc>::deallocate(alloc, pool, N);
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
int main(int, char**)
{
test();
template <class It>
TEST_CONSTEXPR_CXX20 void test() {
using Alloc = std::allocator<Counted>;
int counter = 0;
Alloc alloc;
Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
for (Counted* p = pool; p != pool + 5; ++p)
std::allocator_traits<Alloc>::construct(alloc, p, &counter);
assert(counter == 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, 5);
}
TEST_CONSTEXPR_CXX20 bool tests() {
test<Counted*>();
test<forward_iterator<Counted*>>();
return true;
}
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;
}

View File

@ -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;
}

View File

@ -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()
{
using Alloc = std::allocator<Counted>;
int counter = 0;
int const N = 5;
Alloc alloc;
Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, N);
#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 (Counted* p = pool; p != pool + N; ++p)
std::allocator_traits<Alloc>::construct(alloc, p, &counter);
assert(counter == 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);
Counted* np = std::destroy_n(pool, 1);
assert(np == pool + 1);
assert(counter == 4);
Array* p = std::destroy_n(pool, 5);
ASSERT_SAME_TYPE(decltype(std::destroy_n(pool, 5)), Array*);
assert(p == pool + 5);
assert(counter == 0);
forward_iterator<Counted*> it = std::destroy_n(forward_iterator<Counted*>(pool + 1), 4);
assert(it == forward_iterator<Counted*>(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);
std::allocator_traits<Alloc>::deallocate(alloc, pool, N);
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
int main(int, char**)
{
test();
template <class It>
TEST_CONSTEXPR_CXX20 void test() {
using Alloc = std::allocator<Counted>;
int counter = 0;
Alloc alloc;
Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, 5);
for (Counted* p = pool; p != pool + 5; ++p)
std::allocator_traits<Alloc>::construct(alloc, p, &counter);
assert(counter == 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, 5);
}
TEST_CONSTEXPR_CXX20 bool tests() {
test<Counted*>();
test<forward_iterator<Counted*>>();
return true;
}
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;
}