Instead of calculating 'allen - start' every time, just keep the 'newlen' variable up to date and use it instead.
The math in g_realloc() call is now more complex (using 'start + newlen' instead of 'allen'), but it be simplified later, when the g_realloc() calls get moved to a separate function. Now behavior change, just calculation method changes. Signed-off-by: Eduardo Habkost <ehabk...@redhat.com> --- hw/acpi.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/hw/acpi.c b/hw/acpi.c index e69b37a..e116528 100644 --- a/hw/acpi.c +++ b/hw/acpi.c @@ -189,7 +189,7 @@ static int acpi_make_table_header(const char *t, bool has_header, char *f, int acpi_table_add(const char *t) { char buf[1024], *f; - size_t start, allen; + size_t start; size_t newlen; /* length of the new table */ bool has_header; int r; @@ -214,10 +214,9 @@ int acpi_table_add(const char *t) init_acpi_tables(); - allen = acpi_tables_len; - start = allen; + start = acpi_tables_len; acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE); - allen += has_header ? ACPI_TABLE_PFX_SIZE : 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 */ @@ -235,9 +234,9 @@ int acpi_table_add(const char *t) if (r == 0) { break; } else if (r > 0) { - acpi_tables = g_realloc(acpi_tables, allen + r); - memcpy(acpi_tables + allen, data, r); - allen += r; + acpi_tables = g_realloc(acpi_tables, start + newlen + r); + memcpy(acpi_tables + start + newlen, data, r); + newlen += r; } else if (errno != EINTR) { fprintf(stderr, "can't read file %s: %s\n", f, strerror(errno)); @@ -253,9 +252,6 @@ int acpi_table_add(const char *t) f = acpi_tables + start; /* start of the table */ - /* length of the whole table, including our prefix */ - newlen = allen - start; - if (acpi_make_table_header(t, has_header, f, newlen) < 0) { return -1; } @@ -264,7 +260,7 @@ int acpi_table_add(const char *t) (*(uint16_t *)acpi_tables) = cpu_to_le32(le32_to_cpu(*(uint16_t *)acpi_tables) + 1); - acpi_tables_len = allen; + acpi_tables_len = start + newlen; return 0; } -- 1.7.10.4