diff mbox

[19/22] migration: convert current_migration from pointer to struct

Message ID 39b44717601123c208083457b8282e3df94c800a.1298421307.git.quintela@redhat.com
State New
Headers show

Commit Message

Juan Quintela Feb. 23, 2011, 12:44 a.m. UTC
This cleans up a lot the code as we don't have to check anymore if
the variable is NULL or not.

Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 migration.c |  119 ++++++++++++++++++++++++----------------------------------
 1 files changed, 49 insertions(+), 70 deletions(-)

Comments

Yoshiaki Tamura Feb. 23, 2011, 8:53 a.m. UTC | #1
2011/2/23 Juan Quintela <quintela@redhat.com>:
> This cleans up a lot the code as we don't have to check anymore if
> the variable is NULL or not.
>
> Signed-off-by: Juan Quintela <quintela@redhat.com>
> ---
>  migration.c |  119 ++++++++++++++++++++++++----------------------------------
>  1 files changed, 49 insertions(+), 70 deletions(-)
>
> diff --git a/migration.c b/migration.c
> index 4014330..7b1e679 100644
> --- a/migration.c
> +++ b/migration.c
> @@ -34,7 +34,9 @@
>  /* Migration speed throttling */
>  static int64_t max_throttle = (32 << 20);
>
> -static MigrationState *current_migration;
> +static MigrationState current_migration = {
> +    .state = MIG_STATE_NONE,
> +};
>
>  static NotifierList migration_state_notifiers =
>     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
> @@ -135,37 +137,34 @@ void do_info_migrate(Monitor *mon, QObject **ret_data)
>  {
>     QDict *qdict;
>
> -    if (current_migration) {
> -
> -        switch (current_migration->state) {
> -        case MIG_STATE_NONE:
> -            /* no migration has happened ever */
> -            break;
> -        case MIG_STATE_ACTIVE:
> -            qdict = qdict_new();
> -            qdict_put(qdict, "status", qstring_from_str("active"));
> -
> -            migrate_put_status(qdict, "ram", ram_bytes_transferred(),
> -                               ram_bytes_remaining(), ram_bytes_total());
> -
> -            if (blk_mig_active()) {
> -                migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
> -                                   blk_mig_bytes_remaining(),
> -                                   blk_mig_bytes_total());
> -            }
> -
> -            *ret_data = QOBJECT(qdict);
> -            break;
> -        case MIG_STATE_COMPLETED:
> -            *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
> -            break;
> -        case MIG_STATE_ERROR:
> -            *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
> -            break;
> -        case MIG_STATE_CANCELLED:
> -            *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
> -            break;
> +    switch (current_migration.state) {
> +    case MIG_STATE_NONE:
> +        /* no migration has happened ever */
> +        break;
> +    case MIG_STATE_ACTIVE:
> +        qdict = qdict_new();
> +        qdict_put(qdict, "status", qstring_from_str("active"));
> +
> +        migrate_put_status(qdict, "ram", ram_bytes_transferred(),
> +                           ram_bytes_remaining(), ram_bytes_total());
> +
> +        if (blk_mig_active()) {
> +            migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
> +                               blk_mig_bytes_remaining(),
> +                               blk_mig_bytes_total());
>         }
> +
> +        *ret_data = QOBJECT(qdict);
> +        break;
> +    case MIG_STATE_COMPLETED:
> +        *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
> +        break;
> +    case MIG_STATE_ERROR:
> +        *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
> +        break;
> +    case MIG_STATE_CANCELLED:
> +        *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
> +        break;
>     }
>  }
>
> @@ -339,11 +338,7 @@ void remove_migration_state_change_notifier(Notifier *notify)
>
>  int get_migration_state(void)
>  {
> -    if (current_migration) {
> -        return current_migration->state;
> -    } else {
> -        return MIG_STATE_ERROR;
> -    }
> +    return current_migration.state;
>  }
>
>  void migrate_fd_connect(MigrationState *s)
> @@ -369,27 +364,22 @@ void migrate_fd_connect(MigrationState *s)
>     migrate_fd_put_ready(s);
>  }
>
> -static MigrationState *migrate_create_state(Monitor *mon, int64_t bandwidth_limit,
> -                                            int detach, int blk, int inc)
> +static void migrate_init_state(Monitor *mon, int64_t bandwidth_limit,
> +                               int detach, int blk, int inc)
>  {
> -    MigrationState *s = qemu_mallocz(sizeof(*s));
> -
> -    s->blk = blk;
> -    s->shared = inc;
> -    s->mon = NULL;
> -    s->bandwidth_limit = bandwidth_limit;
> -    s->state = MIG_STATE_NONE;
> +    current_migration.blk = blk;
> +    current_migration.shared = inc;
> +    current_migration.mon = NULL;
> +    current_migration.bandwidth_limit = bandwidth_limit;
> +    current_migration.state = MIG_STATE_NONE;
>
>     if (!detach) {
> -        migrate_fd_monitor_suspend(s, mon);
> +        migrate_fd_monitor_suspend(&current_migration, mon);
>     }
> -
> -    return s;
>  }
>
>  int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
>  {
> -    MigrationState *s = NULL;
>     const char *p;
>     int detach = qdict_get_try_bool(qdict, "detach", 0);
>     int blk = qdict_get_try_bool(qdict, "blk", 0);
> @@ -397,8 +387,7 @@ int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
>     const char *uri = qdict_get_str(qdict, "uri");
>     int ret;
>
> -    if (current_migration &&
> -        current_migration->state == MIG_STATE_ACTIVE) {
> +    if (current_migration.state == MIG_STATE_ACTIVE) {
>         monitor_printf(mon, "migration already in progress\n");
>         return -1;
>     }
> @@ -407,42 +396,35 @@ int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
>         return -1;
>     }
>
> -    s = migrate_create_state(mon, max_throttle, detach, blk, inc);
> +    migrate_init_state(mon, max_throttle, detach, blk, inc);
>
>     if (strstart(uri, "tcp:", &p)) {
> -        ret = tcp_start_outgoing_migration(s, p);
> +        ret = tcp_start_outgoing_migration(&current_migration, p);
>  #if !defined(WIN32)
>     } else if (strstart(uri, "exec:", &p)) {
> -        ret = exec_start_outgoing_migration(s, p);
> +        ret = exec_start_outgoing_migration(&current_migration, p);
>     } else if (strstart(uri, "unix:", &p)) {
> -        ret = unix_start_outgoing_migration(s, p);
> +        ret = unix_start_outgoing_migration(&current_migration, p);
>     } else if (strstart(uri, "fd:", &p)) {
> -        ret = fd_start_outgoing_migration(s, p);
> +        ret = fd_start_outgoing_migration(&current_migration, p);
>  #endif
>     } else {
>         monitor_printf(mon, "unknown migration protocol: %s\n", uri);
> -        ret  = -EINVAL;
> -        goto free_migrate_state;
> +        return -EINVAL;
>     }
>
>     if (ret < 0) {
>         monitor_printf(mon, "migration failed\n");
> -        goto free_migrate_state;
> +        return ret;
>     }
>
> -    qemu_free(current_migration);
> -    current_migration = s;
>     notifier_list_notify(&migration_state_notifiers);
>     return 0;
> -free_migrate_state:
> -    qemu_free(s);
> -    return -1;
>  }
>
>  int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
>  {
> -    if (current_migration)
> -        migrate_fd_cancel(current_migration);
> +    migrate_fd_cancel(&current_migration);
>
>     return 0;
>  }
> @@ -457,10 +439,7 @@ int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
>     }
>     max_throttle = d;
>
> -    if (current_migration) {
> -        qemu_file_set_rate_limit(current_migration->file, max_throttle);
> -    }
> -
> +    qemu_file_set_rate_limit(current_migration.file, max_throttle);
>     return 0;
>  }
>

