cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
2382db
From 8f92bf7e5891ec22b84b7969f24e6918ee5a9e38 Mon Sep 17 00:00:00 2001
2382db
From: Michael S. Tsirkin <mst@redhat.com>
2382db
Date: Wed, 14 May 2014 08:24:25 +0200
2382db
Subject: [PATCH 01/10] vmstate: reduce code duplication
2382db
2382db
Message-id: <1400055633-6261-1-git-send-email-mst@redhat.com>
2382db
Patchwork-id: 58848
2382db
O-Subject: [PATCH qemu-kvm RHEL7.0.z 1/5] vmstate: reduce code duplication
2382db
Bugzilla: 1095706
2382db
RH-Acked-by: Juan Quintela <quintela@redhat.com>
2382db
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
2382db
RH-Acked-by: Marcel Apfelbaum <marcel.a@redhat.com>
2382db
RH-Acked-by: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
2382db
2382db
move size offset and number of elements math out
2382db
to functions, to reduce code duplication.
2382db
2382db
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
2382db
Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
2382db
Signed-off-by: Juan Quintela <quintela@redhat.com>
2382db
(cherry picked from commit 35fc1f71899fd42323bd8f33da18f0211e0d2727)
2382db
---
2382db
 savevm.c | 100 +++++++++++++++++++++++++++++++++------------------------------
2382db
 1 file changed, 52 insertions(+), 48 deletions(-)
2382db
2382db
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
2382db
---
2382db
 savevm.c |  100 ++++++++++++++++++++++++++++++++-----------------------------
2382db
 1 files changed, 52 insertions(+), 48 deletions(-)
2382db
2382db
diff --git a/savevm.c b/savevm.c
2382db
index 5d1b563..94512dd 100644
2382db
--- a/savevm.c
2382db
+++ b/savevm.c
2382db
@@ -1675,6 +1675,50 @@ static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2382db
 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2382db
                                    void *opaque);
2382db
 
2382db
+static int vmstate_n_elems(void *opaque, VMStateField *field)
2382db
+{
2382db
+    int n_elems = 1;
2382db
+
2382db
+    if (field->flags & VMS_ARRAY) {
2382db
+        n_elems = field->num;
2382db
+    } else if (field->flags & VMS_VARRAY_INT32) {
2382db
+        n_elems = *(int32_t *)(opaque+field->num_offset);
2382db
+    } else if (field->flags & VMS_VARRAY_UINT32) {
2382db
+        n_elems = *(uint32_t *)(opaque+field->num_offset);
2382db
+    } else if (field->flags & VMS_VARRAY_UINT16) {
2382db
+        n_elems = *(uint16_t *)(opaque+field->num_offset);
2382db
+    } else if (field->flags & VMS_VARRAY_UINT8) {
2382db
+        n_elems = *(uint8_t *)(opaque+field->num_offset);
2382db
+    }
2382db
+
2382db
+    return n_elems;
2382db
+}
2382db
+
2382db
+static int vmstate_size(void *opaque, VMStateField *field)
2382db
+{
2382db
+    int size = field->size;
2382db
+
2382db
+    if (field->flags & VMS_VBUFFER) {
2382db
+        size = *(int32_t *)(opaque+field->size_offset);
2382db
+        if (field->flags & VMS_MULTIPLY) {
2382db
+            size *= field->size;
2382db
+        }
2382db
+    }
2382db
+
2382db
+    return size;
2382db
+}
2382db
+
2382db
+static void *vmstate_base_addr(void *opaque, VMStateField *field)
2382db
+{
2382db
+    void *base_addr = opaque + field->offset;
2382db
+
2382db
+    if (field->flags & VMS_POINTER) {
2382db
+        base_addr = *(void **)base_addr + field->start;
2382db
+    }
2382db
+
2382db
+    return base_addr;
2382db
+}
2382db
+
2382db
 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
2382db
                        void *opaque, int version_id)
