Skip to content

Commit 14c47bd

Browse files
committed
[libc++] Extract destroy algorithms into a single header
This is useful to start using those algorithms from the upcoming relocation utilities.
1 parent 0115849 commit 14c47bd

File tree

8 files changed

+79
-42
lines changed

8 files changed

+79
-42
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ set(files
549549
__memory/compressed_pair.h
550550
__memory/concepts.h
551551
__memory/construct_at.h
552+
__memory/destroy.h
552553
__memory/destruct_n.h
553554
__memory/inout_ptr.h
554555
__memory/noexcept_move_assign_container.h

libcxx/include/__memory/construct_at.h

Lines changed: 3 additions & 34 deletions
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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 <__memory/pointer_traits.h>
17+
#include <__utility/move.h>
18+
19+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20+
# pragma GCC system_header
21+
#endif
22+
23+
_LIBCPP_PUSH_MACROS
24+
#include <__undef_macros>
25+
26+
_LIBCPP_BEGIN_NAMESPACE_STD
27+
28+
template <class _ForwardIterator>
29+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
30+
__destroy(_ForwardIterator __first, _ForwardIterator __last) {
31+
for (; __first != __last; ++__first)
32+
std::__destroy_at(std::addressof(*__first));
33+
return __first;
34+
}
35+
36+
template <class _BidirectionalIterator>
37+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
38+
__reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
39+
while (__last != __first) {
40+
--__last;
41+
std::__destroy_at(std::addressof(*__last));
42+
}
43+
return __last;
44+
}
45+
46+
// Destroy all elements in [__first, __last) from left to right using allocator destruction.
47+
template <class _Alloc, class _Iter, class _Sent>
48+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
49+
__allocator_destroy(_Alloc& __alloc, _Iter __first, _Sent __last) {
50+
for (; __first != __last; ++__first)
51+
allocator_traits<_Alloc>::destroy(__alloc, std::__to_address(__first));
52+
}
53+
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+
66+
_LIBCPP_END_NAMESPACE_STD
67+
68+
_LIBCPP_POP_MACROS
69+
70+
#endif // _LIBCPP___MEMORY_DESTROY_H

libcxx/include/__memory/ranges_construct_at.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <__iterator/iterator_traits.h>
1717
#include <__memory/concepts.h>
1818
#include <__memory/construct_at.h>
19+
#include <__memory/destroy.h>
1920
#include <__ranges/access.h>
2021
#include <__ranges/concepts.h>
2122
#include <__ranges/dangling.h>

libcxx/include/__memory/shared_ptr.h

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <__iterator/iterator_traits.h>
1919
#include <__iterator/reverse_iterator.h>
2020
#include <__memory/addressof.h>
21+
#include <__memory/destroy.h>
2122
#include <__memory/allocator_traits.h>
2223
#include <__memory/construct_at.h>
2324
#include <__memory/pointer_traits.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

Lines changed: 1 addition & 0 deletions
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 <__numeric/reduce.h>
2728
#include <__pstl/backend_fwd.h>

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,7 @@ module std [system] {
15351535
module compressed_pair { header "__memory/compressed_pair.h" }
15361536
module concepts { header "__memory/concepts.h" }
15371537
module construct_at { header "__memory/construct_at.h" }
1538+
module destroy { header "__memory/destroy.h" }
15381539
module destruct_n { header "__memory/destruct_n.h" }
15391540
module fwd { header "__fwd/memory.h" }
15401541
module inout_ptr { header "__memory/inout_ptr.h" }

0 commit comments

Comments
 (0)