diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2024-03-21 13:25:15 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2024-06-11 13:15:28 +0100 |
commit | c394e299491ce6dc7e1a51b89165078e11cf75be (patch) | |
tree | f91752fdda095cda85a19f9da6c0f98b75869f82 /libstdc++-v3/include | |
parent | d4126b329b2ae4f2b60efa1c7ad51b576de168bd (diff) | |
download | gcc-c394e299491ce6dc7e1a51b89165078e11cf75be.zip gcc-c394e299491ce6dc7e1a51b89165078e11cf75be.tar.gz gcc-c394e299491ce6dc7e1a51b89165078e11cf75be.tar.bz2 |
libstdc++: Destroy allocators in re-inserted container nodes [PR114401]
The allocator objects in container node handles were not being destroyed
after the node was re-inserted into a container. They are stored in a
union and so need to be explicitly destroyed when the node becomes
empty. The containers were zeroing the node handle's pointer, which
makes it empty, causing the handle's destructor to think there's nothing
to clean up.
Add a new member function to the node handle which destroys the
allocator and zeros the pointer. Change the containers to call that
instead of just changing the pointer manually.
We can also remove the _M_empty member of the union which is not
necessary.
libstdc++-v3/ChangeLog:
PR libstdc++/114401
* include/bits/hashtable.h (_Hashtable::_M_reinsert_node): Call
release() on node handle instead of just zeroing its pointer.
(_Hashtable::_M_reinsert_node_multi): Likewise.
(_Hashtable::_M_merge_unique): Likewise.
(_Hashtable::_M_merge_multi): Likewise.
* include/bits/node_handle.h (_Node_handle_common::release()):
New member function.
(_Node_handle_common::_Optional_alloc::_M_empty): Remove
unnecessary union member.
(_Node_handle_common): Declare _Hashtable as a friend.
* include/bits/stl_tree.h (_Rb_tree::_M_reinsert_node_unique):
Call release() on node handle instead of just zeroing its
pointer.
(_Rb_tree::_M_reinsert_node_equal): Likewise.
(_Rb_tree::_M_reinsert_node_hint_unique): Likewise.
(_Rb_tree::_M_reinsert_node_hint_equal): Likewise.
* testsuite/23_containers/multiset/modifiers/114401.cc: New test.
* testsuite/23_containers/set/modifiers/114401.cc: New test.
* testsuite/23_containers/unordered_multiset/modifiers/114401.cc: New test.
* testsuite/23_containers/unordered_set/modifiers/114401.cc: New test.
(cherry picked from commit c2e28df90a1640cebadef6c6c8ab5ea964071bb1)
Diffstat (limited to 'libstdc++-v3/include')
-rw-r--r-- | libstdc++-v3/include/bits/hashtable.h | 10 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/node_handle.h | 19 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/stl_tree.h | 10 |
3 files changed, 26 insertions, 13 deletions
diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h index 1a505ff..3823c7e 100644 --- a/libstdc++-v3/include/bits/hashtable.h +++ b/libstdc++-v3/include/bits/hashtable.h @@ -985,7 +985,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // DR 1189. // reserve, if present, comes from _Rehash_base. -#if __cplusplus > 201402L +#if __cplusplus > 201404L /// Re-insert an extracted node into a container with unique keys. insert_return_type _M_reinsert_node(node_type&& __nh) @@ -1010,7 +1010,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __ret.position = _M_insert_unique_node(__bkt, __code, __nh._M_ptr); - __nh._M_ptr = nullptr; + __nh.release(); __ret.inserted = true; } } @@ -1030,7 +1030,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION auto __code = this->_M_hash_code(__k); auto __ret = _M_insert_multi_node(__hint._M_cur, __code, __nh._M_ptr); - __nh._M_ptr = nullptr; + __nh.release(); return __ret; } @@ -1112,7 +1112,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { auto __nh = __src.extract(__pos); _M_insert_unique_node(__bkt, __code, __nh._M_ptr, __n_elt); - __nh._M_ptr = nullptr; + __nh.release(); __n_elt = 1; } else if (__n_elt != 1) @@ -1139,7 +1139,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION = _M_src_hash_code(__src.hash_function(), __k, *__pos._M_cur); auto __nh = __src.extract(__pos); __hint = _M_insert_multi_node(__hint, __code, __nh._M_ptr)._M_cur; - __nh._M_ptr = nullptr; + __nh.release(); } } #endif // C++17 diff --git a/libstdc++-v3/include/bits/node_handle.h b/libstdc++-v3/include/bits/node_handle.h index d7a7a30..92ed27c 100644 --- a/libstdc++-v3/include/bits/node_handle.h +++ b/libstdc++-v3/include/bits/node_handle.h @@ -168,6 +168,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_ptr = nullptr; } + // Destroys the allocator. Does not deallocate or destroy the node. + // Precondition: !empty() + // Postcondition: empty() + void + release() noexcept + { + _M_alloc.release(); + _M_ptr = nullptr; + } + protected: typename _AllocTraits::pointer _M_ptr; @@ -219,9 +229,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __tmp; } - struct _Empty { }; - - [[__no_unique_address__]] _Empty _M_empty; [[__no_unique_address__]] _NodeAlloc _M_alloc; }; @@ -231,6 +238,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename _Compare, typename _ValueAlloc> friend class _Rb_tree; + template<typename _Key2, typename _Value2, typename _ValueAlloc, + typename _ExtractKey, typename _Equal, + typename _Hash, typename _RangeHash, typename _Unused, + typename _RehashPolicy, typename _Traits> + friend class _Hashtable; + /// @endcond }; diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index 6ced032..ab18f1b 100644 --- a/libstdc++-v3/include/bits/stl_tree.h +++ b/libstdc++-v3/include/bits/stl_tree.h @@ -1436,7 +1436,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_move_assign(_Rb_tree&, false_type); #endif -#if __cplusplus > 201402L +#if __cplusplus > 201404L public: /// Re-insert an extracted node. insert_return_type @@ -1454,7 +1454,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __ret.position = _M_insert_node(__res.first, __res.second, __nh._M_ptr); - __nh._M_ptr = nullptr; + __nh.release(); __ret.inserted = true; } else @@ -1482,7 +1482,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr); else __ret = _M_insert_equal_lower_node(__nh._M_ptr); - __nh._M_ptr = nullptr; + __nh.release(); } return __ret; } @@ -1501,7 +1501,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__res.second) { __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr); - __nh._M_ptr = nullptr; + __nh.release(); } else __ret = iterator(__res.first); @@ -1524,7 +1524,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr); else __ret = _M_insert_equal_lower_node(__nh._M_ptr); - __nh._M_ptr = nullptr; + __nh.release(); } return __ret; } |