Patchwork [01/11] acpi_table_add(): simplify acpi_tables on-demand initialization

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

Comments

Eduardo Habkost - June 27, 2012, 8:22 p.m.
Change the code to set acpi_tables_len as soon as acpi_tables is
allocated, so the rest of the code can simply use acpi_tables_len
safely.

There's a small behavior change if the first acpi_table_add() call fails
to open a file, but the change is actually desirable, because the old
code kept 'acpi_tables_len = 0' but a non-NULL 'acpi_tables' on error,
generating an invalid table if a second acpi_add() call were made.

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

Patch

diff --git a/hw/acpi.c b/hw/acpi.c
index effc7ec..21e573b 100644
--- a/hw/acpi.c
+++ b/hw/acpi.c
@@ -102,12 +102,11 @@  int acpi_table_add(const char *t)
     }
 
     if (!acpi_tables) {
-        allen = sizeof(uint16_t);
-        acpi_tables = g_malloc0(allen);
-    } else {
-        allen = acpi_tables_len;
+        acpi_tables_len = sizeof(uint16_t);
+        acpi_tables = g_malloc0(acpi_tables_len);
     }
 
+    allen = acpi_tables_len;
     start = allen;
     acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE);
     allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE;