Patchwork [09/11] acpi_table_add(): extract acpi_tables reallocation code to a separate function

login
register
mail settings
Submitter Eduardo Habkost
Date June 27, 2012, 8:23 p.m.
Message ID <1340828587-15201-10-git-send-email-ehabkost@redhat.com>
Download mbox | patch
Permalink /patch/167737/
State New
Headers show

Comments

Eduardo Habkost - June 27, 2012, 8:23 p.m.
This was the maint point of most of the previous variable renames and
calculation-method changes: to isolate the g_realloc() call to avoid
doing pointer math with acpi_tables everywhere.

No behavior change, just code movement.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/acpi.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

Patch

diff --git a/hw/acpi.c b/hw/acpi.c
index 251c770..cbc2ba7 100644
--- a/hw/acpi.c
+++ b/hw/acpi.c
@@ -81,6 +81,18 @@  static void init_acpi_tables(void)
     }
 }
 
+/* Resize acpi_tables to fit a new (under construction) table of size 'newlen'
+ *
+ * acpi_tables_len is _not_ changed, as the table is still incomplete.
+ *
+ * Returns a pointer to the reallocated _new_ table at end of acpi_tables.
+ */
+static char *acpi_newtable_resize(size_t newlen)
+{
+    acpi_tables = g_realloc(acpi_tables, acpi_tables_len + newlen);
+    return acpi_tables + acpi_tables_len;
+}
+
 static int acpi_make_table_header(const char *t, bool has_header, char *f,
                                   size_t qemu_len)
 {
@@ -213,7 +225,7 @@  int acpi_table_add(const char *t)
 
     init_acpi_tables();
 
-    acpi_tables = g_realloc(acpi_tables, acpi_tables_len + ACPI_TABLE_HDR_SIZE);
+    acpi_newtable_resize(ACPI_TABLE_HDR_SIZE);
     newlen = has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE;
 
     /* now read in the data files, reallocating buffer as needed */
@@ -232,8 +244,7 @@  int acpi_table_add(const char *t)
             if (r == 0) {
                 break;
             } else if (r > 0) {
-                acpi_tables = g_realloc(acpi_tables, acpi_tables_len + newlen
-                                                                     + r);
+                acpi_newtable_resize(newlen + r);
                 memcpy(acpi_tables + acpi_tables_len + newlen, data, r);
                 newlen += r;
             } else if (errno != EINTR) {