[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 04/97] vmstate: reduce code duplication
From: |
Juan Quintela |
Subject: |
[Qemu-devel] [PATCH 04/97] vmstate: reduce code duplication |
Date: |
Mon, 7 Apr 2014 05:20:22 +0200 |
From: "Michael S. Tsirkin" <address@hidden>
move size offset and number of elements math out
to functions, to reduce code duplication.
Signed-off-by: Michael S. Tsirkin <address@hidden>
Cc: "Dr. David Alan Gilbert" <address@hidden>
Signed-off-by: Juan Quintela <address@hidden>
---
vmstate.c | 100 ++++++++++++++++++++++++++++++++------------------------------
1 file changed, 52 insertions(+), 48 deletions(-)
diff --git a/vmstate.c b/vmstate.c
index d82cccf..cb0af5e 100644
--- a/vmstate.c
+++ b/vmstate.c
@@ -10,6 +10,50 @@ static void vmstate_subsection_save(QEMUFile *f, const
VMStateDescription *vmsd,
static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
void *opaque);
+static int vmstate_n_elems(void *opaque, VMStateField *field)
+{
+ int n_elems = 1;
+
+ if (field->flags & VMS_ARRAY) {
+ n_elems = field->num;
+ } else if (field->flags & VMS_VARRAY_INT32) {
+ n_elems = *(int32_t *)(opaque+field->num_offset);
+ } else if (field->flags & VMS_VARRAY_UINT32) {
+ n_elems = *(uint32_t *)(opaque+field->num_offset);
+ } else if (field->flags & VMS_VARRAY_UINT16) {
+ n_elems = *(uint16_t *)(opaque+field->num_offset);
+ } else if (field->flags & VMS_VARRAY_UINT8) {
+ n_elems = *(uint8_t *)(opaque+field->num_offset);
+ }
+
+ return n_elems;
+}
+
+static int vmstate_size(void *opaque, VMStateField *field)
+{
+ int size = field->size;
+
+ if (field->flags & VMS_VBUFFER) {
+ size = *(int32_t *)(opaque+field->size_offset);
+ if (field->flags & VMS_MULTIPLY) {
+ size *= field->size;
+ }
+ }
+
+ return size;
+}
+
+static void *vmstate_base_addr(void *opaque, VMStateField *field)
+{
+ void *base_addr = opaque + field->offset;
+
+ if (field->flags & VMS_POINTER) {
+ base_addr = *(void **)base_addr + field->start;
+ }
+
+ return base_addr;
+}
+
int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
void *opaque, int version_id)
{
@@ -37,30 +81,10 @@ int vmstate_load_state(QEMUFile *f, const
VMStateDescription *vmsd,
field->field_exists(opaque, version_id)) ||
(!field->field_exists &&
field->version_id <= version_id)) {
- void *base_addr = opaque + field->offset;
- int i, n_elems = 1;
- int size = field->size;
-
- if (field->flags & VMS_VBUFFER) {
- size = *(int32_t *)(opaque+field->size_offset);
- if (field->flags & VMS_MULTIPLY) {
- size *= field->size;
- }
- }
- if (field->flags & VMS_ARRAY) {
- n_elems = field->num;
- } else if (field->flags & VMS_VARRAY_INT32) {
- n_elems = *(int32_t *)(opaque+field->num_offset);
- } else if (field->flags & VMS_VARRAY_UINT32) {
- n_elems = *(uint32_t *)(opaque+field->num_offset);
- } else if (field->flags & VMS_VARRAY_UINT16) {
- n_elems = *(uint16_t *)(opaque+field->num_offset);
- } else if (field->flags & VMS_VARRAY_UINT8) {
- n_elems = *(uint8_t *)(opaque+field->num_offset);
- }
- if (field->flags & VMS_POINTER) {
- base_addr = *(void **)base_addr + field->start;
- }
+ void *base_addr = vmstate_base_addr(opaque, field);
+ int i, n_elems = vmstate_n_elems(opaque, field);
+ int size = vmstate_size(opaque, field);
+
for (i = 0; i < n_elems; i++) {
void *addr = base_addr + size * i;
@@ -106,30 +130,10 @@ void vmstate_save_state(QEMUFile *f, const
VMStateDescription *vmsd,
while (field->name) {
if (!field->field_exists ||
field->field_exists(opaque, vmsd->version_id)) {
- void *base_addr = opaque + field->offset;
- int i, n_elems = 1;
- int size = field->size;
-
- if (field->flags & VMS_VBUFFER) {
- size = *(int32_t *)(opaque+field->size_offset);
- if (field->flags & VMS_MULTIPLY) {
- size *= field->size;
- }
- }
- if (field->flags & VMS_ARRAY) {
- n_elems = field->num;
- } else if (field->flags & VMS_VARRAY_INT32) {
- n_elems = *(int32_t *)(opaque+field->num_offset);
- } else if (field->flags & VMS_VARRAY_UINT32) {
- n_elems = *(uint32_t *)(opaque+field->num_offset);
- } else if (field->flags & VMS_VARRAY_UINT16) {
- n_elems = *(uint16_t *)(opaque+field->num_offset);
- } else if (field->flags & VMS_VARRAY_UINT8) {
- n_elems = *(uint8_t *)(opaque+field->num_offset);
- }
- if (field->flags & VMS_POINTER) {
- base_addr = *(void **)base_addr + field->start;
- }
+ void *base_addr = vmstate_base_addr(opaque, field);
+ int i, n_elems = vmstate_n_elems(opaque, field);
+ int size = vmstate_size(opaque, field);
+
for (i = 0; i < n_elems; i++) {
void *addr = base_addr + size * i;
--
1.9.0
- [Qemu-devel] [PATCH for 2.1 00/97] VMState simplification (massive), Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 01/97] savevm: Ignore minimum_version_id_old if there is no load_state_old, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 03/97] vmstate: return error in case of error, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 05/97] vmstate: refactor opening of files, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 07/97] vmstate: create test functions for versions until 15, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 04/97] vmstate: reduce code duplication,
Juan Quintela <=
- [Qemu-devel] [PATCH 06/97] vmstate: Refactor & increase tests for primitive types, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 08/97] vmstate: Remove VMSTATE_UINTL_EQUAL_V, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 09/97] vmstate: Change VMSTATE_INTTL_V to VMSTATE_INTTL_TEST, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 10/97] vmstate: Remove unused VMSTATE_UINTTL_ARRAY_V, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 11/97] vmstate: Test for VMSTATE_BOOL_TEST, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 12/97] vmstate: Test for VMSTATE_INT8_TEST, Juan Quintela, 2014/04/06
- [Qemu-devel] [PATCH 02/97] savevm: Remove all the unneded version_minimum_id_old (Massive), Juan Quintela, 2014/04/06