vl: Use MachineClass instead of global QEMUMachine list

The machine registration flow is refactored to use the QOM functionality.
Instead of linking the machines into a list, each machine has a type
and the types can be traversed in the QOM way.

Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
Signed-off-by: Andreas Färber <afaerber@suse.de>
This commit is contained in:
Marcel Apfelbaum 2014-03-05 19:30:46 +02:00 committed by Andreas Färber
parent 36d20cb2b3
commit 261747f176
2 changed files with 55 additions and 21 deletions

View File

@ -51,6 +51,7 @@ struct QEMUMachine {
const char *hw_version; const char *hw_version;
}; };
#define TYPE_MACHINE_SUFFIX "-machine"
int qemu_register_machine(QEMUMachine *m); int qemu_register_machine(QEMUMachine *m);
QEMUMachine *find_default_machine(void); QEMUMachine *find_default_machine(void);

75
vl.c
View File

@ -1571,54 +1571,81 @@ void pcmcia_info(Monitor *mon, const QDict *qdict)
/***********************************************************/ /***********************************************************/
/* machine registration */ /* machine registration */
static QEMUMachine *first_machine = NULL;
QEMUMachine *current_machine = NULL; QEMUMachine *current_machine = NULL;
static void machine_class_init(ObjectClass *oc, void *data)
{
MachineClass *mc = MACHINE_CLASS(oc);
mc->qemu_machine = data;
}
int qemu_register_machine(QEMUMachine *m) int qemu_register_machine(QEMUMachine *m)
{ {
QEMUMachine **pm; TypeInfo ti = {
pm = &first_machine; .name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL),
while (*pm != NULL) .parent = TYPE_MACHINE,
pm = &(*pm)->next; .class_init = machine_class_init,
m->next = NULL; .class_data = (void *)m,
*pm = m; };
type_register(&ti);
return 0; return 0;
} }
static QEMUMachine *find_machine(const char *name) static QEMUMachine *find_machine(const char *name)
{ {
QEMUMachine *m; GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
QEMUMachine *m = NULL;
for(m = first_machine; m != NULL; m = m->next) { for (el = machines; el; el = el->next) {
if (!strcmp(m->name, name)) MachineClass *mc = el->data;
return m;
if (m->alias && !strcmp(m->alias, name)) if (!strcmp(mc->qemu_machine->name, name)) {
return m; m = mc->qemu_machine;
break;
}
if (mc->qemu_machine->alias && !strcmp(mc->qemu_machine->alias, name)) {
m = mc->qemu_machine;
break;
}
} }
return NULL;
g_slist_free(machines);
return m;
} }
QEMUMachine *find_default_machine(void) QEMUMachine *find_default_machine(void)
{ {
QEMUMachine *m; GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
QEMUMachine *m = NULL;
for(m = first_machine; m != NULL; m = m->next) { for (el = machines; el; el = el->next) {
if (m->is_default) { MachineClass *mc = el->data;
return m;
if (mc->qemu_machine->is_default) {
m = mc->qemu_machine;
break;
} }
} }
return NULL;
g_slist_free(machines);
return m;
} }
MachineInfoList *qmp_query_machines(Error **errp) MachineInfoList *qmp_query_machines(Error **errp)
{ {
GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
MachineInfoList *mach_list = NULL; MachineInfoList *mach_list = NULL;
QEMUMachine *m; QEMUMachine *m;
for (m = first_machine; m; m = m->next) { for (el = machines; el; el = el->next) {
MachineClass *mc = el->data;
MachineInfoList *entry; MachineInfoList *entry;
MachineInfo *info; MachineInfo *info;
m = mc->qemu_machine;
info = g_malloc0(sizeof(*info)); info = g_malloc0(sizeof(*info));
if (m->is_default) { if (m->is_default) {
info->has_is_default = true; info->has_is_default = true;
@ -1639,6 +1666,7 @@ MachineInfoList *qmp_query_machines(Error **errp)
mach_list = entry; mach_list = entry;
} }
g_slist_free(machines);
return mach_list; return mach_list;
} }
@ -2608,6 +2636,7 @@ static int debugcon_parse(const char *devname)
static QEMUMachine *machine_parse(const char *name) static QEMUMachine *machine_parse(const char *name)
{ {
QEMUMachine *m, *machine = NULL; QEMUMachine *m, *machine = NULL;
GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
if (name) { if (name) {
machine = find_machine(name); machine = find_machine(name);
@ -2616,13 +2645,17 @@ static QEMUMachine *machine_parse(const char *name)
return machine; return machine;
} }
printf("Supported machines are:\n"); printf("Supported machines are:\n");
for (m = first_machine; m != NULL; m = m->next) { for (el = machines; el; el = el->next) {
MachineClass *mc = el->data;
m = mc->qemu_machine;
if (m->alias) { if (m->alias) {
printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name); printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
} }
printf("%-20s %s%s\n", m->name, m->desc, printf("%-20s %s%s\n", m->name, m->desc,
m->is_default ? " (default)" : ""); m->is_default ? " (default)" : "");
} }
g_slist_free(machines);
exit(!name || !is_help_option(name)); exit(!name || !is_help_option(name));
} }