Skip to content

Commit

Permalink
char: allocate CharDriverState as a single object
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
elmarco authored and bonzini committed Jan 27, 2017
1 parent 5ebd670 commit 41ac54b
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 214 deletions.
23 changes: 10 additions & 13 deletions backends/baum.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
#define BUF_SIZE 256

typedef struct {
CharDriverState *chr;
CharDriverState parent;

brlapi_handle_t *brlapi;
int brlapi_fd;
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions backends/msmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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 = {
Expand All @@ -165,19 +166,18 @@ 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;
}

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,
Expand Down
22 changes: 6 additions & 16 deletions backends/testdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#define BUF_SIZE 32

typedef struct {
CharDriverState *chr;
CharDriverState parent;

uint8_t in_buf[32];
int in_buf_used;
} TestdevCharState;
Expand Down Expand Up @@ -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) {
Expand All @@ -102,39 +103,28 @@ 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,
ChardevReturn *ret,
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;
}

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);
}
Expand Down
1 change: 1 addition & 0 deletions gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,7 @@ int gdbserver_start(const char *device)
CharDriverState *mon_chr;
ChardevCommon common = { 0 };
static const CharDriver driver = {
.instance_size = sizeof(CharDriverState),
.kind = -1,
.chr_write = gdb_monitor_write,
};
Expand Down
10 changes: 5 additions & 5 deletions hw/bt/hci-csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include "hw/bt.h"

struct csrhci_s {
CharDriverState chr;
int enable;
qemu_irq *pins;
int pin_state;
int modem_state;
CharDriverState chr;
#define FIFO_LEN 4096
int out_start;
int out_len;
Expand Down Expand Up @@ -314,7 +314,7 @@ static void csrhci_ready_for_next_inpkt(struct csrhci_s *s)
static int csrhci_write(struct CharDriverState *chr,
const uint8_t *buf, int len)
{
struct csrhci_s *s = (struct csrhci_s *) chr->opaque;
struct csrhci_s *s = (struct csrhci_s *)chr;
int total = 0;

if (!s->enable)
Expand Down Expand Up @@ -387,7 +387,7 @@ static void csrhci_out_hci_packet_acl(void *opaque,
static int csrhci_ioctl(struct CharDriverState *chr, int cmd, void *arg)
{
QEMUSerialSetParams *ssp;
struct csrhci_s *s = (struct csrhci_s *) chr->opaque;
struct csrhci_s *s = (struct csrhci_s *) chr;
int prev_state = s->modem_state;

switch (cmd) {
Expand Down Expand Up @@ -455,22 +455,22 @@ static void csrhci_pins(void *opaque, int line, int level)

qemu_irq *csrhci_pins_get(CharDriverState *chr)
{
struct csrhci_s *s = (struct csrhci_s *) chr->opaque;
struct csrhci_s *s = (struct csrhci_s *) chr;

return s->pins;
}

CharDriverState *uart_hci_init(void)
{
static const CharDriver hci_driver = {
.instance_size = sizeof(struct csrhci_s),
.kind = -1,
.chr_write = csrhci_write,
.chr_ioctl = csrhci_ioctl,
};
struct csrhci_s *s = (struct csrhci_s *)
g_malloc0(sizeof(struct csrhci_s));

s->chr.opaque = s;
s->chr.driver = &hci_driver;

s->hci = qemu_next_hci();
Expand Down
2 changes: 1 addition & 1 deletion include/sysemu/char.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ struct CharDriverState {
const CharDriver *driver;
QemuMutex chr_write_lock;
CharBackend *be;
void *opaque;
char *label;
char *filename;
int logfd;
Expand Down Expand Up @@ -484,6 +483,7 @@ struct CharDriver {
ChardevBackend *backend,
ChardevReturn *ret, bool *be_opened,
Error **errp);
size_t instance_size;

int (*chr_write)(struct CharDriverState *s, const uint8_t *buf, int len);
int (*chr_sync_read)(struct CharDriverState *s,
Expand Down
Loading

0 comments on commit 41ac54b

Please sign in to comment.