ref: e82f565015fd0b75085d8d2a4f1aaca53e875f64
dir: /tests/testpkdstruct.c/
#include <u.h> #include <libc.h> typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; typedef unsigned long long u64; typedef struct Spanid Spanid; typedef struct Tag Tag; /* this program shows inconsistent behaviour when returning packed structs. Everything works fine if the Tag is defined with this definition. Bottom line: Cannot count on return values of packed structures. struct Tag { u8 type; u64 path; }; */ #pragma pack on struct Tag { u64 path; /* Qid.path, unique identifier */ u8 type; /* Tmagic, Tdentry, Tdata, Tindn */ }; struct Spanid /* Spanid identifier */ { u16 len; /* number of blocks */ u64 blkno; /* starting block number */ Tag; // Tag1; }; #pragma pack off Spanid t[10] = {0}; void save(void) { t[0].blkno = 888888; t[0].len = 9; t[0].type = 9; t[0].path = 789789; } Spanid get(int i) { print("get t[i] blkno %llud len %d" " type %d path %llud\n", t[i].blkno, t[i].len, t[i].type, t[i].path); return t[i]; } void put(Spanid s) { print("put s blkno %llud len %d\n", s.blkno, s.len); t[1].blkno = s.blkno; t[1].len = s.len; } void main(int, void**) { Spanid s; save(); s = get(0); print(" blkno %llud len %d" " type %d path %llud\n", s.blkno, s.len, s.type, s.path); s.blkno = 77777; s.len = 8; put(s); s.len = 10; s = get(1); print(" blkno %llud len %d\n", s.blkno, s.len); exits(0); }