From patchwork Tue Mar 25 20:17:21 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Dr. David Alan Gilbert" X-Patchwork-Id: 333704 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)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F6F91400A2 for ; Wed, 26 Mar 2014 07:39:44 +1100 (EST) Received: from localhost ([::1]:44059 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSXs4-0006L5-AD for incoming@patchwork.ozlabs.org; Tue, 25 Mar 2014 16:22:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46487) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSXoM-000407-W8 for qemu-devel@nongnu.org; Tue, 25 Mar 2014 16:19:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WSXoG-0008Tf-L5 for qemu-devel@nongnu.org; Tue, 25 Mar 2014 16:19:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57924) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSXoG-0008TT-2w for qemu-devel@nongnu.org; Tue, 25 Mar 2014 16:18:56 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2PKHqgG018687 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 25 Mar 2014 16:17:52 -0400 Received: from dgilbert-t530.home.treblig.org (vpn1-7-106.ams2.redhat.com [10.36.7.106]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s2PKHSSe029837; Tue, 25 Mar 2014 16:17:50 -0400 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org Date: Tue, 25 Mar 2014 20:17:21 +0000 Message-Id: <1395778647-30925-11-git-send-email-dgilbert@redhat.com> In-Reply-To: <1395778647-30925-1-git-send-email-dgilbert@redhat.com> References: <1395778647-30925-1-git-send-email-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: stefanb@linux.vnet.ibm.com, quintela@redhat.com, mdroth@linux.vnet.ibm.com, agraf@suse.de, mst@redhat.com, aliguori@amazon.com, afaerber@suse.de Subject: [Qemu-devel] [RFC PATCH 10/16] Visitor: Common types to use visitors 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 From: "Dr. David Alan Gilbert" Rework the common types in vmstate.c to use .visit rather than .get/.put and now call the correct visitor function. Signed-off-by: Dr. David Alan Gilbert --- vmstate.c | 376 ++++++++++++-------------------------------------------------- 1 file changed, 73 insertions(+), 303 deletions(-) diff --git a/vmstate.c b/vmstate.c index af332b7..5a90cbe 100644 --- a/vmstate.c +++ b/vmstate.c @@ -1,3 +1,5 @@ +#include + #include "qemu-common.h" #include "migration/migration.h" #include "migration/qemu-file.h" @@ -357,320 +359,88 @@ static void vmstate_subsection_save(Visitor *v, const VMStateDescription *vmsd, LOCAL_ERR_REPORT(return;); } -/* bool */ - -static int get_bool(QEMUFile *f, void *pv, size_t size) -{ - bool *v = pv; - *v = qemu_get_byte(f); - return 0; -} - -static void put_bool(QEMUFile *f, void *pv, size_t size) -{ - bool *v = pv; - qemu_put_byte(f, *v); -} - -const VMStateInfo vmstate_info_bool = { - .name = "bool", - .get = get_bool, - .put = put_bool, -}; - -/* 8 bit int */ - -static int get_int8(QEMUFile *f, void *pv, size_t size) -{ - int8_t *v = pv; - qemu_get_s8s(f, v); - return 0; -} - -static void put_int8(QEMUFile *f, void *pv, size_t size) -{ - int8_t *v = pv; - qemu_put_s8s(f, v); -} - -const VMStateInfo vmstate_info_int8 = { - .name = "int8", - .get = get_int8, - .put = put_int8, -}; - -/* 16 bit int */ - -static int get_int16(QEMUFile *f, void *pv, size_t size) -{ - int16_t *v = pv; - qemu_get_sbe16s(f, v); - return 0; -} - -static void put_int16(QEMUFile *f, void *pv, size_t size) -{ - int16_t *v = pv; - qemu_put_sbe16s(f, v); -} - -const VMStateInfo vmstate_info_int16 = { - .name = "int16", - .get = get_int16, - .put = put_int16, -}; - -/* 32 bit int */ - -static int get_int32(QEMUFile *f, void *pv, size_t size) -{ - int32_t *v = pv; - qemu_get_sbe32s(f, v); - return 0; -} - -static void put_int32(QEMUFile *f, void *pv, size_t size) -{ - int32_t *v = pv; - qemu_put_sbe32s(f, v); -} - -const VMStateInfo vmstate_info_int32 = { - .name = "int32", - .get = get_int32, - .put = put_int32, -}; - -/* 32 bit int. See that the received value is the same than the one - in the field */ - -static int get_int32_equal(QEMUFile *f, void *pv, size_t size) -{ - int32_t *v = pv; - int32_t v2; - qemu_get_sbe32s(f, &v2); - - if (*v == v2) { - return 0; - } - return -EINVAL; -} - -const VMStateInfo vmstate_info_int32_equal = { - .name = "int32 equal", - .get = get_int32_equal, - .put = put_int32, -}; +#define VMSTATE_INFO_FORTYPE(vn, vt) \ + static int visit_ ## vn(Visitor *v, void *pdata, const char *name, \ + size_t size, Error **errp) \ + { \ + visit_type_ ## vn(v, (vt *)pdata, name, errp); \ + return 0; \ + } \ + const VMStateInfo vmstate_info_ ## vn = { \ + .name = #vn , \ + .visit = visit_ ## vn, \ +} + +VMSTATE_INFO_FORTYPE(bool, bool); +VMSTATE_INFO_FORTYPE(int8, int8_t); +VMSTATE_INFO_FORTYPE(int16, int16_t); +VMSTATE_INFO_FORTYPE(int32, int32_t); +VMSTATE_INFO_FORTYPE(int64, int64_t); +VMSTATE_INFO_FORTYPE(uint8, uint8_t); +VMSTATE_INFO_FORTYPE(uint16, uint16_t); +VMSTATE_INFO_FORTYPE(uint32, uint32_t); +VMSTATE_INFO_FORTYPE(uint64, uint64_t); + +/* equality entries; Check that the value read from the stream is the same + * as the one in the field */ +#define VMSTATE_INFO_EQ_FORTYPE(vn, vt, vf) \ + static int visit_ ## vn ## _equal(Visitor *v, void *pdata, \ + const char *name, size_t size, Error **errp) \ + { \ + if (visitor_loading(v)) { \ + vt received; \ + \ + visit_type_ ## vn(v, &received, name, errp); \ + if (*(vt *)pdata == received) { \ + return 0; \ + } \ + error_setg(errp, \ + "Unexpected value for field '%s', expected 0x%" vf \ + " but got 0x%" vf, name, *(vt *)pdata, received); \ + return -EINVAL; \ + } else { \ + visit_type_ ## vn(v, (vt *)pdata, name, errp); \ + return 0; \ + } \ + } \ + const VMStateInfo vmstate_info_ ## vn ## _equal = { \ + .name = #vn " equal", \ + .visit = visit_ ## vn ## _equal, \ +} + +VMSTATE_INFO_EQ_FORTYPE(int32, int32_t, PRIx32); +VMSTATE_INFO_EQ_FORTYPE(uint8, uint8_t, PRIx8); +VMSTATE_INFO_EQ_FORTYPE(uint16, uint16_t, PRIx16); +VMSTATE_INFO_EQ_FORTYPE(uint32, uint32_t, PRIx32); +VMSTATE_INFO_EQ_FORTYPE(uint64, uint64_t, PRIx64); /* 32 bit int. Check that the received value is less than or equal to the one in the field */ - -static int get_int32_le(QEMUFile *f, void *pv, size_t size) +static int visit_int32_le(Visitor *v, void *pdata, const char *name, + size_t size, Error **errp) { - int32_t *cur = pv; - int32_t loaded; - qemu_get_sbe32s(f, &loaded); + if (visitor_loading(v)) { + int32_t loaded; - if (loaded <= *cur) { - *cur = loaded; + visit_type_int32(v, &loaded, name, errp); + if (loaded <= *(int32_t *)pdata) { + *(int32_t *)pdata = loaded; + return 0; + } + error_setg(errp, "Unexpected value for field '%s', expected a value" + " no larger than %d but got %d", name, *(int32_t *)pdata, + loaded); + return -EINVAL; + } else { + visit_type_int32(v, (int32_t *)pdata, name, errp); return 0; } - return -EINVAL; + } const VMStateInfo vmstate_info_int32_le = { .name = "int32 le", - .get = get_int32_le, - .put = put_int32, -}; - -/* 64 bit int */ - -static int get_int64(QEMUFile *f, void *pv, size_t size) -{ - int64_t *v = pv; - qemu_get_sbe64s(f, v); - return 0; -} - -static void put_int64(QEMUFile *f, void *pv, size_t size) -{ - int64_t *v = pv; - qemu_put_sbe64s(f, v); -} - -const VMStateInfo vmstate_info_int64 = { - .name = "int64", - .get = get_int64, - .put = put_int64, -}; - -/* 8 bit unsigned int */ - -static int get_uint8(QEMUFile *f, void *pv, size_t size) -{ - uint8_t *v = pv; - qemu_get_8s(f, v); - return 0; -} - -static void put_uint8(QEMUFile *f, void *pv, size_t size) -{ - uint8_t *v = pv; - qemu_put_8s(f, v); -} - -const VMStateInfo vmstate_info_uint8 = { - .name = "uint8", - .get = get_uint8, - .put = put_uint8, -}; - -/* 16 bit unsigned int */ - -static int get_uint16(QEMUFile *f, void *pv, size_t size) -{ - uint16_t *v = pv; - qemu_get_be16s(f, v); - return 0; -} - -static void put_uint16(QEMUFile *f, void *pv, size_t size) -{ - uint16_t *v = pv; - qemu_put_be16s(f, v); -} - -const VMStateInfo vmstate_info_uint16 = { - .name = "uint16", - .get = get_uint16, - .put = put_uint16, -}; - -/* 32 bit unsigned int */ - -static int get_uint32(QEMUFile *f, void *pv, size_t size) -{ - uint32_t *v = pv; - qemu_get_be32s(f, v); - return 0; -} - -static void put_uint32(QEMUFile *f, void *pv, size_t size) -{ - uint32_t *v = pv; - qemu_put_be32s(f, v); -} - -const VMStateInfo vmstate_info_uint32 = { - .name = "uint32", - .get = get_uint32, - .put = put_uint32, -}; - -/* 32 bit uint. See that the received value is the same than the one - in the field */ - -static int get_uint32_equal(QEMUFile *f, void *pv, size_t size) -{ - uint32_t *v = pv; - uint32_t v2; - qemu_get_be32s(f, &v2); - - if (*v == v2) { - return 0; - } - return -EINVAL; -} - -const VMStateInfo vmstate_info_uint32_equal = { - .name = "uint32 equal", - .get = get_uint32_equal, - .put = put_uint32, -}; - -/* 64 bit unsigned int */ - -static int get_uint64(QEMUFile *f, void *pv, size_t size) -{ - uint64_t *v = pv; - qemu_get_be64s(f, v); - return 0; -} - -static void put_uint64(QEMUFile *f, void *pv, size_t size) -{ - uint64_t *v = pv; - qemu_put_be64s(f, v); -} - -const VMStateInfo vmstate_info_uint64 = { - .name = "uint64", - .get = get_uint64, - .put = put_uint64, -}; - -/* 64 bit unsigned int. See that the received value is the same than the one - in the field */ - -static int get_uint64_equal(QEMUFile *f, void *pv, size_t size) -{ - uint64_t *v = pv; - uint64_t v2; - qemu_get_be64s(f, &v2); - - if (*v == v2) { - return 0; - } - return -EINVAL; -} - -const VMStateInfo vmstate_info_uint64_equal = { - .name = "int64 equal", - .get = get_uint64_equal, - .put = put_uint64, -}; - -/* 8 bit int. See that the received value is the same than the one - in the field */ - -static int get_uint8_equal(QEMUFile *f, void *pv, size_t size) -{ - uint8_t *v = pv; - uint8_t v2; - qemu_get_8s(f, &v2); - - if (*v == v2) { - return 0; - } - return -EINVAL; -} - -const VMStateInfo vmstate_info_uint8_equal = { - .name = "uint8 equal", - .get = get_uint8_equal, - .put = put_uint8, -}; - -/* 16 bit unsigned int int. See that the received value is the same than the one - in the field */ - -static int get_uint16_equal(QEMUFile *f, void *pv, size_t size) -{ - uint16_t *v = pv; - uint16_t v2; - qemu_get_be16s(f, &v2); - - if (*v == v2) { - return 0; - } - return -EINVAL; -} - -const VMStateInfo vmstate_info_uint16_equal = { - .name = "uint16 equal", - .get = get_uint16_equal, - .put = put_uint16, + .visit = visit_int32_le, }; /* floating point */