zig-bare-metal-microbit
zig-bare-metal-microbit copied to clipboard
Packed structs
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.
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
@FireFox317
Discovered another issue with using packed struct for mmio: https://github.com/ziglang/zig/issues/4056
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.
And regarding ziglang/zig#2627, as long as your fields in the packed struct are a multiple of u8's, it should work fine.
https://github.com/ziglang/zig/issues/3133#issuecomment-570803595
@FireFox317 @fengb now using extern struct