Looks good to me.

Yoshi

> --
> 1.7.4
>
>
>
diff mbox

Patch

diff --git a/migration.c b/migration.c
index 4014330..7b1e679 100644
--- a/migration.c
+++ b/migration.c
@@ -34,7 +34,9 @@ 
 /* Migration speed throttling */
 static int64_t max_throttle = (32 << 20);

-static MigrationState *current_migration;
+static MigrationState current_migration = {
+    .state = MIG_STATE_NONE,
+};

 static NotifierList migration_state_notifiers =
     NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
@@ -135,37 +137,34 @@  void do_info_migrate(Monitor *mon, QObject **ret_data)
 {
     QDict *qdict;

-    if (current_migration) {
-
-        switch (current_migration->state) {
-        case MIG_STATE_NONE:
-            /* no migration has happened ever */
-            break;
-        case MIG_STATE_ACTIVE:
-            qdict = qdict_new();
-            qdict_put(qdict, "status", qstring_from_str("active"));
-
-            migrate_put_status(qdict, "ram", ram_bytes_transferred(),
-                               ram_bytes_remaining(), ram_bytes_total());
-
-            if (blk_mig_active()) {
-                migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
-                                   blk_mig_bytes_remaining(),
-                                   blk_mig_bytes_total());
-            }
-
-            *ret_data = QOBJECT(qdict);
-            break;
-        case MIG_STATE_COMPLETED:
-            *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
-            break;
-        case MIG_STATE_ERROR:
-            *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
-            break;
-        case MIG_STATE_CANCELLED:
-            *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
-            break;
+    switch (current_migration.state) {
+    case MIG_STATE_NONE:
+        /* no migration has happened ever */
+        break;
+    case MIG_STATE_ACTIVE:
+        qdict = qdict_new();
+        qdict_put(qdict, "status", qstring_from_str("active"));
+
+        migrate_put_status(qdict, "ram", ram_bytes_transferred(),
+                           ram_bytes_remaining(), ram_bytes_total());
+
+        if (blk_mig_active()) {
+            migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
+                               blk_mig_bytes_remaining(),
+                               blk_mig_bytes_total());
         }
+
+        *ret_data = QOBJECT(qdict);
+        break;
+    case MIG_STATE_COMPLETED:
+        *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
+        break;
+    case MIG_STATE_ERROR:
+        *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
+        break;
+    case MIG_STATE_CANCELLED:
+        *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
+        break;
     }
 }

