From: minyard@acm.org
To: qemu-devel@nongnu.org
Cc: Corey Minyard <cminyard@mvista.com>
Subject: [Qemu-devel] [PATCH 04/18] qemu-char: Allocate CharDriverState in qemu_chr_new_from_opts
Date: Thu, 19 Jul 2012 13:53:19 -0500 [thread overview]
Message-ID: <1342724013-1633-5-git-send-email-minyard@acm.org> (raw)
In-Reply-To: <1342724013-1633-1-git-send-email-minyard@acm.org>
From: Corey Minyard <cminyard@mvista.com>
This allocates the CharDriverState structure in a more logical place.
It reduces code size and it allows a coming option to automatically
attempt to reconnect a chardev if the connection fails.
Signed-off-by: Corey Minyard <cminyard@mvista.com>
---
console.c | 6 +---
console.h | 2 +-
hw/baum.c | 6 +--
hw/baum.h | 2 +-
hw/msmouse.c | 5 +--
hw/msmouse.h | 2 +-
qemu-char.c | 93 +++++++++++++++++++++-------------------------------
spice-qemu-char.c | 4 +--
ui/qemu-spice.h | 2 +-
9 files changed, 47 insertions(+), 75 deletions(-)
diff --git a/console.c b/console.c
index 6a463f5..9a6538e 100644
--- a/console.c
+++ b/console.c
@@ -1513,15 +1513,12 @@ static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
chr->init(chr);
}
-CharDriverState *text_console_init(QemuOpts *opts)
+CharDriverState *text_console_init(CharDriverState *chr, QemuOpts *opts)
{
- CharDriverState *chr;
TextConsole *s;
unsigned width;
unsigned height;
- chr = g_malloc0(sizeof(CharDriverState));
-
width = qemu_opt_get_number(opts, "width", 0);
if (width == 0)
width = qemu_opt_get_number(opts, "cols", 0) * FONT_WIDTH;
@@ -1537,7 +1534,6 @@ CharDriverState *text_console_init(QemuOpts *opts)
}
if (!s) {
- g_free(chr);
return NULL;
}
diff --git a/console.h b/console.h
index 4334db5..d4b930d 100644
--- a/console.h
+++ b/console.h
@@ -359,7 +359,7 @@ void vga_hw_text_update(console_ch_t *chardata);
int is_graphic_console(void);
int is_fixedsize_console(void);
-CharDriverState *text_console_init(QemuOpts *opts);
+CharDriverState *text_console_init(CharDriverState *chr, QemuOpts *opts);
void text_consoles_set_display(DisplayState *ds);
void console_select(unsigned int index);
void console_color_init(DisplayState *ds);
diff --git a/hw/baum.c b/hw/baum.c
index 3e94f84..b5df8a6 100644
--- a/hw/baum.c
+++ b/hw/baum.c
@@ -562,10 +562,9 @@ static void baum_close(struct CharDriverState *chr)
g_free(baum);
}
-CharDriverState *chr_baum_init(QemuOpts *opts)
+CharDriverState *chr_baum_init(CharDriverState *chr, QemuOpts *opts)
{
BaumDriverState *baum;
- CharDriverState *chr;
brlapi_handle_t *handle;
#ifdef CONFIG_SDL
SDL_SysWMinfo info;
@@ -573,7 +572,7 @@ CharDriverState *chr_baum_init(QemuOpts *opts)
int tty;
baum = g_malloc0(sizeof(BaumDriverState));
- baum->chr = chr = g_malloc0(sizeof(CharDriverState));
+ baum->chr = chr;
chr->opaque = baum;
chr->chr_write = baum_write;
@@ -621,7 +620,6 @@ fail:
brlapi__closeConnection(handle);
fail_handle:
g_free(handle);
- g_free(chr);
g_free(baum);
return NULL;
}
diff --git a/hw/baum.h b/hw/baum.h
index 8af710f..599d48b 100644
--- a/hw/baum.h
+++ b/hw/baum.h
@@ -23,4 +23,4 @@
*/
/* char device */
-CharDriverState *chr_baum_init(QemuOpts *opts);
+CharDriverState *chr_baum_init(CharDriverState *chr, QemuOpts *opts);
diff --git a/hw/msmouse.c b/hw/msmouse.c
index 9c492a4..8a8a1d3 100644
--- a/hw/msmouse.c
+++ b/hw/msmouse.c
@@ -64,11 +64,8 @@ static void msmouse_chr_close (struct CharDriverState *chr)
g_free (chr);
}
-CharDriverState *qemu_chr_open_msmouse(QemuOpts *opts)
+CharDriverState *qemu_chr_open_msmouse(CharDriverState *chr, QemuOpts *opts)
{
- CharDriverState *chr;
-
- chr = g_malloc0(sizeof(CharDriverState));
chr->chr_write = msmouse_chr_write;
chr->chr_close = msmouse_chr_close;
diff --git a/hw/msmouse.h b/hw/msmouse.h
index 456cb21..925af1a 100644
--- a/hw/msmouse.h
+++ b/hw/msmouse.h
@@ -1,2 +1,2 @@
/* msmouse.c */
-CharDriverState *qemu_chr_open_msmouse(QemuOpts *opts);
+CharDriverState *qemu_chr_open_msmouse(CharDriverState *chr, QemuOpts *opts);
diff --git a/qemu-char.c b/qemu-char.c
index c2aaaee..ce3c05d 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -221,11 +221,8 @@ static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
return len;
}
-static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_null(CharDriverState *chr, QemuOpts *opts)
{
- CharDriverState *chr;
-
- chr = g_malloc0(sizeof(CharDriverState));
chr->chr_write = null_chr_write;
return chr;
}
@@ -618,12 +615,11 @@ static void fd_chr_close(struct CharDriverState *chr)
}
/* open a character device to a unix fd */
-static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
+static CharDriverState *qemu_chr_open_fd(CharDriverState *chr,
+ int fd_in, int fd_out)
{
- CharDriverState *chr;
FDCharDriver *s;
- chr = g_malloc0(sizeof(CharDriverState));
s = g_malloc0(sizeof(FDCharDriver));
s->fd_in = fd_in;
s->fd_out = fd_out;
@@ -637,7 +633,8 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
return chr;
}
-static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_file_out(CharDriverState *chr,
+ QemuOpts *opts)
{
int fd_out;
@@ -646,10 +643,10 @@ static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
if (fd_out < 0) {
return NULL;
}
- return qemu_chr_open_fd(-1, fd_out);
+ return qemu_chr_open_fd(chr, -1, fd_out);
}
-static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_pipe(CharDriverState *chr, QemuOpts *opts)
{
int fd_in, fd_out;
char filename_in[256], filename_out[256];
@@ -674,7 +671,7 @@ static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
return NULL;
}
}
- return qemu_chr_open_fd(fd_in, fd_out);
+ return qemu_chr_open_fd(chr, fd_in, fd_out);
}
@@ -765,10 +762,9 @@ static void qemu_chr_close_stdio(struct CharDriverState *chr)
fd_chr_close(chr);
}
-static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_stdio(CharDriverState *chr,
+ QemuOpts *opts)
{
- CharDriverState *chr;
-
if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
return NULL;
}
@@ -779,7 +775,7 @@ static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
atexit(term_exit);
}
- chr = qemu_chr_open_fd(0, 1);
+ qemu_chr_open_fd(chr, 0, 1);
chr->chr_close = qemu_chr_close_stdio;
chr->chr_set_echo = qemu_chr_set_echo_stdio;
qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
@@ -975,9 +971,8 @@ static void pty_chr_close(struct CharDriverState *chr)
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
-static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_pty(CharDriverState *chr, QemuOpts *opts)
{
- CharDriverState *chr;
PtyCharDriver *s;
struct termios tty;
int master_fd, slave_fd, len;
@@ -999,8 +994,6 @@ static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
tcsetattr(slave_fd, TCSAFLUSH, &tty);
close(slave_fd);
- chr = g_malloc0(sizeof(CharDriverState));
-
len = strlen(q_ptsname(master_fd)) + 5;
chr->filename = g_malloc(len);
snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
@@ -1217,10 +1210,9 @@ static void qemu_chr_close_tty(CharDriverState *chr)
}
}
-static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_tty(CharDriverState *chr, QemuOpts *opts)
{
const char *filename = qemu_opt_get(opts, "path");
- CharDriverState *chr;
int fd;
TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
@@ -1228,7 +1220,7 @@ static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
return NULL;
}
tty_serial_init(fd, 115200, 'N', 8, 1);
- chr = qemu_chr_open_fd(fd, fd);
+ qemu_chr_open_fd(chr, fd, fd);
chr->chr_ioctl = tty_serial_ioctl;
chr->chr_close = qemu_chr_close_tty;
return chr;
@@ -1350,10 +1342,9 @@ static void pp_close(CharDriverState *chr)
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
-static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_pp(CharDriverState *chr, QemuOpts *opts)
{
const char *filename = qemu_opt_get(opts, "path");
- CharDriverState *chr;
ParallelCharDriver *drv;
int fd;
@@ -1371,7 +1362,6 @@ static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
drv->fd = fd;
drv->mode = IEEE1284_MODE_COMPAT;
- chr = g_malloc0(sizeof(CharDriverState));
chr->chr_write = null_chr_write;
chr->chr_ioctl = pp_ioctl;
chr->chr_close = pp_close;
@@ -1652,13 +1642,12 @@ static int win_chr_poll(void *opaque)
return 0;
}
-static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_win(CharDriverState *chr,
+ QemuOpts *opts)
{
const char *filename = qemu_opt_get(opts, "path");
- CharDriverState *chr;
WinCharState *s;
- chr = g_malloc0(sizeof(CharDriverState));
s = g_malloc0(sizeof(WinCharState));
chr->opaque = s;
chr->chr_write = win_chr_write;
@@ -1666,7 +1655,6 @@ static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
if (win_chr_init(chr, filename) < 0) {
g_free(s);
- g_free(chr);
return NULL;
}
qemu_chr_generic_open(chr);
@@ -1752,13 +1740,12 @@ static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
}
-static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_win_pipe(CharDriverState *chr,
+ QemuOpts *opts)
{
const char *filename = qemu_opt_get(opts, "path");
- CharDriverState *chr;
WinCharState *s;
- chr = g_malloc0(sizeof(CharDriverState));
s = g_malloc0(sizeof(WinCharState));
chr->opaque = s;
chr->chr_write = win_chr_write;
@@ -1766,19 +1753,17 @@ static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
if (win_chr_pipe_init(chr, filename) < 0) {
g_free(s);
- g_free(chr);
return NULL;
}
qemu_chr_generic_open(chr);
return chr;
}
-static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
+static CharDriverState *qemu_chr_open_win_file(CharDriverState *chr,
+ HANDLE fd_out)
{
- CharDriverState *chr;
WinCharState *s;
- chr = g_malloc0(sizeof(CharDriverState));
s = g_malloc0(sizeof(WinCharState));
s->hcom = fd_out;
chr->opaque = s;
@@ -1787,12 +1772,14 @@ static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
return chr;
}
-static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_win_con(CharDriverState *chr,
+ QemuOpts *opts)
{
- return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
+ return qemu_chr_open_win_file(chr, GetStdHandle(STD_OUTPUT_HANDLE));
}
-static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_win_file_out(CharDriverState *chr,
+ QemuOpts *opts)
{
const char *file_out = qemu_opt_get(opts, "path");
HANDLE fd_out;
@@ -1803,7 +1790,7 @@ static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
return NULL;
}
- return qemu_chr_open_win_file(fd_out);
+ return qemu_chr_open_win_file(chr, fd_out);
}
static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
@@ -1944,9 +1931,9 @@ static void win_stdio_close(CharDriverState *chr)
stdio_nb_clients--;
}
-static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_win_stdio(CharDriverState *chr,
+ QemuOpts *opts)
{
- CharDriverState *chr;
WinStdioCharState *stdio;
DWORD dwMode;
int is_console = 0;
@@ -1956,7 +1943,6 @@ static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
return NULL;
}
- chr = g_malloc0(sizeof(CharDriverState));
stdio = g_malloc0(sizeof(WinStdioCharState));
stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
@@ -2093,13 +2079,11 @@ static void udp_chr_close(CharDriverState *chr)
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
-static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_udp(CharDriverState *chr, QemuOpts *opts)
{
- CharDriverState *chr = NULL;
NetCharDriver *s = NULL;
int fd = -1;
- chr = g_malloc0(sizeof(CharDriverState));
s = g_malloc0(sizeof(NetCharDriver));
fd = inet_dgram_opts(opts);
@@ -2118,7 +2102,6 @@ static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
return chr;
return_err:
- g_free(chr);
g_free(s);
if (fd >= 0) {
closesocket(fd);
@@ -2311,7 +2294,8 @@ static void tcp_chr_read(void *opaque)
#ifndef _WIN32
CharDriverState *qemu_chr_open_eventfd(int eventfd)
{
- return qemu_chr_open_fd(eventfd, eventfd);
+ CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
+ return qemu_chr_open_fd(chr, eventfd, eventfd);
}
#endif
@@ -2414,9 +2398,9 @@ static void tcp_chr_close(CharDriverState *chr)
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
-static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_socket(CharDriverState *chr,
+ QemuOpts *opts)
{
- CharDriverState *chr = NULL;
TCPCharDriver *s = NULL;
int fd = -1;
int is_listen;
@@ -2433,7 +2417,6 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
if (!is_listen)
is_waitconnect = 0;
- chr = g_malloc0(sizeof(CharDriverState));
s = g_malloc0(sizeof(TCPCharDriver));
if (is_unix) {
@@ -2510,7 +2493,6 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
if (fd >= 0)
closesocket(fd);
g_free(s);
- g_free(chr);
return NULL;
}
@@ -2708,7 +2690,7 @@ fail:
static const struct {
const char *name;
- CharDriverState *(*open)(QemuOpts *opts);
+ CharDriverState *(*open)(CharDriverState *chr, QemuOpts *opts);
} backend_table[] = {
{ .name = "null", .open = qemu_chr_open_null },
{ .name = "socket", .open = qemu_chr_open_socket },
@@ -2770,8 +2752,9 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
return NULL;
}
- chr = backend_table[i].open(opts);
- if (!chr) {
+ chr = g_malloc0(sizeof(CharDriverState));
+ if (!backend_table[i].open(chr, opts)) {
+ g_free(chr);
fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
qemu_opt_get(opts, "backend"));
return NULL;
diff --git a/spice-qemu-char.c b/spice-qemu-char.c
index 09aa22d..2fb8a10 100644
--- a/spice-qemu-char.c
+++ b/spice-qemu-char.c
@@ -188,9 +188,8 @@ static void print_allowed_subtypes(void)
fprintf(stderr, "\n");
}
-CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
+CharDriverState *qemu_chr_open_spice(CharDriverState *chr, QemuOpts *opts)
{
- CharDriverState *chr;
SpiceCharDriver *s;
const char* name = qemu_opt_get(opts, "name");
uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
@@ -214,7 +213,6 @@ CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
return NULL;
}
- chr = g_malloc0(sizeof(CharDriverState));
s = g_malloc0(sizeof(SpiceCharDriver));
s->chr = chr;
s->debug = debug;
diff --git a/ui/qemu-spice.h b/ui/qemu-spice.h
index 3299da8..fb582c5 100644
--- a/ui/qemu-spice.h
+++ b/ui/qemu-spice.h
@@ -45,7 +45,7 @@ int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
void do_info_spice_print(Monitor *mon, const QObject *data);
void do_info_spice(Monitor *mon, QObject **ret_data);
-CharDriverState *qemu_chr_open_spice(QemuOpts *opts);
+CharDriverState *qemu_chr_open_spice(CharDriverState *chr, QemuOpts *opts);
#else /* CONFIG_SPICE */
#include "monitor.h"
--
1.7.4.1
next prev parent reply other threads:[~2012-07-19 18:54 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-19 18:53 [Qemu-devel] Third shot at adding IPMI to qemu minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 01/18] smbios: Add a function to directly add an entry minyard
2012-07-30 15:37 ` Anthony Liguori
2012-07-30 16:44 ` Corey Minyard
2012-07-30 17:25 ` Anthony Liguori
2012-07-30 17:40 ` Corey Minyard
2012-08-02 1:15 ` Kevin O'Connor
2012-08-02 2:11 ` Corey Minyard
2012-08-02 2:40 ` Anthony Liguori
2012-08-02 12:17 ` Corey Minyard
2012-08-02 18:32 ` Anthony Liguori
2012-08-02 19:20 ` Corey Minyard
2012-08-02 21:05 ` Anthony Liguori
2012-08-06 15:38 ` Corey Minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 02/18] pc: move SMBIOS setup to after device init minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 03/18] vl: Move init_timer_alarm() earlier minyard
2012-07-19 18:53 ` minyard [this message]
2012-07-19 18:53 ` [Qemu-devel] [PATCH 05/18] qemu-char: Allow a chardev to reconnect if disconnected minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 06/18] qemu-char: Fix a race reporting opens and closes minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 07/18] qemu-char: remove free of chr from win_stdio_close minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 08/18] qemu-char: Close fd at end of file minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 09/18] qdev: Add a pre-firmware init capability minyard
2012-07-30 14:36 ` Andreas Färber
2012-07-30 15:27 ` Corey Minyard
2012-07-30 15:40 ` Anthony Liguori
2012-07-19 18:53 ` [Qemu-devel] [PATCH 10/18] qom: release previous object when setting minyard
2012-07-30 13:51 ` Andreas Färber
2012-09-10 14:34 ` Andreas Färber
2012-07-19 18:53 ` [Qemu-devel] [PATCH 11/18] Add a base IPMI interface minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 12/18] IPMI: Add a PC ISA type structure minyard
2012-07-30 13:45 ` Andreas Färber
2012-07-30 17:09 ` Corey Minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 13/18] IPMI: Add a KCS low-level interface minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 14/18] IPMI: Add a BT " minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 15/18] IPMI: Add a local BMC simulation minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 16/18] IPMI: Add an external connection simulation interface minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 17/18] IPMI: Add tests minyard
2012-07-19 18:53 ` [Qemu-devel] [PATCH 18/18] IPMI: Add documentation minyard
2012-07-20 6:48 ` [Qemu-devel] Third shot at adding IPMI to qemu Paolo Bonzini
2012-07-30 13:34 ` Corey Minyard
2012-07-30 14:05 ` Andreas Färber
2012-07-30 15:17 ` Corey Minyard
2012-09-10 14:48 ` Andreas Färber
2012-09-10 16:52 ` Corey Minyard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1342724013-1633-5-git-send-email-minyard@acm.org \
--to=minyard@acm.org \
--cc=cminyard@mvista.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).