[RFC PATCH v1 09/17] vmstate: Increase scope of vmstate_handle_alloc

Fabiano Rosas posted 17 patches 1 week, 2 days ago
[RFC PATCH v1 09/17] vmstate: Increase scope of vmstate_handle_alloc
Posted by Fabiano Rosas 1 week, 2 days ago
Move more code into vmstate_handle_alloc. If it's going to handle,
make it handle it all.

Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
 migration/vmstate.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/migration/vmstate.c b/migration/vmstate.c
index 8437b5ef3e..0cdfde2232 100644
--- a/migration/vmstate.c
+++ b/migration/vmstate.c
@@ -100,15 +100,23 @@ static int vmstate_size(void *opaque, const VMStateField *field)
     return size;
 }
 
-static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
+static void *vmstate_handle_alloc(void *ptr, const VMStateField *field,
                                  int size, int n_elems)
 {
-    if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
-        size *= n_elems;
-        if (size) {
-            *(void **)ptr = g_malloc(size);
+    void *new = ptr;
+
+    if (field->flags & VMS_POINTER) {
+        if (field->flags & VMS_ALLOC) {
+            size *= n_elems;
+            if (size) {
+                new = g_malloc(size);
+                *(void **)ptr = new;
+            }
         }
+        assert(new || !n_elems || !size);
+        return *(void **)ptr;
     }
+    return new;
 }
 
 static bool vmstate_ptr_marker_load(QEMUFile *f, bool *load_field,
@@ -247,11 +255,7 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
             int i, n_elems = vmstate_n_elems(opaque, field);
             int size = vmstate_size(opaque, field);
 
-            vmstate_handle_alloc(first_elem, field, size, n_elems);
-            if (field->flags & VMS_POINTER) {
-                first_elem = *(void **)first_elem;
-                assert(first_elem || !n_elems || !size);
-            }
+            first_elem = vmstate_handle_alloc(first_elem, field, size, n_elems);
 
             for (i = 0; i < n_elems; i++) {
                 /* If we will process the load of field? */
-- 
2.51.0