From: "Andreas Färber" <afaerber@suse.de>
To: qemu-devel@nongnu.org
Cc: "Andreas Färber" <afaerber@suse.de>,
"Juan Quintela" <quintela@redhat.com>
Subject: [Qemu-devel] [RFC for-next 1/2] qdev: Construct VMStateDescription from type hierarchy
Date: Mon, 29 Jul 2013 04:03:57 +0200 [thread overview]
Message-ID: <1375063438-3149-2-git-send-email-afaerber@suse.de> (raw)
In-Reply-To: <1375063438-3149-1-git-send-email-afaerber@suse.de>
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 <mst@redhat.com>
Cc: Juan Quintela <quintela@redhat.com>
Signed-off-by: Andreas Färber <afaerber@suse.de>
---
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
next prev parent reply other threads:[~2013-07-29 2:04 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-29 2:03 [Qemu-devel] [RFC for-next 0/2] QOM VMStateDescription remix Andreas Färber
2013-07-29 2:03 ` Andreas Färber [this message]
2013-07-29 2:03 ` [Qemu-devel] [RFC for-next 2/2] cpu: Move VMSTATE_CPU() into TYPE_CPU VMStateDescription Andreas Färber
2013-07-29 4:30 ` Jia Liu
2013-09-02 11:28 ` [Qemu-devel] [RFC for-next 0/2] QOM VMStateDescription remix Andreas Färber
2013-09-02 11:41 ` Michael S. Tsirkin
2014-02-07 21:28 ` Andreas Färber
2014-02-07 21:39 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1375063438-3149-2-git-send-email-afaerber@suse.de \
--to=afaerber@suse.de \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).