This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: will __attribute__((aligned(CACHE_LINE_SIZE))) break struct alignment
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Yubin Ruan <ablacktshirt at gmail dot com>
- Cc: Florian Weimer <fweimer at redhat dot com>, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Mon, 6 Nov 2017 16:41:02 +0000
- Subject: Re: will __attribute__((aligned(CACHE_LINE_SIZE))) break struct alignment
- Authentication-results: sourceware.org; auth=none
- References: <CAJYFCiPAUA4msc3np+RR-fyM3wgc5wVvX5+u5+k0TcbR4rQLbw@mail.gmail.com> <719bc667-234a-ac75-b134-be6238758f49@redhat.com> <CAJYFCiP=Y+k=2C=t11LvbtsT4QwjibnkG1q66_6kp--OmRYn2Q@mail.gmail.com>
On 6 November 2017 at 15:46, Yubin Ruan wrote:
> Thanks Florian,
>
> 2017年11月6日星期一,Florian Weimer <fweimer@redhat.com> 写道:
>
>> On 11/06/2017 06:04 AM, Yubin Ruan wrote:
>>
>>> I am using something like this:
>>>
>>> struct S1 {
>>> int index __attribute__((aligned(CACHE_LINE_SIZE)));
>>> struct S2 some_other;
>>> };
>>>
>>> I am not using anything like `#pragma pack`.
>>>
>>> I am wondering whether adding that
>>> `__attribute__((aligned(CACHE_LINE_SIZE)))` will break GCC's automatic
>>> alignment for S1 and S2. Can anyone point me to any document which
>>> specify this behavior?
>>>
>>
>> It will change the alignment of struct S1, so code which allocates objects
>> of struct S1 will have to be modified, and the offset in structs of a
>> member of type struct S1 will likely change (and so will the offsets of
>> members after it).
>>
>> So in general, this is not an ABI-compatible change.
>>
>
> Hmm... if struct S1 was aligned at a power of 4 boundary, then will that
> alignment be changed after adding that
> "__attribute__((aligned(CACHE_LINE_SIZE)))"? The same for "someother" ?
Assuming that CACHE_LINE_SIZE is also a power of four (which must be
true unless it equals 1 or 2) then it will still be a power of four,
but it might be a different power of four.
If the original alignment is 16 and CACHE_LINE_SIZE == 64 then it
changes from 16 to 64.
If the original alignment is 32 and CACHE_LINE_SIZE == 32 it won't change.