[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 02/17] block: Use a GString in bdrv_perm_names()
From: |
Max Reitz |
Subject: |
[PULL 02/17] block: Use a GString in bdrv_perm_names() |
Date: |
Thu, 6 Feb 2020 13:51:17 +0100 |
From: Alberto Garcia <address@hidden>
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 <address@hidden>
Message-id: address@hidden
Reviewed-by: Eric Blake <address@hidden>
Signed-off-by: Max Reitz <address@hidden>
---
block.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/block.c b/block.c
index 6c2b2bd2e2..fe5050c53f 100644
--- a/block.c
+++ b/block.c
@@ -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);
}
/*
--
2.24.1
- [PULL 00/17] Block patches, Max Reitz, 2020/02/06
- [PULL 01/17] qcow2: Assert that host cluster offsets fit in L2 table entries, Max Reitz, 2020/02/06
- [PULL 02/17] block: Use a GString in bdrv_perm_names(),
Max Reitz <=
- [PULL 03/17] block: fix memleaks in bdrv_refresh_filename, Max Reitz, 2020/02/06
- [PULL 04/17] qcow2: Use a GString in report_unsupported_feature(), Max Reitz, 2020/02/06
- [PULL 05/17] iotests: remove 'linux' from default supported platforms, Max Reitz, 2020/02/06
- [PULL 06/17] iotests: Test 041 only works on certain systems, Max Reitz, 2020/02/06
- [PULL 07/17] iotests: Test 183 does not work on macOS and OpenBSD, Max Reitz, 2020/02/06
- [PULL 08/17] iotests: Check for the availability of the required devices in 267 and 127, Max Reitz, 2020/02/06
- [PULL 09/17] iotests: Skip Python-based tests if QEMU does not support virtio-blk, Max Reitz, 2020/02/06
- [PULL 10/17] iotests: Enable more tests in the 'auto' group to improve test coverage, Max Reitz, 2020/02/06
- [PULL 12/17] qcow2: Tighten cluster_offset alignment assertions, Max Reitz, 2020/02/06
- [PULL 11/17] qcow2: Don't round the L1 table allocation up to the sector size, Max Reitz, 2020/02/06