qcow2: Use a GString in report_unsupported_feature()
PATCH 1/1 qcow2: Use a GString in report_unsupported_feature()
This is a bit more efficient than having to allocate and free memory for each item.
The default size (60) is enough for all the existing incompatible features.
--- block/qcow2.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
block/qcow2.c +12 -12
@@ -453,16 +453,15 @@ static void cleanup_unknown_header_ext(BlockDriverState *bs)static void report_unsupported_feature (Error **errp,Qcow2Feature *table,uint64_t mask) { -char *features =g_strdup ("" ); -char *old; +GString *features =g_string_sized_new (60 );while (table && table->name[0 ] !='\0' ) {if (table->type ==QCOW2_FEAT_TYPE_INCOMPATIBLE ) {if (mask & (1ULL << table->bit)) { - old = features; - features =g_strdup_printf ("%s%s%.46s" , old, *old ?", " :"" , - table->name); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features,"%.46s" , table->name); mask &= ~(1ULL << table->bit); } }
@@ -470,14 +469,15 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table, }if (mask) { - old = features; - features =g_strdup_printf ("%s%sUnknown incompatible feature: %" PRIx64, - old, *old ?", " :"" , mask); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features, +"Unknown incompatible feature: %" PRIx64, mask); } -error_setg (errp,"Unsupported qcow2 feature(s): %s" , features); -g_free (features); +error_setg (errp,"Unsupported qcow2 feature(s): %s" , features->str); +g_string_free (features, TRUE); }/*
Signature · 1 line
2.20.1
Quoted 17 lines · Alberto Garcia
This is a bit more efficient than having to allocate and free memory for each item.
The default size (60) is enough for all the existing incompatible features.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Alberto Garcia <[email protected]>--- block/qcow2.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)block/qcow2.c +0
-0
block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs) · line 456
Quoted 6 lines
block/qcow2.c +1
-2@@ -453,16 +453,15 @@ static void cleanup_unknown_header_ext(BlockDriverState *bs)static void report_unsupported_feature (Error **errp,Qcow2Feature *table,uint64_t mask) { -char *features =g_strdup ("" ); -char *old; +GString *features =g_string_sized_new (60 );
g_autoptr(GString) features = g_string_sized_new(60);
will save you the clean-up later.
block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs)
Quoted 13 lines
block/qcow2.c +4
-4while (table && table->name[0 ] !='\0' ) {if (table->type ==QCOW2_FEAT_TYPE_INCOMPATIBLE ) {if (mask & (1ULL << table->bit)) { - old = features; - features =g_strdup_printf ("%s%s%.46s" , old, *old ?", " :"" , - table->name); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features,"%.46s" , table->name);
We have a number of cases of this sort of idiom in the code base. I wonder if it calls for a utility function:
qemu_append_with_sep(features, ", ", "%.46s", table->name)
Anyway not mandatory for this patch so with the autoptr fix:
Quoted 25 lines
block/qcow2.c +7
-6mask &= ~(1ULL << table->bit); } }@@ -470,14 +469,15 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table, }if (mask) { - old = features; - features =g_strdup_printf ("%s%sUnknown incompatible feature: %" PRIx64, - old, *old ?", " :"" , mask); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features, +"Unknown incompatible feature: %" PRIx64, mask); } -error_setg (errp,"Unsupported qcow2 feature(s): %s" , features); -g_free (features); +error_setg (errp,"Unsupported qcow2 feature(s): %s" , features->str); +g_string_free (features, TRUE); }/*
Signature · 1 line
Alex Bennée
Quoted 28 lines · Alex Bennée
Quoted 17 lines · Alberto Garcia
This is a bit more efficient than having to allocate and free memory for each item.
The default size (60) is enough for all the existing incompatible features.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Alberto Garcia <[email protected]>--- block/qcow2.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)block/qcow2.c +0
-0block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs) · line 456
Quoted 6 lines
block/qcow2.c +1
-2@@ -453,16 +453,15 @@ static void cleanup_unknown_header_ext(BlockDriverState *bs)static void report_unsupported_feature (Error **errp,Qcow2Feature *table,uint64_t mask) { -char *features =g_strdup ("" ); -char *old; +GString *features =g_string_sized_new (60 );g_autoptr(GString) features = g_string_sized_new(60);
Quoted 1 line
will save you the clean-up later.
Does this work with g_string_free() too?
Quoted 14 lines
block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs)
Quoted 13 lines
block/qcow2.c +4
-4while (table && table->name[0 ] !='\0' ) {if (table->type ==QCOW2_FEAT_TYPE_INCOMPATIBLE ) {if (mask & (1ULL << table->bit)) { - old = features; - features =g_strdup_printf ("%s%s%.46s" , old, *old ?", " :"" , - table->name); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features,"%.46s" , table->name);
Quoted 4 lines
We have a number of cases of this sort of idiom in the code base. I wonder if it calls for a utility function:
qemu_append_with_sep(features, ", ", "%.46s", table->name)
Good idea for https://wiki.qemu.org/Contribute/BiteSizedTasks
Quoted 31 lines
Anyway not mandatory for this patch so with the autoptr fix:
Reviewed-by Alex Bennée <[email protected]>Quoted 25 lines
block/qcow2.c +7
-6mask &= ~(1ULL << table->bit); } }@@ -470,14 +469,15 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table, }if (mask) { - old = features; - features =g_strdup_printf ("%s%sUnknown incompatible feature: %" PRIx64, - old, *old ?", " :"" , mask); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features, +"Unknown incompatible feature: %" PRIx64, mask); } -error_setg (errp,"Unsupported qcow2 feature(s): %s" , features); -g_free (features); +error_setg (errp,"Unsupported qcow2 feature(s): %s" , features->str); +g_string_free (features, TRUE); }/*
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 26 lines · Alex Bennée
Quoted 17 lines · Alberto Garcia
This is a bit more efficient than having to allocate and free memory for each item.
The default size (60) is enough for all the existing incompatible features.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Alberto Garcia <[email protected]>--- block/qcow2.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)block/qcow2.c +0
-0block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs) · line 456
Quoted 6 lines
block/qcow2.c +1
-2@@ -453,16 +453,15 @@ static void cleanup_unknown_header_ext(BlockDriverState *bs)static void report_unsupported_feature (Error **errp,Qcow2Feature *table,uint64_t mask) { -char *features =g_strdup ("" ); -char *old; +GString *features =g_string_sized_new (60 );g_autoptr(GString) features = g_string_sized_new(60);Quoted 1 line
will save you the clean-up later.
Quoted 1 line
Does this work with
g_string_free()too?
It does:
static inline void g_autoptr_cleanup_gstring_free (GString *string)
{
if (string)
g_string_free (string, TRUE);
}If you want to keep the allocated string but drop the GString you are still best doing that yourself.
Quoted 46 lines
Quoted 13 lines
block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs)
Quoted 13 lines
block/qcow2.c +4
-4while (table && table->name[0 ] !='\0' ) {if (table->type ==QCOW2_FEAT_TYPE_INCOMPATIBLE ) {if (mask & (1ULL << table->bit)) { - old = features; - features =g_strdup_printf ("%s%s%.46s" , old, *old ?", " :"" , - table->name); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features,"%.46s" , table->name);Quoted 3 lines
We have a number of cases of this sort of idiom in the code base. I wonder if it calls for a utility function:
qemu_append_with_sep(features, ", ", "%.46s", table->name)Good idea for https://wiki.qemu.org/Contribute/BiteSizedTasks
Quoted 27 lines
Anyway not mandatory for this patch so with the autoptr fix:
Reviewed-by Alex Bennée <[email protected]>Quoted 23 lines
block/qcow2.c +7
-5mask &= ~(1ULL << table->bit); } }@@ -470,14 +469,15 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table, }if (mask) { - old = features; - features =g_strdup_printf ("%s%sUnknown incompatible feature: %" PRIx64, - old, *old ?", " :"" , mask); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features, +"Unknown incompatible feature: %" PRIx64, mask); } -error_setg (errp,"Unsupported qcow2 feature(s): %s" , features); -g_free (features); +error_setg (errp,"Unsupported qcow2 feature(s): %s" , features->str); +g_string_free (features, TRUE); }/*
Signature · 1 line
Alex Bennée
Quoted 33 lines · Alex Bennée
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 26 lines · Alex Bennée
Quoted 17 lines · Alberto Garcia
This is a bit more efficient than having to allocate and free memory for each item.
The default size (60) is enough for all the existing incompatible features.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Alberto Garcia <[email protected]>--- block/qcow2.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)block/qcow2.c +0
-0block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs) · line 456
Quoted 6 lines
block/qcow2.c +1
-2@@ -453,16 +453,15 @@ static void cleanup_unknown_header_ext(BlockDriverState *bs)static void report_unsupported_feature (Error **errp,Qcow2Feature *table,uint64_t mask) { -char *features =g_strdup ("" ); -char *old; +GString *features =g_string_sized_new (60 );g_autoptr(GString) features = g_string_sized_new(60);Quoted 1 line
will save you the clean-up later.
Quoted 1 line
Does this work with
g_string_free()too?
Quoted 7 lines
It does:
static inline void
g_autoptr_cleanup_gstring_free(GString *string){ if (string) g_string_free (string, TRUE); }
The implicit use of free_segment=TRUE was not obvious to me. Thanks for checking it in glib source.
Quoted 2 lines
If you want to keep the allocated string but drop the GString you are still best doing that yourself.
I agree. I asked because I had the other patch from Alberto in mind:
https://www.mail-archive.com/[email protected]/msg669862.html
In this case we can not use the g_autoptr feature.
Quoted 45 lines
Quoted 45 lines
Quoted 12 lines
block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs)
Quoted 12 lines
block/qcow2.c +4
-4while (table && table->name[0 ] !='\0' ) {if (table->type ==QCOW2_FEAT_TYPE_INCOMPATIBLE ) {if (mask & (1ULL << table->bit)) { - old = features; - features =g_strdup_printf ("%s%s%.46s" , old, *old ?", " :"" , - table->name); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features,"%.46s" , table->name);Quoted 3 lines
We have a number of cases of this sort of idiom in the code base. I wonder if it calls for a utility function:
qemu_append_with_sep(features, ", ", "%.46s", table->name)Good idea for https://wiki.qemu.org/Contribute/BiteSizedTasks
Quoted 27 lines
Anyway not mandatory for this patch so with the autoptr fix:
Reviewed-by Alex Bennée <[email protected]>Quoted 23 lines
block/qcow2.c +7
-5mask &= ~(1ULL << table->bit); } }@@ -470,14 +469,15 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table, }if (mask) { - old = features; - features =g_strdup_printf ("%s%sUnknown incompatible feature: %" PRIx64, - old, *old ?", " :"" , mask); -g_free (old); +if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features, +"Unknown incompatible feature: %" PRIx64, mask); } -error_setg (errp,"Unsupported qcow2 feature(s): %s" , features); -g_free (features); +error_setg (errp,"Unsupported qcow2 feature(s): %s" , features->str); +g_string_free (features, TRUE); }/*
Quoted 3 lines · Alex Bennée
g_autoptr(GString) features = g_string_sized_new(60);will save you the clean-up later.
Ok, will send v2 now.
Quoted 10 lines
block/qcow2.c static void cleanup_unknown_header_ext(BlockDriverState *bs)
Quoted 5 lines
block/qcow2.c +4
-0+if (features->len >0 ) { +g_string_append (features,", " ); + } +g_string_append_printf (features,"%.46s" , table->name);We have a number of cases of this sort of idiom in the code base. I wonder if it calls for a utility function:
qemu_append_with_sep(features, ", ", "%.46s", table->name)
Maybe it's worth checking. I can have a look once this is applied (there was another similar one in the queue already).
Berto