diff mbox

[v3,3/7] vl.c: Fix machine registration so QEMUMachine structs can be const

Message ID 1301407704-24541-4-git-send-email-peter.maydell@linaro.org
State New
Headers show

Commit Message

Peter Maydell March 29, 2011, 2:08 p.m. UTC
Reimplement the list of QEMUMachine structures so that we don't keep the
'next' pointer inside the QEMUMachine struct itself. This allows us to
accept a const struct pointer in qemu_register_machine. The few places
in vl.c which were implicitly assuming that QEMUMachine structs were
writable have been updated.

We also take the opportunity to correct the return type of
qemu_register_machine from 'int' to 'void', since it can never fail
and none of its callers check the return value.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 hw/boards.h |    5 +--
 vl.c        |   99 ++++++++++++++++++++++++++++++++++------------------------
 2 files changed, 60 insertions(+), 44 deletions(-)
diff mbox

Patch

diff --git a/hw/boards.h b/hw/boards.h
index 5f41fce..731d8c7 100644
--- a/hw/boards.h
+++ b/hw/boards.h
@@ -29,11 +29,10 @@  typedef struct QEMUMachine {
         no_sdcard:1;
     int is_default;
     GlobalProperty *compat_props;
-    struct QEMUMachine *next;
 } QEMUMachine;
 
-int qemu_register_machine(QEMUMachine *m);
+void qemu_register_machine(const QEMUMachine *m);
 
-extern QEMUMachine *current_machine;
+extern const QEMUMachine *current_machine;
 
 #endif
diff --git a/vl.c b/vl.c
index 69cb29b..d58bbf8 100644
--- a/vl.c
+++ b/vl.c
@@ -1090,45 +1090,62 @@  int qemu_set_fd_handler(int fd,
 /***********************************************************/
 /* machine registration */
 
-static QEMUMachine *first_machine = NULL;
-QEMUMachine *current_machine = NULL;
+typedef struct QEMUMachineListEntry {
+    QTAILQ_ENTRY(QEMUMachineListEntry) entry;
+    const QEMUMachine *machine;
+} QEMUMachineListEntry;
 
-int qemu_register_machine(QEMUMachine *m)
+static QTAILQ_HEAD(machine_list, QEMUMachineListEntry) machine_list =
+    QTAILQ_HEAD_INITIALIZER(machine_list);
+
+const QEMUMachine *current_machine = NULL;
+
+void qemu_register_machine(const QEMUMachine *m)
 {
-    QEMUMachine **pm;
-    pm = &first_machine;
-    while (*pm != NULL)
-        pm = &(*pm)->next;
-    m->next = NULL;
-    *pm = m;
-    return 0;
+    QEMUMachineListEntry *me = qemu_mallocz(sizeof(QEMUMachineListEntry));
+    me->machine = m;
+    QTAILQ_INSERT_TAIL(&machine_list, me, entry);
 }
 
-static QEMUMachine *find_machine(const char *name)
+static const QEMUMachine *find_machine(const char *name)
 {
-    QEMUMachine *m;
+    QEMUMachineListEntry *me;
 
-    for(m = first_machine; m != NULL; m = m->next) {
-        if (!strcmp(m->name, name))
-            return m;
-        if (m->alias && !strcmp(m->alias, name))
-            return m;
+    QTAILQ_FOREACH(me, &machine_list, entry) {
+        if (!strcmp(me->machine->name, name)) {
+            return me->machine;
+        }
+        if (me->machine->alias && !strcmp(me->machine->alias, name)) {
+            return me->machine;
+        }
     }
     return NULL;
 }
 
-static QEMUMachine *find_default_machine(void)
+static const QEMUMachine *find_default_machine(void)
 {
-    QEMUMachine *m;
-
-    for(m = first_machine; m != NULL; m = m->next) {
-        if (m->is_default) {
-            return m;
+    QEMUMachineListEntry *me;
+    QTAILQ_FOREACH(me, &machine_list, entry) {
+        if (me->machine->is_default) {
+            return me->machine;
         }
     }
     return NULL;
 }
 
+static void print_machines(void)
+{
+    QEMUMachineListEntry *me;
+    QTAILQ_FOREACH(me, &machine_list, entry) {
+        const QEMUMachine *m = me->machine;
+        if (m->alias) {
+            printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
+        }
+        printf("%-10s %s%s\n", m->name, m->desc,
+               m->is_default ? " (default)" : "");
+    }
+}
+
 /***********************************************************/
 /* main execution loop */
 
@@ -2050,7 +2067,7 @@  int main(int argc, char **argv, char **envp)
     int optind;
     const char *optarg;
     const char *loadvm = NULL;
-    QEMUMachine *machine;
+    const QEMUMachine *machine;
     const char *cpu_model;
     int tb_size;
     const char *pid_file = NULL;
@@ -2146,16 +2163,8 @@  int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_M:
                 machine = find_machine(optarg);
                 if (!machine) {
-                    QEMUMachine *m;
                     printf("Supported machines are:\n");
-                    for(m = first_machine; m != NULL; m = m->next) {
-                        if (m->alias)
-                            printf("%-10s %s (alias of %s)\n",
-                                   m->alias, m->desc, m->name);
-                        printf("%-10s %s%s\n",
-                               m->name, m->desc,
-                               m->is_default ? " (default)" : "");
-                    }
+                    print_machines();
                     exit(*optarg != '?');
                 }
                 break;
@@ -2926,12 +2935,14 @@  int main(int argc, char **argv, char **envp)
     if (!max_cpus)
         max_cpus = smp_cpus;
 
-    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
-    if (smp_cpus > machine->max_cpus) {
-        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
-                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
-                machine->max_cpus);
-        exit(1);
+    {
+        int machine_max_cpus = MAX(machine->max_cpus, 1);
+        if (smp_cpus > machine_max_cpus) {
+            fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max "
+                    "cpus supported by machine `%s' (%d)\n",
+                    smp_cpus, machine->name, machine_max_cpus);
+            exit(1);
+        }
     }
 
     qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
@@ -3073,8 +3084,14 @@  int main(int argc, char **argv, char **envp)
     /* open the virtual block devices */
     if (snapshot)
         qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
-    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
-        exit(1);
+
+    {
+        int use_scsi = machine->use_scsi;
+        if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
+                              &use_scsi, 1) != 0) {
+            exit(1);
+        }
+    }
 
     default_drive(default_cdrom, snapshot, machine->use_scsi,
                   IF_DEFAULT, 2, CDROM_OPTS);