re PR libstdc++/65147 (alignment of std::atomic object is not correct)
authorJonathan Wakely <jwakely@redhat.com>
Thu, 9 Apr 2015 11:15:44 +0000 (12:15 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 9 Apr 2015 11:15:44 +0000 (12:15 +0100)
2015-04-09  Jonathan Wakely  <jwakely@redhat.com>
    Richard Henderson  <rth@redhat.com>

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 <rth@redhat.com>
From-SVN: r221945

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/atomic_base.h
libstdc++-v3/include/std/atomic
libstdc++-v3/testsuite/29_atomics/atomic/60695.cc
libstdc++-v3/testsuite/29_atomics/atomic/65147.cc [new file with mode: 0644]
libstdc++-v3/testsuite/29_atomics/atomic_integral/65147.cc [new file with mode: 0644]

index ad50478..335b244 100644 (file)
@@ -1,3 +1,15 @@
+2015-04-09  Jonathan Wakely  <jwakely@redhat.com>
+           Richard Henderson  <rth@redhat.com>
+
+       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  <ro@CeBiTec.Uni-Bielefeld.DE>
 
        * config/abi/post/solaris2.10/baseline_symbols.txt: Regenerate.
index 8104c98..79769cf 100644 (file)
@@ -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;
index 88c8b17..125e37a 100644 (file)
@@ -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);
index 6f618a0..d3fa23d 100644 (file)
@@ -27,4 +27,4 @@ struct X {
   char stuff[0]; // GNU extension, type has zero size
 };
 
-std::atomic<X> a;  // { dg-error "not supported" "" { target *-*-* } 189 }
+std::atomic<X> 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 (file)
index 0000000..e05ec17
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+#include <atomic>
+
+struct S16 {
+   char c[16];
+};
+
+static_assert( alignof(std::atomic<S16>) >= 16,
+    "atomic<S16> 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 (file)
index 0000000..a5f5b74
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+#include <atomic>
+
+static_assert( alignof(std::atomic<char>) >= sizeof(char),
+    "atomic<char> must be aligned to at least its size" );
+static_assert( alignof(std::atomic<short>) >= sizeof(short),
+    "atomic<short> must be aligned to at least its size" );
+static_assert( alignof(std::atomic<int>) >= sizeof(int),
+    "atomic<int> must be aligned to at least its size" );
+static_assert( alignof(std::atomic<long>) >= sizeof(long),
+    "atomic<long> must be aligned to at least its size" );
+static_assert( alignof(std::atomic<long long>) >= sizeof(long long),
+    "atomic<long long> must be aligned to at least its size" );