Skip to content

Commit 373f11d

Browse files
committed
[libc++] Destroy elements when exceptions are thrown in __construct_at_end
1 parent 0246f33 commit 373f11d

File tree

3 files changed

+378
-8
lines changed

3 files changed

+378
-8
lines changed

libcxx/include/__memory/uninitialized_algorithms.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp&
124124

125125
// uninitialized_fill_n
126126

127+
template <class _Alloc, class _ForwardIterator, class _Size, class _Tp>
128+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
129+
__uninitialized_allocator_fill_n(_Alloc& __alloc, _ForwardIterator __first, _Size __n, const _Tp& __x) {
130+
_ForwardIterator __idx = __first;
131+
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
132+
for (; __n > 0; ++__idx, (void)--__n)
133+
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__idx), __x);
134+
__guard.__complete();
135+
136+
return __idx;
137+
}
138+
127139
template <class _ValueType, class _ForwardIterator, class _Size, class _Tp>
128140
inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator
129141
__uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) {
@@ -143,6 +155,20 @@ uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) {
143155
return std::__uninitialized_fill_n<_ValueType>(__first, __n, __x);
144156
}
145157

158+
// __uninitialized_allocator_value_construct_n
159+
160+
template <class _Alloc, class _ForwardIterator, class _Size>
161+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
162+
__uninitialized_allocator_value_construct_n(_Alloc& __alloc, _ForwardIterator __first, _Size __n) {
163+
auto __idx = __first;
164+
auto __guard = std::__make_exception_guard([&] { std::__destroy(__first, __idx); });
165+
for (; __n > 0; ++__idx, (void)--__n)
166+
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__idx));
167+
__guard.__complete();
168+
169+
return __idx;
170+
}
171+
146172
#if _LIBCPP_STD_VER >= 17
147173

148174
// uninitialized_default_construct

libcxx/include/__vector/vector.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,7 @@ vector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
941941
template <class _Tp, class _Allocator>
942942
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(size_type __n) {
943943
_ConstructTransaction __tx(*this, __n);
944-
const_pointer __new_end = __tx.__new_end_;
945-
for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
946-
__alloc_traits::construct(this->__alloc_, std::__to_address(__pos));
947-
}
944+
__tx.__pos_ = std::__uninitialized_allocator_value_construct_n(this->__alloc_, this->__end_, __n);
948945
}
949946

950947
// Copy constructs __n objects starting at __end_ from __x
@@ -957,10 +954,7 @@ template <class _Tp, class _Allocator>
957954
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
958955
vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
959956
_ConstructTransaction __tx(*this, __n);
960-
const_pointer __new_end = __tx.__new_end_;
961-
for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
962-
__alloc_traits::construct(this->__alloc_, std::__to_address(__pos), __x);
963-
}
957+
__tx.__pos_ = std::__uninitialized_allocator_fill_n(this->__alloc_, this->__end_, __n, __x);
964958
}
965959

966960
template <class _Tp, class _Allocator>

0 commit comments

Comments
 (0)