qapi: Simplify semantics of visit_next_list()

The semantics of the list visit are somewhat baroque, with the
following pseudocode when FooList is used:

start()
for (prev = head; cur = next(prev); prev = &cur) {
visit(&cur->value)
}

Note that these semantics (advance before visit) requires that
the first call to next() return the list head, while all other
calls return the next element of the list; that is, every visitor
implementation is required to track extra state to decide whether
to return the input as-is, or to advance. It also requires an
argument of 'GenericList **' to next(), solely because the first
iteration might need to modify the caller's GenericList head, so
that all other calls have to do a layer of dereferencing.

Thankfully, we only have two uses of list visits in the entire
code base: one in spapr_drc (which completely avoids
visit_next_list(), feeding in integers from a different source
than uint8List), and one in qapi-visit.py. That is, all other
list visitors are generated in qapi-visit.c, and share the same
paradigm based on a qapi FooList type, so we can refactor how
lists are laid out with minimal churn among clients.

We can greatly simplify things by hoisting the special case
into the start() routine, and flipping the order in the loop
to visit before advance:

start(head)
for (tail = *head; tail; tail = next(tail)) {
visit(&tail->value)
}

With the simpler semantics, visitors have less state to track,
the argument to next() is reduced to 'GenericList *', and it
also becomes obvious whether an input visitor is allocating a
FooList during visit_start_list() (rather than the old way of
not knowing if an allocation happened until the first
visit_next_list()). As a minor drawback, we now allocate in
two functions instead of one, and have to pass the size to
both functions (unless we were to tweak the input visitors to
cache the size to start_list for reuse during next_list, but
that defeats the goal of less visitor state).

The signature of visit_start_list() is chosen to match
visit_start_struct(), with the new parameters after 'name'.

The spapr_drc case is a virtual visit, done by passing NULL for
list, similarly to how NULL is passed to visit_start_struct()
when a qapi type is not used in those visits. It was easy to
provide these semantics for qmp-output and dealloc visitors,
and a bit harder for qmp-input (several prerequisite patches
refactored things to make this patch straightforward). But it
turned out that the string and opts visitors munge enough other
state during visit_next_list() to make it easier to just
document and require a GenericList visit for now; an assertion
will remind us to adjust things if we need the semantics in the
future.

Several pre-requisite cleanup patches made the reshuffling of
the various visitors easier; particularly the qmp input visitor.

Backports commit d9f62dde1303286b24ac8ce88be27e2b9b9c5f46 from qemu
This commit is contained in:
Eric Blake 2018-02-23 19:44:26 -05:00 committed by Lioncash
parent ed72ba0f8b
commit 0d52542da2
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
11 changed files with 277 additions and 303 deletions

View file

