qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
To: qemu-devel@nongnu.org
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
Date: Tue, 25 Mar 2014 20:17:21 +0000	[thread overview]
Message-ID: <1395778647-30925-11-git-send-email-dgilbert@redhat.com> (raw)
In-Reply-To: <1395778647-30925-1-git-send-email-dgilbert@redhat.com>

From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>

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 <dgilbert@redhat.com>
---
 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 <inttypes.h>
+
 #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 */
-- 
1.8.5.3

  parent reply	other threads:[~2014-03-25 20:19 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-25 20:17 [Qemu-devel] [RFC PATCH 00/16] visitor+BER migration format Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 01/16] Visitor: Add methods for migration format use Dr. David Alan Gilbert (git)
2014-06-05 17:00   ` Markus Armbruster
2014-06-05 17:43     ` Dr. David Alan Gilbert
2014-06-05 18:59       ` Dr. David Alan Gilbert
2014-06-06  8:19       ` Markus Armbruster
2014-06-06 11:44         ` Dr. David Alan Gilbert
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 02/16] QEMUSizedBuffer/QEMUFile Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 03/16] qemu-file: Add set/get tmp_visitor Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 04/16] Header/constant/types fixes for visitors Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 05/16] Visitor: Binary compatible output visitor Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 06/16] Visitor: Debug " Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 07/16] Visitor: Binary compatible input visitor Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 08/16] Visitor: Output path Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 09/16] Visitor: Load path Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` Dr. David Alan Gilbert (git) [this message]
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 11/16] Choose output visitor based on env variable Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 12/16] BER Visitor: Create output visitor Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 13/16] BER Visitor: Create input visitor Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 14/16] Start some BER format docs Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 15/16] ASN.1 schema for new migration format Dr. David Alan Gilbert (git)
2014-03-25 20:17 ` [Qemu-devel] [RFC PATCH 16/16] Wire in BER visitors Dr. David Alan Gilbert (git)
2014-03-25 20:43 ` [Qemu-devel] [RFC PATCH 00/16] visitor+BER migration format Michael S. Tsirkin
2014-03-26  9:16   ` Dr. David Alan Gilbert

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=1395778647-30925-11-git-send-email-dgilbert@redhat.com \
    --to=dgilbert@redhat.com \
    --cc=afaerber@suse.de \
    --cc=agraf@suse.de \
    --cc=aliguori@amazon.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=stefanb@linux.vnet.ibm.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).