diff mbox series

[14/46] qemu-option: Factor out helper opt_create()

Message ID 20200624164344.3778251-15-armbru@redhat.com
State New
Headers show
Series Less clumsy error checking | expand

Commit Message

Markus Armbruster June 24, 2020, 4:43 p.m. UTC
There is just one use so far.  The next commit will add more.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 util/qemu-option.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

Comments

Eric Blake June 24, 2020, 7:47 p.m. UTC | #1
On 6/24/20 11:43 AM, Markus Armbruster wrote:
> There is just one use so far.  The next commit will add more.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   util/qemu-option.c | 27 ++++++++++++++++++---------
>   1 file changed, 18 insertions(+), 9 deletions(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>
Vladimir Sementsov-Ogievskiy June 29, 2020, 10:09 a.m. UTC | #2
24.06.2020 19:43, Markus Armbruster wrote:
> There is just one use so far.  The next commit will add more.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   util/qemu-option.c | 27 ++++++++++++++++++---------
>   1 file changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/util/qemu-option.c b/util/qemu-option.c
> index d9293814b4..3cdf0c0800 100644
> --- a/util/qemu-option.c
> +++ b/util/qemu-option.c
> @@ -502,6 +502,23 @@ int qemu_opt_unset(QemuOpts *opts, const char *name)
>       }
>   }
>   
> +static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,
> +                           bool prepend)
> +{
> +    QemuOpt *opt = g_malloc0(sizeof(*opt));

I'd prefer g_new0(QemuOpt, 1)

anyway:
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

> +
> +    opt->name = g_strdup(name);
> +    opt->str = value;
> +    opt->opts = opts;
> +    if (prepend) {
> +        QTAILQ_INSERT_HEAD(&opts->head, opt, next);
> +    } else {
> +        QTAILQ_INSERT_TAIL(&opts->head, opt, next);
> +    }
> +
> +    return opt;
> +}
> +
>   static void opt_set(QemuOpts *opts, const char *name, char *value,
>                       bool prepend, bool *help_wanted, Error **errp)
>   {
> @@ -519,16 +536,8 @@ static void opt_set(QemuOpts *opts, const char *name, char *value,
>           return;
>       }
>   
> -    opt = g_malloc0(sizeof(*opt));
> -    opt->name = g_strdup(name);
> -    opt->opts = opts;
> -    if (prepend) {
> -        QTAILQ_INSERT_HEAD(&opts->head, opt, next);
> -    } else {
> -        QTAILQ_INSERT_TAIL(&opts->head, opt, next);
> -    }
> +    opt = opt_create(opts, name, value, prepend);
>       opt->desc = desc;
> -    opt->str = value;
>       qemu_opt_parse(opt, &local_err);
>       if (local_err) {
>           error_propagate(errp, local_err);
>
Markus Armbruster July 1, 2020, 8:13 a.m. UTC | #3
Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> writes:

> 24.06.2020 19:43, Markus Armbruster wrote:
>> There is just one use so far.  The next commit will add more.
>>
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>   util/qemu-option.c | 27 ++++++++++++++++++---------
>>   1 file changed, 18 insertions(+), 9 deletions(-)
>>
>> diff --git a/util/qemu-option.c b/util/qemu-option.c
>> index d9293814b4..3cdf0c0800 100644
>> --- a/util/qemu-option.c
>> +++ b/util/qemu-option.c
>> @@ -502,6 +502,23 @@ int qemu_opt_unset(QemuOpts *opts, const char *name)
>>       }
>>   }
>>   +static QemuOpt *opt_create(QemuOpts *opts, const char *name, char
>> *value,
>> +                           bool prepend)
>> +{
>> +    QemuOpt *opt = g_malloc0(sizeof(*opt));
>
> I'd prefer g_new0(QemuOpt, 1)

I generally prefer g_new0() over g_malloc0(), too.  But the pattern

    lhs = g_malloc0(sizeof(*lhs))

is fine with me, provided sizeof(*lhs) is at least as readable as the
type of *lhs.  Looks like a wash here, so I'm refraining from messing
with the moved code.

> anyway:
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

Thanks!
diff mbox series

Patch

diff --git a/util/qemu-option.c b/util/qemu-option.c
index d9293814b4..3cdf0c0800 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -502,6 +502,23 @@  int qemu_opt_unset(QemuOpts *opts, const char *name)
     }
 }
 
+static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,
+                           bool prepend)
+{
+    QemuOpt *opt = g_malloc0(sizeof(*opt));
+
+    opt->name = g_strdup(name);
+    opt->str = value;
+    opt->opts = opts;
+    if (prepend) {
+        QTAILQ_INSERT_HEAD(&opts->head, opt, next);
+    } else {
+        QTAILQ_INSERT_TAIL(&opts->head, opt, next);
+    }
+
+    return opt;
+}
+
 static void opt_set(QemuOpts *opts, const char *name, char *value,
                     bool prepend, bool *help_wanted, Error **errp)
 {
@@ -519,16 +536,8 @@  static void opt_set(QemuOpts *opts, const char *name, char *value,
         return;
     }
 
-    opt = g_malloc0(sizeof(*opt));
-    opt->name = g_strdup(name);
-    opt->opts = opts;
-    if (prepend) {
-        QTAILQ_INSERT_HEAD(&opts->head, opt, next);
-    } else {
-        QTAILQ_INSERT_TAIL(&opts->head, opt, next);
-    }
+    opt = opt_create(opts, name, value, prepend);
     opt->desc = desc;
-    opt->str = value;
     qemu_opt_parse(opt, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);