alignment and packing of struct types (was Re: [PATCH 2/3] Import fw-ohci driver.)

Stefan Richter
2006-12-10T23:01:06Z

I wrote:
> Kristian Høgsberg wrote:
>> Stefan Richter wrote:
>>> Kristian Høgsberg wrote:
>>>> Yup, I've done away with the bitfields and switched to a mix of __le16
>>>> and __le32 struct fields.
>>> I suppose the struct should get __attribute__((packed)) then.
>> I guess it wouldn't harm, but is it really necessary? Would gcc ever
>> insert padding here, all the 32 bit fields a 32 bit aligned, and so are
>> the 16 bit fields.
>
> Is 2-byte alignment of 16bit struct members guaranteed on all platforms?
>
> A related question:
> If I specify a struct which, among else, contains 32bit quantities, then
> any variable of this struct type is supposed to be at least 4-byte-aligned.
> No if I specifiy this struct as packed, will variables of this type still
> be aligned on 4 byte boundaries or will the compiler assume no alignment?
> In other words, should it be __attribute__((packed,aligned(4))) then?
> I'm speaking about situations where I not only wish to avoid unnecessarily
> bad machine code due to unaligned access but where the device requires
> 4-byte alignment too.

After sending this, I realized I should have changed the subject. :-)
--
Stefan Richter
-=====-=-==- ==-- -=-=-