2382db
 {
2382db
@@ -1700,30 +1744,10 @@ int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
2382db
              field->field_exists(opaque, version_id)) ||
2382db
             (!field->field_exists &&
2382db
              field->version_id <= version_id)) {
2382db
-            void *base_addr = opaque + field->offset;
2382db
-            int i, n_elems = 1;
2382db
-            int size = field->size;
2382db
-
2382db
-            if (field->flags & VMS_VBUFFER) {
2382db
-                size = *(int32_t *)(opaque+field->size_offset);
2382db
-                if (field->flags & VMS_MULTIPLY) {
2382db
-                    size *= field->size;
2382db
-                }
2382db
-            }
2382db
-            if (field->flags & VMS_ARRAY) {
2382db
-                n_elems = field->num;
2382db
-            } else if (field->flags & VMS_VARRAY_INT32) {
2382db
-                n_elems = *(int32_t *)(opaque+field->num_offset);
2382db
-            } else if (field->flags & VMS_VARRAY_UINT32) {
2382db
-                n_elems = *(uint32_t *)(opaque+field->num_offset);
2382db
-            } else if (field->flags & VMS_VARRAY_UINT16) {
2382db
-                n_elems = *(uint16_t *)(opaque+field->num_offset);
2382db
-            } else if (field->flags & VMS_VARRAY_UINT8) {
2382db
-                n_elems = *(uint8_t *)(opaque+field->num_offset);
2382db
-            }
2382db
-            if (field->flags & VMS_POINTER) {
2382db
-                base_addr = *(void **)base_addr + field->start;
2382db
-            }
2382db
+            void *base_addr = vmstate_base_addr(opaque, field);
2382db
+            int i, n_elems = vmstate_n_elems(opaque, field);
2382db
+            int size = vmstate_size(opaque, field);
2382db
+
2382db
             for (i = 0; i < n_elems; i++) {
2382db
                 void *addr = base_addr + size * i;
2382db
 
2382db
@@ -1768,30 +1792,10 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
2382db
     while(field->name) {
2382db
         if (!field->field_exists ||
2382db
             field->field_exists(opaque, vmsd->version_id)) {
2382db
-            void *base_addr = opaque + field->offset;
2382db
-            int i, n_elems = 1;
2382db
-            int size = field->size;
2382db
-
2382db
-            if (field->flags & VMS_VBUFFER) {
2382db
-                size = *(int32_t *)(opaque+field->size_offset);
2382db
-                if (field->flags & VMS_MULTIPLY) {
2382db
-                    size *= field->size;
2382db
-                }
2382db
-            }
2382db
-            if (field->flags & VMS_ARRAY) {
2382db
-                n_elems = field->num;
2382db
-            } else if (field->flags & VMS_VARRAY_INT32) {
2382db
-                n_elems = *(int32_t *)(opaque+field->num_offset);
2382db
-            } else if (field->flags & VMS_VARRAY_UINT32) {
2382db
-                n_elems = *(uint32_t *)(opaque+field->num_offset);
2382db
-            } else if (field->flags & VMS_VARRAY_UINT16) {
2382db
-                n_elems = *(uint16_t *)(opaque+field->num_offset);
2382db
-            } else if (field->flags & VMS_VARRAY_UINT8) {
2382db
-                n_elems = *(uint8_t *)(opaque+field->num_offset);
2382db
-            }
2382db
-            if (field->flags & VMS_POINTER) {
2382db
-                base_addr = *(void **)base_addr + field->start;
2382db
-            }
2382db
+            void *base_addr = vmstate_base_addr(opaque, field);
2382db
+            int i, n_elems = vmstate_n_elems(opaque, field);
2382db
+            int size = vmstate_size(opaque, field);
2382db
+
2382db
             for (i = 0; i < n_elems; i++) {
2382db
                 void *addr = base_addr + size * i;
2382db
 
2382db
-- 
2382db
1.7.1
2382db