Skip to content

Commit f332455

Browse files
authored
[libc++] Extract destroy algorithms into separate headers (#126449)
This patch separates the destroy algorithms from the primitive construct_at and destroy_at operations, which are conceptually not algorithms. This makes it easier to start using these destroy algorithms from upcoming relocation facilities. As part of this, it also implements `std::destroy_at` for arrays without relying on the `std::destroy` algorithm, which is conceptually a higher-level facility.
1 parent 872aadd commit f332455

File tree

11 files changed

+165
-81
lines changed

11 files changed

+165
-81
lines changed

libcxx/include/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,14 @@ set(files
557557
__memory/compressed_pair.h
558558
__memory/concepts.h
559559
__memory/construct_at.h
560+
__memory/destroy.h
560561
__memory/destruct_n.h
561562
__memory/inout_ptr.h
562563
__memory/noexcept_move_assign_container.h
563564
__memory/out_ptr.h
564565
__memory/pointer_traits.h
565566
__memory/ranges_construct_at.h
567+
__memory/ranges_destroy.h
566568
__memory/ranges_uninitialized_algorithms.h
567569
__memory/raw_storage_iterator.h
568570
__memory/shared_count.h

libcxx/include/__format/buffer.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <__algorithm/max.h>
1616
#include <__algorithm/min.h>
1717
#include <__algorithm/ranges_copy.h>
18-
#include <__algorithm/ranges_copy_n.h>
1918
#include <__algorithm/transform.h>
2019
#include <__algorithm/unwrap_iter.h>
2120
#include <__concepts/same_as.h>
@@ -33,7 +32,7 @@
3332
#include <__memory/allocator.h>
3433
#include <__memory/allocator_traits.h>
3534
#include <__memory/construct_at.h>
36-
#include <__memory/ranges_construct_at.h>
35+
#include <__memory/destroy.h>
3736
#include <__memory/uninitialized_algorithms.h>
3837
#include <__type_traits/add_pointer.h>
3938
#include <__type_traits/conditional.h>
@@ -621,7 +620,7 @@ class _LIBCPP_TEMPLATE_VIS __retarget_buffer {
621620
}
622621

623622
_LIBCPP_HIDE_FROM_ABI ~__retarget_buffer() {
624-
ranges::destroy_n(__ptr_, __size_);
623+
std::destroy_n(__ptr_, __size_);
625624
allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __capacity_);
626625
}
627626

@@ -686,7 +685,7 @@ class _LIBCPP_TEMPLATE_VIS __retarget_buffer {
686685
// guard is optimized away so there is no runtime overhead.
687686
std::uninitialized_move_n(__ptr_, __size_, __result.ptr);
688687
__guard.__complete();
689-
ranges::destroy_n(__ptr_, __size_);
688+
std::destroy_n(__ptr_, __size_);
690689
allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __capacity_);
691690

692691
__ptr_ = __result.ptr;

libcxx/include/__memory/construct_at.h

+3-34
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __construct_at(_Tp* __l
5757
// The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
5858
// taking an array).
5959

60-
template <class _ForwardIterator>
61-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);
62-
6360
template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
6461
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
6562
_LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
@@ -70,28 +67,12 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc
7067
template <class _Tp, __enable_if_t<is_array<_Tp>::value, int> = 0>
7168
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
7269
_LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
73-
std::__destroy(std::begin(*__loc), std::end(*__loc));
70+
auto const __end = std::end(*__loc);
71+
for (auto __it = std::begin(*__loc); __it != __end; ++__it)
72+
std::__destroy_at(__it);
7473
}
7574
#endif
7675

77-
template <class _ForwardIterator>
78-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
79-
__destroy(_ForwardIterator __first, _ForwardIterator __last) {
80-
for (; __first != __last; ++__first)
81-
std::__destroy_at(std::addressof(*__first));
82-
return __first;
83-
}
84-
85-
template <class _BidirectionalIterator>
86-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
87-
__reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
88-
while (__last != __first) {
89-
--__last;
90-
std::__destroy_at(std::addressof(*__last));
91-
}
92-
return __last;
93-
}
94-
9576
#if _LIBCPP_STD_VER >= 17
9677

9778
template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
@@ -106,18 +87,6 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc)
10687
}
10788
# endif
10889

109-
template <class _ForwardIterator>
110-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy(_ForwardIterator __first, _ForwardIterator __last) {
111-
(void)std::__destroy(std::move(__first), std::move(__last));
112-
}
113-
114-
template <class _ForwardIterator, class _Size>
115-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
116-
for (; __n > 0; (void)++__first, --__n)
117-
std::__destroy_at(std::addressof(*__first));
118-
return __first;
119-
}
120-
12190
#endif // _LIBCPP_STD_VER >= 17
12291

