1

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #ifdef __GNUC__
  4. #define PACKED_STRUCT(__Declaration__) \
  5. __Declaration__ __attribute__((__packed__))
  6. #endif
  7. #ifdef _MSC_VER
  8. #define PACKED_STRUCT(__Declaration__) \
  9. __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
  10. #endif
  11. // structure of bar
  12. struct vastai_bar1 {
  13. char id; // bar id
  14. uint32_t : 8; // reserved
  15. uint64_t size; // base address of ddr
  16. };
  17. PACKED_STRUCT(struct vastai_bar2 {
  18. char id; // bar id
  19. uint32_t : 8; // reserved
  20. uint64_t size; // base address of ddr
  21. });
  22. typedef PACKED_STRUCT(struct vastai_bar3 {
  23. char id; // bar id
  24. uint32_t : 8; // reserved
  25. uint64_t size; // base address of ddr
  26. }) vastai_bar3_t;
  27. int main(void)
  28. {
  29. struct vastai_bar1 bar1 = { .id = 1, .size = 1 };
  30. printf("size of struct vastai_bar1:%zu\n", sizeof(struct vastai_bar1));
  31. printf("size of bar1:%zu\n", sizeof(bar1));
  32. printf("bar1: id=%d, size=%llu\n\n", bar1.id, bar1.size);
  33. struct vastai_bar2 bar2 = { .id = 2, .size = 2 };
  34. printf("size of struct vastai_bar2:%zu\n", sizeof(struct vastai_bar2));
  35. printf("size of bar2:%zu\n", sizeof(bar2));
  36. printf("bar2: id=%d, size=%llu\n\n", bar2.id, bar2.size);
  37. struct vastai_bar3 bar3 = { .id = 3, .size = 3 };
  38. printf("size of struct vastai_bar3:%zu\n", sizeof(struct vastai_bar3));
  39. printf("size of bar3:%zu\n", sizeof(bar3));
  40. printf("bar3: id=%d, size=%llu\n\n", bar3.id, bar3.size);
  41. }

linux gcc运行结果

  1. size of struct vastai_bar1:16
  2. size of bar1:16
  3. bar1: id=1, size=1
  4. size of struct vastai_bar2:10
  5. size of bar2:10
  6. bar2: id=2, size=2
  7. size of struct vastai_bar3:10
  8. size of bar3:10
  9. bar3: id=3, size=3

windows vs2022运行结果:

  1. size of struct vastai_bar1:16
  2. size of bar1:16
  3. bar1: id=1, size=1
  4. size of struct vastai_bar2:13
  5. size of bar2:13
  6. bar2: id=2, size=2
  7. size of struct vastai_bar3:13
  8. size of bar3:13
  9. bar3: id=3, size=3

2

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #ifdef __GNUC__
  4. #define PACKED_STRUCT(__Declaration__) \
  5. __Declaration__ __attribute__((__packed__))
  6. #endif
  7. #ifdef _MSC_VER
  8. #define PACKED_STRUCT(__Declaration__) \
  9. __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
  10. #endif
  11. // structure of bar
  12. struct vastai_bar1 {
  13. char id; // bar id
  14. uint32_t : 16; // reserved
  15. uint64_t size; // base address of ddr
  16. };
  17. PACKED_STRUCT(struct vastai_bar2 {
  18. char id; // bar id
  19. uint32_t : 16; // reserved
  20. uint64_t size; // base address of ddr
  21. });
  22. typedef PACKED_STRUCT(struct vastai_bar3 {
  23. char id; // bar id
  24. uint32_t : 16; // reserved
  25. uint64_t size; // base address of ddr
  26. }) vastai_bar3_t;
  27. int main(void)
  28. {
  29. struct vastai_bar1 bar1 = { .id = 1, .size = 1 };
  30. printf("size of struct vastai_bar1:%zu\n", sizeof(struct vastai_bar1));
  31. printf("size of bar1:%zu\n", sizeof(bar1));
  32. printf("bar1: id=%d, size=%llu\n\n", bar1.id, bar1.size);
  33. struct vastai_bar2 bar2 = { .id = 2, .size = 2 };
  34. printf("size of struct vastai_bar2:%zu\n", sizeof(struct vastai_bar2));
  35. printf("size of bar2:%zu\n", sizeof(bar2));
  36. printf("bar2: id=%d, size=%llu\n\n", bar2.id, bar2.size);
  37. struct vastai_bar3 bar3 = { .id = 3, .size = 3 };
  38. printf("size of struct vastai_bar3:%zu\n", sizeof(struct vastai_bar3));
  39. printf("size of bar3:%zu\n", sizeof(bar3));
  40. printf("bar3: id=%d, size=%llu\n\n", bar3.id, bar3.size);
  41. }

linux gcc运行结果

  1. size of struct vastai_bar1:16
  2. size of bar1:16
  3. bar1: id=1, size=1
  4. size of struct vastai_bar2:11
  5. size of bar2:11
  6. bar2: id=2, size=2
  7. size of struct vastai_bar3:11
  8. size of bar3:11
  9. bar3: id=3, size=3

windows vs2022运行结果:

  1. size of struct vastai_bar1:16
  2. size of bar1:16
  3. bar1: id=1, size=1
  4. size of struct vastai_bar2:13
  5. size of bar2:13
  6. bar2: id=2, size=2
  7. size of struct vastai_bar3:13
  8. size of bar3:13
  9. bar3: id=3, size=3

3

  1. typedef PACKED_STRUCT(struct process {
  2. // host pid
  3. uint32_t pid : 32;
  4. // container pid, pid==vpid means host, else means container
  5. uint32_t vpid : 32;
  6. // namespace, we use ns to check whether 2 processes are in the same container
  7. uint64_t ns : 64;
  8. // level, 0 means host, else means container
  9. uint32_t level : 32;
  10. // VACC_GET_PROCESS
  11. // 0 process exit, 1 process not exit
  12. // VACC_RELEASE_PROCESS
  13. // 0 process not used, 1 process used not to release
  14. uint32_t state : 2;
  15. // VACC_RELEASE_PROCESS
  16. // 0 normal release
  17. // 1 warn release, not all results were taken
  18. // 2 error release, not getting enough task results
  19. uint32_t release : 2;
  20. uint32_t : 28;
  21. uint64_t : 64;
  22. }) process_t;

该结构体在linux,windows的大小都是32, 因为它已经对齐了。