3 ms·
What if your struct is actually a memory-mapped I/O device? Far from optimizing you need to give a hard guarantee that it will NEVER be optimized by the compile
by gaius 10y ago
What if your struct is actually a memory-mapped I/O device? Far from optimizing you need to give a hard guarantee that it will NEVER be optimized by the compiler.
- kazinator 10y agoSuppose C didn't require order of struct members. How could we support I/O devices? Simply by wrapping them behind functions or macros which take a base address and access a specific register relative to that address using pointer arithmetic and dereferencing. You see this being done anyway! if ((SERIAL_STATUS_PORT(port_addr) & SERIAL_DTR) != 0) { /* DTR line is asserted */ }
- gaius 10y agoMore likely you'd have to drop down into ASM for those bits... You see in a lot of high performance networking code too, you just get a block of bytes off the wire, cast it to a struct and use it straight away. High risk, high reward :-)
- Grishnakh 10y agoWell if we were making an all-new C-like language, we could just add a compiler directive which prevents optimization for cases like this. For all I know, other languages may do just this (there's a comment farther down about Rust which appears to say this).