qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC for-next 1/2] qdev: Construct VMStateDescription from


From: Andreas Färber
Subject: [Qemu-devel] [RFC for-next 1/2] qdev: Construct VMStateDescription from type hierarchy
Date: Mon, 29 Jul 2013 04:03:57 +0200

To avoid subclasses having to explicitly add their parent's state, such
as VMSTATE_PCI_DEVICE(), automatically assemble a VMStateDescription on
QOM realize. Use the most specific name and versions and prepend base
types' fields and subsections respectively.

TBD: Check if any types rely on subclasses overwriting non-NULL dc->vmsd.

Suggested-by: Michael S. Tsirkin <address@hidden>
Cc: Juan Quintela <address@hidden>
Signed-off-by: Andreas Färber <address@hidden>
---
 hw/core/qdev.c         | 97 ++++++++++++++++++++++++++++++++++++++++++++------
 include/hw/qdev-core.h |  1 +
 2 files changed, 87 insertions(+), 11 deletions(-)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 9190a7e..0430fba 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -672,13 +672,86 @@ void qdev_property_add_static(DeviceState *dev, Property 
*prop,
     assert_no_error(local_err);
 }
 
+static void device_register_vmstate(DeviceState *dev, Error **errp)
+{
+    ObjectClass *oc;
+    DeviceClass *dc;
+    const VMStateDescription *vmsd = NULL;
+    VMStateField *field;
+    const VMStateSubsection *sect;
+    int fields = 0, subsections = 0, cur_fields, cur_subsections;
+
+    /* Determine how much there is to do */
+    oc = object_get_class(OBJECT(dev));
+    do {
+        dc = DEVICE_CLASS(oc);
+        if (dc->vmsd != NULL) {
+            if (vmsd == NULL) {
+                vmsd = dc->vmsd;
+            }
+            for (field = dc->vmsd->fields; field && field->name; field++) {
+                fields++;
+            }
+            for (sect = dc->vmsd->subsections; sect && sect->vmsd; sect++) {
+                subsections++;
+            }
+        }
+        oc = object_class_get_parent(oc);
+    } while (oc != object_class_by_name(TYPE_DEVICE));
+
+    if (fields == 0 && subsections == 0) {
+        return;
+    }
+
+    /* Adopt the first vmsd */
+    dev->vmsd = g_malloc(sizeof(VMStateDescription));
+    memcpy(dev->vmsd, vmsd, sizeof(VMStateDescription));
+    if (fields > 0) {
+        dev->vmsd->fields = g_new0(VMStateField, fields + 1);
+    }
+    if (subsections > 0) {
+        dev->vmsd->subsections = g_new0(VMStateSubsection, subsections + 1);
+    }
+
+    /* Copy fields and subsections from back to front */
+    oc = object_get_class(OBJECT(dev));
+    do {
+        dc = DEVICE_CLASS(oc);
+        if (dc->vmsd != NULL) {
+            cur_fields = 0;
+            for (field = dc->vmsd->fields; field && field->name; field++) {
+                cur_fields++;
+            }
+            memcpy(dev->vmsd->fields + (fields - cur_fields),
+                   dc->vmsd->fields,
+                   cur_fields * sizeof(VMStateField));
+            fields -= cur_fields;
+
+            cur_subsections = 0;
+            for (sect = dc->vmsd->subsections; sect && sect->vmsd; sect++) {
+                cur_subsections++;
+            }
+            memcpy((VMStateSubsection *)dev->vmsd->subsections
+                   + (subsections - cur_subsections),
+                   dc->vmsd->subsections,
+                   cur_subsections * sizeof(VMStateSubsection));
+            subsections -= cur_subsections;
+        }
+        oc = object_class_get_parent(oc);
+    } while (oc != object_class_by_name(TYPE_DEVICE));
+
+    vmstate_register_with_alias_id(dev, -1, dev->vmsd, dev,
+                                   dev->instance_id_alias,
+                                   dev->alias_required_for_version);
+}
+
 static bool device_get_realized(Object *obj, Error **err)
 {
     DeviceState *dev = DEVICE(obj);
     return dev->realized;
 }
 
-static void device_set_realized(Object *obj, bool value, Error **err)
+static void device_set_realized(Object *obj, bool value, Error **errp)
 {
     DeviceState *dev = DEVICE(obj);
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
@@ -699,17 +772,18 @@ static void device_set_realized(Object *obj, bool value, 
Error **err)
             dc->realize(dev, &local_err);
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
-            vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
-                                           dev->instance_id_alias,
-                                           dev->alias_required_for_version);
+        if (local_err == NULL) {
+            device_register_vmstate(dev, errp);
         }
         if (dev->hotplugged && local_err == NULL) {
             device_reset(dev);
         }
     } else if (!value && dev->realized) {
-        if (qdev_get_vmsd(dev)) {
-            vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
+        if (dev->vmsd) {
+            vmstate_unregister(dev, dev->vmsd, dev);
+            g_free(dev->vmsd->fields);
+            g_free((void *)dev->vmsd->subsections);
+            g_free(dev->vmsd);
         }
         if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
@@ -717,7 +791,7 @@ static void device_set_realized(Object *obj, bool value, 
Error **err)
     }
 
     if (local_err != NULL) {
-        error_propagate(err, local_err);
+        error_propagate(errp, local_err);
         return;
     }
 
@@ -775,12 +849,13 @@ static void device_finalize(Object *obj)
 
 static void device_class_base_init(ObjectClass *class, void *data)
 {
-    DeviceClass *klass = DEVICE_CLASS(class);
+    DeviceClass *dc = DEVICE_CLASS(class);
 
-    /* We explicitly look up properties in the superclasses,
+    /* We explicitly look up properties and VMState in the superclasses,
      * so do not propagate them to the subclasses.
      */
-    klass->props = NULL;
+    dc->props = NULL;
+    dc->vmsd = NULL;
 }
 
 static void device_unparent(Object *obj)
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 7fbffcb..2e46fcc 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -125,6 +125,7 @@ struct DeviceState {
     int num_child_bus;
     int instance_id_alias;
     int alias_required_for_version;
+    struct VMStateDescription *vmsd;
 };
 
 #define TYPE_BUS "bus"
-- 
1.8.1.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]