qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Laurent Vivier <laurent@vivier.eu>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Michael Tokarev <mjt@tls.msk.ru>,
	qemu-trivial@nongnu.org, Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PULL 09/10] console: use linked list for QemuConsoles
Date: Tue, 15 May 2018 11:19:00 +0200	[thread overview]
Message-ID: <20180515091901.27774-10-kraxel@redhat.com> (raw)
In-Reply-To: <20180515091901.27774-1-kraxel@redhat.com>

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Message-id: 20180507095424.16220-1-kraxel@redhat.com
---
 ui/console.c | 105 +++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 58 insertions(+), 47 deletions(-)

diff --git a/ui/console.c b/ui/console.c
index b02510cdca..945f05d728 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -165,6 +165,8 @@ struct QemuConsole {
     QEMUFIFO out_fifo;
     uint8_t out_fifo_buf[16];
     QEMUTimer *kbd_timer;
+
+    QTAILQ_ENTRY(QemuConsole) next;
 };
 
 struct DisplayState {
@@ -180,8 +182,8 @@ struct DisplayState {
 
 static DisplayState *display_state;
 static QemuConsole *active_console;
-static QemuConsole **consoles;
-static int nb_consoles = 0;
+static QTAILQ_HEAD(consoles_head, QemuConsole) consoles =
+    QTAILQ_HEAD_INITIALIZER(consoles);
 static bool cursor_visible_phase;
 static QEMUTimer *cursor_timer;
 
@@ -197,7 +199,7 @@ static void gui_update(void *opaque)
     uint64_t dcl_interval;
     DisplayState *ds = opaque;
     DisplayChangeListener *dcl;
-    int i;
+    QemuConsole *con;
 
     ds->refreshing = true;
     dpy_refresh(ds);
@@ -212,9 +214,9 @@ static void gui_update(void *opaque)
     }
     if (ds->update_interval != interval) {
         ds->update_interval = interval;
-        for (i = 0; i < nb_consoles; i++) {
-            if (consoles[i]->hw_ops->update_interval) {
-                consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
+        QTAILQ_FOREACH(con, &consoles, next) {
+            if (con->hw_ops->update_interval) {
+                con->hw_ops->update_interval(con->hw, interval);
             }
         }
         trace_console_refresh(interval);
@@ -1292,10 +1294,13 @@ static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
     s->ds = ds;
     s->console_type = console_type;
 
-    consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
-    if (console_type != GRAPHIC_CONSOLE || qdev_hotplug) {
-        s->index = nb_consoles;
-        consoles[nb_consoles++] = s;
+    if (QTAILQ_EMPTY(&consoles)) {
+        s->index = 0;
+        QTAILQ_INSERT_TAIL(&consoles, s, next);
+    } else if (console_type != GRAPHIC_CONSOLE || qdev_hotplug) {
+        QemuConsole *last = QTAILQ_LAST(&consoles, consoles_head);
+        s->index = last->index + 1;
+        QTAILQ_INSERT_TAIL(&consoles, s, next);
     } else {
         /*
          * HACK: Put graphical consoles before text consoles.
@@ -1303,15 +1308,24 @@ static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
          * Only do that for coldplugged devices.  After initial device
          * initialization we will not renumber the consoles any more.
          */
-        for (i = nb_consoles; i > 0; i--) {
-            if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
-                break;
-            consoles[i] = consoles[i - 1];
-            consoles[i]->index = i;
+        QemuConsole *c = QTAILQ_FIRST(&consoles);
+
+        while (QTAILQ_NEXT(c, next) != NULL &&
+               c->console_type == GRAPHIC_CONSOLE) {
+            c = QTAILQ_NEXT(c, next);
+        }
+        if (c->console_type == GRAPHIC_CONSOLE) {
+            /* have no text consoles */
+            s->index = c->index + 1;
+            QTAILQ_INSERT_AFTER(&consoles, c, s, next);
+        } else {
+            s->index = c->index;
+            QTAILQ_INSERT_BEFORE(c, s, next);
+            /* renumber text consoles */
+            for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
+                c->index = i;
+            }
         }
-        s->index = i;
-        consoles[i] = s;
-        nb_consoles++;
     }
     return s;
 }
@@ -1861,21 +1875,21 @@ static DisplayState *get_alloc_displaystate(void)
 DisplayState *init_displaystate(void)
 {
     gchar *name;
-    int i;
+    QemuConsole *con;
 
     get_alloc_displaystate();
-    for (i = 0; i < nb_consoles; i++) {
-        if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
-            consoles[i]->ds == NULL) {
-            text_console_do_init(consoles[i]->chr, display_state);
+    QTAILQ_FOREACH(con, &consoles, next) {
+        if (con->console_type != GRAPHIC_CONSOLE &&
+            con->ds == NULL) {
+            text_console_do_init(con->chr, display_state);
         }
 
         /* Hook up into the qom tree here (not in new_console()), once
          * all QemuConsoles are created and the order / numbering
          * doesn't change any more */
-        name = g_strdup_printf("console[%d]", i);
+        name = g_strdup_printf("console[%d]", con->index);
         object_property_add_child(container_get(object_get_root(), "/backend"),
-                                  name, OBJECT(consoles[i]), &error_abort);
+                                  name, OBJECT(con), &error_abort);
         g_free(name);
     }
 
@@ -1957,33 +1971,34 @@ void graphic_console_close(QemuConsole *con)
 
 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
 {
-    if (index >= nb_consoles) {
-        return NULL;
+    QemuConsole *con;
+
+    QTAILQ_FOREACH(con, &consoles, next) {
+        if (con->index == index) {
+            return con;
+        }
     }
-    return consoles[index];
+    return NULL;
 }
 
 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
 {
+    QemuConsole *con;
     Object *obj;
     uint32_t h;
-    int i;
 
-    for (i = 0; i < nb_consoles; i++) {
-        if (!consoles[i]) {
-            continue;
-        }
-        obj = object_property_get_link(OBJECT(consoles[i]),
+    QTAILQ_FOREACH(con, &consoles, next) {
+        obj = object_property_get_link(OBJECT(con),
                                        "device", &error_abort);
         if (DEVICE(obj) != dev) {
             continue;
         }
-        h = object_property_get_uint(OBJECT(consoles[i]),
+        h = object_property_get_uint(OBJECT(con),
                                      "head", &error_abort);
         if (h != head) {
             continue;
         }
-        return consoles[i];
+        return con;
     }
     return NULL;
 }
@@ -2013,22 +2028,19 @@ QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
 
 QemuConsole *qemu_console_lookup_unused(void)
 {
+    QemuConsole *con;
     Object *obj;
-    int i;
 
-    for (i = 0; i < nb_consoles; i++) {
-        if (!consoles[i]) {
+    QTAILQ_FOREACH(con, &consoles, next) {
+        if (con->hw_ops != &unused_ops) {
             continue;
         }
-        if (consoles[i]->hw_ops != &unused_ops) {
-            continue;
-        }
-        obj = object_property_get_link(OBJECT(consoles[i]),
+        obj = object_property_get_link(OBJECT(con),
                                        "device", &error_abort);
         if (obj != NULL) {
             continue;
         }
-        return consoles[i];
+        return con;
     }
     return NULL;
 }
@@ -2130,12 +2142,11 @@ static void text_console_update_cursor_timer(void)
 static void text_console_update_cursor(void *opaque)
 {
     QemuConsole *s;
-    int i, count = 0;
+    int count = 0;
 
     cursor_visible_phase = !cursor_visible_phase;
 
-    for (i = 0; i < nb_consoles; i++) {
-        s = consoles[i];
+    QTAILQ_FOREACH(s, &consoles, next) {
         if (qemu_console_is_graphic(s) ||
             !qemu_console_is_visible(s)) {
             continue;
-- 
2.9.3

  parent reply	other threads:[~2018-05-15  9:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-15  9:18 [Qemu-devel] [PULL 00/10] Ui 20180515 patches Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 01/10] sdl2: move opts assignment into loop Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 02/10] gtk: make it possible to hide the menu bar Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 03/10] ui/gtk: Only try to initialize EGL/X11 if GtkGlArea failed Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 04/10] vnc: add magic cookie to VncState Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 05/10] ui: add qapi parser for -display Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 06/10] ui: switch trivial displays to qapi parser Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 07/10] ui: switch gtk display " Gerd Hoffmann
2018-05-15  9:18 ` [Qemu-devel] [PULL 08/10] ui: document non-qapi parser cases Gerd Hoffmann
2018-05-15  9:19 ` Gerd Hoffmann [this message]
2018-05-15  9:19 ` [Qemu-devel] [PULL 10/10] gtk: disable the F10 menubar key Gerd Hoffmann
2018-05-15 11:49 ` [Qemu-devel] [PULL 00/10] Ui 20180515 patches Peter Maydell

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=20180515091901.27774-10-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=laurent@vivier.eu \
    --cc=mjt@tls.msk.ru \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@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).