diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index 31169711a48..81fe3825064 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -1607,6 +1607,39 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER clear() _GLIBCXX_NOEXCEPT { _M_erase_at_end(this->_M_impl._M_start); } + private: + // RAII guard for allocated storage. + struct _Guard_alloc + { + pointer _M_storage; // Storage to deallocate + size_type _M_len; + _Base& _M_vect; + + _GLIBCXX20_CONSTEXPR + _Guard_alloc(pointer __s, size_type __l, _Base& __vect) + : _M_storage(__s), _M_len(__l), _M_vect(__vect) + { } + + _GLIBCXX20_CONSTEXPR + ~_Guard_alloc() + { + if (_M_storage) + _M_vect._M_deallocate(_M_storage, _M_len); + } + + _GLIBCXX20_CONSTEXPR + pointer + _M_release() + { + pointer __res = _M_storage; + _M_storage = pointer(); + return __res; + } + + private: + _Guard_alloc(const _Guard_alloc&); + }; + protected: /** * Memory expansion handler. Uses the member allocation function to @@ -1618,18 +1651,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last) { - pointer __result = this->_M_allocate(__n); - __try - { - std::__uninitialized_copy_a(__first, __last, __result, - _M_get_Tp_allocator()); - return __result; - } - __catch(...) - { - _M_deallocate(__result, __n); - __throw_exception_again; - } + _Guard_alloc __guard(this->_M_allocate(__n), __n, *this); + std::__uninitialized_copy_a + (__first, __last, __guard._M_storage, _M_get_Tp_allocator()); + return __guard._M_release(); } @@ -1642,13 +1667,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER // 438. Ambiguity in the "do the right thing" clause template void - _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type) + _M_initialize_dispatch(_Integer __int_n, _Integer __value, __true_type) { - this->_M_impl._M_start = _M_allocate(_S_check_init_len( - static_cast(__n), _M_get_Tp_allocator())); - this->_M_impl._M_end_of_storage = - this->_M_impl._M_start + static_cast(__n); - _M_fill_initialize(static_cast(__n), __value); + const size_type __n = static_cast(__int_n); + pointer __start = + _M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator())); + _Guard_alloc __guard(__start, __n, *this); + this->_M_impl._M_finish = + std::__uninitialized_fill_n_a + (__start, __n, __value, _M_get_Tp_allocator()); + this->_M_impl._M_start = __start; + (void) __guard._M_release(); + this->_M_impl._M_end_of_storage = __start + __n; } // Called by the range constructor to implement [23.1.1]/9 @@ -1690,17 +1720,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER std::forward_iterator_tag) { const size_type __n = std::distance(__first, __last); - this->_M_impl._M_start - = this->_M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator())); - this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; - this->_M_impl._M_finish = - std::__uninitialized_copy_a(__first, __last, - this->_M_impl._M_start, - _M_get_Tp_allocator()); + pointer __start = + this->_M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator())); + _Guard_alloc __guard(__start, __n, *this); + this->_M_impl._M_finish = std::__uninitialized_copy_a + (__first, __last, __start, _M_get_Tp_allocator()); + this->_M_impl._M_start = __start; + (void) __guard._M_release(); + this->_M_impl._M_end_of_storage = __start + __n; } - // Called by the first initialize_dispatch above and by the - // vector(n,value,a) constructor. + // Called by the vector(n,value,a) constructor. _GLIBCXX20_CONSTEXPR void _M_fill_initialize(size_type __n, const value_type& __value) diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index 25df060beee..36b27dce7b9 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -467,32 +467,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER pointer __new_start(this->_M_allocate(__len)); pointer __new_finish(__new_start); - // RAII guard for allocated storage. - struct _Guard { - pointer _M_storage; // Storage to deallocate - size_type _M_len; - _Tp_alloc_type& _M_alloc; - - _GLIBCXX20_CONSTEXPR - _Guard(pointer __s, size_type __l, _Tp_alloc_type& __a) - : _M_storage(__s), _M_len(__l), _M_alloc(__a) - { } - - _GLIBCXX20_CONSTEXPR - ~_Guard() - { - if (_M_storage) - __gnu_cxx::__alloc_traits<_Tp_alloc_type>:: - deallocate(_M_alloc, _M_storage, _M_len); - } - - private: - _Guard(const _Guard&); - }; - - { - _Guard __guard(__new_start, __len, _M_impl); + _Guard_alloc __guard(__new_start, __len, *this); // The order of the three operations is dictated by the C++11 // case, where the moves could alter a new element belonging @@ -596,32 +572,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER pointer __new_start(this->_M_allocate(__len)); pointer __new_finish(__new_start); - // RAII guard for allocated storage. - struct _Guard { - pointer _M_storage; // Storage to deallocate - size_type _M_len; - _Tp_alloc_type& _M_alloc; - - _GLIBCXX20_CONSTEXPR - _Guard(pointer __s, size_type __l, _Tp_alloc_type& __a) - : _M_storage(__s), _M_len(__l), _M_alloc(__a) - { } - - _GLIBCXX20_CONSTEXPR - ~_Guard() - { - if (_M_storage) - __gnu_cxx::__alloc_traits<_Tp_alloc_type>:: - deallocate(_M_alloc, _M_storage, _M_len); - } - - private: - _Guard(const _Guard&); - }; - - { - _Guard __guard(__new_start, __len, _M_impl); + _Guard_alloc __guard(__new_start, __len, *this); // The order of the three operations is dictated by the C++11 // case, where the moves could alter a new element belonging @@ -833,32 +785,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _M_check_len(__n, "vector::_M_default_append"); pointer __new_start(this->_M_allocate(__len)); - // RAII guard for allocated storage. - struct _Guard - { - pointer _M_storage; // Storage to deallocate - size_type _M_len; - _Tp_alloc_type& _M_alloc; - - _GLIBCXX20_CONSTEXPR - _Guard(pointer __s, size_type __l, _Tp_alloc_type& __a) - : _M_storage(__s), _M_len(__l), _M_alloc(__a) - { } - - _GLIBCXX20_CONSTEXPR - ~_Guard() - { - if (_M_storage) - __gnu_cxx::__alloc_traits<_Tp_alloc_type>:: - deallocate(_M_alloc, _M_storage, _M_len); - } - - private: - _Guard(const _Guard&); - }; - { - _Guard __guard(__new_start, __len, _M_impl); + _Guard_alloc __guard(__new_start, __len, *this); std::__uninitialized_default_n_a(__new_start + __size, __n, _M_get_Tp_allocator()); diff --git a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc index 6b3d48f0c92..d732ca8bed8 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc @@ -19,7 +19,7 @@ // { dg-do compile } // { dg-prune-output "cannot convert" } -// { dg-prune-output "no matching function .*_M_fill_initialize" } +// { dg-prune-output "no matching function .*__uninitialized_fill_n_a" } #include diff --git a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc index 65ce2fd30e5..58c4c8f9a73 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc @@ -19,7 +19,7 @@ // { dg-do compile } // { dg-prune-output "cannot convert" } -// { dg-prune-output "no matching function .*_M_fill_initialize" } +// { dg-prune-output "no matching function .*__uninitialized_fill_n_a" } #include #include