diff options
author | Alberto Garcia <berto@igalia.com> | 2020-01-10 18:15:18 +0100 |
---|---|---|
committer | Max Reitz <mreitz@redhat.com> | 2020-02-06 13:47:45 +0100 |
commit | e2a7423a119660f33abcbbf4b7827a81ef23872f (patch) | |
tree | 3c1b875b6bf423922b3a8a1c5032011682e4f9ac /block.c | |
parent | 3a75a870ef51f79c2b5f7fc8f7756f9efb500a14 (diff) |
block: Use a GString in bdrv_perm_names()
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>
Message-id: 20200110171518.22168-1-berto@igalia.com
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
Diffstat (limited to 'block.c')
-rw-r--r-- | block.c | 11 |
1 files changed, 6 insertions, 5 deletions
@@ -1998,18 +1998,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); } /* |