From patchwork Mon Jul 29 02:03:57 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andreas_F=C3=A4rber?= X-Patchwork-Id: 262641 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1B4282C0109 for ; Mon, 29 Jul 2013 12:05:36 +1000 (EST) Received: from localhost ([::1]:46446 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V3cq6-0004aX-6x for incoming@patchwork.ozlabs.org; Sun, 28 Jul 2013 22:05:34 -0400 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 Received: from relay1.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 0C3CCA52C6; Mon, 29 Jul 2013 04:04:13 +0200 (CEST) From: =?UTF-8?q?Andreas=20F=C3=A4rber?= To: qemu-devel@nongnu.org Date: Mon, 29 Jul 2013 04:03:57 +0200 Message-Id: <1375063438-3149-2-git-send-email-afaerber@suse.de> X-Mailer: git-send-email 1.8.1.4 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 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-Received-From: 195.135.220.15 Cc: =?UTF-8?q?Andreas=20F=C3=A4rber?= , Juan Quintela Subject: [Qemu-devel] [RFC for-next 1/2] qdev: Construct VMStateDescription from type hierarchy X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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ärber --- 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"