From 07cfc2d75d85c5fc961bdd8434673f0a5f40587d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Dumont?= Date: Mon, 12 Jun 2017 20:38:16 +0000 Subject: [PATCH] stl_tree.h (_Rb_tree_impl()): Restore _Node_allocator default init. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2017-06-12 François Dumont * include/bits/stl_tree.h (_Rb_tree_impl()): Restore _Node_allocator default init. * testsuite/util/testsuite_allocator.h (__gnu_test::default_init_allocator<>) New. * testsuite/23_containers/set/allocator/default_init.cc: New. * testsuite/23_containers/map/allocator/default_init.cc: New. From-SVN: r249136 --- libstdc++-v3/ChangeLog | 9 +++ libstdc++-v3/include/bits/stl_tree.h | 15 +++-- .../map/allocator/default_init.cc | 67 +++++++++++++++++++ .../set/allocator/default_init.cc | 67 +++++++++++++++++++ .../testsuite/util/testsuite_allocator.h | 32 +++++++++ 5 files changed, 183 insertions(+), 7 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/map/allocator/default_init.cc create mode 100644 libstdc++-v3/testsuite/23_containers/set/allocator/default_init.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 9e0a5908e65..5ee9e68af68 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2017-06-12 François Dumont + + * include/bits/stl_tree.h (_Rb_tree_impl()): Restore _Node_allocator + default init. + * testsuite/util/testsuite_allocator.h + (__gnu_test::default_init_allocator<>) New. + * testsuite/23_containers/set/allocator/default_init.cc: New. + * testsuite/23_containers/map/allocator/default_init.cc: New. + 2017-06-12 Jonathan Wakely PR libstdc++/55917 diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index 3f133b0dad0..c2417f1f8cc 100644 --- a/libstdc++-v3/include/bits/stl_tree.h +++ b/libstdc++-v3/include/bits/stl_tree.h @@ -572,11 +572,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Node_allocator& _M_get_Node_allocator() _GLIBCXX_NOEXCEPT - { return *static_cast<_Node_allocator*>(&this->_M_impl); } + { return this->_M_impl; } const _Node_allocator& _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT - { return *static_cast(&this->_M_impl); } + { return this->_M_impl; } allocator_type get_allocator() const _GLIBCXX_NOEXCEPT @@ -685,13 +685,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare; -#if __cplusplus < 201103L _Rb_tree_impl() + _GLIBCXX_NOEXCEPT_IF( + is_nothrow_default_constructible<_Node_allocator>::value + && is_nothrow_default_constructible<_Base_key_compare>::value ) + : _Node_allocator() { } -#else - _Rb_tree_impl() = default; - _Rb_tree_impl(_Rb_tree_impl&&) = default; -#endif _Rb_tree_impl(const _Rb_tree_impl& __x) : _Node_allocator(_Alloc_traits::_S_select_on_copy(__x)) @@ -703,6 +702,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _Node_allocator(__a), _Base_key_compare(__comp) { } #else + _Rb_tree_impl(_Rb_tree_impl&&) = default; + _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a) : _Node_allocator(std::move(__a)), _Base_key_compare(__comp) { } diff --git a/libstdc++-v3/testsuite/23_containers/map/allocator/default_init.cc b/libstdc++-v3/testsuite/23_containers/map/allocator/default_init.cc new file mode 100644 index 00000000000..1ef13d9d22d --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/allocator/default_init.cc @@ -0,0 +1,67 @@ +// Copyright (C) 2017 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 +// . + +// { dg-do run { target c++11 } } +// { dg-options "-O0" } +// { dg-xfail-run-if "PR c++/65816" { *-*-* } } + +#include +#include +#include + +#include + +using T = int; + +using __gnu_test::default_init_allocator; + +void test01() +{ + typedef default_init_allocator> alloc_type; + typedef std::map, alloc_type> test_type; + + __gnu_cxx::__aligned_buffer buf; + __builtin_memset(buf._M_addr(), ~0, sizeof(test_type)); + + test_type *tmp = ::new(buf._M_addr()) test_type; + + VERIFY( tmp->get_allocator().state == 0 ); + + tmp->~test_type(); +} + +void test02() +{ + typedef default_init_allocator> alloc_type; + typedef std::map, alloc_type> test_type; + + __gnu_cxx::__aligned_buffer buf; + __builtin_memset(buf._M_addr(), ~0, sizeof(test_type)); + + test_type *tmp = ::new(buf._M_addr()) test_type(); + + VERIFY( tmp->get_allocator().state == 0 ); + + tmp->~test_type(); +} + +int main() +{ + test01(); + test02(); + return 0; +} diff --git a/libstdc++-v3/testsuite/23_containers/set/allocator/default_init.cc b/libstdc++-v3/testsuite/23_containers/set/allocator/default_init.cc new file mode 100644 index 00000000000..4e14e184a71 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/set/allocator/default_init.cc @@ -0,0 +1,67 @@ +// Copyright (C) 2017 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 +// . + +// { dg-do run { target c++11 } } +// { dg-options "-O0" } +// { dg-xfail-run-if "PR c++/65816" { *-*-* } } + +#include +#include +#include + +#include + +using T = int; + +using __gnu_test::default_init_allocator; + +void test01() +{ + typedef default_init_allocator alloc_type; + typedef std::set, alloc_type> test_type; + + __gnu_cxx::__aligned_buffer buf; + __builtin_memset(buf._M_addr(), ~0, sizeof(test_type)); + + test_type *tmp = ::new(buf._M_addr()) test_type; + + VERIFY( tmp->get_allocator().state == 0 ); + + tmp->~test_type(); +} + +void test02() +{ + typedef default_init_allocator alloc_type; + typedef std::set, alloc_type> test_type; + + __gnu_cxx::__aligned_buffer buf; + __builtin_memset(buf._M_addr(), ~0, sizeof(test_type)); + + test_type *tmp = ::new(buf._M_addr()) test_type(); + + VERIFY( tmp->get_allocator().state == 0 ); + + tmp->~test_type(); +} + +int main() +{ + test01(); + test02(); + return 0; +} diff --git a/libstdc++-v3/testsuite/util/testsuite_allocator.h b/libstdc++-v3/testsuite/util/testsuite_allocator.h index 56c27089e84..233ea0baefc 100644 --- a/libstdc++-v3/testsuite/util/testsuite_allocator.h +++ b/libstdc++-v3/testsuite/util/testsuite_allocator.h @@ -508,6 +508,38 @@ namespace __gnu_test bool operator!=(const SimpleAllocator&, const SimpleAllocator&) { return false; } + template + struct default_init_allocator + { + using value_type = T; + + default_init_allocator() = default; + + template + default_init_allocator(const default_init_allocator& a) + : state(a.state) + { } + + T* + allocate(std::size_t n) + { return std::allocator().allocate(n); } + + void + deallocate(T* p, std::size_t n) + { std::allocator().deallocate(p, n); } + + int state; + }; + + template + bool operator==(const default_init_allocator& t, + const default_init_allocator& u) + { return t.state == u.state; } + + template + bool operator!=(const default_init_allocator& t, + const default_init_allocator& u) + { return !(t == u); } #endif template -- 2.30.2