@@ -339,11 +338,7 @@  void remove_migration_state_change_notifier(Notifier *notify)

 int get_migration_state(void)
 {
-    if (current_migration) {
-        return current_migration->state;
-    } else {
-        return MIG_STATE_ERROR;
-    }
+    return current_migration.state;
 }

 void migrate_fd_connect(MigrationState *s)
@@ -369,27 +364,22 @@  void migrate_fd_connect(MigrationState *s)
     migrate_fd_put_ready(s);
 }

-static MigrationState *migrate_create_state(Monitor *mon, int64_t bandwidth_limit,
-                                            int detach, int blk, int inc)
+static void migrate_init_state(Monitor *mon, int64_t bandwidth_limit,
+                               int detach, int blk, int inc)
 {
-    MigrationState *s = qemu_mallocz(sizeof(*s));
-
-    s->blk = blk;
-    s->shared = inc;
-    s->mon = NULL;
-    s->bandwidth_limit = bandwidth_limit;
-    s->state = MIG_STATE_NONE;
+    current_migration.blk = blk;
+    current_migration.shared = inc;
+    current_migration.mon = NULL;
+    current_migration.bandwidth_limit = bandwidth_limit;
+    current_migration.state = MIG_STATE_NONE;

     if (!detach) {
-        migrate_fd_monitor_suspend(s, mon);
+        migrate_fd_monitor_suspend(&current_migration, mon);
     }
-
-    return s;
 }

 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
-    MigrationState *s = NULL;
     const char *p;
     int detach = qdict_get_try_bool(qdict, "detach", 0);
     int blk = qdict_get_try_bool(qdict, "blk", 0);
@@ -397,8 +387,7 @@  int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
     const char *uri = qdict_get_str(qdict, "uri");
     int ret;

-    if (current_migration &&
-        current_migration->state == MIG_STATE_ACTIVE) {
+    if (current_migration.state == MIG_STATE_ACTIVE) {
         monitor_printf(mon, "migration already in progress\n");
         return -1;
     }
@@ -407,42 +396,35 @@  int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
         return -1;
     }

-    s = migrate_create_state(mon, max_throttle, detach, blk, inc);
+    migrate_init_state(mon, max_throttle, detach, blk, inc);

     if (strstart(uri, "tcp:", &p)) {
-        ret = tcp_start_outgoing_migration(s, p);
+        ret = tcp_start_outgoing_migration(&current_migration, p);
 #if !defined(WIN32)
     } else if (strstart(uri, "exec:", &p)) {
-        ret = exec_start_outgoing_migration(s, p);
+        ret = exec_start_outgoing_migration(&current_migration, p);
     } else if (strstart(uri, "unix:", &p)) {
-        ret = unix_start_outgoing_migration(s, p);
+        ret = unix_start_outgoing_migration(&current_migration, p);
     } else if (strstart(uri, "fd:", &p)) {
-        ret = fd_start_outgoing_migration(s, p);
+        ret = fd_start_outgoing_migration(&current_migration, p);
 #endif
     } else {
         monitor_printf(mon, "unknown migration protocol: %s\n", uri);
-        ret  = -EINVAL;
-        goto free_migrate_state;
+        return -EINVAL;
     }

     if (ret < 0) {
         monitor_printf(mon, "migration failed\n");
-        goto free_migrate_state;
+        return ret;
     }

-    qemu_free(current_migration);
-    current_migration = s;
     notifier_list_notify(&migration_state_notifiers);
     return 0;
-free_migrate_state:
-    qemu_free(s);
-    return -1;
 }

 int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
-    if (current_migration)
-        migrate_fd_cancel(current_migration);
+    migrate_fd_cancel(&current_migration);

     return 0;
 }
@@ -457,10 +439,7 @@  int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
     }
     max_throttle = d;

-    if (current_migration) {
-        qemu_file_set_rate_limit(current_migration->file, max_throttle);
-    }
-
+    qemu_file_set_rate_limit(current_migration.file, max_throttle);
     return 0;
 }