public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/65147] New: alignment of std::atomic object is not correct
@ 2015-02-20 20:01 alexey.lapshin at oracle dot com
  2015-02-20 22:11 ` [Bug c++/65147] " joseph at codesourcery dot com
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: alexey.lapshin at oracle dot com @ 2015-02-20 20:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65147

            Bug ID: 65147
           Summary: alignment of std::atomic object is not correct
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexey.lapshin at oracle dot com

According to the documentation -
https://gcc.gnu.org/wiki/Atomic/GCCMM/UnalignedPolicy alignment of atomic
object should match it`s size.
Alignment in the test case below does not match with documentation.

~/atomic_test$ cat unaligned_atomic.cpp

#include <atomic>
#include <stdio.h>

typedef struct {
    char c [8];
} power_of_two_obj;

typedef struct {
   char c[1];
   std::atomic<power_of_two_obj> ao;
} container_struct; 

int main ( void ) {

    std::atomic<power_of_two_obj> obj1;
    container_struct              obj2; 


    printf("\n Size and Alignment of std::atomic  object "); 
    printf(" : sizeof(obj1) %d alignof(obj1) %d ", sizeof(obj1), alignof(obj1)
);

    printf("\n Size and Alignment of std::atomic member object "); 
    printf(" : sizeof(obj2.ao) %d alignof(obj2.ao) %d \n", sizeof(obj2.ao),
alignof(obj2.ao) );

    return 0;
}

~/atomic_test$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/gcc/libexec/gcc/i386-pc-solaris2.11/4.9.2/lto-wrapper
Target: i386-pc-solaris2.11
Configured with: ./configure --prefix=/opt/gcc/
Thread model: posix
gcc version 4.9.2 (GCC) 

~/atomic_test$ g++ -O -latomic -std=c++11 unaligned_atomic.cpp -m32

~/atomic_test$ ./a.out

 Size and Alignment of std::atomic  object  : sizeof(obj1) 8 alignof(obj1) 1 
 Size and Alignment of std::atomic member object  : sizeof(obj2.ao) 8
alignof(obj2.ao) 1 

Alignment should be 8-bytes in above test case.

This behavior also differs from gcc. Gcc aligns 8-bytes objects to 8-bytes.

The bug was found on Solarix x86 -m32, but it could be on other platforms -
SPARC, Linux, -m64.

There is another problem with similar test case. If size of object is not power
of two then size of corresponding atomic object is also not power of two.
According to  https://gcc.gnu.org/wiki/Atomic/GCCMM/UnalignedPolicy the size
should be upsized in such case.


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2015-04-22 12:59 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-20 20:01 [Bug c++/65147] New: alignment of std::atomic object is not correct alexey.lapshin at oracle dot com
2015-02-20 22:11 ` [Bug c++/65147] " joseph at codesourcery dot com
2015-03-20 22:15 ` [Bug libstdc++/65147] " jason at gcc dot gnu.org
2015-03-23 18:07 ` alexey.lapshin at oracle dot com
2015-03-23 18:36 ` redi at gcc dot gnu.org
2015-03-26 19:59 ` redi at gcc dot gnu.org
2015-03-26 20:24 ` redi at gcc dot gnu.org
2015-03-27 15:58 ` alexey.lapshin at oracle dot com
2015-03-27 19:15 ` redi at gcc dot gnu.org
2015-03-28 10:29 ` redi at gcc dot gnu.org
2015-03-30 11:42 ` redi at gcc dot gnu.org
2015-04-09 11:16 ` redi at gcc dot gnu.org
2015-04-09 11:18 ` redi at gcc dot gnu.org
2015-04-10 14:46 ` alexey.lapshin at oracle dot com
2015-04-22 12:02 ` jakub at gcc dot gnu.org
2015-04-22 12:59 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).