From 94a40fc56036b5058b0b194d9e372a22e65ce7be Mon Sep 17 00:00:00 2001 From: Marc-André Lureau Date: Sat, 22 Oct 2016 12:52:49 +0300 Subject: char: introduce CharBackend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This new structure is meant to keep the details associated with a char driver usage. On initialization, it gets a tag from the mux backend. It can change its handlers thanks to qemu_chr_fe_set_handlers(). This structure is introduced so that all frontend will be moved to hold and use a CharBackend. This will allow to better track char usage and allocation, and help prevent some memory leaks or corruption. Signed-off-by: Marc-André Lureau Message-Id: <20161022095318.17775-10-marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini --- qemu-char.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'qemu-char.c') diff --git a/qemu-char.c b/qemu-char.c index 29b339fd32..9c27371e3c 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -910,6 +910,53 @@ static CharDriverState *qemu_chr_open_mux(const char *id, return chr; } +CharDriverState *qemu_chr_fe_get_driver(CharBackend *be) +{ + return be->chr; +} + +bool qemu_chr_fe_init(CharBackend *b, CharDriverState *s, Error **errp) +{ + int tag = 0; + + if (s->is_mux) { + tag = mux_chr_new_handler_tag(s, errp); + if (tag < 0) { + return false; + } + } + + b->tag = tag; + b->chr = s; + + return true; +} + +void qemu_chr_fe_set_handlers(CharBackend *b, + IOCanReadHandler *fd_can_read, + IOReadHandler *fd_read, + IOEventHandler *fd_event, + void *opaque, + GMainContext *context) +{ + if (!b->chr) { + return; + } + + qemu_chr_set_handlers(b->chr, fd_can_read, fd_read, + fd_event, opaque, context, b->tag); + + if (b->chr->is_mux) { + mux_chr_set_handlers(b->chr, context); + } +} + +void qemu_chr_fe_take_focus(CharBackend *b) +{ + if (b->chr->is_mux) { + mux_set_focus(b->chr->opaque, b->tag); + } +} typedef struct IOWatchPoll { @@ -4184,6 +4231,9 @@ void qemu_chr_disconnect(CharDriverState *chr) static void qemu_chr_free_common(CharDriverState *chr) { + if (chr->be) { + chr->be->chr = NULL; + } g_free(chr->filename); g_free(chr->label); if (chr->logfd != -1) { -- cgit 1.4.1