Sorry for dup, resent from the subscribed address.
On Tue, Mar 04, 2025 at 07:15:42PM -0800, Larry McVoy wrote:
On Wed, Mar 05, 2025 at 10:54:01AM +1100, Rob Pike
wrote:
The notion that the struct layout must correspond
to the hardware device's
layout is about as non-portable as thinking can get.
I agree but as a side note, I ported Lachman's (nee Convergent I believe)
TCP/IP stack to ETA-10's Sys V OS and to SCO's v7 or whatever they had.
Somewhere I have a note book where I laid out in structs every packet
format with notes about what that packet did, I was learning.
Not once did I think about packing, the structs somehow just worked on
the machines I was working on. Maybe the TCP/IP guys knew about spacing
in the structs.
I get it, I was naive, but sometimes the structs work.
All questions about type sizes and alignment, as well as the layout of
structures, are defined by platform ABI. Any useful compiler for C
would follow ABI, and any non-C compiler hoping to interoperate with
the system libraries, would provide some way to communicate using the ABI.
Then, after some time, but long time ago, all Unix-like systems converged
to just two practically observable ABIs: ILP32 and LP64. Both of them
require 8bit bytes, byte addressability, natural alignment for the basic
integer types, and equiality of uint8_t/unsigned char, uint16_t/short,
uint32_t/long.
From there, it is easy to see that e.g. layout of most ip protocol packets
can be trivially written as C structs. The only trouble there is the
need to align whole incoming packets on suitable boundary, but e.g. for
IP/TCP/UDP the 4-bytes is enough, and most hardware enforces it.