Skip to content

Commit 3713eaf

Browse files
authored
Merge pull request #57 from shengwen-tw/packed
Introduce PACKED macro
2 parents ba86045 + 4d54c73 commit 3713eaf

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ static inline int ilog2(int x)
2525
*/
2626
#define RANGE_CHECK(x, minx, size) \
2727
((int32_t) ((x - minx) | (minx + size - 1 - x)) >= 0)
28+
29+
/* Packed macro */
30+
#if defined(__GNUC__) || defined(__clang__)
31+
#define PACKED(name) name __attribute__((packed))
32+
#elif defined(_MSC_VER)
33+
#define PACKED(name) __pragma(pack(push, 1)) name __pragma(pack(pop))
34+
#else /* unsupported compilers */
35+
#define PACKED(name)
36+
#endif

virtio-blk.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

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

29-
struct virtio_blk_config {
29+
PACKED(struct virtio_blk_config {
3030
uint64_t capacity;
3131
uint32_t size_max;
3232
uint32_t seg_max;
@@ -55,14 +55,14 @@ struct virtio_blk_config {
5555
uint32_t max_write_zeroes_seg;
5656
uint8_t write_zeroes_may_unmap;
5757
uint8_t unused1[3];
58-
} __attribute__((packed));
58+
});
5959

60-
struct vblk_req_header {
60+
PACKED(struct vblk_req_header {
6161
uint32_t type;
6262
uint32_t reserved;
6363
uint64_t sector;
6464
uint8_t status;
65-
} __attribute__((packed));
65+
});
6666

6767
static struct virtio_blk_config vblk_configs[VBLK_DEV_CNT_MAX];
6868
static int vblk_dev_cnt = 0;

virtio-net.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030

3131
enum { VNET_QUEUE_RX = 0, VNET_QUEUE_TX = 1 };
3232

33-
struct virtio_net_config {
33+
PACKED(struct virtio_net_config {
3434
uint8_t mac[6];
3535
uint16_t status;
3636
uint16_t max_virtqueue_pairs;
3737
uint16_t mtu;
38-
} __attribute__((packed));
38+
});
3939

4040
static struct virtio_net_config vnet_configs[VNET_DEV_CNT_MAX];
4141
static int vnet_dev_cnt = 0;

0 commit comments

Comments
 (0)