From: Fabiano Rosas <farosas@suse.de>
To: qemu-devel@nongnu.org
Cc: Peter Xu <peterx@redhat.com>,
Alexander Mikhalitsyn <alexander@mihalicyn.com>,
Juraj Marcin <jmarcin@redhat.com>
Subject: [RFC PATCH v1 11/17] vmstate: Introduce vmstate_first
Date: Tue, 24 Mar 2026 16:43:26 -0300 [thread overview]
Message-ID: <20260324194333.30004-12-farosas@suse.de> (raw)
In-Reply-To: <20260324194333.30004-1-farosas@suse.de>
The vmstate_save|load_vmsd[_v] functions do a bit of juggling of
pointers to allocate/dereference arrays. Introduce a getter for the
first field. Take field->flags into consideration and also allocate
memory when necessary.
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
maybe we can soon have an iterator
---
migration/vmstate.c | 67 +++++++++++++++++++++++++--------------------
1 file changed, 38 insertions(+), 29 deletions(-)
diff --git a/migration/vmstate.c b/migration/vmstate.c
index 85e63305f6..ab7c6fa4ab 100644
--- a/migration/vmstate.c
+++ b/migration/vmstate.c
@@ -100,23 +100,31 @@ static int vmstate_size(void *opaque, const VMStateField *field)
return size;
}
-static void *vmstate_handle_alloc(void *ptr, const VMStateField *field,
- int size, int n_elems)
+static void *vmstate_handle_alloc(void **ptr, int size, int n_elems)
{
- void *new = ptr;
+ size *= n_elems;
+ if (size) {
+ *ptr = g_malloc(size);
+ }
+
+ return *ptr;
+}
+
+static void *vmstate_first(void *opaque, const VMStateField *field,
+ int size, int n, bool alloc)
+{
+ void **first = opaque + field->offset;
+
+ if (alloc) {
+ return vmstate_handle_alloc(first, size, n);
+ }
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;
+ assert(first || !n || !size);
+ return *first;
}
- return new;
+
+ return first;
}
static bool vmstate_ptr_marker_load(QEMUFile *f, bool *load_field,
@@ -251,11 +259,12 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
trace_vmstate_load_state_field(vmsd->name, field->name, exists);
if (exists) {
- void *first_elem = opaque + field->offset;
+ void *head;
int i, n_elems = vmstate_n_elems(opaque, field);
int size = vmstate_size(opaque, field);
- first_elem = vmstate_handle_alloc(first_elem, field, size, n_elems);
+ head = vmstate_first(opaque, field, size, n_elems,
+ field->flags & VMS_ALLOC);
for (i = 0; i < n_elems; i++) {
/* If we will process the load of field? */
@@ -264,7 +273,7 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
void *curr_elem;
if (field->flags & VMS_ARRAY_OF_POINTER) {
- void **array_elem = (void **)first_elem + i;
+ void **array_elem = (void **)head + i;
bool use_dynamic_array =
field->flags & VMS_ARRAY_OF_POINTER_AUTO_ALLOC;
bool use_marker_field;
@@ -290,13 +299,13 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
* need the object size, not entry size of the
* array.
*/
- curr_elem = g_malloc0(field->size);
- /* Remember to update the root pointer! */
- *(void **)array_elem = curr_elem;
+ assert(!curr_elem);
+ curr_elem = vmstate_handle_alloc(array_elem,
+ field->size, 1);
}
}
} else {
- curr_elem = first_elem + size * i;
+ curr_elem = head + size * i;
}
if (load_field) {
@@ -627,28 +636,26 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
while (field->name) {
if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
- void *first_elem = opaque + field->offset;
+ void *head;
int i, n_elems = vmstate_n_elems(opaque, field);
int size = vmstate_size(opaque, field);
bool is_null_prev = false;
bool use_vmdesc = true;
trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
- if (field->flags & VMS_POINTER) {
- first_elem = *(void **)first_elem;
- assert(first_elem || !n_elems || !size);
- }
+ head = vmstate_first(opaque, field, size, n_elems, false);
for (i = 0; i < n_elems; i++) {
bool save_field = true;
- void *curr_elem = first_elem + size * i;
+ void *curr_elem;
int max_elems = n_elems - i;
if (field->flags & VMS_ARRAY_OF_POINTER) {
bool use_marker_field, is_null, use_dynamic_array;
+ void **array_elem = (void **)head + i;
- assert(curr_elem);
- curr_elem = *(void **)curr_elem;
+ assert(array_elem);
+ curr_elem = *array_elem;
is_null = !curr_elem;
@@ -681,7 +688,7 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
use_vmdesc = true;
for (int j = i + 1; j < n_elems; j++) {
- void *elem = *(void **)(first_elem + size * j);
+ void *elem = *(void **)(head + size * j);
bool elem_is_null = !elem;
if (is_null != elem_is_null) {
@@ -706,6 +713,8 @@ static bool vmstate_save_vmsd_v(QEMUFile *f, const VMStateDescription *vmsd,
save_field = !!curr_elem;
}
+ } else {
+ curr_elem = head + size * i;
}
if (save_field) {
--
2.51.0
next prev parent reply other threads:[~2026-03-24 19:45 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-24 19:43 [RFC PATCH v1 00/17] migration: vmstate_save|load changes for peterx Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 01/17] vmstate: fixup the use of AUTO_ALLOC flag Fabiano Rosas
2026-03-25 16:18 ` Peter Xu
2026-03-24 19:43 ` [RFC PATCH v1 02/17] vmstate: Remove vmstate_use_marker_field Fabiano Rosas
2026-03-25 16:37 ` Peter Xu
2026-03-25 17:51 ` Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 03/17] vmstate: Stop checking size for nullptr compression Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 04/17] vmstate: Set error inside of vmstate_save_field_with_vmdesc Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 05/17] vmstate: Remove vmdesc_loop Fabiano Rosas
2026-03-25 17:07 ` Peter Xu
2026-03-25 18:11 ` Fabiano Rosas
2026-03-25 21:43 ` Peter Xu
2026-03-24 19:43 ` [RFC PATCH v1 06/17] vmstate: Put array of pointers code together Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 07/17] vmstate: Create and save ptr marker in same function Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 08/17] vmstate: Don't recompute size and n_elems in vmstate_size Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 09/17] vmstate: Increase scope of vmstate_handle_alloc Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 10/17] vmstate: Remove curr_elem_p Fabiano Rosas
2026-03-24 19:43 ` Fabiano Rosas [this message]
2026-03-24 19:43 ` [RFC PATCH v1 12/17] vmstate: Introduce vmstate_next Fabiano Rosas
2026-03-26 14:18 ` Peter Xu
2026-03-26 21:45 ` Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 13/17] vmstate: Drop VMS_ARRAY_OF_POINTER_AUTO_ALLOC Fabiano Rosas
2026-03-25 19:29 ` Peter Xu
2026-03-25 21:49 ` Peter Xu
2026-03-25 21:57 ` Peter Xu
2026-03-24 19:43 ` [RFC PATCH v1 14/17] vmstate: Move VMS_MUST_EXIST check Fabiano Rosas
2026-03-25 19:38 ` Peter Xu
2026-03-24 19:43 ` [RFC PATCH v1 15/17] vmstate: Invert exists check Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 16/17] vmstate: Declare variables at the top Fabiano Rosas
2026-03-24 19:43 ` [RFC PATCH v1 17/17] vmstate: Reduce indentation levels Fabiano Rosas
2026-03-25 19:43 ` [RFC PATCH v1 00/17] migration: vmstate_save|load changes for peterx Peter Xu
2026-03-25 20:10 ` Fabiano Rosas
2026-03-26 19:42 ` Peter Xu
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=20260324194333.30004-12-farosas@suse.de \
--to=farosas@suse.de \
--cc=alexander@mihalicyn.com \
--cc=jmarcin@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/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