From patchwork Tue Mar 29 14:08:20 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 828 Return-Path: Delivered-To: unknown Received: from imap.gmail.com (74.125.159.109) by localhost6.localdomain6 with IMAP4-SSL; 08 Jun 2011 14:46:12 -0000 Delivered-To: patches@linaro.org Received: by 10.42.161.68 with SMTP id s4cs190888icx; Tue, 29 Mar 2011 07:08:38 -0700 (PDT) Received: by 10.216.143.134 with SMTP id l6mr4104501wej.2.1301407718007; Tue, 29 Mar 2011 07:08:38 -0700 (PDT) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk [81.2.115.146]) by mx.google.com with ESMTPS id d31si8664473wej.188.2011.03.29.07.08.36 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 29 Mar 2011 07:08:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 81.2.115.146 as permitted sender) client-ip=81.2.115.146; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 81.2.115.146 as permitted sender) smtp.mail=pm215@archaic.org.uk Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1Q4Zaq-0006OM-Vh; Tue, 29 Mar 2011 15:08:24 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Blue Swirl Subject: [PATCH v3 3/7] vl.c: Fix machine registration so QEMUMachine structs can be const Date: Tue, 29 Mar 2011 15:08:20 +0100 Message-Id: <1301407704-24541-4-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1301407704-24541-1-git-send-email-peter.maydell@linaro.org> References: <1301407704-24541-1-git-send-email-peter.maydell@linaro.org> 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 --- hw/boards.h | 5 +-- vl.c | 99 ++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 60 insertions(+), 44 deletions(-) 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);