Patchwork [RFC,3/5] hw/boards: converted current_machine to be an instance of QemuMachineCLass

login
register
mail settings
Submitter Marcel Apfelbaum
Date Jan. 30, 2014, 2:47 p.m.
Message ID <1391093245-14277-4-git-send-email-marcel.a@redhat.com>
Download mbox | patch
Permalink /patch/315379/
State New
Headers show

Comments

Marcel Apfelbaum - Jan. 30, 2014, 2:47 p.m.
In order to allow attaching machine options to a machine instance,
current_machine is converted into QemuMachineState.
As a first step of deprecating QEMUMachine, some of the functions
were modified to return QemuMachineCLass.

Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
---
 device-hotplug.c    |  4 +++-
 include/hw/boards.h |  6 +++---
 qmp.c               |  7 +++++--
 vl.c                | 55 +++++++++++++++++++++++++++++------------------------
 4 files changed, 41 insertions(+), 31 deletions(-)

Patch

diff --git a/device-hotplug.c b/device-hotplug.c
index 103d34a..fb5eb01 100644
--- a/device-hotplug.c
+++ b/device-hotplug.c
@@ -33,12 +33,14 @@  DriveInfo *add_init_drive(const char *optstr)
 {
     DriveInfo *dinfo;
     QemuOpts *opts;
+    QemuMachineClass *machine_class;
 
     opts = drive_def(optstr);
     if (!opts)
         return NULL;
 
-    dinfo = drive_init(opts, current_machine->block_default_type);
+    machine_class = QEMU_MACHINE_GET_CLASS(current_machine);
+    dinfo = drive_init(opts, machine_class->qemu_machine->block_default_type);
     if (!dinfo) {
         qemu_opts_del(opts);
         return NULL;
diff --git a/include/hw/boards.h b/include/hw/boards.h
index 682cd7f..3cd48fe 100644
--- a/include/hw/boards.h
+++ b/include/hw/boards.h
@@ -50,9 +50,6 @@  struct QEMUMachine {
 };
 
 int qemu_register_machine(QEMUMachine *m);
-QEMUMachine *find_default_machine(void);
-
-extern QEMUMachine *current_machine;
 
 #define TYPE_QEMU_MACHINE "machine"
 #define QEMU_MACHINE(obj) \
@@ -65,6 +62,9 @@  extern QEMUMachine *current_machine;
 typedef struct QemuMachineState QemuMachineState;
 typedef struct QemuMachineClass QemuMachineClass;
 
+QemuMachineClass *find_default_machine(void);
+extern QemuMachineState *current_machine;
+
 /**
  * @QemuMachineClass
  *
diff --git a/qmp.c b/qmp.c
index 0f46171..a88694f 100644
--- a/qmp.c
+++ b/qmp.c
@@ -113,8 +113,11 @@  void qmp_cpu(int64_t index, Error **errp)
 
 void qmp_cpu_add(int64_t id, Error **errp)
 {
-    if (current_machine->hot_add_cpu) {
-        current_machine->hot_add_cpu(id, errp);
+    QemuMachineClass *machine_class;
+
+    machine_class = QEMU_MACHINE_GET_CLASS(current_machine);
+    if (machine_class->qemu_machine->hot_add_cpu) {
+        machine_class->qemu_machine->hot_add_cpu(id, errp);
     } else {
         error_setg(errp, "Not supported");
     }
diff --git a/vl.c b/vl.c
index 9c7f599..4f4722e 100644
--- a/vl.c
+++ b/vl.c
@@ -1581,7 +1581,7 @@  void pcmcia_info(Monitor *mon, const QDict *qdict)
 /***********************************************************/
 /* machine registration */
 
-QEMUMachine *current_machine = NULL;
+QemuMachineState *current_machine;
 
 static void qemu_machine_class_init(ObjectClass *klass, void *data)
 {
@@ -1604,44 +1604,41 @@  int qemu_register_machine(QEMUMachine *m)
     return 0;
 }
 
-static QEMUMachine *find_machine(const char *name)
+static QemuMachineClass *find_machine(const char *name)
 {
     GSList *el, *machines = object_class_get_list(TYPE_QEMU_MACHINE, false);
-    QEMUMachine *m = NULL;
+    QemuMachineClass *k = NULL;
 
     for (el = machines; el; el = el->next) {
-        QemuMachineClass *k = el->data;
+        k = el->data;
 
         if (!strcmp(k->qemu_machine->name, name)) {
-            m = k->qemu_machine;
             break;
         }
         if (k->qemu_machine->alias && !strcmp(k->qemu_machine->alias, name)) {
-            m = k->qemu_machine;
             break;
         }
     }
 
     g_slist_free(machines);
-    return m;
+    return k;
 }
 
-QEMUMachine *find_default_machine(void)
+QemuMachineClass *find_default_machine(void)
 {
     GSList *el, *machines = object_class_get_list(TYPE_QEMU_MACHINE, false);
-    QEMUMachine *m = NULL;
+    QemuMachineClass *k = NULL;
 
     for (el = machines; el; el = el->next) {
-        QemuMachineClass *k = el->data;
+        k = el->data;
 
         if (k->qemu_machine->is_default) {
-            m = k->qemu_machine;
             break;
         }
     }
 
     g_slist_free(machines);
-    return m;
+    return k;
 }
 
 MachineInfoList *qmp_query_machines(Error **errp)
@@ -1870,8 +1867,13 @@  void qemu_devices_reset(void)
 
 void qemu_system_reset(bool report)
 {
-    if (current_machine && current_machine->reset) {
-        current_machine->reset();
+    QemuMachineClass *machine_class;
+
+    machine_class = current_machine ? QEMU_MACHINE_GET_CLASS(current_machine)
+                    : NULL;
+
+    if (machine_class && machine_class->qemu_machine->reset) {
+        machine_class->qemu_machine->reset();
     } else {
         qemu_devices_reset();
     }
@@ -2617,21 +2619,21 @@  static int debugcon_parse(const char *devname)
     return 0;
 }
 
-static QEMUMachine *machine_parse(const char *name)
+static QemuMachineClass *machine_parse(const char *name)
 {
-    QEMUMachine *m, *machine = NULL;
+    QemuMachineClass *machine_class = NULL;
     GSList *el, *machines = object_class_get_list(TYPE_QEMU_MACHINE, false);
 
     if (name) {
-        machine = find_machine(name);
+        machine_class = find_machine(name);
     }
-    if (machine) {
-        return machine;
+    if (machine_class) {
+        return machine_class;
     }
     printf("Supported machines are:\n");
     for (el = machines; el; el = el->next) {
         QemuMachineClass *k = el->data;
-        m = k->qemu_machine;
+        QEMUMachine *m = k->qemu_machine;
         if (m->alias) {
             printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
         }
@@ -2869,6 +2871,7 @@  int main(int argc, char **argv, char **envp)
     int optind;
     const char *optarg;
     const char *loadvm = NULL;
+    QemuMachineClass *machine_class;
     QEMUMachine *machine;
     const char *cpu_model;
     const char *vga_model = "none";
@@ -2941,7 +2944,7 @@  int main(int argc, char **argv, char **envp)
     os_setup_early_signal_handling();
 
     module_call_init(MODULE_INIT_MACHINE);
-    machine = find_default_machine();
+    machine_class = find_default_machine();
     cpu_model = NULL;
     ram_size = 0;
     snapshot = 0;
@@ -3007,7 +3010,7 @@  int main(int argc, char **argv, char **envp)
             }
             switch(popt->index) {
             case QEMU_OPTION_M:
-                machine = machine_parse(optarg);
+                machine_class = machine_parse(optarg);
                 break;
             case QEMU_OPTION_no_kvm_irqchip: {
                 olist = qemu_find_opts("machine");
@@ -3553,7 +3556,7 @@  int main(int argc, char **argv, char **envp)
                 }
                 optarg = qemu_opt_get(opts, "type");
                 if (optarg) {
-                    machine = machine_parse(optarg);
+                    machine_class = machine_parse(optarg);
                 }
                 break;
              case QEMU_OPTION_no_kvm:
@@ -3867,11 +3870,12 @@  int main(int argc, char **argv, char **envp)
     }
 #endif
 
-    if (machine == NULL) {
+    if (machine_class == NULL) {
         fprintf(stderr, "No machine found.\n");
         exit(1);
     }
 
+    machine = machine_class->qemu_machine;
     if (machine->hw_version) {
         qemu_set_version(machine->hw_version);
     }
@@ -4300,7 +4304,8 @@  int main(int argc, char **argv, char **envp)
 
     set_numa_modes();
 
-    current_machine = machine;
+    current_machine = QEMU_MACHINE(object_new(object_class_get_name(
+                          OBJECT_CLASS(machine_class))));
 
     /* init USB devices */
     if (usb_enabled(false)) {