Re: __attribute__ aligned could be more efficient

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I think that sizeof(struct bar2) equal 192 is pretty obvious. Size of
struct foo2 is 128 bytes and after that comes 4/8 bytes of int. This
means that size of struct foo2 is 132/136, but requested alignment is
64 bytes, so alignment of bar2 must be also 64 bytes. This means that
if array of bar2 structures is created, each of them must start at 64
byte boundary. 128 is out of question, but 192 is fine, since it's
nearest value divisible by 64. So 192 bytes it is.

Correct me if I'm wrong.

On Sun, Mar 29, 2015 at 6:56 PM, Mason <slash.tmp@xxxxxxx> wrote:
> Hello everyone,
>
> Consider the following program.
>
> #include <stdio.h>
> #include <stddef.h>
> struct foo1 { char s[80]; };
> struct foo2 { char s[80]; }  __attribute__ ((aligned (64)));
> struct bar1 { struct foo1 f; int i; };
> struct bar2 { struct foo2 f; int i; };
> #define P(arg) printf("sizeof(" #arg ") = %u\n", (unsigned)sizeof(arg))
>
> int main(void)
> {
>   P(struct foo1);
>   P(struct foo2);
>   P(struct bar1); printf("offset=%u\n", (unsigned)offsetof(struct bar1, i));
>   P(struct bar2); printf("offset=%u\n", (unsigned)offsetof(struct bar2, i));
>   return 0;
> }
>
> $ gcc -O3 -Wall align.c
> $ ./a.out
> sizeof(struct foo1) = 80
> sizeof(struct foo2) = 128
> sizeof(struct bar1) = 84
> offset=80
> sizeof(struct bar2) = 192
> offset=128
>
> I didn't expect sizeof(struct bar2) to be 192.
>
> NB: I first sent this message to the wrong mailing list, where
> Andreas pointed out that the "aligned" attribute effectively
> changes the *size* of bar2.
>
> In my real-life scenario, foo2 is typically used to define a
> static object of type foo2, and the padding is expected.
>
> But in some cases, we want to define a super-struct like bar2,
> where the whole struct is 64-byte aligned, not the inner struct.
>
> So how do we drop / cancel / override the alignment request
> for foo2 in the bar2 definition? This doesn't work:
>
> struct bar3 {
>   struct foo2 f __attribute__ ((aligned (4)));
>   int i;
> } __attribute__ ((aligned (64)));
>
> Do I have to duplicate the definition for struct foo2, one with
> the align attribute, one without?
>
> Regards.



-- 
Jędrzej Dudkiewicz

I really hate this damn machine, I wish that they would sell it.
It never does just what I want, but only what I tell it.





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux