@@ -279,11 +279,9 @@ static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
VirtIODevice *virtio_balloon_init(DeviceState *dev)
{
- VirtIOBalloon *s;
+ VirtIOBalloon *s = qemu_mallocz(sizeof(*s));
- s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
- VIRTIO_ID_BALLOON,
- 8, sizeof(VirtIOBalloon));
+ virtio_common_init(&s->vdev, "virtio-balloon", VIRTIO_ID_BALLOON, 8);
s->vdev.get_config = virtio_balloon_get_config;
s->vdev.set_config = virtio_balloon_set_config;
@@ -478,13 +478,12 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
{
- VirtIOBlock *s;
+ VirtIOBlock *s = qemu_mallocz(sizeof(*s));
int cylinders, heads, secs;
static int virtio_blk_id;
- s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
- sizeof(struct virtio_blk_config),
- sizeof(VirtIOBlock));
+ virtio_common_init(&s->vdev, "virtio-blk", VIRTIO_ID_BLOCK,
+ sizeof(struct virtio_blk_config));
s->vdev.get_config = virtio_blk_update_config;
s->vdev.get_features = virtio_blk_get_features;
@@ -833,12 +833,11 @@ static NetClientInfo net_virtio_info = {
VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
{
- VirtIONet *n;
+ VirtIONet *n = qemu_mallocz(sizeof(*n));
static int virtio_net_id;
- n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
- sizeof(struct virtio_net_config),
- sizeof(VirtIONet));
+ virtio_common_init(&n->vdev, "virtio-net", VIRTIO_ID_NET,
+ sizeof(struct virtio_net_config))
n->vdev.get_config = virtio_net_get_config;
n->vdev.set_config = virtio_net_set_config;
@@ -560,18 +560,17 @@ void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports)
{
- VirtIOSerial *vser;
+ VirtIOSerial *vser = qemu_mallocz(sizeof(*vser));
VirtIODevice *vdev;
uint32_t i;
if (!max_nr_ports)
return NULL;
- vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
- sizeof(struct virtio_console_config),
- sizeof(VirtIOSerial));
+ vdev = &vser->vdev;
- vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
+ virtio_common_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE,
+ sizeof(struct virtio_console_config))
/* Spawn a new virtio-serial bus on which the ports will ride as devices */
vser->bus = virtser_bus_new(dev);
@@ -700,13 +700,12 @@ void virtio_cleanup(VirtIODevice *vdev)
qemu_free(vdev->vq);
}
-VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
- size_t config_size, size_t struct_size)
+void virtio_common_init(VirtIODevice *vdev, const char *name,
+ uint16_t device_id, size_t config_size)
{
- VirtIODevice *vdev;
int i;
- vdev = qemu_mallocz(struct_size);
+ memset(vdev, 0, sizeof(*vdev));
vdev->device_id = device_id;
vdev->status = 0;
@@ -149,8 +149,9 @@ int virtio_queue_empty(VirtQueue *vq);
/* Host binding interface. */
-VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
- size_t config_size, size_t struct_size);
+void virtio_common_init(VirtIODevice *dev, const char *name,
+ uint16_t device_id, size_t config_size);
+
uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
This makes virtio_common_init typesafe in a way that will also let us get rid of struct layout assumptions and of zero initialization assumptions. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> --- hw/virtio-balloon.c | 6 ++---- hw/virtio-blk.c | 7 +++---- hw/virtio-net.c | 7 +++---- hw/virtio-serial-bus.c | 9 ++++----- hw/virtio.c | 7 +++---- hw/virtio.h | 5 +++-- 6 files changed, 18 insertions(+), 23 deletions(-)