Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce PACKED macro #57

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ static inline int ilog2(int x)
*/
#define RANGE_CHECK(x, minx, size) \
((int32_t) ((x - minx) | (minx + size - 1 - x)) >= 0)

/* Packed macro */
#if defined(__GNUC__) || defined(__clang__)
#define PACKED(name) name __attribute__((packed))
#elif defined(_MSC_VER)
#define PACKED(name) __pragma(pack(push, 1)) name __pragma(pack(pop))
#else /* unsupported compilers */
#define PACKED(name)
#endif
8 changes: 4 additions & 4 deletions virtio-blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#define PRIV(x) ((struct virtio_blk_config *) x->priv)

struct virtio_blk_config {
PACKED(struct virtio_blk_config {
uint64_t capacity;
uint32_t size_max;
uint32_t seg_max;
Expand Down Expand Up @@ -55,14 +55,14 @@ struct virtio_blk_config {
uint32_t max_write_zeroes_seg;
uint8_t write_zeroes_may_unmap;
uint8_t unused1[3];
} __attribute__((packed));
});

struct vblk_req_header {
PACKED(struct vblk_req_header {
uint32_t type;
uint32_t reserved;
uint64_t sector;
uint8_t status;
} __attribute__((packed));
});

static struct virtio_blk_config vblk_configs[VBLK_DEV_CNT_MAX];
static int vblk_dev_cnt = 0;
Expand Down
4 changes: 2 additions & 2 deletions virtio-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@

enum { VNET_QUEUE_RX = 0, VNET_QUEUE_TX = 1 };

struct virtio_net_config {
PACKED(struct virtio_net_config {
uint8_t mac[6];
uint16_t status;
uint16_t max_virtqueue_pairs;
uint16_t mtu;
} __attribute__((packed));
});

static struct virtio_net_config vnet_configs[VNET_DEV_CNT_MAX];
static int vnet_dev_cnt = 0;
Expand Down