char: use a const CharDriver

No need to allocate & copy fields, let's use static const struct instead.

Add an alias field to the CharDriver structure to cover the cases where
we previously registered a driver twice under two names.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Marc-André Lureau 2016-10-21 16:07:45 +03:00 committed by Paolo Bonzini
parent bd1caa3f8e
commit 0b812f3192
7 changed files with 150 additions and 85 deletions

View File

@ -671,8 +671,12 @@ fail_handle:
static void register_types(void) static void register_types(void)
{ {
register_char_driver("braille", CHARDEV_BACKEND_KIND_BRAILLE, NULL, static const CharDriver driver = {
chr_baum_init); .kind = CHARDEV_BACKEND_KIND_BRAILLE,
.create = chr_baum_init,
};
register_char_driver(&driver);
} }
type_init(register_types); type_init(register_types);

View File

@ -179,8 +179,11 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id,
static void register_types(void) static void register_types(void)
{ {
register_char_driver("msmouse", CHARDEV_BACKEND_KIND_MSMOUSE, NULL, static const CharDriver driver = {
qemu_chr_open_msmouse); .kind = CHARDEV_BACKEND_KIND_MSMOUSE,
.create = qemu_chr_open_msmouse,
};
register_char_driver(&driver);
} }
type_init(register_types); type_init(register_types);

View File

@ -130,8 +130,11 @@ static CharDriverState *chr_testdev_init(const char *id,
static void register_types(void) static void register_types(void)
{ {
register_char_driver("testdev", CHARDEV_BACKEND_KIND_TESTDEV, NULL, static const CharDriver driver = {
chr_testdev_init); .kind = CHARDEV_BACKEND_KIND_TESTDEV,
.create = chr_testdev_init,
};
register_char_driver(&driver);
} }
type_init(register_types); type_init(register_types);

View File

@ -475,15 +475,17 @@ void qemu_chr_set_feature(CharDriverState *chr,
CharDriverFeature feature); CharDriverFeature feature);
QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename); QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
typedef void CharDriverParse(QemuOpts *opts, ChardevBackend *backend, typedef struct CharDriver {
Error **errp); ChardevBackendKind kind;
typedef CharDriverState *CharDriverCreate(const char *id, const char *alias;
ChardevBackend *backend, void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
ChardevReturn *ret, bool *be_opened, CharDriverState *(*create)(const char *id,
Error **errp); ChardevBackend *backend,
ChardevReturn *ret, bool *be_opened,
Error **errp);
} CharDriver;
void register_char_driver(const char *name, ChardevBackendKind kind, void register_char_driver(const CharDriver *driver);
CharDriverParse *parse, CharDriverCreate *create);
extern int term_escape_char; extern int term_escape_char;

View File