12392
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__memory/destroy.h

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___MEMORY_DESTROY_H
10+
#define _LIBCPP___MEMORY_DESTROY_H
11+
12+
#include <__config>
13+
#include <__memory/addressof.h>
14+
#include <__memory/allocator_traits.h>
15+
#include <__memory/construct_at.h>
16+
#include <__utility/move.h>
17+
18+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19+
# pragma GCC system_header
20+
#endif
21+
22+
_LIBCPP_PUSH_MACROS
23+
#include <__undef_macros>
24+
25+
_LIBCPP_BEGIN_NAMESPACE_STD
26+
27+
template <class _ForwardIterator>
28+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
29+
__destroy(_ForwardIterator __first, _ForwardIterator __last) {
30+
for (; __first != __last; ++__first)
31+
std::__destroy_at(std::addressof(*__first));
32+
return __first;
33+
}
34+
35+
template <class _BidirectionalIterator>
36+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
37+
__reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
38+
while (__last != __first) {
39+
--__last;
40+
std::__destroy_at(std::addressof(*__last));
41+
}
42+
return __last;
43+
}
44+
45+
// Destroy all elements in [__first, __last) from left to right using allocator destruction.
46+
template <class _Alloc, class _Iter, class _Sent>
47+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
48+
__allocator_destroy(_Alloc& __alloc, _Iter __first, _Sent __last) {
49+
for (; __first != __last; ++__first)
50+
allocator_traits<_Alloc>::destroy(__alloc, std::addressof(*__first));
51+
}
52+
53+
#if _LIBCPP_STD_VER >= 17
54+
template <class _ForwardIterator>
55+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy(_ForwardIterator __first, _ForwardIterator __last) {
56+
(void)std::__destroy(std::move(__first), std::move(__last));
57+
}
58+
59+
template <class _ForwardIterator, class _Size>
60+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
61+
for (; __n > 0; (void)++__first, --__n)
62+
std::__destroy_at(std::addressof(*__first));
63+
return __first;
64+
}
65+
#endif
66+
67+
_LIBCPP_END_NAMESPACE_STD
68+
69+
_LIBCPP_POP_MACROS
70+
71+
#endif // _LIBCPP___MEMORY_DESTROY_H

libcxx/include/__memory/ranges_construct_at.h

-35
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,6 @@ inline namespace __cpo {
6161
inline constexpr auto destroy_at = __destroy_at{};
6262
} // namespace __cpo
6363

64-
// destroy
65-
66-
struct __destroy {
67-
template <__nothrow_input_iterator _InputIterator, __nothrow_sentinel_for<_InputIterator> _Sentinel>
68-
requires destructible<iter_value_t<_InputIterator>>
69-
_LIBCPP_HIDE_FROM_ABI constexpr _InputIterator operator()(_InputIterator __first, _Sentinel __last) const noexcept {
70-
return std::__destroy(std::move(__first), std::move(__last));
71-
}
72-
73-
template <__nothrow_input_range _InputRange>
74-
requires destructible<range_value_t<_InputRange>>
75-
_LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_InputRange> operator()(_InputRange&& __range) const noexcept {
76-
return (*this)(ranges::begin(__range), ranges::end(__range));
77-
}
78-
};
79-
80-
inline namespace __cpo {
81-
inline constexpr auto destroy = __destroy{};
82-
} // namespace __cpo
83-
84-
// destroy_n
85-
86-
struct __destroy_n {
87-
template <__nothrow_input_iterator _InputIterator>
88-
requires destructible<iter_value_t<_InputIterator>>
89-
_LIBCPP_HIDE_FROM_ABI constexpr _InputIterator
90-
operator()(_InputIterator __first, iter_difference_t<_InputIterator> __n) const noexcept {
91-
return std::destroy_n(std::move(__first), __n);
92-
}
93-
};
94-
95-
inline namespace __cpo {
96-
inline constexpr auto destroy_n = __destroy_n{};
97-
} // namespace __cpo
98-
9964
} // namespace ranges
10065

10166
#endif // _LIBCPP_STD_VER >= 20
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___MEMORY_RANGES_DESTROY_H
11+
#define _LIBCPP___MEMORY_RANGES_DESTROY_H
12+
13+
#include <__concepts/destructible.h>
14+
#include <__config>
15+
#include <__iterator/incrementable_traits.h>
16+
#include <__iterator/iterator_traits.h>
17+
#include <__memory/concepts.h>
18+
#include <__memory/destroy.h>
19+
#include <__ranges/access.h>
20+
#include <__ranges/concepts.h>
21+
#include <__ranges/dangling.h>
22+
#include <__utility/move.h>
23+
24+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25+
# pragma GCC system_header
26+
#endif
27+
28+
_LIBCPP_PUSH_MACROS
29+
#include <__undef_macros>
30+
31+
_LIBCPP_BEGIN_NAMESPACE_STD
32+
33+
#if _LIBCPP_STD_VER >= 20
34+
namespace ranges {
35+
36+
// destroy
37+
38+
struct __destroy {
39+
template <__nothrow_input_iterator _InputIterator, __nothrow_sentinel_for<_InputIterator> _Sentinel>
40+
requires destructible<iter_value_t<_InputIterator>>
41+
_LIBCPP_HIDE_FROM_ABI constexpr _InputIterator operator()(_InputIterator __first, _Sentinel __last) const noexcept {
42+
return std::__destroy(std::move(__first), std::move(__last));
43+
}
44+
45+
template <__nothrow_input_range _InputRange>
46+
requires destructible<range_value_t<_InputRange>>
47+
_LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_InputRange> operator()(_InputRange&& __range) const noexcept {
48+
return (*this)(ranges::begin(__range), ranges::end(__range));
49+
}
50+
};
51+
52+
inline namespace __cpo {
53+
inline constexpr auto destroy = __destroy{};
54+
} // namespace __cpo
55+
56+
// destroy_n
57+
58+
struct __destroy_n {
59+
template <__nothrow_input_iterator _InputIterator>
60+
requires destructible<iter_value_t<_InputIterator>>
61+
_LIBCPP_HIDE_FROM_ABI constexpr _InputIterator
62+
operator()(_InputIterator __first, iter_difference_t<_InputIterator> __n) const noexcept {
63+
return std::destroy_n(std::move(__first), __n);
64+
}
65+
};
66+
67+
inline namespace __cpo {
68+
inline constexpr auto destroy_n = __destroy_n{};
69+
} // namespace __cpo
70+
71+
} // namespace ranges
72+
73+
#endif // _LIBCPP_STD_VER >= 20
74+
75+
_LIBCPP_END_NAMESPACE_STD
76+
77+
_LIBCPP_POP_MACROS
78+
79+
#endif // _LIBCPP___MEMORY_RANGES_DESTROY_H

