Patchwork [RFC,10/16] Visitor: Common types to use visitors

login
register
mail settings
Submitter Dave Gilbert
Date March 25, 2014, 8:17 p.m.
Message ID <1395778647-30925-11-git-send-email-dgilbert@redhat.com>
Download mbox | patch
Permalink /patch/333704/
State New
Headers show

Comments

Dave Gilbert - March 25, 2014, 8:17 p.m.
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(-)

Patch

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 */