Subject: Re: Excess use of packed attribute

From: David Miller <[email-masked]>
Date: Wed, 09 Aug 2006 21:11:36 -0700 (PDT)

From: Roland Dreier <[email-masked]>
Date: Wed, 09 Aug 2006 11:37:31 -0700

> Agreed (although not really RISC -- sparc64 and ia64 have this
> problem, while ppc is fine with unaligned access).  However
> __attribute__((packed,aligned)) has just been brought to my attention.
> For example, on sparc64,
> 
> 	struct foo { char x; int a; } __attribute__((packed,aligned));
> 	struct bar { char x; int b; } __attribute__((packed));
> 	
> 	int c(struct foo *x) { return x->a; }
> 	int d(struct bar *x) { return x->b; }
> 
> compiles to:
 ...
> which suggests that adding "aligned" is a good idea for many of the
> uses of "packed".  However I don't know how all gcc version do with
> this.  Anyone have any comments on "__attribute__((packed,aligned))"?

Good find.

It might usable in some of the questionable cases, however it changes
the size of the struct which eliminates most of the gains.

For example:

davem@sunset:~/src/GIT/net-2.6$ cat foo.c
#include <stdio.h>

struct foo_packed {
	char x; int a;
} __attribute__((packed));

struct foo_packed_aligned {
	char x; int a;
} __attribute__((packed,aligned));

int main(void)
{
	printf("foo_packed: sizeof(%Zd)\n",
	       sizeof(struct foo_packed));
	printf("foo_packed_aligned: sizeof(%Zd)\n",
	       sizeof(struct foo_packed_aligned));

	return 0;
}
davem@sunset:~/src/GIT/net-2.6$ gcc -O -o foo foo.c
davem@sunset:~/src/GIT/net-2.6$ ./foo
foo_packed: sizeof(5)
foo_packed_aligned: sizeof(8)
davem@sunset:~/src/GIT/net-2.6$ 

This doesn't work for things like packet headers, for example.

Oh well.