diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 4674e1f7318..9618c9eb275 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,29 @@ +2014-01-28 Jonathan Wakely + + * include/bits/alloc_traits.h (allocator_traits::_S_allocate): Do + not use varargs when argument could be non-POD. + (__alloctr_rebind_helper): Eliminate static const bool member by + using true_type and false_type. + (allocator_traits::__allocate_helper): Likewise. + (allocator_traits::__construct_helper): Likewise. + (allocator_traits::__destroy_helper): Likewise. + (allocator_traits::__maxsize_helper): Likewise. + (allocator_traits::__select_helper): Likewise. + * include/bits/ptr_traits.h (__ptrtr_rebind_helper): Likewise. + * include/bits/stl_tree.h (_Rb_tree::operator=(const _Rb_tree&)): + Remove redundant condition. + * include/bits/stl_vector.h (vector::operator=(const vector&)): + Likewise. + (_Vector_impl::_M_allocate, _Vector_impl::_M_deallocate): Use + indirection through __alloc_traits. + * include/ext/alloc_traits.h (__allocator_always_compares_equal): + Eliminate static const bool members by using true_type and false_type. + (__gnu_cxx::__alloc_traits::__is_custom_pointer): Optimize. + * testsuite/util/testsuite_allocator.h (PointerBase): Define. + * testsuite/20_util/allocator_traits/members/allocate_hint_nonpod.cc: + New. + * testsuite/20_util/allocator_traits/requirements/typedefs2.cc: New. + 2014-01-28 Jonathan Wakely Kyle Lippincott diff --git a/libstdc++-v3/include/bits/alloc_traits.h b/libstdc++-v3/include/bits/alloc_traits.h index 86ed222ccec..23fe8de0947 100644 --- a/libstdc++-v3/include/bits/alloc_traits.h +++ b/libstdc++-v3/include/bits/alloc_traits.h @@ -44,24 +44,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION class __alloctr_rebind_helper { template - static constexpr bool - _S_chk(typename _Alloc2::template rebind<_Tp2>::other*) - { return true; } + static constexpr true_type + _S_chk(typename _Alloc2::template rebind<_Tp2>::other*); template - static constexpr bool - _S_chk(...) - { return false; } + static constexpr false_type + _S_chk(...); public: - static const bool __value = _S_chk<_Alloc, _Tp>(nullptr); + using __type = decltype(_S_chk<_Alloc, _Tp>(nullptr)); }; - template - const bool __alloctr_rebind_helper<_Alloc, _Tp>::__value; - template::__value> + bool = __alloctr_rebind_helper<_Alloc, _Tp>::__type::value> struct __alloctr_rebind; template @@ -71,7 +66,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }; template class _Alloc, typename _Tp, - typename _Up, typename... _Args> + typename _Up, typename... _Args> struct __alloctr_rebind<_Alloc<_Up, _Args...>, _Tp, false> { typedef _Alloc<_Tp, _Args...> __type; @@ -140,7 +135,7 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer, typedef __const_void_pointer const_void_pointer; _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type, - typename pointer_traits::difference_type) + typename pointer_traits::difference_type) /** * @brief The allocator's difference type @@ -151,7 +146,7 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type, typedef __difference_type difference_type; _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type, - typename make_unsigned::type) + typename make_unsigned::type) /** * @brief The allocator's size type @@ -162,7 +157,7 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type, typedef __size_type size_type; _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment, - false_type) + false_type) /** * @brief How the allocator is propagated on copy assignment @@ -171,10 +166,10 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment, * otherwise @c false_type */ typedef __propagate_on_container_copy_assignment - propagate_on_container_copy_assignment; + propagate_on_container_copy_assignment; _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment, - false_type) + false_type) /** * @brief How the allocator is propagated on move assignment @@ -183,10 +178,10 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment, * otherwise @c false_type */ typedef __propagate_on_container_move_assignment - propagate_on_container_move_assignment; + propagate_on_container_move_assignment; _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, - false_type) + false_type) /** * @brief How the allocator is propagated on swap @@ -199,9 +194,9 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, #undef _GLIBCXX_ALLOC_TR_NESTED_TYPE template - using rebind_alloc = typename __alloctr_rebind<_Alloc, _Tp>::__type; + using rebind_alloc = typename __alloctr_rebind<_Alloc, _Tp>::__type; template - using rebind_traits = allocator_traits>; + using rebind_traits = allocator_traits>; private: template @@ -216,20 +211,22 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, template static false_type __test(...); - typedef decltype(__test<_Alloc>(0)) type; - static const bool value = type::value; + using type = decltype(__test<_Alloc>(0)); }; template - static typename - enable_if<__allocate_helper<_Alloc2>::value, pointer>::type - _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint) + using __has_allocate = typename __allocate_helper<_Alloc2>::type; + + template>> + static pointer + _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint) { return __a.allocate(__n, __hint); } - template - static typename - enable_if::value, pointer>::type - _S_allocate(_Alloc2& __a, size_type __n, ...) + template>>> + static pointer + _S_allocate(_Alloc2& __a, size_type __n, _UnusedHint) { return __a.allocate(__n); } template @@ -243,21 +240,23 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, template static false_type __test(...); - typedef decltype(__test<_Alloc>(0)) type; - static const bool value = type::value; + using type = decltype(__test<_Alloc>(0)); }; template - static typename - enable_if<__construct_helper<_Tp, _Args...>::value, void>::type - _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args) + using __has_construct + = typename __construct_helper<_Tp, _Args...>::type; + + template + static _Require<__has_construct<_Tp, _Args...>> + _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args) { __a.construct(__p, std::forward<_Args>(__args)...); } template - static typename - enable_if<__and_<__not_<__construct_helper<_Tp, _Args...>>, - is_constructible<_Tp, _Args...>>::value, void>::type - _S_construct(_Alloc&, _Tp* __p, _Args&&... __args) + static + _Require<__and_<__not_<__has_construct<_Tp, _Args...>>, + is_constructible<_Tp, _Args...>>> + _S_construct(_Alloc&, _Tp* __p, _Args&&... __args) { ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); } template @@ -271,18 +270,20 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, template static false_type __test(...); - typedef decltype(__test<_Alloc>(0)) type; - static const bool value = type::value; + using type = decltype(__test<_Alloc>(0)); }; template - static typename enable_if<__destroy_helper<_Tp>::value, void>::type - _S_destroy(_Alloc& __a, _Tp* __p) + using __has_destroy = typename __destroy_helper<_Tp>::type; + + template + static _Require<__has_destroy<_Tp>> + _S_destroy(_Alloc& __a, _Tp* __p) { __a.destroy(__p); } template - static typename enable_if::value, void>::type - _S_destroy(_Alloc&, _Tp* __p) + static _Require<__not_<__has_destroy<_Tp>>> + _S_destroy(_Alloc&, _Tp* __p) { __p->~_Tp(); } template @@ -295,20 +296,22 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, template static false_type __test(...); - typedef decltype(__test<_Alloc2>(0)) type; - static const bool value = type::value; + using type = decltype(__test<_Alloc2>(0)); }; template - static typename - enable_if<__maxsize_helper<_Alloc2>::value, size_type>::type - _S_max_size(_Alloc2& __a) + using __has_max_size = typename __maxsize_helper<_Alloc2>::type; + + template>> + static size_type + _S_max_size(_Alloc2& __a, int) { return __a.max_size(); } - template - static typename - enable_if::value, size_type>::type - _S_max_size(_Alloc2&) + template>>> + static size_type + _S_max_size(_Alloc2&, ...) { return __gnu_cxx::__numeric_traits::__max; } template @@ -322,19 +325,22 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, template static false_type __test(...); - typedef decltype(__test<_Alloc2>(0)) type; - static const bool value = type::value; + using type = decltype(__test<_Alloc2>(0)); }; - template - static typename - enable_if<__select_helper<_Alloc2>::value, _Alloc2>::type - _S_select(_Alloc2& __a) - { return __a.select_on_container_copy_construction(); } template - static typename - enable_if::value, _Alloc2>::type - _S_select(_Alloc2& __a) + using __has_soccc = typename __select_helper<_Alloc2>::type; + + template>> + static _Alloc2 + _S_select(_Alloc2& __a, int) + { return __a.select_on_container_copy_construction(); } + + template>>> + static _Alloc2 + _S_select(_Alloc2& __a, ...) { return __a; } public: @@ -413,7 +419,7 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, * otherwise returns @c numeric_limits::max() */ static size_type max_size(const _Alloc& __a) noexcept - { return _S_max_size(__a); } + { return _S_max_size(__a, 0); } /** * @brief Obtain an allocator to use when copying a container. @@ -425,30 +431,9 @@ _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap, */ static _Alloc select_on_container_copy_construction(const _Alloc& __rhs) - { return _S_select(__rhs); } + { return _S_select(__rhs, 0); } }; - template - template - const bool allocator_traits<_Alloc>::__allocate_helper<_Alloc2>::value; - - template - template - const bool - allocator_traits<_Alloc>::__construct_helper<_Tp, _Args...>::value; - - template - template - const bool allocator_traits<_Alloc>::__destroy_helper<_Tp>::value; - - template - template - const bool allocator_traits<_Alloc>::__maxsize_helper<_Alloc2>::value; - - template - template - const bool allocator_traits<_Alloc>::__select_helper<_Alloc2>::value; - template inline void __do_alloc_on_copy(_Alloc& __one, const _Alloc& __two, true_type) diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h index 714db790320..94995c8fb2e 100644 --- a/libstdc++-v3/include/bits/ptr_traits.h +++ b/libstdc++-v3/include/bits/ptr_traits.h @@ -73,24 +73,19 @@ _GLIBCXX_HAS_NESTED_TYPE(difference_type) class __ptrtr_rebind_helper { template - static constexpr bool - _S_chk(typename _Ptr2::template rebind<_Up2>*) - { return true; } + static constexpr true_type + _S_chk(typename _Ptr2::template rebind<_Up2>*); template - static constexpr bool - _S_chk(...) - { return false; } + static constexpr false_type + _S_chk(...); public: - static const bool __value = _S_chk<_Ptr, _Up>(nullptr); + using __type = decltype(_S_chk<_Ptr, _Up>(nullptr)); }; - template - const bool __ptrtr_rebind_helper<_Ptr, _Up>::__value; - template::__value> + bool = __ptrtr_rebind_helper<_Tp, _Up>::__type::value> struct __ptrtr_rebind; template diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index d24b1f750bc..4bc3c602c20 100644 --- a/libstdc++-v3/include/bits/stl_tree.h +++ b/libstdc++-v3/include/bits/stl_tree.h @@ -1080,9 +1080,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION clear(); if (__x._M_root() != 0) _M_move_data(__x, std::true_type()); - if (_Alloc_traits::_S_propagate_on_move_assign()) - std::__alloc_on_move(_M_get_Node_allocator(), - __x._M_get_Node_allocator()); + std::__alloc_on_move(_M_get_Node_allocator(), + __x._M_get_Node_allocator()); return true; } return false; diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index f4829575e73..98ac708e8f1 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -165,13 +165,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER pointer _M_allocate(size_t __n) - { return __n != 0 ? _M_impl.allocate(__n) : 0; } + { + typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr; + return __n != 0 ? _Tr::allocate(_M_impl, __n) : 0; + } void _M_deallocate(pointer __p, size_t __n) { + typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr; if (__p) - _M_impl.deallocate(__p, __n); + _Tr::deallocate(_M_impl, __p, __n); } private: @@ -1446,9 +1450,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER vector __tmp(get_allocator()); this->_M_impl._M_swap_data(__tmp._M_impl); this->_M_impl._M_swap_data(__x._M_impl); - if (_Alloc_traits::_S_propagate_on_move_assign()) - std::__alloc_on_move(_M_get_Tp_allocator(), - __x._M_get_Tp_allocator()); + std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator()); } // Do move assignment when it might not be possible to move source diff --git a/libstdc++-v3/include/ext/alloc_traits.h b/libstdc++-v3/include/ext/alloc_traits.h index 26e457ff253..14fbc4359ec 100644 --- a/libstdc++-v3/include/ext/alloc_traits.h +++ b/libstdc++-v3/include/ext/alloc_traits.h @@ -44,73 +44,47 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cplusplus >= 201103L template - struct __allocator_always_compares_equal - { static const bool value = false; }; - - template - const bool __allocator_always_compares_equal<_Alloc>::value; + struct __allocator_always_compares_equal : std::false_type { }; template struct __allocator_always_compares_equal> - { static const bool value = true; }; - - template - const bool __allocator_always_compares_equal>::value; + : std::true_type { }; template struct array_allocator; template struct __allocator_always_compares_equal> - { static const bool value = true; }; - - template - const bool - __allocator_always_compares_equal>::value; + : std::true_type { }; template struct bitmap_allocator; template struct __allocator_always_compares_equal> - { static const bool value = true; }; - - template - const bool __allocator_always_compares_equal>::value; + : std::true_type { }; template struct malloc_allocator; template struct __allocator_always_compares_equal> - { static const bool value = true; }; - - template - const bool __allocator_always_compares_equal>::value; + : std::true_type { }; template struct mt_allocator; template struct __allocator_always_compares_equal> - { static const bool value = true; }; - - template - const bool __allocator_always_compares_equal>::value; + : std::true_type { }; template struct new_allocator; template struct __allocator_always_compares_equal> - { static const bool value = true; }; - - template - const bool __allocator_always_compares_equal>::value; + : std::true_type { }; template struct pool_allocator; template struct __allocator_always_compares_equal> - { static const bool value = true; }; - - template - const bool __allocator_always_compares_equal>::value; + : std::true_type { }; #endif /** @@ -131,7 +105,7 @@ template typedef typename _Base_type::const_pointer const_pointer; typedef typename _Base_type::size_type size_type; typedef typename _Base_type::difference_type difference_type; - // C++0x allocators do not define reference or const_reference + // C++11 allocators do not define reference or const_reference typedef value_type& reference; typedef const value_type& const_reference; using _Base_type::allocate; @@ -142,10 +116,9 @@ template private: template - struct __is_custom_pointer - : std::integral_constant::value - && !std::is_pointer<_Ptr>::value> - { }; + using __is_custom_pointer + = std::__and_, + std::__not_>>; public: // overload construct for non-standard pointer types diff --git a/libstdc++-v3/testsuite/20_util/allocator_traits/members/allocate_hint_nonpod.cc b/libstdc++-v3/testsuite/20_util/allocator_traits/members/allocate_hint_nonpod.cc new file mode 100644 index 00000000000..b5883dcccce --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/allocator_traits/members/allocate_hint_nonpod.cc @@ -0,0 +1,69 @@ +// { dg-options "-std=gnu++11" } + +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +// User-defined pointer type with non-trivial destructor. +template +struct Pointer : __gnu_test::PointerBase, T> +{ + using __gnu_test::PointerBase, T>::PointerBase; + + ~Pointer() { /* non-trivial */ } +}; + +// Minimal allocator with user-defined pointer type. +template +struct Alloc +{ + typedef T value_type; + typedef Pointer pointer; + + Alloc() = default; + + template + Alloc(const Alloc&) { } + + pointer allocate(std::size_t n) // does not take a hint + { return pointer(std::allocator().allocate(n)); } + + void deallocate(pointer p, std::size_t n) + { std::allocator().deallocate(p.value, n); } +}; + +template +bool operator==(Alloc l, Alloc r) { return true; } + +template +bool operator!=(Alloc l, Alloc r) { return false; } + +void test01() +{ + typedef std::allocator_traits> traits_type; + traits_type::allocator_type a; + traits_type::const_void_pointer v; + traits_type::pointer p = traits_type::allocate(a, 1, v); + traits_type::deallocate(a, p, 1); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/allocator_traits/requirements/typedefs2.cc b/libstdc++-v3/testsuite/20_util/allocator_traits/requirements/typedefs2.cc new file mode 100644 index 00000000000..04521a269c8 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/allocator_traits/requirements/typedefs2.cc @@ -0,0 +1,93 @@ +// { dg-options "-std=gnu++11" } +// +// Copyright (C) 2013 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +// { dg-do compile } + +template +struct ptr +{ + ptr() = default; + template + ptr(ptr const& p) { } +}; + +// This doesn't meet the allocator requirements, it's only to check +// that allocator_traits finds the right nested types. +template +struct alloc +{ + typedef T value_type; + + typedef ptr pointer; + typedef ptr const_pointer; + typedef ptr void_pointer; + typedef ptr const_void_pointer; + typedef int difference_type; + typedef int size_type; + + typedef std::false_type propagate_on_container_copy_assignment; + typedef std::false_type propagate_on_container_move_assignment; + typedef std::false_type propagate_on_container_swap; +}; + +typedef alloc alloc_type; +typedef std::allocator_traits traits; + +using std::is_same; + +static_assert( is_same::value, + "pointer" ); + +static_assert( is_same::value, + "const_pointer" ); + +static_assert( is_same::value, + "void_pointer" ); + +static_assert( is_same::value, + "const_void_pointer"); + +static_assert( is_same::value, + "difference_type" ); + +static_assert( is_same::value, + "size_type" ); + +static_assert( is_same::value, + "size_type" ); + +static_assert( is_same::value, + "propagate_on_container_copy_assignment" ); + +static_assert( is_same::value, + "propagate_on_container_move_assignment" ); + +static_assert( is_same::value, + "propagate_on_container_swap" ); diff --git a/libstdc++-v3/testsuite/util/testsuite_allocator.h b/libstdc++-v3/testsuite/util/testsuite_allocator.h index 748557cf51c..822a025f4fa 100644 --- a/libstdc++-v3/testsuite/util/testsuite_allocator.h +++ b/libstdc++-v3/testsuite/util/testsuite_allocator.h @@ -517,6 +517,106 @@ namespace __gnu_test void deallocate(pointer p, std::size_t n) { std::allocator::deallocate(std::addressof(*p), n); } }; + + // Utility for use as CRTP base class of custom pointer types + template + struct PointerBase + { + typedef T element_type; + + // typedefs for iterator_traits + typedef T value_type; + typedef std::ptrdiff_t difference_type; + typedef std::random_access_iterator_tag iterator_category; + typedef Derived pointer; + typedef T& reference; + + T* value; + + explicit PointerBase(T* p = nullptr) : value(p) { } + + template(std::declval()))> + PointerBase(const PointerBase& p) : value(p.value) { } + + T& operator*() const { return *value; } + T* operator->() const { return value; } + + Derived& operator++() { ++value; return derived(); } + Derived operator++(int) { Derived tmp(derived()); ++value; return tmp; } + Derived& operator--() { --value; return derived(); } + Derived operator--(int) { Derived tmp(derived()); --value; return tmp; } + + Derived& operator+=(difference_type n) { value += n; return derived(); } + Derived& operator-=(difference_type n) { value -= n; return derived(); } + + explicit operator bool() const { return value != nullptr; } + + Derived + operator+(difference_type n) const + { + Derived p(derived()); + return p += n; + } + + Derived + operator-(difference_type n) const + { + Derived p(derived()); + return p -= n; + } + + private: + Derived& derived() { return static_cast(*this); } + }; + + template + std::ptrdiff_t operator-(PointerBase l, PointerBase r) + { return l.value - r.value; } + + template + bool operator==(PointerBase l, PointerBase r) + { return l.value == r.value; } + + template + bool operator!=(PointerBase l, PointerBase r) + { return l.value != r.value; } + + // implementation for void specializations + template + struct PointerBase_void + { + typedef T element_type; + + // typedefs for iterator_traits + typedef T value_type; + typedef std::ptrdiff_t difference_type; + typedef std::random_access_iterator_tag iterator_category; + + T* value; + + explicit PointerBase_void(T* p = nullptr) : value(p) { } + + template(std::declval()))> + PointerBase_void(const PointerBase& p) : value(p.value) { } + + explicit operator bool() const { return value != nullptr; } + }; + + template + struct PointerBase : PointerBase_void + { + using PointerBase_void::PointerBase_void; + typedef Derived pointer; + }; + + template + struct PointerBase : PointerBase_void + { + using PointerBase_void::PointerBase_void; + typedef Derived pointer; + }; #endif } // namespace __gnu_test