From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60859) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V3cou-0003hy-HU for qemu-devel@nongnu.org; Sun, 28 Jul 2013 22:04:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V3con-0005pA-Oe for qemu-devel@nongnu.org; Sun, 28 Jul 2013 22:04:20 -0400 Received: from cantor2.suse.de ([195.135.220.15]:46325 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V3con-0005ow-G6 for qemu-devel@nongnu.org; Sun, 28 Jul 2013 22:04:13 -0400 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Mon, 29 Jul 2013 04:03:57 +0200 Message-Id: <1375063438-3149-2-git-send-email-afaerber@suse.de> In-Reply-To: <1375063438-3149-1-git-send-email-afaerber@suse.de> References: <1375063438-3149-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [RFC for-next 1/2] qdev: Construct VMStateDescription from type hierarchy List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Andreas=20F=C3=A4rber?= , Juan Quintela 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 Cc: Juan Quintela Signed-off-by: Andreas F=C3=A4rber --- 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, Pro= perty *prop, assert_no_error(local_err); } =20 +static void device_register_vmstate(DeviceState *dev, Error **errp) +{ + ObjectClass *oc; + DeviceClass *dc; + const VMStateDescription *vmsd =3D NULL; + VMStateField *field; + const VMStateSubsection *sect; + int fields =3D 0, subsections =3D 0, cur_fields, cur_subsections; + + /* Determine how much there is to do */ + oc =3D object_get_class(OBJECT(dev)); + do { + dc =3D DEVICE_CLASS(oc); + if (dc->vmsd !=3D NULL) { + if (vmsd =3D=3D NULL) { + vmsd =3D dc->vmsd; + } + for (field =3D dc->vmsd->fields; field && field->name; field= ++) { + fields++; + } + for (sect =3D dc->vmsd->subsections; sect && sect->vmsd; sec= t++) { + subsections++; + } + } + oc =3D object_class_get_parent(oc); + } while (oc !=3D object_class_by_name(TYPE_DEVICE)); + + if (fields =3D=3D 0 && subsections =3D=3D 0) { + return; + } + + /* Adopt the first vmsd */ + dev->vmsd =3D g_malloc(sizeof(VMStateDescription)); + memcpy(dev->vmsd, vmsd, sizeof(VMStateDescription)); + if (fields > 0) { + dev->vmsd->fields =3D g_new0(VMStateField, fields + 1); + } + if (subsections > 0) { + dev->vmsd->subsections =3D g_new0(VMStateSubsection, subsections= + 1); + } + + /* Copy fields and subsections from back to front */ + oc =3D object_get_class(OBJECT(dev)); + do { + dc =3D DEVICE_CLASS(oc); + if (dc->vmsd !=3D NULL) { + cur_fields =3D 0; + for (field =3D dc->vmsd->fields; field && field->name; field= ++) { + cur_fields++; + } + memcpy(dev->vmsd->fields + (fields - cur_fields), + dc->vmsd->fields, + cur_fields * sizeof(VMStateField)); + fields -=3D cur_fields; + + cur_subsections =3D 0; + for (sect =3D dc->vmsd->subsections; sect && sect->vmsd; sec= t++) { + cur_subsections++; + } + memcpy((VMStateSubsection *)dev->vmsd->subsections + + (subsections - cur_subsections), + dc->vmsd->subsections, + cur_subsections * sizeof(VMStateSubsection)); + subsections -=3D cur_subsections; + } + oc =3D object_class_get_parent(oc); + } while (oc !=3D 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 =3D DEVICE(obj); return dev->realized; } =20 -static void device_set_realized(Object *obj, bool value, Error **err) +static void device_set_realized(Object *obj, bool value, Error **errp) { DeviceState *dev =3D DEVICE(obj); DeviceClass *dc =3D DEVICE_GET_CLASS(dev); @@ -699,17 +772,18 @@ static void device_set_realized(Object *obj, bool v= alue, Error **err) dc->realize(dev, &local_err); } =20 - if (qdev_get_vmsd(dev) && local_err =3D=3D NULL) { - vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), = dev, - dev->instance_id_alias, - dev->alias_required_for_versi= on); + if (local_err =3D=3D NULL) { + device_register_vmstate(dev, errp); } if (dev->hotplugged && local_err =3D=3D 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 val= ue, Error **err) } =20 if (local_err !=3D NULL) { - error_propagate(err, local_err); + error_propagate(errp, local_err); return; } =20 @@ -775,12 +849,13 @@ static void device_finalize(Object *obj) =20 static void device_class_base_init(ObjectClass *class, void *data) { - DeviceClass *klass =3D DEVICE_CLASS(class); + DeviceClass *dc =3D DEVICE_CLASS(class); =20 - /* 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 =3D NULL; + dc->props =3D NULL; + dc->vmsd =3D NULL; } =20 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; }; =20 #define TYPE_BUS "bus" --=20 1.8.1.4