libcxx/include/__memory/shared_ptr.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <__memory/auto_ptr.h>
3030
#include <__memory/compressed_pair.h>
3131
#include <__memory/construct_at.h>
32+
#include <__memory/destroy.h>
3233
#include <__memory/pointer_traits.h>
3334
#include <__memory/shared_count.h>
3435
#include <__memory/uninitialized_algorithms.h>

libcxx/include/__memory/uninitialized_algorithms.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <__memory/addressof.h>
2222
#include <__memory/allocator_traits.h>
2323
#include <__memory/construct_at.h>
24+
#include <__memory/destroy.h>
2425
#include <__memory/pointer_traits.h>
2526
#include <__type_traits/enable_if.h>
2627
#include <__type_traits/extent.h>
@@ -511,14 +512,6 @@ __uninitialized_allocator_value_construct_n_multidimensional(_Alloc& __alloc, _B
511512

512513
#endif // _LIBCPP_STD_VER >= 17
513514

514-
// Destroy all elements in [__first, __last) from left to right using allocator destruction.
515-
template <class _Alloc, class _Iter, class _Sent>
516-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
517-
__allocator_destroy(_Alloc& __alloc, _Iter __first, _Sent __last) {
518-
for (; __first != __last; ++__first)
519-
allocator_traits<_Alloc>::destroy(__alloc, std::__to_address(__first));
520-
}
521-
522515
template <class _Alloc, class _Iter>
523516
class _AllocatorDestroyRangeReverse {
524517
public:

libcxx/include/__pstl/backends/libdispatch.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <__iterator/move_iterator.h>
2323
#include <__memory/allocator.h>
2424
#include <__memory/construct_at.h>
25+
#include <__memory/destroy.h>
2526
#include <__memory/unique_ptr.h>
2627
#include <__new/exceptions.h>
2728
#include <__numeric/reduce.h>

libcxx/include/memory

+2
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,14 @@ template<class Pointer = void, class Smart, class... Args>
958958

959959
# if _LIBCPP_STD_VER >= 17
960960
# include <__memory/construct_at.h>
961+
# include <__memory/destroy.h>
961962
# endif
962963

963964
# if _LIBCPP_STD_VER >= 20
964965
# include <__memory/assume_aligned.h>
965966
# include <__memory/concepts.h>
966967
# include <__memory/ranges_construct_at.h>
968+
# include <__memory/ranges_destroy.h>
967969
# include <__memory/ranges_uninitialized_algorithms.h>
968970
# include <__memory/uses_allocator_construction.h>
969971
# endif

libcxx/include/module.modulemap

+2
Original file line numberDiff line numberDiff line change
@@ -1567,13 +1567,15 @@ module std [system] {
15671567
module compressed_pair { header "__memory/compressed_pair.h" }
15681568
module concepts { header "__memory/concepts.h" }
15691569
module construct_at { header "__memory/construct_at.h" }
1570+
module destroy { header "__memory/destroy.h" }
15701571
module destruct_n { header "__memory/destruct_n.h" }
15711572
module fwd { header "__fwd/memory.h" }
15721573
module inout_ptr { header "__memory/inout_ptr.h" }
15731574
module noexcept_move_assign_container { header "__memory/noexcept_move_assign_container.h" }
15741575
module out_ptr { header "__memory/out_ptr.h" }
15751576
module pointer_traits { header "__memory/pointer_traits.h" }
15761577
module ranges_construct_at { header "__memory/ranges_construct_at.h" }
1578+
module ranges_destroy { header "__memory/ranges_destroy.h" }
15771579
module ranges_uninitialized_algorithms {
15781580
header "__memory/ranges_uninitialized_algorithms.h"
15791581
export std.algorithm.in_out_result

0 commit comments

Comments
 (0)