[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Packing of union fields and bool_bf
From: |
Tom Tromey |
Subject: |
Re: Packing of union fields and bool_bf |
Date: |
Sat, 13 Oct 2018 09:58:00 -0600 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1.50 (gnu/linux) |
>>>>> "Eli" == Eli Zaretskii <address@hidden> writes:
Eli> While working on bug#33017, I found out something unexpected: using
Eli> bool_bf in union members bloats the union.
...
Eli> Note the difference between handling 'unsigned int' fields and
Eli> 'bool_bf' fields: the former are nicely packed, whereas the latter are
Eli> not. Which causes the size of union to be 12 bytes, instead of the
Eli> expected 4. The 'bool' type is 1-byte wide on this platform.
Eli> Is this expected? Should we continue using bool_bf in these cases?
I think the MS ABI has special rules about packing bit fields.
>From
>https://docs.microsoft.com/en-us/cpp/c-language/padding-and-alignment-of-structure-members?view=vs-2017:
Adjacent bit fields are packed into the same 1-, 2-, or 4-byte
allocation unit if the integral types are the same size and if the next
bit field fits into the current allocation unit without crossing the
boundary imposed by the common alignment requirements of the bit fields.
So in this case, I think if the underlying type of bool_bf is "unsigned int",
then it will continue to pack as expected.
Tom