@ -4101,27 +4101,12 @@ static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
} }
} }
typedef struct CharDriver {
const char *name;
ChardevBackendKind kind;
CharDriverParse *parse;
CharDriverCreate *create;
} CharDriver;
static GSList *backends; static GSList *backends;
void register_char_driver(const char *name, ChardevBackendKind kind, void register_char_driver(const CharDriver *driver)
CharDriverParse *parse, CharDriverCreate *create)
{ {
CharDriver *s; /* casting away const */
backends = g_slist_append(backends, (void *)driver);
s = g_malloc0(sizeof(*s));
s->name = g_strdup(name);
s->kind = kind;
s->parse = parse;
s->create = create;
backends = g_slist_append(backends, s);
} }
CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
@ -4133,22 +4118,26 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
GSList *i; GSList *i;
ChardevReturn *ret = NULL; ChardevReturn *ret = NULL;
ChardevBackend *backend; ChardevBackend *backend;
const char *name = qemu_opt_get(opts, "backend");
const char *id = qemu_opts_id(opts); const char *id = qemu_opts_id(opts);
char *bid = NULL; char *bid = NULL;
if (qemu_opt_get(opts, "backend") == NULL) { if (name == NULL) {
error_setg(errp, "chardev: \"%s\" missing backend", error_setg(errp, "chardev: \"%s\" missing backend",
qemu_opts_id(opts)); qemu_opts_id(opts));
goto err; goto err;
} }
if (is_help_option(qemu_opt_get(opts, "backend"))) { if (is_help_option(name)) {
fprintf(stderr, "Available chardev backend types:\n"); fprintf(stderr, "Available chardev backend types:\n");
for (i = backends; i; i = i->next) { for (i = backends; i; i = i->next) {
cd = i->data; cd = i->data;
fprintf(stderr, "%s\n", cd->name); fprintf(stderr, "%s\n", ChardevBackendKind_lookup[cd->kind]);
if (cd->alias) {
fprintf(stderr, "%s\n", cd->alias);
}
} }
exit(!is_help_option(qemu_opt_get(opts, "backend"))); exit(0);
} }
if (id == NULL) { if (id == NULL) {
@ -4159,13 +4148,13 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
for (i = backends; i; i = i->next) { for (i = backends; i; i = i->next) {
cd = i->data; cd = i->data;
if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) { if (g_strcmp0(ChardevBackendKind_lookup[cd->kind], name) == 0 ||
g_strcmp0(cd->alias, name) == 0) {
break; break;
} }
} }
if (i == NULL) { if (i == NULL) {
error_setg(errp, "chardev: backend \"%s\" not found", error_setg(errp, "chardev: backend \"%s\" not found", name);
qemu_opt_get(opts, "backend"));
goto err; goto err;
} }
@ -4368,20 +4357,29 @@ ChardevInfoList *qmp_query_chardev(Error **errp)
return chr_list; return chr_list;
} }
static ChardevBackendInfoList *
qmp_prepend_backend(ChardevBackendInfoList *list, const char *name)
{
ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
info->value = g_malloc0(sizeof(*info->value));
info->value->name = g_strdup(name);
info->next = list;
return info;
}
ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
{ {
ChardevBackendInfoList *backend_list = NULL; ChardevBackendInfoList *backend_list = NULL;
CharDriver *c = NULL; CharDriver *c;
GSList *i = NULL; GSList *i;
for (i = backends; i; i = i->next) { for (i = backends; i; i = i->next) {
ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
c = i->data; c = i->data;
info->value = g_malloc0(sizeof(*info->value)); backend_list = qmp_prepend_backend(backend_list,
info->value->name = g_strdup(c->name); ChardevBackendKind_lookup[c->kind]);
if (c->alias) {
info->next = backend_list; backend_list = qmp_prepend_backend(backend_list, c->alias);
backend_list = info; }
} }
return backend_list; return backend_list;
@ -4914,45 +4912,88 @@ void qemu_chr_cleanup(void)
static void register_types(void) static void register_types(void)
{ {
register_char_driver("null", CHARDEV_BACKEND_KIND_NULL, NULL, int i;
qemu_chr_open_null); static const CharDriver drivers[] = {
register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET, {
qemu_chr_parse_socket, qmp_chardev_open_socket); .kind = CHARDEV_BACKEND_KIND_NULL,
register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP, qemu_chr_parse_udp, .create = qemu_chr_open_null,
qmp_chardev_open_udp); },
register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF, {
qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf); .kind = CHARDEV_BACKEND_KIND_SOCKET,
register_char_driver("file", CHARDEV_BACKEND_KIND_FILE, .parse = qemu_chr_parse_socket,
qemu_chr_parse_file_out, qmp_chardev_open_file); .create = qmp_chardev_open_socket,
register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO, },
qemu_chr_parse_stdio, qemu_chr_open_stdio); {
.kind = CHARDEV_BACKEND_KIND_UDP,
.parse = qemu_chr_parse_udp,
.create = qmp_chardev_open_udp,
},
{
.kind = CHARDEV_BACKEND_KIND_RINGBUF,
.parse = qemu_chr_parse_ringbuf,
.create = qemu_chr_open_ringbuf,
},
{
.kind = CHARDEV_BACKEND_KIND_FILE,
.parse = qemu_chr_parse_file_out,
.create = qmp_chardev_open_file,
},
{
.kind = CHARDEV_BACKEND_KIND_STDIO,
.parse = qemu_chr_parse_stdio,
.create = qemu_chr_open_stdio,
},
#if defined HAVE_CHARDEV_SERIAL #if defined HAVE_CHARDEV_SERIAL
register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL, {
qemu_chr_parse_serial, qmp_chardev_open_serial); .kind = CHARDEV_BACKEND_KIND_SERIAL,
register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL, .alias = "tty",
qemu_chr_parse_serial, qmp_chardev_open_serial); .parse = qemu_chr_parse_serial,
.create = qmp_chardev_open_serial,
},
#endif #endif
#ifdef HAVE_CHARDEV_PARPORT #ifdef HAVE_CHARDEV_PARPORT
register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL, {
qemu_chr_parse_parallel, qmp_chardev_open_parallel); .kind = CHARDEV_BACKEND_KIND_PARALLEL,
register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL, .alias = "parport",
qemu_chr_parse_parallel, qmp_chardev_open_parallel); .parse = qemu_chr_parse_parallel,
.create = qmp_chardev_open_parallel,
},
#endif #endif
#ifdef HAVE_CHARDEV_PTY #ifdef HAVE_CHARDEV_PTY
register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY, NULL, {
qemu_chr_open_pty); .kind = CHARDEV_BACKEND_KIND_PTY,
.create = qemu_chr_open_pty,
},
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL, {
qemu_chr_open_win_con); .kind = CHARDEV_BACKEND_KIND_CONSOLE,
.create = qemu_chr_open_win_con,
},
#endif #endif
register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE, {
qemu_chr_parse_pipe, qemu_chr_open_pipe); .kind = CHARDEV_BACKEND_KIND_PIPE,
register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX, qemu_chr_parse_mux, .parse = qemu_chr_parse_pipe,
qemu_chr_open_mux); .create = qemu_chr_open_pipe,
/* Bug-compatibility: */ },
register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY, {
qemu_chr_parse_ringbuf, qemu_chr_open_ringbuf); .kind = CHARDEV_BACKEND_KIND_MUX,
.parse = qemu_chr_parse_mux,
.create = qemu_chr_open_mux,
},
/* Bug-compatibility: */
{
.kind = CHARDEV_BACKEND_KIND_MEMORY,
.parse = qemu_chr_parse_ringbuf,
.create = qemu_chr_open_ringbuf,
},
};
for (i = 0; i < ARRAY_SIZE(drivers); i++) {
register_char_driver(&drivers[i]);
}
/* this must be done after machine init, since we register FEs with muxes /* this must be done after machine init, since we register FEs with muxes
* as part of realize functions like serial_isa_realizefn when -nographic * as part of realize functions like serial_isa_realizefn when -nographic
* is specified * is specified

View File

@ -389,10 +389,18 @@ static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
static void register_types(void) static void register_types(void)
{ {
register_char_driver("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC, static const CharDriver vmc_driver = {
qemu_chr_parse_spice_vmc, qemu_chr_open_spice_vmc); .kind = CHARDEV_BACKEND_KIND_SPICEVMC,
register_char_driver("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT, .parse = qemu_chr_parse_spice_vmc,
qemu_chr_parse_spice_port, qemu_chr_open_spice_port); .create = qemu_chr_open_spice_vmc,
};
static const CharDriver port_driver = {
.kind = CHARDEV_BACKEND_KIND_SPICEPORT,
.parse = qemu_chr_parse_spice_port,
.create = qemu_chr_open_spice_port,
};
register_char_driver(&vmc_driver);
register_char_driver(&port_driver);
} }
type_init(register_types); type_init(register_types);

View File

@ -2191,12 +2191,16 @@ static const TypeInfo qemu_console_info = {
.class_size = sizeof(QemuConsoleClass), .class_size = sizeof(QemuConsoleClass),
}; };
static void register_types(void) static void register_types(void)
{ {
static const CharDriver vc_driver = {
.kind = CHARDEV_BACKEND_KIND_VC,
.parse = qemu_chr_parse_vc,
.create = vc_init,
};
type_register_static(&qemu_console_info); type_register_static(&qemu_console_info);
register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc, register_char_driver(&vc_driver);
vc_init);
} }
type_init(register_types); type_init(register_types);