Commit 41ac54b2 authored by Marc-André Lureau's avatar Marc-André Lureau Committed by Paolo Bonzini
Browse files

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: default avatarMarc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: default avatarEric Blake <eblake@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 5ebd6703
Loading
Loading
Loading
Loading
+10 −13
Original line number Diff line number Diff line
@@ -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,
+8 −8
Original line number Diff line number Diff line
@@ -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,
+6 −16
Original line number Diff line number Diff line
@@ -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);
}
+1 −0
Original line number Diff line number Diff line
@@ -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,
    };
+5 −5
Original line number Diff line number Diff line
@@ -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;
@@ -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)
@@ -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) {
@@ -455,7 +455,7 @@ 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;
}
@@ -463,6 +463,7 @@ qemu_irq *csrhci_pins_get(CharDriverState *chr)
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,
@@ -470,7 +471,6 @@ CharDriverState *uart_hci_init(void)
    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();
Loading