@ -38,141 +38,99 @@ extern const char *const QType_lookup[];
typedef struct anyList anyList;
struct anyList {
union {
QObject *value;
uint64_t padding;
};
struct anyList *next;
anyList *next;
QObject *value;
};
void qapi_free_anyList(anyList *obj);
typedef struct boolList boolList;
struct boolList {
union {
bool value;
uint64_t padding;
};
struct boolList *next;
boolList *next;
bool value;
};
void qapi_free_boolList(boolList *obj);
typedef struct int16List int16List;
struct int16List {
union {
int16_t value;
uint64_t padding;
};
struct int16List *next;
int16List *next;
int16_t value;
};
void qapi_free_int16List(int16List *obj);
typedef struct int32List int32List;
struct int32List {
union {
int32_t value;
uint64_t padding;
};
struct int32List *next;
int32List *next;
int32_t value;
};
void qapi_free_int32List(int32List *obj);
typedef struct int64List int64List;
struct int64List {
union {
int64_t value;
uint64_t padding;
};
struct int64List *next;
int64List *next;
int64_t value;
};
void qapi_free_int64List(int64List *obj);
typedef struct int8List int8List;
struct int8List {
union {
int8_t value;
uint64_t padding;
};
struct int8List *next;
int8List *next;
int8_t value;
};
void qapi_free_int8List(int8List *obj);
typedef struct intList intList;
struct intList {
union {
int64_t value;
uint64_t padding;
};
struct intList *next;
intList *next;
int64_t value;
};
void qapi_free_intList(intList *obj);
typedef struct numberList numberList;
struct numberList {
union {
double value;
uint64_t padding;
};
struct numberList *next;
numberList *next;
double value;
};
void qapi_free_numberList(numberList *obj);
typedef struct sizeList sizeList;
struct sizeList {
union {
uint64_t value;
uint64_t padding;
};
struct sizeList *next;
sizeList *next;
uint64_t value;
};
void qapi_free_sizeList(sizeList *obj);
typedef struct strList strList;
struct strList {
union {
char *value;
uint64_t padding;
};
struct strList *next;
strList *next;
char *value;
};
void qapi_free_strList(strList *obj);
typedef struct uint16List uint16List;
struct uint16List {
union {
uint16_t value;
uint64_t padding;
};
struct uint16List *next;
uint16List *next;
uint16_t value;
};
void qapi_free_uint16List(uint16List *obj);
typedef struct uint32List uint32List;
struct uint32List {
union {
uint32_t value;
uint64_t padding;
};
struct uint32List *next;
uint32List *next;
uint32_t value;
};
void qapi_free_uint32List(uint32List *obj);
typedef struct uint64List uint64List;
struct uint64List {
union {
uint64_t value;
uint64_t padding;
};
struct uint64List *next;
uint64List *next;
uint64_t value;
};
void qapi_free_uint64List(uint64List *obj);
typedef struct uint8List uint8List;
struct uint8List {
union {
uint8_t value;
uint64_t padding;
};
struct uint8List *next;
uint8List *next;
uint8_t value;
};
void qapi_free_uint8List(uint8List *obj);
@ -223,11 +181,8 @@ struct X86CPUFeatureWordInfo {
};
void qapi_free_X86CPUFeatureWordInfo(X86CPUFeatureWordInfo *obj);
struct X86CPUFeatureWordInfoList {
union {
X86CPUFeatureWordInfo *value;
uint64_t padding;
};
struct X86CPUFeatureWordInfoList *next;
X86CPUFeatureWordInfoList *next;
X86CPUFeatureWordInfo *value;
};
void qapi_free_X86CPUFeatureWordInfoList(X86CPUFeatureWordInfoList *obj);

View file

