qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: Use a GString in bdrv_perm_names()
@ 2020-01-10 17:15 Alberto Garcia
  2020-01-10 19:25 ` Eric Blake
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alberto Garcia @ 2020-01-10 17:15 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, Alberto Garcia, qemu-block, Max Reitz

This is a bit more efficient than having to allocate and free memory
for each new permission.

The default size (30) is enough for "consistent read, write, resize".

Signed-off-by: Alberto Garcia <berto@igalia.com>
---
 block.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/block.c b/block.c
index 1b6f7c86e8..2bc9e58392 100644
--- a/block.c
+++ b/block.c
@@ -1976,18 +1976,19 @@ char *bdrv_perm_names(uint64_t perm)
         { 0, NULL }
     };
 
-    char *result = g_strdup("");
+    GString *result = g_string_sized_new(30);
     struct perm_name *p;
 
     for (p = permissions; p->name; p++) {
         if (perm & p->perm) {
-            char *old = result;
-            result = g_strdup_printf("%s%s%s", old, *old ? ", " : "", p->name);
-            g_free(old);
+            if (result->len > 0) {
+                g_string_append(result, ", ");
+            }
+            g_string_append(result, p->name);
         }
     }
 
-    return result;
+    return g_string_free(result, FALSE);
 }
 
 /*
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] block: Use a GString in bdrv_perm_names()
  2020-01-10 17:15 [PATCH] block: Use a GString in bdrv_perm_names() Alberto Garcia
@ 2020-01-10 19:25 ` Eric Blake
  2020-01-14 12:11 ` Max Reitz
  2020-01-14 12:50 ` Philippe Mathieu-Daudé
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Blake @ 2020-01-10 19:25 UTC (permalink / raw)
  To: Alberto Garcia, qemu-devel; +Cc: Kevin Wolf, qemu-block, Max Reitz

On 1/10/20 11:15 AM, Alberto Garcia wrote:
> This is a bit more efficient than having to allocate and free memory
> for each new permission.
> 
> The default size (30) is enough for "consistent read, write, resize".
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>   block.c | 11 ++++++-----
>   1 file changed, 6 insertions(+), 5 deletions(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] block: Use a GString in bdrv_perm_names()
  2020-01-10 17:15 [PATCH] block: Use a GString in bdrv_perm_names() Alberto Garcia
  2020-01-10 19:25 ` Eric Blake
@ 2020-01-14 12:11 ` Max Reitz
  2020-01-14 12:50 ` Philippe Mathieu-Daudé
  2 siblings, 0 replies; 4+ messages in thread
From: Max Reitz @ 2020-01-14 12:11 UTC (permalink / raw)
  To: Alberto Garcia, qemu-devel; +Cc: Kevin Wolf, qemu-block


[-- Attachment #1.1: Type: text/plain, Size: 472 bytes --]

On 10.01.20 18:15, Alberto Garcia wrote:
> This is a bit more efficient than having to allocate and free memory
> for each new permission.
> 
> The default size (30) is enough for "consistent read, write, resize".
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>  block.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)

Thanks, applied to my block branch:

https://git.xanclic.moe/XanClic/qemu/commits/branch/block

Max


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] block: Use a GString in bdrv_perm_names()
  2020-01-10 17:15 [PATCH] block: Use a GString in bdrv_perm_names() Alberto Garcia
  2020-01-10 19:25 ` Eric Blake
  2020-01-14 12:11 ` Max Reitz
@ 2020-01-14 12:50 ` Philippe Mathieu-Daudé
  2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-01-14 12:50 UTC (permalink / raw)
  To: Alberto Garcia, qemu-devel; +Cc: Kevin Wolf, qemu-block, Max Reitz

On 1/10/20 6:15 PM, Alberto Garcia wrote:
> This is a bit more efficient than having to allocate and free memory
> for each new permission.
> 
> The default size (30) is enough for "consistent read, write, resize".
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>   block.c | 11 ++++++-----
>   1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/block.c b/block.c
> index 1b6f7c86e8..2bc9e58392 100644
> --- a/block.c
> +++ b/block.c
> @@ -1976,18 +1976,19 @@ char *bdrv_perm_names(uint64_t perm)
>           { 0, NULL }
>       };
>   
> -    char *result = g_strdup("");
> +    GString *result = g_string_sized_new(30);
>       struct perm_name *p;
>   
>       for (p = permissions; p->name; p++) {
>           if (perm & p->perm) {
> -            char *old = result;
> -            result = g_strdup_printf("%s%s%s", old, *old ? ", " : "", p->name);
> -            g_free(old);
> +            if (result->len > 0) {
> +                g_string_append(result, ", ");
> +            }
> +            g_string_append(result, p->name);
>           }
>       }
>   
> -    return result;
> +    return g_string_free(result, FALSE);
>   }

Maybe similar cleanup can be done to report_unsupported_feature() in 
block/qcow2.c.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-01-14 12:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-10 17:15 [PATCH] block: Use a GString in bdrv_perm_names() Alberto Garcia
2020-01-10 19:25 ` Eric Blake
2020-01-14 12:11 ` Max Reitz
2020-01-14 12:50 ` Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).