提交 e2a7423a 编写于 作者: A Alberto Garcia 提交者: Max Reitz

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: NAlberto Garcia <berto@igalia.com>
Message-id: 20200110171518.22168-1-berto@igalia.com
Reviewed-by: NEric Blake <eblake@redhat.com>
Signed-off-by: NMax Reitz <mreitz@redhat.com>
上级 3a75a870
......@@ -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);
}
/*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册