char: allocate CharDriverState as a single object

Use a single allocation for CharDriverState, this avoids extra
allocations & pointers, and is a step towards more object-oriented
CharDriver.

Gtk console is a bit peculiar, gd_vc_chr_set_echo() used to have a
temporary VirtualConsole to save the echo bit. Instead now, we consider
whether vcd->console is set or not, and restore the echo bit saved in
VCDriverState when calling gd_vc_vte_init().

The casts added are temporary, they are replaced with QOM type-safe
macros in a later patch in this series.

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 23:44:44 +03:00 committed by Paolo Bonzini
parent 5ebd67030c
commit 41ac54b253
10 changed files with 230 additions and 214 deletions

View file

@ -85,7 +85,7 @@
#define BUF_SIZE 256
typedef struct {
CharDriverState *chr;
CharDriverState parent;
brlapi_handle_t *brlapi;
int brlapi_fd;
@ -255,7 +255,7 @@ static int baum_deferred_init(BaumDriverState *baum)
/* The serial port can receive more of our data */
static void baum_accept_input(struct CharDriverState *chr)
{
BaumDriverState *baum = chr->opaque;
BaumDriverState *baum = (BaumDriverState *)chr;
int room, first;
if (!baum->out_buf_used)
@ -281,22 +281,23 @@ static void baum_accept_input(struct CharDriverState *chr)
/* We want to send a packet */
static void baum_write_packet(BaumDriverState *baum, const uint8_t *buf, int len)
{
CharDriverState *chr = (CharDriverState *)baum;
uint8_t io_buf[1 + 2 * len], *cur = io_buf;
int room;
*cur++ = ESC;
while (len--)
if ((*cur++ = *buf++) == ESC)
*cur++ = ESC;
room = qemu_chr_be_can_write(baum->chr);
room = qemu_chr_be_can_write(chr);
len = cur - io_buf;
if (len <= room) {
/* Fits */
qemu_chr_be_write(baum->chr, io_buf, len);
qemu_chr_be_write(chr, io_buf, len);
} else {
int first;
uint8_t out;
/* Can't fit all, send what can be, and store the rest. */
qemu_chr_be_write(baum->chr, io_buf, room);
qemu_chr_be_write(chr, io_buf, room);
len -= room;
cur = io_buf + room;
if (len > BUF_SIZE - baum->out_buf_used) {
@ -471,7 +472,7 @@ static int baum_eat_packet(BaumDriverState *baum, const uint8_t *buf, int len)
/* The other end is writing some data. Store it and try to interpret */
static int baum_write(CharDriverState *chr, const uint8_t *buf, int len)
{
BaumDriverState *baum = chr->opaque;
BaumDriverState *baum = (BaumDriverState *)chr;
int tocopy, cur, eaten, orig_len = len;
if (!len)
@ -612,14 +613,13 @@ static void baum_chr_read(void *opaque)
static void baum_free(struct CharDriverState *chr)
{
BaumDriverState *baum = chr->opaque;
BaumDriverState *baum = (BaumDriverState *)chr;
timer_free(baum->cellCount_timer);
if (baum->brlapi) {
brlapi__closeConnection(baum->brlapi);
g_free(baum->brlapi);
}
g_free(baum);
}
static CharDriverState *chr_baum_init(const CharDriver *driver,
@ -638,10 +638,7 @@ static CharDriverState *chr_baum_init(const CharDriver *driver,
if (!chr) {
return NULL;
}
baum = g_malloc0(sizeof(BaumDriverState));
baum->chr = chr;
chr->opaque = baum;
baum = (BaumDriverState *)chr;
handle = g_malloc0(brlapi_getHandleSize());
baum->brlapi = handle;
@ -663,13 +660,13 @@ static CharDriverState *chr_baum_init(const CharDriver *driver,
fail_handle:
g_free(handle);
g_free(chr);
g_free(baum);
return NULL;
}
static void register_types(void)
{
static const CharDriver driver = {
.instance_size = sizeof(BaumDriverState),
.kind = CHARDEV_BACKEND_KIND_BRAILLE,
.create = chr_baum_init,
.chr_write = baum_write,

View file

@ -31,7 +31,8 @@
#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
typedef struct {
CharDriverState *chr;
CharDriverState parent;
QemuInputHandlerState *hs;
int axis[INPUT_AXIS__MAX];
bool btns[INPUT_BUTTON__MAX];
@ -42,7 +43,7 @@ typedef struct {
static void msmouse_chr_accept_input(CharDriverState *chr)
{
MouseState *mouse = chr->opaque;
MouseState *mouse = (MouseState *)chr;
int len;
len = qemu_chr_be_can_write(chr);
@ -122,9 +123,10 @@ static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
static void msmouse_input_sync(DeviceState *dev)
{
MouseState *mouse = (MouseState *)dev;
CharDriverState *chr = (CharDriverState *)dev;
msmouse_queue_event(mouse);
msmouse_chr_accept_input(mouse->chr);
msmouse_chr_accept_input(chr);
}
static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, int len)
@ -135,10 +137,9 @@ static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, int
static void msmouse_chr_free(struct CharDriverState *chr)
{
MouseState *mouse = chr->opaque;
MouseState *mouse = (MouseState *)chr;
qemu_input_handler_unregister(mouse->hs);
g_free(mouse);
}
static QemuInputHandler msmouse_handler = {
@ -165,12 +166,10 @@ static CharDriverState *qemu_chr_open_msmouse(const CharDriver *driver,
}
*be_opened = false;
mouse = g_new0(MouseState, 1);
mouse = (MouseState *)chr;
mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
&msmouse_handler);
mouse->chr = chr;
chr->opaque = mouse;
return chr;
}
@ -178,6 +177,7 @@ static CharDriverState *qemu_chr_open_msmouse(const CharDriver *driver,
static void register_types(void)
{
static const CharDriver driver = {
.instance_size = sizeof(MouseState),
.kind = CHARDEV_BACKEND_KIND_MSMOUSE,
.create = qemu_chr_open_msmouse,
.chr_write = msmouse_chr_write,

View file

@ -30,7 +30,8 @@
#define BUF_SIZE 32
typedef struct {
CharDriverState *chr;
CharDriverState parent;
uint8_t in_buf[32];
int in_buf_used;
} TestdevCharState;
@ -79,7 +80,7 @@ static int testdev_eat_packet(TestdevCharState *testdev)
/* The other end is writing some data. Store it and try to interpret */
static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)
{
TestdevCharState *testdev = chr->opaque;
TestdevCharState *testdev = (TestdevCharState *)chr;
int tocopy, eaten, orig_len = len;
while (len) {
@ -102,13 +103,6 @@ static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)
return orig_len;
}
static void testdev_free(struct CharDriverState *chr)
{
TestdevCharState *testdev = chr->opaque;
g_free(testdev);
}
static CharDriverState *chr_testdev_init(const CharDriver *driver,
const char *id,
ChardevBackend *backend,
@ -116,14 +110,10 @@ static CharDriverState *chr_testdev_init(const CharDriver *driver,
bool *be_opened,
Error **errp)
{
TestdevCharState *testdev;
CharDriverState *chr;
testdev = g_new0(TestdevCharState, 1);
testdev->chr = chr = g_new0(CharDriverState, 1);
TestdevCharState *testdev = g_new0(TestdevCharState, 1);;
CharDriverState *chr = (CharDriverState *)testdev;
chr->driver = driver;
chr->opaque = testdev;
return chr;
}
@ -131,10 +121,10 @@ static CharDriverState *chr_testdev_init(const CharDriver *driver,
static void register_types(void)
{
static const CharDriver driver = {
.instance_size = sizeof(TestdevCharState),
.kind = CHARDEV_BACKEND_KIND_TESTDEV,
.create = chr_testdev_init,
.chr_write = testdev_write,
.chr_free = testdev_free,
};
register_char_driver(&driver);
}