diff mbox series

block: Use a GString in bdrv_perm_names()

Message ID 20200110171518.22168-1-berto@igalia.com (mailing list archive)
State New, archived
Headers show
Series block: Use a GString in bdrv_perm_names() | expand

Commit Message

Alberto Garcia Jan. 10, 2020, 5:15 p.m. UTC
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(-)

Comments

Eric Blake Jan. 10, 2020, 7:25 p.m. UTC | #1
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>
Max Reitz Jan. 14, 2020, 12:11 p.m. UTC | #2
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
Philippe Mathieu-Daudé Jan. 14, 2020, 12:50 p.m. UTC | #3
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.
diff mbox series

Patch

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);
 }
 
 /*