zig-bare-metal-microbit icon indicating copy to clipboard operation
zig-bare-metal-microbit copied to clipboard

Packed structs

Open FireFox317 opened this issue 5 years ago • 6 comments

You should use packed structs for mmio instead of normal structs, since normal structs don't have a in-memory defined layout and the compiler is allowed to change the order of fields for performance or other reasons. Packed structs will always have the correct layout as specified.

FireFox317 avatar Dec 31 '19 14:12 FireFox317

Yes but I’ve found that a regular struct with just u32 fields retain the coded order. I will review this - if packed structs are not defective in those cases then I might as well use packed struct now.

ziglang/zig/issues/2713 ziglang/zig/issues/2627

markfirmware avatar Dec 31 '19 16:12 markfirmware

@FireFox317

Discovered another issue with using packed struct for mmio: https://github.com/ziglang/zig/issues/4056

markfirmware avatar Jan 03 '20 07:01 markfirmware

Zig currently retains coded order as an implementation detail, but it's almost guaranteed to change in the future. As a possible workaround, maybe use extern struct as that also guarantees consistent (C ABI implementation defined) memory layout while not having as many buggy edge cases.

fengb avatar Jan 03 '20 22:01 fengb

And regarding ziglang/zig#2627, as long as your fields in the packed struct are a multiple of u8's, it should work fine.

FireFox317 avatar Jan 04 '20 12:01 FireFox317

https://github.com/ziglang/zig/issues/3133#issuecomment-570803595

markfirmware avatar Jan 04 '20 18:01 markfirmware

@FireFox317 @fengb now using extern struct

markfirmware avatar Jan 22 '20 17:01 markfirmware