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: 88799 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7C41EB6EED for ; Wed, 30 Mar 2011 02:17:23 +1100 (EST) Received: from localhost ([127.0.0.1]:49283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q4ZkX-0008Oh-Rk for incoming@patchwork.ozlabs.org; Tue, 29 Mar 2011 10:18:25 -0400 Received: from [140.186.70.92] (port=40318 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q4Zi4-00085u-CQ for qemu-devel@nongnu.org; Tue, 29 Mar 2011 10:15:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q4Zb1-0005KO-BY for qemu-devel@nongnu.org; Tue, 29 Mar 2011 10:08:36 -0400 Received: from mnementh.archaic.org.uk ([81.2.115.146]:55528) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q4Zb0-0005J0-UI for qemu-devel@nongnu.org; Tue, 29 Mar 2011 10:08:35 -0400 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 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> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 81.2.115.146 Cc: Blue Swirl , patches@linaro.org Subject: [Qemu-devel] [PATCH v3 3/7] vl.c: Fix machine registration so QEMUMachine structs can be const X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.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);