Skip to content

Commit ecbf356

Browse files
pbo-linarostsquad
authored andcommitted
docs/devel/style: add a section about bitfield, and disallow them for 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]>
1 parent 8f5a4cf commit ecbf356

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/devel/style.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,26 @@ definitions instead of typedefs in headers and function prototypes; this
416416
avoids problems with duplicated typedefs and reduces the need to include
417417
headers from other headers.
418418

419+
Bitfields
420+
---------
421+
422+
C bitfields can be a cause of non-portability issues, especially under windows
423+
where `MSVC has a different way to lay them out than GCC
424+
<https://gcc.gnu.org/onlinedocs/gcc/x86-Type-Attributes.html>`_, or where
425+
endianness matters.
426+
427+
For this reason, we disallow usage of bitfields in packed structures and in any
428+
structures which are supposed to exactly match a specific layout in guest
429+
memory. Some existing code may use it, and we carefully ensured the layout was
430+
the one expected.
431+
432+
We also suggest avoiding bitfields even in structures where the exact
433+
layout does not matter, unless you can show that they provide a significant
434+
usability benefit.
435+
436+
We encourage the usage of ``include/hw/registerfields.h`` as a safe replacement
437+
for bitfields.
438+
419439
Reserved namespaces in C and POSIX
420440
----------------------------------
421441

0 commit comments

Comments
 (0)