qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v21 03/25] improve some functions in qemu-option.c


From: Chunyan Liu
Subject: [Qemu-devel] [PATCH v21 03/25] improve some functions in qemu-option.c
Date: Fri, 21 Feb 2014 18:35:26 +0800

Improve opt_get and opt_set group of functions. For opt_get, check and handle
NULL input; for opt_set, when set to an existing option, rewrite the option
with new value.

Signed-off-by: Dong Xu Wang <address@hidden>
Signed-off-by: Chunyan Liu <address@hidden>
---
changes to v20:
  * fix Eric's comments
    - change QemuOpt name and str to (char *)
    - delete Opts == NULL check which is added in v20

 include/qemu/option_int.h |    4 +-
 util/qemu-option.c        |   81 +++++++++++++++++++++++++++++++++++++--------
 2 files changed, 69 insertions(+), 16 deletions(-)

diff --git a/include/qemu/option_int.h b/include/qemu/option_int.h
index 8212fa4..db9ed91 100644
--- a/include/qemu/option_int.h
+++ b/include/qemu/option_int.h
@@ -30,8 +30,8 @@
 #include "qemu/error-report.h"
 
 struct QemuOpt {
-    const char   *name;
-    const char   *str;
+    char   *name;
+    char   *str;
 
     const QemuOptDesc *desc;
     union {
diff --git a/util/qemu-option.c b/util/qemu-option.c
index edd4b55..b2d1a62 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -509,8 +509,13 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char 
*name)
 
 const char *qemu_opt_get(QemuOpts *opts, const char *name)
 {
-    QemuOpt *opt = qemu_opt_find(opts, name);
+    QemuOpt *opt;
 
+    if (opts == NULL) {
+        return NULL;
+    }
+
+    opt = qemu_opt_find(opts, name);
     if (!opt) {
         const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
         if (desc && desc->def_value_str) {
@@ -534,7 +539,13 @@ bool qemu_opt_has_help_opt(QemuOpts *opts)
 
 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
 {
-    QemuOpt *opt = qemu_opt_find(opts, name);
+    QemuOpt *opt;
+
+    if (opts == NULL) {
+        return defval;
+    }
+
+    opt = qemu_opt_find(opts, name);
 
     if (opt == NULL) {
         const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
@@ -549,7 +560,13 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, 
bool defval)
 
 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
 {
-    QemuOpt *opt = qemu_opt_find(opts, name);
+    QemuOpt *opt;
+
+    if (opts == NULL) {
+        return defval;
+    }
+
+    opt = qemu_opt_find(opts, name);
 
     if (opt == NULL) {
         const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
@@ -565,8 +582,13 @@ uint64_t qemu_opt_get_number(QemuOpts *opts, const char 
*name, uint64_t defval)
 
 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
 {
-    QemuOpt *opt = qemu_opt_find(opts, name);
+    QemuOpt *opt;
 
+    if (opts == NULL) {
+        return defval;
+    }
+
+    opt = qemu_opt_find(opts, name);
     if (opt == NULL) {
         const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
         if (desc && desc->def_value_str) {
@@ -603,6 +625,10 @@ static void qemu_opt_parse(QemuOpt *opt, Error **errp)
 
 static void qemu_opt_del(QemuOpt *opt)
 {
+    if (opt == NULL) {
+        return;
+    }
+
     QTAILQ_REMOVE(&opt->opts->head, opt, next);
     g_free((/* !const */ char*)opt->name);
     g_free((/* !const */ char*)opt->str);
@@ -655,6 +681,13 @@ static void opt_set(QemuOpts *opts, const char *name, 
const char *value,
         return;
     }
 
+    opt = qemu_opt_find(opts, name);
+    if (opt) {
+        g_free((char *)opt->str);
+        opt->str = g_strdup(value);
+        return;
+    }
+
     opt = g_malloc0(sizeof(*opt));
     opt->name = g_strdup(name);
     opt->opts = opts;
@@ -695,16 +728,24 @@ void qemu_opt_set_err(QemuOpts *opts, const char *name, 
const char *value,
 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
 {
     QemuOpt *opt;
-    const QemuOptDesc *desc = opts->list->desc;
+    const QemuOptDesc *desc;
 
-    opt = g_malloc0(sizeof(*opt));
-    opt->desc = find_desc_by_name(desc, name);
-    if (!opt->desc && !opts_accepts_any(opts)) {
+    desc = find_desc_by_name(opts->list->desc, name);
+    if (!desc && !opts_accepts_any(opts)) {
         qerror_report(QERR_INVALID_PARAMETER, name);
-        g_free(opt);
         return -1;
     }
 
+    opt = qemu_opt_find(opts, name);
+    if (opt) {
+        g_free((char *)opt->str);
+        opt->value.boolean = val;
+        opt->str = g_strdup(val ? "on" : "off");
+        return 0;
+    }
+
+    opt = g_malloc0(sizeof(*opt));
+    opt->desc = desc;
     opt->name = g_strdup(name);
     opt->opts = opts;
     opt->value.boolean = !!val;
@@ -717,16 +758,24 @@ int qemu_opt_set_bool(QemuOpts *opts, const char *name, 
bool val)
 int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)
 {
     QemuOpt *opt;
-    const QemuOptDesc *desc = opts->list->desc;
+    const QemuOptDesc *desc;
 
-    opt = g_malloc0(sizeof(*opt));
-    opt->desc = find_desc_by_name(desc, name);
-    if (!opt->desc && !opts_accepts_any(opts)) {
+    desc = find_desc_by_name(opts->list->desc, name);
+    if (!desc && !opts_accepts_any(opts)) {
         qerror_report(QERR_INVALID_PARAMETER, name);
-        g_free(opt);
         return -1;
     }
 
+    opt = qemu_opt_find(opts, name);
+    if (opt) {
+        g_free((char *)opt->str);
+        opt->value.uint = val;
+        opt->str = g_strdup_printf("%" PRId64, val);
+        return 0;
+    }
+
+    opt = g_malloc0(sizeof(*opt));
+    opt->desc = desc;
     opt->name = g_strdup(name);
     opt->opts = opts;
     opt->value.uint = val;
@@ -861,6 +910,10 @@ void qemu_opts_del(QemuOpts *opts)
 {
     QemuOpt *opt;
 
+    if (opts == NULL) {
+        return;
+    }
+
     for (;;) {
         opt = QTAILQ_FIRST(&opts->head);
         if (opt == NULL)
-- 
1.6.0.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]