target/s390x/arch_dump: Simplify memory allocation in s390x_write_elf64_notes()
PATCH 1/1 target/s390x/arch_dump: Simplify memory allocation in s390x_write_elf64_notes()
We are not on a hot path here, so there is no real need for the logic here with the split heap and stack space allocation. Simplify it by always allocating memory from the heap.
---
Based-on: <[email protected]>
target/s390x/arch_dump.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)
target/s390x/arch_dump.c +6 -14
@@ -227,25 +227,23 @@ static int s390x_write_elf64_notes(const char *note_name,DumpState *s, const NoteFuncDesc *funcs) { - Note note, *notep; + g_autofree Note *notep = NULL;const NoteFuncDesc *nf; -int note_size, content_size; +int note_size, prev_size =0 , content_size;int ret =-1 ; -assert (strlen (note_name) <sizeof (note.name)); +assert (strlen (note_name) <sizeof (notep->name));for (nf = funcs; nf->note_contents_func; nf++) { - notep = ¬e;if (nf->pvonly && !s390_is_pv ()) {continue ; } content_size = nf->note_size_func ? nf->note_size_func () : nf->contents_size; - note_size =sizeof (note) -sizeof (notep->contents) + content_size; + note_size =sizeof (Note) -sizeof (notep->contents) + content_size; -/* Notes with dynamic sizes need to allocate a note */ -if (nf->note_size_func) { - notep =g_malloc (note_size); +if (prev_size < note_size) { + notep =g_realloc (notep, note_size); }memset (notep,0 , note_size);
@@ -258,15 +256,9 @@ static int s390x_write_elf64_notes(const char *note_name,/* Get contents and write them out */ (*nf->note_contents_func)(notep, cpu, id); ret =f (notep, note_size, s); - -if (nf->note_size_func) { -g_free (notep); - } -if (ret <0 ) {return -1 ; } - }return 0 ;
Signature · 1 line
2.31.1
Quoted 17 lines · Thomas Huth
We are not on a hot path here, so there is no real need for the logic here with the split heap and stack space allocation. Simplify it by always allocating memory from the heap.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Thomas Huth <[email protected]>---
Based-on: <[email protected]>target/s390x/arch_dump.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)target/s390x/arch_dump.c +0
-0
target/s390x/arch_dump.c static int s390x_write_elf64_notes(const char *note_name, · line 232
Quoted 8 lines
target/s390x/arch_dump.c +2
-2@@ -227,25 +227,23 @@ static int s390x_write_elf64_notes(const char *note_name,DumpState *s, const NoteFuncDesc *funcs) { - Note note, *notep; + g_autofree Note *notep = NULL;const NoteFuncDesc *nf; -int note_size, content_size; +int note_size, prev_size =0 , content_size;
We can start with:
prev_size = sizeof(Note);
If this goes thru your tree, feel free to modify without respining.
Thanks!
Quoted 21 lines
target/s390x/arch_dump.c +4
-6int ret =-1 ; -assert (strlen (note_name) <sizeof (note.name)); +assert (strlen (note_name) <sizeof (notep->name));for (nf = funcs; nf->note_contents_func; nf++) { - notep = ¬e;if (nf->pvonly && !s390_is_pv ()) {continue ; } content_size = nf->note_size_func ? nf->note_size_func () : nf->contents_size; - note_size =sizeof (note) -sizeof (notep->contents) + content_size; + note_size =sizeof (Note) -sizeof (notep->contents) + content_size; -/* Notes with dynamic sizes need to allocate a note */ -if (nf->note_size_func) { - notep =g_malloc (note_size); +if (prev_size < note_size) { + notep =g_realloc (notep, note_size); }
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Thomas Huth
We are not on a hot path here, so there is no real need for the logic here with the split heap and stack space allocation. Simplify it by always allocating memory from the heap.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Thomas Huth <[email protected]>---
Based-on: <[email protected]>
target/s390x/arch_dump.c| 20 ++++++--------------1 file changed, 6 insertions(+), 14 deletions(-)target/s390x/arch_dump.c +0
-0target/s390x/arch_dump.c static int s390x_write_elf64_notes(const char · line 233
Quoted 9 lines
target/s390x/arch_dump.c +2
-2@@ -227,25 +227,23 @@ static int s390x_write_elf64_notes(const char *note_name, DumpState *s, const NoteFuncDesc *funcs) { - Note note, *notep; + g_autofree Note *notep = NULL; const NoteFuncDesc *nf; - int note_size, content_size; + int note_size, prev_size = 0, content_size;We can start with:
Quoted 3 lines
prev_size= sizeof(Note);If this goes thru your tree, feel free to modify without respining.
But then I'd also need to initialize notep above differently, don't I?
And if I've got it right, this function sometimes also deals with chunks that are smaller, so I think it's cleaner if we start with zero instead of sizeof(Note).
Quoted 1 line
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Thanks!
Thomas
Quoted 31 lines · Thomas Huth
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Thomas Huth
We are not on a hot path here, so there is no real need for the logic here with the split heap and stack space allocation. Simplify it by always allocating memory from the heap.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Thomas Huth <[email protected]>---
Based-on: <[email protected]>
target/s390x/arch_dump.c| 20 ++++++--------------1 file changed, 6 insertions(+), 14 deletions(-)target/s390x/arch_dump.c +0
-0target/s390x/arch_dump.c static int s390x_write_elf64_notes(const char · line 233
Quoted 9 lines
target/s390x/arch_dump.c +2
-2@@ -227,25 +227,23 @@ static int s390x_write_elf64_notes(const char *note_name, DumpState *s, const NoteFuncDesc *funcs) { - Note note, *notep; + g_autofree Note *notep = NULL; const NoteFuncDesc *nf; - int note_size, content_size; + int note_size, prev_size = 0, content_size;We can start with:
Quoted 6 lines
Quoted 3 lines
prev_size= sizeof(Note);If this goes thru your tree, feel free to modify without respining.
But then I'd also need to initialize notep above differently, don't I?
And if I've got it right, this function sometimes also deals with chunks that are smaller, so I think it's cleaner if we start with zero instead of sizeof(Note).
Oh you are right. We are good then!
Quoted 6 lines
Quoted 1 line
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Thanks!
Thomas
Quoted 36 lines · Thomas Huth
We are not on a hot path here, so there is no real need for the logic here with the split heap and stack space allocation. Simplify it by always allocating memory from the heap.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Thomas Huth <[email protected]>---
Based-on: <[email protected]>target/s390x/arch_dump.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)target/s390x/arch_dump.c +3
-4@@ -227,25 +227,23 @@ static int s390x_write_elf64_notes(const char *note_name,DumpState *s, const NoteFuncDesc *funcs) { - Note note, *notep; + g_autofree Note *notep = NULL;const NoteFuncDesc *nf; -int note_size, content_size; +int note_size, prev_size =0 , content_size;int ret =-1 ; -assert (strlen (note_name) <sizeof (note.name)); +assert (strlen (note_name) <sizeof (notep->name));for (nf = funcs; nf->note_contents_func; nf++) { - notep = ¬e;if (nf->pvonly && !s390_is_pv ()) {continue ; }
target/s390x/arch_dump.c static int s390x_write_elf64_notes(const char *note_name, · line 247
Quoted 10 lines
target/s390x/arch_dump.c +3
-4content_size = nf->note_size_func ? nf->note_size_func () : nf->contents_size; - note_size =sizeof (note) -sizeof (notep->contents) + content_size; + note_size =sizeof (Note) -sizeof (notep->contents) + content_size; -/* Notes with dynamic sizes need to allocate a note */ -if (nf->note_size_func) { - notep =g_malloc (note_size); +if (prev_size < note_size) { + notep =g_realloc (notep, note_size); }
-ENOTENOUGHCOFFEEYET
Janosch just told me that there is a prev_size = note_size missing here ... sorry for that, I'll send a v2.
Thomas