qapi: change QmpOutputVisitor to QSLIST

This saves a little memory compared to the doubly-linked QTAILQ.

Backports commit fc76ae8b38783e82c109834573ba5d6f080440b5 from qemu
This commit is contained in:
Paolo Bonzini 2018-02-25 19:52:34 -05:00 committed by Lioncash
parent e39b9d0391
commit b14f1d7a80
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -24,15 +24,13 @@ typedef struct QStackEntry
{ {
QObject *value; QObject *value;
void *qapi; /* sanity check that caller uses same pointer */ void *qapi; /* sanity check that caller uses same pointer */
QTAILQ_ENTRY(QStackEntry) node; QSLIST_ENTRY(QStackEntry) node;
} QStackEntry; } QStackEntry;
typedef QTAILQ_HEAD(QStack, QStackEntry) QStack;
struct QmpOutputVisitor struct QmpOutputVisitor
{ {
Visitor visitor; Visitor visitor;
QStack stack; /* Stack of containers that haven't yet been finished */ QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
QObject *root; /* Root of the output visit */ QObject *root; /* Root of the output visit */
QObject **result; /* User's storage location for result */ QObject **result; /* User's storage location for result */
}; };
@ -57,18 +55,18 @@ static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
assert(value); assert(value);
e->value = value; e->value = value;
e->qapi = qapi; e->qapi = qapi;
QTAILQ_INSERT_HEAD(&qov->stack, e, node); QSLIST_INSERT_HEAD(&qov->stack, e, node);
} }
/* Pop a value off the stack of QObjects being built, and return it. */ /* Pop a value off the stack of QObjects being built, and return it. */
static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi) static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
{ {
QStackEntry *e = QTAILQ_FIRST(&qov->stack); QStackEntry *e = QSLIST_FIRST(&qov->stack);
QObject *value; QObject *value;
assert(e); assert(e);
assert(e->qapi == qapi); assert(e->qapi == qapi);
QTAILQ_REMOVE(&qov->stack, e, node); QSLIST_REMOVE_HEAD(&qov->stack, node);
value = e->value; value = e->value;
assert(value); assert(value);
g_free(e); g_free(e);
@ -81,7 +79,7 @@ static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name, static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
QObject *value) QObject *value)
{ {
QStackEntry *e = QTAILQ_FIRST(&qov->stack); QStackEntry *e = QSLIST_FIRST(&qov->stack);
QObject *cur = e ? e->value : NULL; QObject *cur = e ? e->value : NULL;
if (!cur) { if (!cur) {
@ -207,7 +205,7 @@ static void qmp_output_complete(Visitor *v, void *opaque)
QmpOutputVisitor *qov = to_qov(v); QmpOutputVisitor *qov = to_qov(v);
/* A visit must have occurred, with each start paired with end. */ /* A visit must have occurred, with each start paired with end. */
assert(qov->root && QTAILQ_EMPTY(&qov->stack)); assert(qov->root && QSLIST_EMPTY(&qov->stack));
assert(opaque == qov->result); assert(opaque == qov->result);
*qov->result = qov->root; *qov->result = qov->root;
@ -217,10 +215,11 @@ static void qmp_output_complete(Visitor *v, void *opaque)
static void qmp_output_free(Visitor *v) static void qmp_output_free(Visitor *v)
{ {
QmpOutputVisitor *qov = to_qov(v); QmpOutputVisitor *qov = to_qov(v);
QStackEntry *e, *tmp; QStackEntry *e;
QTAILQ_FOREACH_SAFE(e, &qov->stack, node, tmp) { while (!QSLIST_EMPTY(&qov->stack)) {
QTAILQ_REMOVE(&qov->stack, e, node); e = QSLIST_FIRST(&qov->stack);
QSLIST_REMOVE_HEAD(&qov->stack, node);
g_free(e); g_free(e);
} }
@ -250,7 +249,6 @@ Visitor *qmp_output_visitor_new(QObject **result)
v->visitor.complete = qmp_output_complete; v->visitor.complete = qmp_output_complete;
v->visitor.free = qmp_output_free; v->visitor.free = qmp_output_free;
QTAILQ_INIT(&v->stack);
*result = NULL; *result = NULL;
v->result = result; v->result = result;