@ -14,6 +14,7 @@
*/
#include "qemu-common.h"
#include "qapi/error.h"
#include "qapi-visit.h"
void visit_type_DummyForceArrays_members(Visitor *v, DummyForceArrays *obj, Error **errp)
@ -41,10 +42,12 @@ void visit_type_DummyForceArrays(Visitor *v, const char *name, DummyForceArrays
goto out_obj;
}
visit_type_DummyForceArrays_members(v, *obj, &err);
error_propagate(errp, err);
err = NULL;
if (err) {
goto out_obj;
}
visit_check_struct(v, &err);
out_obj:
visit_end_struct(v, &err);
visit_end_struct(v);
out:
error_propagate(errp, err);
}
@ -102,10 +105,12 @@ void visit_type_X86CPUFeatureWordInfo(Visitor *v, const char *name, X86CPUFeatur
goto out_obj;
}
visit_type_X86CPUFeatureWordInfo_members(v, *obj, &err);
error_propagate(errp, err);
err = NULL;
if (err) {
goto out_obj;
}
visit_check_struct(v, &err);
out_obj:
visit_end_struct(v, &err);
visit_end_struct(v);
out:
error_propagate(errp, err);
}
@ -113,18 +118,20 @@ out:
void visit_type_X86CPUFeatureWordInfoList(Visitor *v, const char *name, X86CPUFeatureWordInfoList **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
X86CPUFeatureWordInfoList *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
X86CPUFeatureWordInfoList *native_i = (X86CPUFeatureWordInfoList *)i;
visit_type_X86CPUFeatureWordInfo(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (X86CPUFeatureWordInfoList *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_X86CPUFeatureWordInfo(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -144,18 +151,20 @@ void visit_type_X86CPURegister32(Visitor *v, const char *name, X86CPURegister32
void visit_type_anyList(Visitor *v, const char *name, anyList **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
anyList *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
anyList *native_i = (anyList *)i;
visit_type_any(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (anyList *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_any(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -168,18 +177,20 @@ out:
void visit_type_boolList(Visitor *v, const char *name, boolList **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
boolList *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
boolList *native_i = (boolList *)i;
visit_type_bool(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (boolList *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_bool(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -192,18 +203,20 @@ out:
void visit_type_int16List(Visitor *v, const char *name, int16List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
int16List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
int16List *native_i = (int16List *)i;
visit_type_int16(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (int16List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_int16(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -216,18 +229,20 @@ out:
void visit_type_int32List(Visitor *v, const char *name, int32List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
int32List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
int32List *native_i = (int32List *)i;
visit_type_int32(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (int32List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_int32(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -240,18 +255,20 @@ out:
void visit_type_int64List(Visitor *v, const char *name, int64List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
int64List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
int64List *native_i = (int64List *)i;
visit_type_int64(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (int64List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_int64(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -264,18 +281,20 @@ out:
void visit_type_int8List(Visitor *v, const char *name, int8List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
int8List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
int8List *native_i = (int8List *)i;
visit_type_int8(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (int8List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_int8(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -288,18 +307,20 @@ out:
void visit_type_intList(Visitor *v, const char *name, intList **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
intList *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
intList *native_i = (intList *)i;
visit_type_int(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (intList *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_int(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -312,18 +333,20 @@ out:
void visit_type_numberList(Visitor *v, const char *name, numberList **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
numberList *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
numberList *native_i = (numberList *)i;
visit_type_number(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (numberList *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_number(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -336,18 +359,20 @@ out:
void visit_type_sizeList(Visitor *v, const char *name, sizeList **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
sizeList *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
sizeList *native_i = (sizeList *)i;
visit_type_size(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (sizeList *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_size(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -360,18 +385,20 @@ out:
void visit_type_strList(Visitor *v, const char *name, strList **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
strList *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
strList *native_i = (strList *)i;
visit_type_str(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (strList *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_str(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -384,18 +411,20 @@ out:
void visit_type_uint16List(Visitor *v, const char *name, uint16List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
uint16List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
uint16List *native_i = (uint16List *)i;
visit_type_uint16(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (uint16List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_uint16(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -408,18 +437,20 @@ out:
void visit_type_uint32List(Visitor *v, const char *name, uint32List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
uint32List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
uint32List *native_i = (uint32List *)i;
visit_type_uint32(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (uint32List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_uint32(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -432,18 +463,20 @@ out:
void visit_type_uint64List(Visitor *v, const char *name, uint64List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
uint64List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
uint64List *native_i = (uint64List *)i;
visit_type_uint64(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (uint64List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_uint64(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);
@ -456,18 +489,20 @@ out:
void visit_type_uint8List(Visitor *v, const char *name, uint8List **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
uint8List *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev)) != NULL;
prev = &i) {
uint8List *native_i = (uint8List *)i;
visit_type_uint8(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (uint8List *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_uint8(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);

View file

@ -19,7 +19,8 @@ typedef struct StringInputVisitor StringInputVisitor;
/*
* The string input visitor does not implement support for visiting
* QAPI structs, alternates, null, or arbitrary QTypes.
* QAPI structs, alternates, null, or arbitrary QTypes. It also
* requires a non-null list argument to visit_start_list().
*/
StringInputVisitor *string_input_visitor_new(const char *str);
void string_input_visitor_cleanup(StringInputVisitor *v);

View file

@ -50,11 +50,13 @@ struct Visitor
/* Must be set to visit structs */
void (*end_struct)(Visitor *v);
/* Must be set */
void (*start_list)(Visitor *v, const char *name, Error **errp);
/* Must be set; implementations may require @list to be non-null,
* but must document it. */
void (*start_list)(Visitor *v, const char *name, GenericList **list,
size_t size, Error **errp);
/* Must be set */
GenericList *(*next_list)(Visitor *v, GenericList **list, size_t size);
GenericList *(*next_list)(Visitor *v, GenericList *tail, size_t size);
/* Must be set */
void (*end_list)(Visitor *v);

View file

@ -178,7 +178,7 @@
* if (err) {
* goto out;
* }
* visit_start_list(v, "list", &err);
* visit_start_list(v, "list", NULL, 0, &err);
* if (err) {
* goto outobj;
* }
@ -285,19 +285,27 @@ void visit_end_struct(Visitor *v);
* @name expresses the relationship of this list to its parent
* container; see the general description of @name above.
*
* @list must be non-NULL for a real walk, in which case @size
* determines how much memory an input visitor will allocate into
* *@list (at least sizeof(GenericList)). Some visitors also allow
* @list to be NULL for a virtual walk, in which case @size is
* ignored.
*
* @errp obeys typical error usage, and reports failures such as a
* member @name is not present, or present but not a list.
* member @name is not present, or present but not a list. On error,
* input visitors set *@list to NULL.
*
* After visit_start_list() succeeds, the caller may visit its members
* one after the other. A real visit uses visit_next_list() for
* traversing the linked list, while a virtual visit uses other means.
* For each list element, call the appropriate visit_type_FOO() with
* name set to NULL and obj set to the address of the value member of
* the list element. Finally, visit_end_list() needs to be called to
* clean up, even if intermediate visits fail. See the examples
* above.
* one after the other. A real visit (where @obj is non-NULL) uses
* visit_next_list() for traversing the linked list, while a virtual
* visit (where @obj is NULL) uses other means. For each list
* element, call the appropriate visit_type_FOO() with name set to
* NULL and obj set to the address of the value member of the list
* element. Finally, visit_end_list() needs to be called to clean up,
* even if intermediate visits fail. See the examples above.
*/
void visit_start_list(Visitor *v, const char *name, Error **errp);
void visit_start_list(Visitor *v, const char *name, GenericList **list,
size_t size, Error **errp);
/*
* Iterate over a GenericList during a non-virtual list visit.
@ -305,20 +313,20 @@ void visit_start_list(Visitor *v, const char *name, Error **errp);
* @size represents the size of a linked list node (at least
* sizeof(GenericList)).
*
* @list must not be NULL; on the first call, @list contains the
* address of the list head, and on subsequent calls *@list must be
* the previously returned value. Should be called in a loop until a
* NULL return or error occurs; for each non-NULL return, the caller
* then calls the appropriate visit_type_*() for the element type
* of the list, with that function's name parameter set to NULL and
* obj set to the address of (*@list)->value.
* @tail must not be NULL; on the first call, @tail is the value of
* *list after visit_start_list(), and on subsequent calls @tail must
* be the previously returned value. Should be called in a loop until
* a NULL return or error occurs; for each non-NULL return, the caller
* then calls the appropriate visit_type_*() for the element type of
* the list, with that function's name parameter set to NULL and obj
* set to the address of @tail->value.
*
* FIXME: This interface is awkward; it requires all callbacks to
* track whether it is the first or a subsequent call. A better
* interface would pass the head of the list through
* visit_start_list().
*/
GenericList *visit_next_list(Visitor *v, GenericList **list, size_t size);
GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size);
/*
* Complete a list visit started earlier.

View file

@ -22,7 +22,6 @@
typedef struct StackEntry
{
void *value;
bool is_list_head;
QTAILQ_ENTRY(StackEntry) node;
} StackEntry;
@ -44,10 +43,6 @@ static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
e->value = value;
/* see if we're just pushing a list head tracker */
if (value == NULL) {
e->is_list_head = true;
}
QTAILQ_INSERT_HEAD(&qov->stack, e, node);
}
@ -94,38 +89,22 @@ static void qapi_dealloc_end_alternate(Visitor *v)
}
}
static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp)
static void qapi_dealloc_start_list(Visitor *v, const char *name,
GenericList **list, size_t size,
Error **errp)
{
QapiDeallocVisitor *qov = to_qov(v);
qapi_dealloc_push(qov, NULL);
}
static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **listp,
static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList *tail,
size_t size)
{
GenericList *list = *listp;
QapiDeallocVisitor *qov = to_qov(v);
StackEntry *e = QTAILQ_FIRST(&qov->stack);
if (e && e->is_list_head) {
e->is_list_head = false;
return list;
}
if (list) {
list = list->next;
g_free(*listp);
return list;
}
return NULL;
GenericList *next = tail->next;
g_free(tail);
return next;
}
static void qapi_dealloc_end_list(Visitor *v)
{
QapiDeallocVisitor *qov = to_qov(v);
void *obj = qapi_dealloc_pop(qov);
assert(obj == NULL); /* should've been list head tracker with no payload */
}
static void qapi_dealloc_type_str(Visitor *v, const char *name, char **obj,

View file

@ -49,15 +49,23 @@ void visit_end_struct(Visitor *v)
v->end_struct(v);
}
void visit_start_list(Visitor *v, const char *name, Error **errp)
void visit_start_list(Visitor *v, const char *name, GenericList **list,
size_t size, Error **errp)
{
v->start_list(v, name, errp);
Error *err = NULL;
assert(!list || size >= sizeof(GenericList));
v->start_list(v, name, list, size, &err);
if (list && v->type == VISITOR_INPUT) {
assert(!(err && *list));
}
error_propagate(errp, err);
}
GenericList *visit_next_list(Visitor *v, GenericList **list, size_t size)
GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
{
assert(list && size >= sizeof(GenericList));
return v->next_list(v, list, size);
assert(tail && size >= sizeof(GenericList));
return v->next_list(v, tail, size);
}
void visit_end_list(Visitor *v)

View file

@ -28,7 +28,6 @@ typedef struct StackObject
GHashTable *h; /* If obj is dict: unvisited keys */
const QListEntry *entry; /* If obj is list: unvisited tail */
bool first; /* If obj is list: next_list() not yet called? */
} StackObject;
struct QmpInputVisitor
@ -80,7 +79,6 @@ static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
} else {
assert(qobject_type(qobj) == QTYPE_QLIST);
assert(!name);
assert(!tos->first);
ret = qlist_entry_obj(tos->entry);
if (consume) {
tos->entry = qlist_next(tos->entry);
@ -96,7 +94,8 @@ static void qdict_add_key(const char *key, QObject *obj, void *opaque)
g_hash_table_insert(h, (gpointer) key, NULL);
}
static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
static const QListEntry *qmp_input_push(QmpInputVisitor *qiv, QObject *obj,
Error **errp)
{
GHashTable *h;
StackObject *tos = &qiv->stack[qiv->nb_stack];
@ -104,7 +103,7 @@ static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
assert(obj);
if (qiv->nb_stack >= QIV_STACK_SIZE) {
error_setg(errp, "An internal buffer overran");
return;
return NULL;
}
tos->obj = obj;
@ -117,10 +116,10 @@ static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
tos->h = h;
} else if (qobject_type(obj) == QTYPE_QLIST) {
tos->entry = qlist_first(qobject_to_qlist(obj));
tos->first = true;
}
qiv->nb_stack++;
return tos->entry;
}
/** Only for qmp_input_pop. */
@ -194,40 +193,43 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
}
}
static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
static void qmp_input_start_list(Visitor *v, const char *name,
GenericList **list, size_t size, Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true);
const QListEntry *entry;
if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
if (list) {
*list = NULL;
}
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"list");
return;
}
qmp_input_push(qiv, qobj, errp);
entry = qmp_input_push(qiv, qobj, errp);
if (list) {
if (entry) {
*list = g_malloc0(size);
} else {
*list = NULL;
}
}
}
static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
static GenericList *qmp_input_next_list(Visitor *v, GenericList *tail,
size_t size)
{
QmpInputVisitor *qiv = to_qiv(v);
GenericList *entry;
StackObject *so = &qiv->stack[qiv->nb_stack - 1];
if (!so->entry) {
return NULL;
}
entry = g_malloc0(size);
if (so->first) {
*list = entry;
so->first = false;
} else {
(*list)->next = entry;
}
return entry;
tail->next = g_malloc0(size);
return tail->next;
}
static void qmp_input_start_alternate(Visitor *v, const char *name,

View file

@ -23,7 +23,6 @@
typedef struct QStackEntry
{
QObject *value;
bool is_list_head;
QTAILQ_ENTRY(QStackEntry) node;
} QStackEntry;
@ -53,9 +52,6 @@ static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value)
assert(qov->root);
assert(value);
e->value = value;
if (qobject_type(e->value) == QTYPE_QLIST) {
e->is_list_head = true;
}
QTAILQ_INSERT_HEAD(&qov->stack, e, node);
}
@ -119,7 +115,9 @@ static void qmp_output_end_struct(Visitor *v)
assert(qobject_type(value) == QTYPE_QDICT);
}
static void qmp_output_start_list(Visitor *v, const char *name, Error **errp)
static void qmp_output_start_list(Visitor *v, const char *name,
GenericList **listp, size_t size,
Error **errp)
{
QmpOutputVisitor *qov = to_qov(v);
QList *list = qlist_new();
@ -128,20 +126,10 @@ static void qmp_output_start_list(Visitor *v, const char *name, Error **errp)
qmp_output_push(qov, list);
}
static GenericList *qmp_output_next_list(Visitor *v, GenericList **listp,
static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
size_t size)
{
GenericList *list = *listp;
QmpOutputVisitor *qov = to_qov(v);
QStackEntry *e = QTAILQ_FIRST(&qov->stack);
assert(e);
if (e->is_list_head) {
e->is_list_head = false;
return list;
}
return list ? list->next : NULL;
return tail->next;
}
static void qmp_output_end_list(Visitor *v)

View file

@ -25,8 +25,6 @@ struct StringInputVisitor
{
Visitor visitor;
bool head;
GList *ranges;
GList *cur_range;
int64_t cur;
@ -128,11 +126,16 @@ error:
}
static void
start_list(Visitor *v, const char *name, Error **errp)
start_list(Visitor *v, const char *name, GenericList **list, size_t size,
Error **errp)
{
StringInputVisitor *siv = to_siv(v);
/* We don't support visits without a list */
assert(list);
if (parse_str(siv, name, errp) < 0) {
*list = NULL;
return;
}
@ -142,13 +145,15 @@ start_list(Visitor *v, const char *name, Error **errp)
if (r) {
siv->cur = r->begin;
}
*list = g_malloc0(size);
} else {
*list = NULL;
}
}
static GenericList *next_list(Visitor *v, GenericList **list, size_t size)
static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
{
StringInputVisitor *siv = to_siv(v);
GenericList **link;
Range *r;
if (!siv->ranges || !siv->cur_range) {
@ -172,22 +177,12 @@ static GenericList *next_list(Visitor *v, GenericList **list, size_t size)
siv->cur = r->begin;
}
if (siv->head) {
link = list;
siv->head = false;
} else {
link = &(*list)->next;
}
*link = g_malloc0(size);
return *link;
tail->next = g_malloc0(size);
return tail->next;
}
static void
end_list(Visitor *v)
static void end_list(Visitor *v)
{
StringInputVisitor *siv = to_siv(v);
siv->head = true;
}
static void parse_type_int64(Visitor *v, const char *name, int64_t *obj,
@ -348,6 +343,5 @@ StringInputVisitor *string_input_visitor_new(const char *str)
v->visitor.optional = parse_optional;
v->string = str;
v->head = true;
return v;
}

View file

@ -115,18 +115,20 @@ def gen_visit_list(name, element_type):
void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error **errp)
{
Error *err = NULL;
GenericList *i, **prev;
%(c_name)s *tail;
size_t size = sizeof(**obj);
visit_start_list(v, name, &err);
visit_start_list(v, name, (GenericList **)obj, size, &err);
if (err) {
goto out;
}
for (prev = (GenericList **)obj;
!err && (i = visit_next_list(v, prev, sizeof(**obj))) != NULL;
prev = &i) {
%(c_name)s *native_i = (%(c_name)s *)i;
visit_type_%(c_elt_type)s(v, NULL, &native_i->value, &err);
for (tail = *obj; tail;
tail = (%(c_name)s *)visit_next_list(v, (GenericList *)tail, size)) {
visit_type_%(c_elt_type)s(v, NULL, &tail->value, &err);
if (err) {
break;
}
}
error_propagate(errp, err);