QEMU-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] qcow2: Use a GString in report_unsupported_feature()
@ 2020-01-14 14:54 Alberto Garcia
  2020-01-14 18:08 ` Alex Bennée
  0 siblings, 1 reply; 6+ messages in thread
From: Alberto Garcia @ 2020-01-14 14:54 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Philippe Mathieu-Daudé,
	Alberto Garcia, qemu-block, Max Reitz

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é <philmd@redhat.com>
Signed-off-by: Alberto Garcia <berto@igalia.com>
---
 block/qcow2.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index cef9d72b3a..ecf6827420 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -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);
 }
 
 /*
-- 
2.20.1



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qcow2: Use a GString in report_unsupported_feature()
  2020-01-14 14:54 [PATCH] qcow2: Use a GString in report_unsupported_feature() Alberto Garcia
@ 2020-01-14 18:08 ` Alex Bennée
  2020-01-14 18:43   ` Philippe Mathieu-Daudé
  2020-01-15 13:49   ` Alberto Garcia
  0 siblings, 2 replies; 6+ messages in thread
From: Alex Bennée @ 2020-01-14 18:08 UTC (permalink / raw)
  To: Alberto Garcia
  Cc: Kevin Wolf, Philippe Mathieu-Daudé,
	qemu-devel, qemu-block, Max Reitz


Alberto Garcia <berto@igalia.com> writes:

> 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é <philmd@redhat.com>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>  block/qcow2.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/block/qcow2.c b/block/qcow2.c
> index cef9d72b3a..ecf6827420 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -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.

>  
>      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);

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:

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

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


-- 
Alex Bennée


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qcow2: Use a GString in report_unsupported_feature()
  2020-01-14 18:08 ` Alex Bennée
@ 2020-01-14 18:43   ` Philippe Mathieu-Daudé
  2020-01-14 21:35     ` Alex Bennée
  2020-01-15 13:49   ` Alberto Garcia
  1 sibling, 1 reply; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-01-14 18:43 UTC (permalink / raw)
  To: Alex Bennée, Alberto Garcia
  Cc: Kevin Wolf, qemu-devel, qemu-block, Max Reitz

On 1/14/20 7:08 PM, Alex Bennée wrote:
> Alberto Garcia <berto@igalia.com> writes:
> 
>> 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é <philmd@redhat.com>
>> Signed-off-by: Alberto Garcia <berto@igalia.com>
>> ---
>>   block/qcow2.c | 24 ++++++++++++------------
>>   1 file changed, 12 insertions(+), 12 deletions(-)
>>
>> diff --git a/block/qcow2.c b/block/qcow2.c
>> index cef9d72b3a..ecf6827420 100644
>> --- a/block/qcow2.c
>> +++ b/block/qcow2.c
>> @@ -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.

Does this work with g_string_free() too?

>>   
>>       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);
> 
> 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

> Anyway not mandatory for this patch so with the autoptr fix:
> 
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> 
>>                   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);
>>   }
>>   
>>   /*
> 
> 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qcow2: Use a GString in report_unsupported_feature()
  2020-01-14 18:43   ` Philippe Mathieu-Daudé
@ 2020-01-14 21:35     ` Alex Bennée
  2020-01-15 14:23       ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 6+ messages in thread
From: Alex Bennée @ 2020-01-14 21:35 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Kevin Wolf, Alberto Garcia, qemu-devel, qemu-block, Max Reitz


Philippe Mathieu-Daudé <philmd@redhat.com> writes:

> On 1/14/20 7:08 PM, Alex Bennée wrote:
>> Alberto Garcia <berto@igalia.com> writes:
>> 
>>> 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é <philmd@redhat.com>
>>> Signed-off-by: Alberto Garcia <berto@igalia.com>
>>> ---
>>>   block/qcow2.c | 24 ++++++++++++------------
>>>   1 file changed, 12 insertions(+), 12 deletions(-)
>>>
>>> diff --git a/block/qcow2.c b/block/qcow2.c
>>> index cef9d72b3a..ecf6827420 100644
>>> --- a/block/qcow2.c
>>> +++ b/block/qcow2.c
>>> @@ -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.
>
> 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.

>
>>>         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);
>> 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
>
>> Anyway not mandatory for this patch so with the autoptr fix:
>> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>> 
>>>                   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);
>>>   }
>>>     /*
>> 


-- 
Alex Bennée


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qcow2: Use a GString in report_unsupported_feature()
  2020-01-14 18:08 ` Alex Bennée
  2020-01-14 18:43   ` Philippe Mathieu-Daudé
@ 2020-01-15 13:49   ` Alberto Garcia
  1 sibling, 0 replies; 6+ messages in thread
From: Alberto Garcia @ 2020-01-15 13:49 UTC (permalink / raw)
  To: Alex Bennée
  Cc: Kevin Wolf, Philippe Mathieu-Daudé,
	qemu-devel, qemu-block, Max Reitz

On Tue 14 Jan 2020 07:08:04 PM CET, Alex Bennée wrote:
>        g_autoptr(GString) features = g_string_sized_new(60);
>
> will save you the clean-up later.

Ok, will send v2 now.

>> +                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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] qcow2: Use a GString in report_unsupported_feature()
  2020-01-14 21:35     ` Alex Bennée
@ 2020-01-15 14:23       ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-01-15 14:23 UTC (permalink / raw)
  To: Alex Bennée
  Cc: Kevin Wolf, Alberto Garcia, qemu-devel, qemu-block, Max Reitz

On 1/14/20 10:35 PM, Alex Bennée wrote:
> Philippe Mathieu-Daudé <philmd@redhat.com> writes:
> 
>> On 1/14/20 7:08 PM, Alex Bennée wrote:
>>> Alberto Garcia <berto@igalia.com> writes:
>>>
>>>> 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é <philmd@redhat.com>
>>>> Signed-off-by: Alberto Garcia <berto@igalia.com>
>>>> ---
>>>>    block/qcow2.c | 24 ++++++++++++------------
>>>>    1 file changed, 12 insertions(+), 12 deletions(-)
>>>>
>>>> diff --git a/block/qcow2.c b/block/qcow2.c
>>>> index cef9d72b3a..ecf6827420 100644
>>>> --- a/block/qcow2.c
>>>> +++ b/block/qcow2.c
>>>> @@ -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.
>>
>> 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);
> }

The implicit use of free_segment=TRUE was not obvious to me.
Thanks for checking it in glib source.

> 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/qemu-devel@nongnu.org/msg669862.html

In this case we can not use the g_autoptr feature.

>>>>          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);
>>> 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
>>
>>> Anyway not mandatory for this patch so with the autoptr fix:
>>> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>>>
>>>>                    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);
>>>>    }
>>>>      /*
>>>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-14 14:54 [PATCH] qcow2: Use a GString in report_unsupported_feature() Alberto Garcia
2020-01-14 18:08 ` Alex Bennée
2020-01-14 18:43   ` Philippe Mathieu-Daudé
2020-01-14 21:35     ` Alex Bennée
2020-01-15 14:23       ` Philippe Mathieu-Daudé
2020-01-15 13:49   ` Alberto Garcia

QEMU-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/qemu-devel/0 qemu-devel/git/0.git
	git clone --mirror https://lore.kernel.org/qemu-devel/1 qemu-devel/git/1.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 qemu-devel qemu-devel/ https://lore.kernel.org/qemu-devel \
		qemu-devel@nongnu.org
	public-inbox-index qemu-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.nongnu.qemu-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git