Subject: Re: CVS commit: src/sys/dev/scsipi
To: None <source-changes@NetBSD.org>
From: Christian Biere <christianbiere@gmx.de>
List: source-changes
Date: 12/01/2006 21:54:42
khym@azeotrope.org wrote:
> On Fri, Dec 01, 2006 at 08:36:23PM +0100, Christian Biere wrote:
> > I suggest using STATIC_ASSERT(sizeof(struct whatever) == whatever) wherever
> > size matters.
> 
> Interesting... I didn't think that the aligned attribute would change
> the sizeof the struct, but it does :( The gcc documentation (info gcc)
> doesn't seem to make any mention of that (although I suppose if you set
> the attribute on a structure member, it'd obviously have to increase
> the sizeof the struct. I didn't think it'd do it when aligning the
> struct itself.)
> 
> So how does one ensure that the start of the struct is aligned to a
> 32-bit boundary, but the sizeof the struct stays unchanged?
> 
> In an off-list email, Martin Husemann had suggested using a union to
> force the appropriate alignment, but it looks like that doesn't work:
> 
> #include <stdio.h>
> 
> struct odd {
>         char x[3];
> } __attribute__((__packed__));
> 
> struct evenunion {
>         union {
>                 int i_x;
>                 char x[4];
>         } u_x;
>         char y[4];
> } __attribute__((__packed__));

I think the __packed__ will just override what you're trying to achieve with
union.

I believe you have no choice to apply it to the variable instead of the type:

         struct odd  __attribute__(__align__(4));

or
	union {
		uint32_t align4;
         	struct evenunion e;
	} u;

Maybe GCC is smart enough to prove alignment statically in which case one could
add static assertions to prevent unintended breaches in the future.

-- 
Christian