diff mbox

[PATCHv2,1/5] virtio: add type safe API

Message ID 5a76f8d2d8703a95297073bf8cfa89f155915820.1268905075.git.mst@redhat.com
State New
Headers show

Commit Message

Michael S. Tsirkin March 18, 2010, 9:42 a.m. UTC
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(-)
diff mbox

Patch

diff --git a/hw/virtio-balloon.c b/hw/virtio-balloon.c
index 6d12024..f8ccc00 100644
--- a/hw/virtio-balloon.c
+++ b/hw/virtio-balloon.c
@@ -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;
diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c
index 9915840..5f61469 100644
--- a/hw/virtio-blk.c
+++ b/hw/virtio-blk.c
@@ -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;
diff --git a/hw/virtio-net.c b/hw/virtio-net.c
index be33c68..d962a75 100644
--- a/hw/virtio-net.c
+++ b/hw/virtio-net.c
@@ -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;
diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c
index 17c1ec1..cf6f979 100644
--- a/hw/virtio-serial-bus.c
+++ b/hw/virtio-serial-bus.c
@@ -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);
diff --git a/hw/virtio.c b/hw/virtio.c
index 7c020a3..2e56b39 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -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;
diff --git a/hw/virtio.h b/hw/virtio.h
index 3baa2a3..51ff421 100644
--- a/hw/virtio.h
+++ b/hw/virtio.h
@@ -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);