* [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new
@ 2017-05-08 18:00 Max Filippov
2017-05-08 18:57 ` Philippe Mathieu-Daudé
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Max Filippov @ 2017-05-08 18:00 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Alex Bennée, QEMU Trivial, Max Filippov
Replace malloc/free/sprintf with g_string/g_string_printf/g_string_free.
Replace g_malloc with g_new when allocating the MemoryRegion to get more
type safety.
Suggested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
---
hw/xtensa/sim.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/hw/xtensa/sim.c b/hw/xtensa/sim.c
index d2d1d3a..b27e28d 100644
--- a/hw/xtensa/sim.c
+++ b/hw/xtensa/sim.c
@@ -41,21 +41,21 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
const char *name)
{
unsigned i;
- char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
+ GString *num_name = g_string_new(NULL);
for (i = 0; i < memory->num; ++i) {
MemoryRegion *m;
- sprintf(num_name, "%s%u", name, i);
- m = g_malloc(sizeof(*m));
- memory_region_init_ram(m, NULL, num_name,
+ g_string_printf(num_name, "%s%u", name, i);
+ m = g_new(MemoryRegion, 1);
+ memory_region_init_ram(m, NULL, num_name->str,
memory->location[i].size,
&error_fatal);
vmstate_register_ram_global(m);
memory_region_add_subregion(get_system_memory(),
memory->location[i].addr, m);
}
- free(num_name);
+ g_string_free(num_name, true);
}
static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
--
2.1.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new
2017-05-08 18:00 [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new Max Filippov
@ 2017-05-08 18:57 ` Philippe Mathieu-Daudé
2017-05-09 8:00 ` Markus Armbruster
2017-05-23 14:53 ` Michael Tokarev
2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2017-05-08 18:57 UTC (permalink / raw)
To: Max Filippov, qemu-devel; +Cc: QEMU Trivial, Paolo Bonzini, Alex Bennée
On 05/08/2017 03:00 PM, Max Filippov wrote:
> Replace malloc/free/sprintf with g_string/g_string_printf/g_string_free.
> Replace g_malloc with g_new when allocating the MemoryRegion to get more
> type safety.
>
> Suggested-by: Alex Bennée <alex.bennee@linaro.org>
> Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
> hw/xtensa/sim.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/hw/xtensa/sim.c b/hw/xtensa/sim.c
> index d2d1d3a..b27e28d 100644
> --- a/hw/xtensa/sim.c
> +++ b/hw/xtensa/sim.c
> @@ -41,21 +41,21 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
> const char *name)
> {
> unsigned i;
> - char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
> + GString *num_name = g_string_new(NULL);
>
> for (i = 0; i < memory->num; ++i) {
> MemoryRegion *m;
>
> - sprintf(num_name, "%s%u", name, i);
> - m = g_malloc(sizeof(*m));
> - memory_region_init_ram(m, NULL, num_name,
> + g_string_printf(num_name, "%s%u", name, i);
> + m = g_new(MemoryRegion, 1);
> + memory_region_init_ram(m, NULL, num_name->str,
> memory->location[i].size,
> &error_fatal);
> vmstate_register_ram_global(m);
> memory_region_add_subregion(get_system_memory(),
> memory->location[i].addr, m);
> }
> - free(num_name);
> + g_string_free(num_name, true);
> }
>
> static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new
2017-05-08 18:00 [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new Max Filippov
2017-05-08 18:57 ` Philippe Mathieu-Daudé
@ 2017-05-09 8:00 ` Markus Armbruster
2017-05-23 14:53 ` Michael Tokarev
2 siblings, 0 replies; 4+ messages in thread
From: Markus Armbruster @ 2017-05-09 8:00 UTC (permalink / raw)
To: Max Filippov; +Cc: qemu-devel, QEMU Trivial, Paolo Bonzini, Alex Bennée
Max Filippov <jcmvbkbc@gmail.com> writes:
> Replace malloc/free/sprintf with g_string/g_string_printf/g_string_free.
> Replace g_malloc with g_new when allocating the MemoryRegion to get more
> type safety.
>
> Suggested-by: Alex Bennée <alex.bennee@linaro.org>
> Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
> ---
> hw/xtensa/sim.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/hw/xtensa/sim.c b/hw/xtensa/sim.c
> index d2d1d3a..b27e28d 100644
> --- a/hw/xtensa/sim.c
> +++ b/hw/xtensa/sim.c
> @@ -41,21 +41,21 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
> const char *name)
> {
> unsigned i;
> - char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
> + GString *num_name = g_string_new(NULL);
>
> for (i = 0; i < memory->num; ++i) {
> MemoryRegion *m;
>
> - sprintf(num_name, "%s%u", name, i);
> - m = g_malloc(sizeof(*m));
> - memory_region_init_ram(m, NULL, num_name,
> + g_string_printf(num_name, "%s%u", name, i);
I'd make that
num_name = g_strdup_printf("%s%u, name, i);
for less churn. Have to free(num_name) in the loop, of course.
> + m = g_new(MemoryRegion, 1);
> + memory_region_init_ram(m, NULL, num_name->str,
> memory->location[i].size,
> &error_fatal);
> vmstate_register_ram_global(m);
> memory_region_add_subregion(get_system_memory(),
> memory->location[i].addr, m);
> }
> - free(num_name);
> + g_string_free(num_name, true);
> }
>
> static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new
2017-05-08 18:00 [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new Max Filippov
2017-05-08 18:57 ` Philippe Mathieu-Daudé
2017-05-09 8:00 ` Markus Armbruster
@ 2017-05-23 14:53 ` Michael Tokarev
2 siblings, 0 replies; 4+ messages in thread
From: Michael Tokarev @ 2017-05-23 14:53 UTC (permalink / raw)
To: Max Filippov, qemu-devel; +Cc: QEMU Trivial, Paolo Bonzini, Alex Bennée
Applied to -trivial, thanks!
/mjt
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-05-23 14:53 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-08 18:00 [Qemu-devel] [PATCH] hw/xtensa: sim: use g_string/g_new Max Filippov
2017-05-08 18:57 ` Philippe Mathieu-Daudé
2017-05-09 8:00 ` Markus Armbruster
2017-05-23 14:53 ` Michael Tokarev
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.