Skip to content

Commit

Permalink
docs/devel/style: add a section about bitfield, and disallow them for…
Browse files Browse the repository at this point in the history
… packed structures

Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Reviewed-by: Richard Henderson <[email protected]>
Signed-off-by: Pierrick Bouvier <[email protected]>
Tested-by: Stefan Weil <[email protected]>
Tested-by: Philippe Mathieu-Daudé <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Alex Bennée <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
pbo-linaro authored and stsquad committed Jan 17, 2025
1 parent 8f5a4cf commit ecbf356
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/devel/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,26 @@ definitions instead of typedefs in headers and function prototypes; this
avoids problems with duplicated typedefs and reduces the need to include
headers from other headers.

Bitfields
---------

C bitfields can be a cause of non-portability issues, especially under windows
where `MSVC has a different way to lay them out than GCC
<https://gcc.gnu.org/onlinedocs/gcc/x86-Type-Attributes.html>`_, or where
endianness matters.

For this reason, we disallow usage of bitfields in packed structures and in any
structures which are supposed to exactly match a specific layout in guest
memory. Some existing code may use it, and we carefully ensured the layout was
the one expected.

We also suggest avoiding bitfields even in structures where the exact
layout does not matter, unless you can show that they provide a significant
usability benefit.

We encourage the usage of ``include/hw/registerfields.h`` as a safe replacement
for bitfields.

Reserved namespaces in C and POSIX
----------------------------------

Expand Down

0 comments on commit ecbf356

Please sign in to comment.