#pragma pack(L1_CACHE_LINE)
struct A {
//...
};
#pragma pack()
A a;
and
struct A {
//...
};
A a __attritube__((aligned(L1_CACHE_LINE)))
What's difference between them?
The #pragma pack(byte-alignment) effect each member of the struct as specified by the byte-alignment input, or on their natural alignment boundary, whichever is less.
The __attribute__((aligned(byte-alignment)))
affect the minimum alignment of the variable (or struct field if specified within the struct)
I believe the following are equivalent
#define L1_CACHE_LINE 2
struct A
{
u_int32_t a __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int32_t b __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int16_t c __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int16_t d __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int32_t e __attribute__ ( (aligned(L1_CACHE_LINE)) );
};
#pragma pack(L1_CACHE_LINE)
struct A
{
u_int32_t a;
u_int32_t b;
u_int16_t c;
u_int16_t d;
u_int32_t e;
};
#pragma pack()
where is A a __attritube__((aligned(L1_CACHE_LINE)))
will insure u_int32_t a
inside struct A
will aligned with 2 byte but will not align the other variable in the same manner.
Reference: