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 bar
struct vastai_bar1 {
char id; // bar id
uint32_t : 8; // reserved
uint64_t size; // base address of ddr
};
PACKED_STRUCT(struct vastai_bar2 {
char id; // bar id
uint32_t : 8; // reserved
uint64_t size; // base address of ddr
});
typedef PACKED_STRUCT(struct vastai_bar3 {
char id; // bar id
uint32_t : 8; // reserved
uint64_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:16
size of bar1:16
bar1: id=1, size=1
size of struct vastai_bar2:10
size of bar2:10
bar2: id=2, size=2
size of struct vastai_bar3:10
size of bar3:10
bar3: id=3, size=3
windows vs2022运行结果:
size of struct vastai_bar1:16
size of bar1:16
bar1: id=1, size=1
size of struct vastai_bar2:13
size of bar2:13
bar2: id=2, size=2
size of struct vastai_bar3:13
size of bar3:13
bar3: 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 bar
struct vastai_bar1 {
char id; // bar id
uint32_t : 16; // reserved
uint64_t size; // base address of ddr
};
PACKED_STRUCT(struct vastai_bar2 {
char id; // bar id
uint32_t : 16; // reserved
uint64_t size; // base address of ddr
});
typedef PACKED_STRUCT(struct vastai_bar3 {
char id; // bar id
uint32_t : 16; // reserved
uint64_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:16
size of bar1:16
bar1: id=1, size=1
size of struct vastai_bar2:11
size of bar2:11
bar2: id=2, size=2
size of struct vastai_bar3:11
size of bar3:11
bar3: id=3, size=3
windows vs2022运行结果:
size of struct vastai_bar1:16
size of bar1:16
bar1: id=1, size=1
size of struct vastai_bar2:13
size of bar2:13
bar2: id=2, size=2
size of struct vastai_bar3:13
size of bar3:13
bar3: id=3, size=3
3
typedef PACKED_STRUCT(struct process {
// host pid
uint32_t pid : 32;
// container pid, pid==vpid means host, else means container
uint32_t vpid : 32;
// namespace, we use ns to check whether 2 processes are in the same container
uint64_t ns : 64;
// level, 0 means host, else means container
uint32_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 release
uint32_t state : 2;
// VACC_RELEASE_PROCESS
// 0 normal release
// 1 warn release, not all results were taken
// 2 error release, not getting enough task results
uint32_t release : 2;
uint32_t : 28;
uint64_t : 64;
}) process_t;
该结构体在linux,windows的大小都是32, 因为它已经对齐了。