From 603aec6775d5191fafd57412364578db68432f74 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 23 Oct 2019 17:14:28 +0100 Subject: [PATCH] Adjust extension types to use allocator_traits This makes these extensions work with types meeting the Cpp17Allocator requirements as well as the C++98 Allocator requirements. * include/backward/hash_set (hash_set): Use __alloc_traits. * include/backward/hashtable.h (_Hashtable): Likewise. * include/ext/alloc_traits.h (__alloc_traits::allocate): Add overload taking a hint. * include/ext/extptr_allocator.h (_ExtPtr_allocator::allocate): Ignore hint. * include/ext/slist (_Slist_base): Use __alloc_traits. * include/tr1/hashtable.h (_Hashtable): Likewise. * include/tr1/regex (match_results): Use vector::const_reference instead of assuming the allocator defines it. * testsuite/backward/hash_map/23528.cc: Use allocator_traits in C++11. * testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc: Use __gnu_test::max_size. * testsuite/tr1/6_containers/unordered_multimap/capacity/ 29134-multimap.cc: Likewise. * testsuite/tr1/6_containers/unordered_multiset/capacity/ 29134-multiset.cc: Likewise. * testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc: Likewise. From-SVN: r277335 --- libstdc++-v3/ChangeLog | 22 +++++++++++++++ libstdc++-v3/include/backward/hash_set | 10 ++++--- libstdc++-v3/include/backward/hashtable.h | 18 +++++++++---- libstdc++-v3/include/ext/alloc_traits.h | 5 ++++ libstdc++-v3/include/ext/extptr_allocator.h | 4 +-- libstdc++-v3/include/ext/slist | 18 +++++++++---- libstdc++-v3/include/tr1/hashtable.h | 27 +++++++++++-------- libstdc++-v3/include/tr1/regex | 2 +- .../testsuite/backward/hash_map/23528.cc | 6 ++++- .../unordered_map/capacity/29134-map.cc | 6 +++-- .../capacity/29134-multimap.cc | 6 +++-- .../capacity/29134-multiset.cc | 5 ++-- .../unordered_set/capacity/29134-set.cc | 5 ++-- 13 files changed, 97 insertions(+), 37 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 87ee636ceb8..9f19e4f8319 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,25 @@ +2019-10-23 Jonathan Wakely + + * include/backward/hash_set (hash_set): Use __alloc_traits. + * include/backward/hashtable.h (_Hashtable): Likewise. + * include/ext/alloc_traits.h (__alloc_traits::allocate): Add overload + taking a hint. + * include/ext/extptr_allocator.h (_ExtPtr_allocator::allocate): Ignore + hint. + * include/ext/slist (_Slist_base): Use __alloc_traits. + * include/tr1/hashtable.h (_Hashtable): Likewise. + * include/tr1/regex (match_results): Use vector::const_reference + instead of assuming the allocator defines it. + * testsuite/backward/hash_map/23528.cc: Use allocator_traits in C++11. + * testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc: Use + __gnu_test::max_size. + * testsuite/tr1/6_containers/unordered_multimap/capacity/ + 29134-multimap.cc: Likewise. + * testsuite/tr1/6_containers/unordered_multiset/capacity/ + 29134-multiset.cc: Likewise. + * testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc: + Likewise. + 2019-10-22 Jonathan Wakely * testsuite/util/testsuite_abi.h: Restore use of tr1/unordered_map diff --git a/libstdc++-v3/include/backward/hash_set b/libstdc++-v3/include/backward/hash_set index 1445aa61e11..7f743fdf3af 100644 --- a/libstdc++-v3/include/backward/hash_set +++ b/libstdc++-v3/include/backward/hash_set @@ -88,6 +88,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __glibcxx_class_requires3(_HashFcn, size_t, _Value, _UnaryFunctionConcept) __glibcxx_class_requires3(_EqualKey, _Value, _Value, _BinaryPredicateConcept) + typedef __alloc_traits<_Alloc> _Alloc_traits; + private: typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>, _EqualKey, _Alloc> _Ht; @@ -101,10 +103,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typedef typename _Ht::size_type size_type; typedef typename _Ht::difference_type difference_type; - typedef typename _Alloc::pointer pointer; - typedef typename _Alloc::const_pointer const_pointer; - typedef typename _Alloc::reference reference; - typedef typename _Alloc::const_reference const_reference; + typedef typename _Alloc_traits::pointer pointer; + typedef typename _Alloc_traits::const_pointer const_pointer; + typedef typename _Alloc_traits::reference reference; + typedef typename _Alloc_traits::const_reference const_reference; typedef typename _Ht::const_iterator iterator; typedef typename _Ht::const_iterator const_iterator; diff --git a/libstdc++-v3/include/backward/hashtable.h b/libstdc++-v3/include/backward/hashtable.h index df6ad85191c..cfb9cf957d2 100644 --- a/libstdc++-v3/include/backward/hashtable.h +++ b/libstdc++-v3/include/backward/hashtable.h @@ -63,6 +63,7 @@ #include #include #include +#include #include namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) @@ -280,14 +281,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typedef _Hashtable_node<_Val> _Node; public: - typedef typename _Alloc::template rebind::other allocator_type; + typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template + rebind::other allocator_type; + allocator_type get_allocator() const { return _M_node_allocator; } private: - typedef typename _Alloc::template rebind<_Node>::other _Node_Alloc; - typedef typename _Alloc::template rebind<_Node*>::other _Nodeptr_Alloc; + typedef __gnu_cxx::__alloc_traits _Alloc_traits; + typedef typename _Alloc_traits::template rebind<_Node>::other + _Node_Alloc; + typedef typename _Alloc_traits::template rebind<_Node*>::other + _Nodeptr_Alloc; typedef std::vector<_Node*, _Nodeptr_Alloc> _Vector_type; _Node_Alloc _M_node_allocator; @@ -608,7 +614,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __n->_M_next = 0; __try { - this->get_allocator().construct(&__n->_M_val, __obj); + allocator_type __a = this->get_allocator(); + _Alloc_traits::construct(__a, &__n->_M_val, __obj); return __n; } __catch(...) @@ -621,7 +628,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void _M_delete_node(_Node* __n) { - this->get_allocator().destroy(&__n->_M_val); + allocator_type __a = this->get_allocator(); + _Alloc_traits::destroy(__a, &__n->_M_val); _M_put_node(__n); } diff --git a/libstdc++-v3/include/ext/alloc_traits.h b/libstdc++-v3/include/ext/alloc_traits.h index 65fa3de271c..65340eb7acc 100644 --- a/libstdc++-v3/include/ext/alloc_traits.h +++ b/libstdc++-v3/include/ext/alloc_traits.h @@ -132,6 +132,11 @@ template allocate(_Alloc& __a, size_type __n) { return __a.allocate(__n); } + template + _GLIBCXX_NODISCARD static pointer + allocate(_Alloc& __a, size_type __n, _Hint __hint) + { return __a.allocate(__n, __hint); } + static void deallocate(_Alloc& __a, pointer __p, size_type __n) { __a.deallocate(__p, __n); } diff --git a/libstdc++-v3/include/ext/extptr_allocator.h b/libstdc++-v3/include/ext/extptr_allocator.h index 2ce87b60c44..4e86fef81e4 100644 --- a/libstdc++-v3/include/ext/extptr_allocator.h +++ b/libstdc++-v3/include/ext/extptr_allocator.h @@ -92,8 +92,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT { return std::__addressof(__x); } - _GLIBCXX_NODISCARD pointer allocate(size_type __n, void* __hint = 0) - { return _M_real_alloc.allocate(__n,__hint); } + _GLIBCXX_NODISCARD pointer allocate(size_type __n, const void* = 0) + { return _M_real_alloc.allocate(__n); } void deallocate(pointer __p, size_type __n) { _M_real_alloc.deallocate(__p.get(), __n); } diff --git a/libstdc++-v3/include/ext/slist b/libstdc++-v3/include/ext/slist index 93522caf1f6..cbdfae0bd90 100644 --- a/libstdc++-v3/include/ext/slist +++ b/libstdc++-v3/include/ext/slist @@ -49,6 +49,7 @@ #include #include #include +#include namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) { @@ -251,7 +252,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next); _Slist_node_base* __next_next = __next->_M_next; __pos->_M_next = __next_next; - get_allocator().destroy(&__next->_M_data); + allocator_type __a = get_allocator(); + __alloc_traits::destroy(__a, &__next->_M_data); _M_put_node(__next); return __next_next; } @@ -268,7 +270,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { _Slist_node<_Tp>* __tmp = __cur; __cur = (_Slist_node<_Tp>*) __cur->_M_next; - get_allocator().destroy(&__tmp->_M_data); + allocator_type __a = get_allocator(); + __alloc_traits::destroy(__a, &__tmp->_M_data); _M_put_node(__tmp); } __before_first->_M_next = __last_node; @@ -318,7 +321,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Node* __node = this->_M_get_node(); __try { - get_allocator().construct(&__node->_M_data, __x); + allocator_type __a = get_allocator(); + __alloc_traits::construct(__a, &__node->_M_data, + __x); __node->_M_next = 0; } __catch(...) @@ -335,7 +340,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Node* __node = this->_M_get_node(); __try { - get_allocator().construct(&__node->_M_data, value_type()); + allocator_type __a = get_allocator(); + __alloc_traits::construct(__a, &__node->_M_data, + value_type()); __node->_M_next = 0; } __catch(...) @@ -481,7 +488,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { _Node* __node = (_Node*) this->_M_head._M_next; this->_M_head._M_next = __node->_M_next; - get_allocator().destroy(&__node->_M_data); + allocator_type __a = get_allocator(); + __alloc_traits::destroy(__a, &__node->_M_data); this->_M_put_node(__node); } diff --git a/libstdc++-v3/include/tr1/hashtable.h b/libstdc++-v3/include/tr1/hashtable.h index 354d7b814eb..e6455ebc3f2 100644 --- a/libstdc++-v3/include/tr1/hashtable.h +++ b/libstdc++-v3/include/tr1/hashtable.h @@ -126,6 +126,8 @@ namespace tr1 __constant_iterators, __unique_keys> > { + typedef __gnu_cxx::__alloc_traits<_Allocator> _Alloc_traits; + public: typedef _Allocator allocator_type; typedef _Value value_type; @@ -135,10 +137,10 @@ namespace tr1 // hasher, if present, comes from _Hash_code_base. typedef typename _Allocator::difference_type difference_type; typedef typename _Allocator::size_type size_type; - typedef typename _Allocator::pointer pointer; - typedef typename _Allocator::const_pointer const_pointer; - typedef typename _Allocator::reference reference; - typedef typename _Allocator::const_reference const_reference; + typedef typename _Alloc_traits::pointer pointer; + typedef typename _Alloc_traits::const_pointer const_pointer; + typedef typename _Alloc_traits::reference reference; + typedef typename _Alloc_traits::const_reference const_reference; typedef __detail::_Node_iterator @@ -162,13 +164,13 @@ namespace tr1 private: typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node; - typedef typename _Allocator::template rebind<_Node>::other - _Node_allocator_type; - typedef typename _Allocator::template rebind<_Node*>::other - _Bucket_allocator_type; + typedef typename _Alloc_traits::template rebind<_Node>::other + _Node_allocator_type; + typedef typename _Alloc_traits::template rebind<_Node*>::other + _Bucket_allocator_type; - typedef typename _Allocator::template rebind<_Value>::other - _Value_allocator_type; + typedef typename _Alloc_traits::template rebind<_Value>::other + _Value_allocator_type; _Node_allocator_type _M_node_allocator; _Node** _M_buckets; @@ -259,7 +261,10 @@ namespace tr1 size_type max_size() const - { return _M_node_allocator.max_size(); } + { + typedef __gnu_cxx::__alloc_traits<_Node_allocator_type> _Traits; + return _Traits::max_size(_M_node_allocator); + } // Observers key_equal diff --git a/libstdc++-v3/include/tr1/regex b/libstdc++-v3/include/tr1/regex index 73c610d2bbd..01c85e99623 100644 --- a/libstdc++-v3/include/tr1/regex +++ b/libstdc++-v3/include/tr1/regex @@ -1789,7 +1789,7 @@ namespace regex_constants */ //@{ typedef sub_match<_Bi_iter> value_type; - typedef typename _Allocator::const_reference const_reference; + typedef typename _Base_type::const_reference const_reference; typedef const_reference reference; typedef typename _Base_type::const_iterator const_iterator; typedef const_iterator iterator; diff --git a/libstdc++-v3/testsuite/backward/hash_map/23528.cc b/libstdc++-v3/testsuite/backward/hash_map/23528.cc index 01ff4bb42b2..712a65d11b9 100644 --- a/libstdc++-v3/testsuite/backward/hash_map/23528.cc +++ b/libstdc++-v3/testsuite/backward/hash_map/23528.cc @@ -30,9 +30,13 @@ void test01() __gnu_cxx::hash_map::value_type *y = a.allocate(1); +#if __cplusplus >= 201103L + std::allocator_traits::construct(a, y, *m.begin()); + std::allocator_traits::destroy(a, y); +#else a.construct(y, *m.begin()); - a.destroy(y); +#endif a.deallocate(y, 1); } diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc index 78febb07e06..0e01c0411fb 100644 --- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc @@ -19,14 +19,16 @@ #include #include +#include // libstdc++/29134 void test01() { std::tr1::unordered_map um; - VERIFY( (um.max_size() == std::allocator, false> >().max_size())); + std::allocator, + false> > a; + VERIFY( um.max_size() == __gnu_test::max_size(a) ); } int main() diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc index 6695fb9c5d4..f38c8df5c26 100644 --- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc @@ -19,14 +19,16 @@ #include #include +#include // libstdc++/29134 void test01() { std::tr1::unordered_multimap umm; - VERIFY( (umm.max_size() == std::allocator, false> >().max_size()) ); + std::allocator, + false> > a; + VERIFY( umm.max_size() == __gnu_test::max_size(a) ); } int main() diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc index e9da207f1fe..01a2c00c18b 100644 --- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc @@ -19,14 +19,15 @@ #include #include +#include // libstdc++/29134 void test01() { std::tr1::unordered_multiset ums; - VERIFY( (ums.max_size() == std::allocator >().max_size()) ); + std::allocator > a; + VERIFY( ums.max_size() == __gnu_test::max_size(a) ); } int main() diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc index 2080aaac01e..5dd133265ea 100644 --- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc +++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc @@ -19,14 +19,15 @@ #include #include +#include // libstdc++/29134 void test01() { std::tr1::unordered_set us; - VERIFY( (us.max_size() == std::allocator >().max_size()) ); + std::allocator > a; + VERIFY( us.max_size() == __gnu_test::max_size(a) ); } int main()