From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Richard Henderson <richard.henderson@linaro.org>
Subject: [PATCH] qgraph: implement stack as a linked list
Date: Wed, 12 Oct 2022 11:08:48 +0200 [thread overview]
Message-ID: <20221012090848.359764-1-pbonzini@redhat.com> (raw)
The stack used to visit the graph is implemented as a fixed-size array,
and the array is sized according to the maximum anticipated length of
a path on the graph. However, the worst case for a depth-first search
is to push all nodes on the graph, and in fact stack overflows have
been observed in the past depending on the ordering of the constructors.
To fix the problem once and for all, use a QSLIST instead of the array,
allocating QOSStackElements from the heap.
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
tests/qtest/libqos/qgraph.c | 35 +++++++++++------------------------
1 file changed, 11 insertions(+), 24 deletions(-)
diff --git a/tests/qtest/libqos/qgraph.c b/tests/qtest/libqos/qgraph.c
index 0a2dddfafa..ff9d389f12 100644
--- a/tests/qtest/libqos/qgraph.c
+++ b/tests/qtest/libqos/qgraph.c
@@ -52,6 +52,7 @@ struct QOSStackElement {
QOSStackElement *parent;
QOSGraphEdge *parent_edge;
int length;
+ QSLIST_ENTRY(QOSStackElement) next;
};
/* Each enty in these hash table will consist of <string, node/edge> pair. */
@@ -59,8 +60,7 @@ static GHashTable *edge_table;
static GHashTable *node_table;
/* stack used by the DFS algorithm to store the path from machine to test */
-static QOSStackElement qos_node_stack[QOS_PATH_MAX_ELEMENT_SIZE];
-static int qos_node_tos;
+static QSLIST_HEAD(, QOSStackElement) qos_node_stack;
/**
* add_edge(): creates an edge of type @type
@@ -325,40 +325,27 @@ static void qos_print_cb(QOSGraphNode *path, int length)
static void qos_push(QOSGraphNode *el, QOSStackElement *parent,
QOSGraphEdge *e)
{
+ QOSStackElement *elem = g_new(QOSStackElement, 1);
int len = 0; /* root is not counted */
- if (qos_node_tos == QOS_PATH_MAX_ELEMENT_SIZE) {
- g_printerr("QOSStack: full stack, cannot push");
- abort();
- }
-
if (parent) {
len = parent->length + 1;
}
- qos_node_stack[qos_node_tos++] = (QOSStackElement) {
+ *elem = (QOSStackElement) {
.node = el,
.parent = parent,
.parent_edge = e,
.length = len,
};
-}
-
-/* qos_tos(): returns the top of stack, without popping */
-static QOSStackElement *qos_tos(void)
-{
- return &qos_node_stack[qos_node_tos - 1];
+ QSLIST_INSERT_HEAD(qos_node_stack, elem, next);
}
/* qos_pop(): pops an element from the tos, setting it unvisited*/
-static QOSStackElement *qos_pop(void)
+static void qos_pop(void)
{
- if (qos_node_tos == 0) {
- g_printerr("QOSStack: empty stack, cannot pop");
- abort();
- }
- QOSStackElement *e = qos_tos();
+ QOSStackElement *e = QSLIST_FIRST(qos_node_stack);
e->node->visited = false;
- qos_node_tos--;
- return e;
+ QSLIST_REMOVE_HEAD(qos_node_stack, next);
+ g_free(e);
}
/**
@@ -400,8 +387,8 @@ static void qos_traverse_graph(QOSGraphNode *root, QOSTestCallback callback)
qos_push(root, NULL, NULL);
- while (qos_node_tos > 0) {
- s_el = qos_tos();
+ while (!QSLIST_EMPTY(qos_node_stack)) {
+ s_el = QSLIST_HEAD(qos_node_stack);
v = s_el->node;
if (v->visited) {
qos_pop();
--
2.37.3
next reply other threads:[~2022-10-12 9:14 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-12 9:08 Paolo Bonzini [this message]
-- strict thread matches above, loose matches on Subject: below --
2022-10-11 10:46 [PATCH] qgraph: implement stack as a linked list Paolo Bonzini
2022-10-11 15:29 ` Richard Henderson
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=20221012090848.359764-1-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.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).