From 4cbaaa459e7f402911bb79fade9ffdac194eae75 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 9 Apr 2015 12:15:44 +0100 Subject: [PATCH] re PR libstdc++/65147 (alignment of std::atomic object is not correct) 2015-04-09 Jonathan Wakely Richard Henderson PR libstdc++/65147 * include/bits/atomic_base.h (__atomic_base<_ITp>): Increase alignment. * include/std/atomic (atomic): For types with a power of two size set alignment to at least the size. * testsuite/29_atomics/atomic/60695.cc: Adjust dg-error line number. * testsuite/29_atomics/atomic/65147.cc: New. * testsuite/29_atomics/atomic_integral/65147.cc: New. Co-Authored-By: Richard Henderson From-SVN: r221945 --- libstdc++-v3/ChangeLog | 12 +++++++ libstdc++-v3/include/bits/atomic_base.h | 5 ++- libstdc++-v3/include/std/atomic | 14 ++------ .../testsuite/29_atomics/atomic/60695.cc | 2 +- .../testsuite/29_atomics/atomic/65147.cc | 28 ++++++++++++++++ .../29_atomics/atomic_integral/65147.cc | 32 +++++++++++++++++++ 6 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic/65147.cc create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_integral/65147.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ad504782b6a..335b2444eec 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2015-04-09 Jonathan Wakely + Richard Henderson + + PR libstdc++/65147 + * include/bits/atomic_base.h (__atomic_base<_ITp>): Increase + alignment. + * include/std/atomic (atomic): For types with a power of two size set + alignment to at least the size. + * testsuite/29_atomics/atomic/60695.cc: Adjust dg-error line number. + * testsuite/29_atomics/atomic/65147.cc: New. + * testsuite/29_atomics/atomic_integral/65147.cc: New. + 2015-04-09 Rainer Orth * config/abi/post/solaris2.10/baseline_symbols.txt: Regenerate. diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index 8104c986b52..79769cf46d8 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -240,7 +240,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION private: typedef _ITp __int_type; - __int_type _M_i; + static constexpr int _S_alignment = + sizeof(_ITp) > alignof(_ITp) ? sizeof(_ITp) : alignof(_ITp); + + alignas(_S_alignment) __int_type _M_i; public: __atomic_base() noexcept = default; diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic index 88c8b17dbc9..125e37a2838 100644 --- a/libstdc++-v3/include/std/atomic +++ b/libstdc++-v3/include/std/atomic @@ -165,18 +165,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct atomic { private: - // Align 1/2/4/8/16-byte types the same as integer types of that size. - // This matches the alignment effects of the C11 _Atomic qualifier. + // Align 1/2/4/8/16-byte types to at least their size. static constexpr int _S_min_alignment - = sizeof(_Tp) == sizeof(char) ? alignof(char) - : sizeof(_Tp) == sizeof(short) ? alignof(short) - : sizeof(_Tp) == sizeof(int) ? alignof(int) - : sizeof(_Tp) == sizeof(long) ? alignof(long) - : sizeof(_Tp) == sizeof(long long) ? alignof(long long) -#ifdef _GLIBCXX_USE_INT128 - : sizeof(_Tp) == sizeof(__int128) ? alignof(__int128) -#endif - : 0; + = (sizeof(_Tp) & (sizeof(_Tp) - 1)) || sizeof(_Tp) > 16 + ? 0 : sizeof(_Tp); static constexpr int _S_alignment = _S_min_alignment > alignof(_Tp) ? _S_min_alignment : alignof(_Tp); diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc b/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc index 6f618a0e797..d3fa23d6644 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/60695.cc @@ -27,4 +27,4 @@ struct X { char stuff[0]; // GNU extension, type has zero size }; -std::atomic a; // { dg-error "not supported" "" { target *-*-* } 189 } +std::atomic a; // { dg-error "not supported" "" { target *-*-* } 181 } diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/65147.cc b/libstdc++-v3/testsuite/29_atomics/atomic/65147.cc new file mode 100644 index 00000000000..e05ec173c31 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic/65147.cc @@ -0,0 +1,28 @@ +// Copyright (C) 2015 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-options "-std=gnu++11" } +// { dg-do compile } + +#include + +struct S16 { + char c[16]; +}; + +static_assert( alignof(std::atomic) >= 16, + "atomic must be aligned to at least its size" ); diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_integral/65147.cc b/libstdc++-v3/testsuite/29_atomics/atomic_integral/65147.cc new file mode 100644 index 00000000000..a5f5b748bc4 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic_integral/65147.cc @@ -0,0 +1,32 @@ +// Copyright (C) 2015 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-options "-std=gnu++11" } +// { dg-do compile } + +#include + +static_assert( alignof(std::atomic) >= sizeof(char), + "atomic must be aligned to at least its size" ); +static_assert( alignof(std::atomic) >= sizeof(short), + "atomic must be aligned to at least its size" ); +static_assert( alignof(std::atomic) >= sizeof(int), + "atomic must be aligned to at least its size" ); +static_assert( alignof(std::atomic) >= sizeof(long), + "atomic must be aligned to at least its size" ); +static_assert( alignof(std::atomic) >= sizeof(long long), + "atomic must be aligned to at least its size" ); -- 2.30.2