1
#include <stdio.h>#include <stdint.h>#ifdef __GNUC__#define PACKED_STRUCT(__Declaration__) \__Declaration__ __attribute__((__packed__))#endif#ifdef _MSC_VER#define PACKED_STRUCT(__Declaration__) \__pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))#endif// structure of barstruct vastai_bar1 {char id; // bar iduint32_t : 8; // reserveduint64_t size; // base address of ddr};PACKED_STRUCT(struct vastai_bar2 {char id; // bar iduint32_t : 8; // reserveduint64_t size; // base address of ddr});typedef PACKED_STRUCT(struct vastai_bar3 {char id; // bar iduint32_t : 8; // reserveduint64_t size; // base address of ddr}) vastai_bar3_t;int main(void){struct vastai_bar1 bar1 = { .id = 1, .size = 1 };printf("size of struct vastai_bar1:%zu\n", sizeof(struct vastai_bar1));printf("size of bar1:%zu\n", sizeof(bar1));printf("bar1: id=%d, size=%llu\n\n", bar1.id, bar1.size);struct vastai_bar2 bar2 = { .id = 2, .size = 2 };printf("size of struct vastai_bar2:%zu\n", sizeof(struct vastai_bar2));printf("size of bar2:%zu\n", sizeof(bar2));printf("bar2: id=%d, size=%llu\n\n", bar2.id, bar2.size);struct vastai_bar3 bar3 = { .id = 3, .size = 3 };printf("size of struct vastai_bar3:%zu\n", sizeof(struct vastai_bar3));printf("size of bar3:%zu\n", sizeof(bar3));printf("bar3: id=%d, size=%llu\n\n", bar3.id, bar3.size);}
linux gcc运行结果
size of struct vastai_bar1:16size of bar1:16bar1: id=1, size=1size of struct vastai_bar2:10size of bar2:10bar2: id=2, size=2size of struct vastai_bar3:10size of bar3:10bar3: id=3, size=3
windows vs2022运行结果:
size of struct vastai_bar1:16size of bar1:16bar1: id=1, size=1size of struct vastai_bar2:13size of bar2:13bar2: id=2, size=2size of struct vastai_bar3:13size of bar3:13bar3: id=3, size=3
2
#include <stdio.h>#include <stdint.h>#ifdef __GNUC__#define PACKED_STRUCT(__Declaration__) \__Declaration__ __attribute__((__packed__))#endif#ifdef _MSC_VER#define PACKED_STRUCT(__Declaration__) \__pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))#endif// structure of barstruct vastai_bar1 {char id; // bar iduint32_t : 16; // reserveduint64_t size; // base address of ddr};PACKED_STRUCT(struct vastai_bar2 {char id; // bar iduint32_t : 16; // reserveduint64_t size; // base address of ddr});typedef PACKED_STRUCT(struct vastai_bar3 {char id; // bar iduint32_t : 16; // reserveduint64_t size; // base address of ddr}) vastai_bar3_t;int main(void){struct vastai_bar1 bar1 = { .id = 1, .size = 1 };printf("size of struct vastai_bar1:%zu\n", sizeof(struct vastai_bar1));printf("size of bar1:%zu\n", sizeof(bar1));printf("bar1: id=%d, size=%llu\n\n", bar1.id, bar1.size);struct vastai_bar2 bar2 = { .id = 2, .size = 2 };printf("size of struct vastai_bar2:%zu\n", sizeof(struct vastai_bar2));printf("size of bar2:%zu\n", sizeof(bar2));printf("bar2: id=%d, size=%llu\n\n", bar2.id, bar2.size);struct vastai_bar3 bar3 = { .id = 3, .size = 3 };printf("size of struct vastai_bar3:%zu\n", sizeof(struct vastai_bar3));printf("size of bar3:%zu\n", sizeof(bar3));printf("bar3: id=%d, size=%llu\n\n", bar3.id, bar3.size);}
linux gcc运行结果
size of struct vastai_bar1:16size of bar1:16bar1: id=1, size=1size of struct vastai_bar2:11size of bar2:11bar2: id=2, size=2size of struct vastai_bar3:11size of bar3:11bar3: id=3, size=3
windows vs2022运行结果:
size of struct vastai_bar1:16size of bar1:16bar1: id=1, size=1size of struct vastai_bar2:13size of bar2:13bar2: id=2, size=2size of struct vastai_bar3:13size of bar3:13bar3: id=3, size=3
3
typedef PACKED_STRUCT(struct process {// host piduint32_t pid : 32;// container pid, pid==vpid means host, else means containeruint32_t vpid : 32;// namespace, we use ns to check whether 2 processes are in the same containeruint64_t ns : 64;// level, 0 means host, else means containeruint32_t level : 32;// VACC_GET_PROCESS// 0 process exit, 1 process not exit// VACC_RELEASE_PROCESS// 0 process not used, 1 process used not to releaseuint32_t state : 2;// VACC_RELEASE_PROCESS// 0 normal release// 1 warn release, not all results were taken// 2 error release, not getting enough task resultsuint32_t release : 2;uint32_t : 28;uint64_t : 64;}) process_t;
该结构体在linux,windows的大小都是32, 因为它已经对齐了。
