Skip to content

Commit

Permalink
Declare read-only pointers as const
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Jan 23, 2024
1 parent 78ec6f7 commit 35517a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
9 changes: 5 additions & 4 deletions virtio-blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static void virtio_blk_write_handler(virtio_blk_state_t *vblk,
uint32_t len)
{
void *dest = (void *) ((uintptr_t) vblk->disk + sector * DISK_BLK_SIZE);
void *src = (void *) ((uintptr_t) vblk->ram + desc_addr);
const void *src = (void *) ((uintptr_t) vblk->ram + desc_addr);
memcpy(dest, src, len);
}

Expand All @@ -116,7 +116,8 @@ static void virtio_blk_read_handler(virtio_blk_state_t *vblk,
uint32_t len)
{
void *dest = (void *) ((uintptr_t) vblk->ram + desc_addr);
void *src = (void *) ((uintptr_t) vblk->disk + sector * DISK_BLK_SIZE);
const void *src =
(void *) ((uintptr_t) vblk->disk + sector * DISK_BLK_SIZE);
memcpy(dest, src, len);
}

Expand All @@ -140,7 +141,7 @@ static int virtio_blk_desc_handler(virtio_blk_state_t *vblk,
/* Collect the descriptors */
for (int i = 0; i < 3; i++) {
/* The size of the `struct virtq_desc` is 4 words */
uint32_t *desc = &vblk->ram[queue->QueueDesc + desc_idx * 4];
const uint32_t *desc = &vblk->ram[queue->QueueDesc + desc_idx * 4];

/* Retrieve the fields of current descriptor */
vq_desc[i].addr = desc[0];
Expand All @@ -162,7 +163,7 @@ static int virtio_blk_desc_handler(virtio_blk_state_t *vblk,
}

/* Process the header */
struct vblk_req_header *header =
const struct vblk_req_header *header =
(struct vblk_req_header *) ((uintptr_t) vblk->ram + vq_desc[0].addr);
uint32_t type = header->type;
uint64_t sector = header->sector;
Expand Down
18 changes: 9 additions & 9 deletions virtio-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ static bool vnet_iovec_read(struct iovec **vecs,
/* Require existing 'desc_idx' to use as iteration variable, and input
* 'buffer_idx'.
*/
#define VNET_ITERATE_BUFFER(checked, body) \
desc_idx = buffer_idx; \
while (1) { \
if (checked && desc_idx >= queue->QueueNum) \
return virtio_net_set_fail(vnet); \
uint32_t *desc = &ram[queue->QueueDesc + desc_idx * 4]; \
uint16_t desc_flags = desc[3]; \
body if (!(desc_flags & VIRTIO_DESC_F_NEXT)) break; \
desc_idx = desc[3] >> 16; \
#define VNET_ITERATE_BUFFER(checked, body) \
desc_idx = buffer_idx; \
while (1) { \
if (checked && desc_idx >= queue->QueueNum) \
return virtio_net_set_fail(vnet); \
const uint32_t *desc = &ram[queue->QueueDesc + desc_idx * 4]; \
uint16_t desc_flags = desc[3]; \
body if (!(desc_flags & VIRTIO_DESC_F_NEXT)) break; \
desc_idx = desc[3] >> 16; \
}

/* Input: 'buffer_idx'.
Expand Down

0 comments on commit 35517a0

Please sign in to comment.