Skip to content

Commit

Permalink
qapi: Add parameter to visit_end_*
Browse files Browse the repository at this point in the history
Rather than making the dealloc visitor track of stack of pointers
remembered during visit_start_* in order to free them during
visit_end_*, it's a lot easier to just make all callers pass the
same pointer to visit_end_*.  The generated code has access to the
same pointer, while all other users are doing virtual walks and
can pass NULL.  The dealloc visitor is then greatly simplified.

All three visit_end_*() functions intentionally take a void**,
even though the visit_start_*() functions differ between void**,
GenericList**, and GenericAlternate**.  This is done for several
reasons: when doing a virtual walk, passing NULL doesn't care
what the type is, but when doing a generated walk, we already
have to cast the caller's specific FOO* to call visit_start,
while using void** lets us use visit_end without a cast. Also,
an upcoming patch will add a clone visitor that wants to use
the same implementation for all three visit_end callbacks,
which is made easier if all three share the same signature.

For visitors with already track per-object state (the QMP visitors
via a stack, and the string visitors which do not allow nesting),
add an assertion that the caller is indeed passing the same
pointer to paired calls.

Signed-off-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Markus Armbruster <[email protected]>
Signed-off-by: Markus Armbruster <[email protected]>
  • Loading branch information
ebblake authored and Markus Armbruster committed Jul 6, 2016
1 parent 911ee36 commit 1158bb2
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 105 deletions.
4 changes: 2 additions & 2 deletions block/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ block_crypto_open_opts_init(QCryptoBlockFormat format,
visit_check_struct(opts_get_visitor(ov), &local_err);
}

visit_end_struct(opts_get_visitor(ov));
visit_end_struct(opts_get_visitor(ov), NULL);

out:
if (local_err) {
Expand Down Expand Up @@ -269,7 +269,7 @@ block_crypto_create_opts_init(QCryptoBlockFormat format,
visit_check_struct(opts_get_visitor(ov), &local_err);
}

visit_end_struct(opts_get_visitor(ov));
visit_end_struct(opts_get_visitor(ov), NULL);

out:
if (local_err) {
Expand Down
8 changes: 4 additions & 4 deletions docs/qapi-code-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ Example:
}
visit_check_struct(v, &err);
out_obj:
visit_end_struct(v);
visit_end_struct(v, (void **)obj);
if (err && visit_is_input(v)) {
qapi_free_UserDefOne(*obj);
*obj = NULL;
Expand Down Expand Up @@ -932,7 +932,7 @@ Example:
}
}

visit_end_list(v);
visit_end_list(v, (void **)obj);
if (err && visit_is_input(v)) {
qapi_free_UserDefOneList(*obj);
*obj = NULL;
Expand Down Expand Up @@ -1022,7 +1022,7 @@ Example:
if (!err) {
visit_check_struct(v, &err);
}
visit_end_struct(v);
visit_end_struct(v, NULL);
if (err) {
goto out;
}
Expand All @@ -1041,7 +1041,7 @@ Example:
v = qapi_dealloc_get_visitor(qdv);
visit_start_struct(v, NULL, NULL, 0, NULL);
visit_type_UserDefOneList(v, "arg1", &arg1, NULL);
visit_end_struct(v);
visit_end_struct(v, NULL);
qapi_dealloc_visitor_cleanup(qdv);
}

Expand Down
4 changes: 2 additions & 2 deletions hw/ppc/spapr_drc.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
/* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
g_assert(fdt_depth > 0);
visit_check_struct(v, &err);
visit_end_struct(v);
visit_end_struct(v, NULL);
if (err) {
error_propagate(errp, err);
return;
Expand All @@ -323,7 +323,7 @@ static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
return;
}
}
visit_end_list(v);
visit_end_list(v, NULL);
break;
}
default:
Expand Down
4 changes: 2 additions & 2 deletions hw/virtio/virtio-balloon.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
}
visit_check_struct(v, &err);
out_nested:
visit_end_struct(v);
visit_end_struct(v, NULL);

if (!err) {
visit_check_struct(v, &err);
}
out_end:
visit_end_struct(v);
visit_end_struct(v, NULL);
out:
error_propagate(errp, err);
}
Expand Down
6 changes: 3 additions & 3 deletions include/qapi/visitor-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct Visitor
void (*check_struct)(Visitor *v, Error **errp);

/* Must be set to visit structs */
void (*end_struct)(Visitor *v);
void (*end_struct)(Visitor *v, void **obj);

/* Must be set; implementations may require @list to be non-null,
* but must document it. */
Expand All @@ -58,7 +58,7 @@ struct Visitor
GenericList *(*next_list)(Visitor *v, GenericList *tail, size_t size);

/* Must be set */
void (*end_list)(Visitor *v);
void (*end_list)(Visitor *v, void **list);

/* Must be set by input and dealloc visitors to visit alternates;
* optional for output visitors. */
Expand All @@ -67,7 +67,7 @@ struct Visitor
bool promote_int, Error **errp);

/* Optional, needed for dealloc visitor */
void (*end_alternate)(Visitor *v);
void (*end_alternate)(Visitor *v, void **obj);

/* Must be set */
void (*type_int64)(Visitor *v, const char *name, int64_t *obj,
Expand Down
32 changes: 19 additions & 13 deletions include/qapi/visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@
* goto outlist;
* }
* outlist:
* visit_end_list(v);
* visit_end_list(v, NULL);
* if (!err) {
* visit_check_struct(v, &err);
* }
* outobj:
* visit_end_struct(v);
* visit_end_struct(v, NULL);
* out:
* error_propagate(errp, err);
* ...clean up v...
Expand Down Expand Up @@ -242,8 +242,8 @@ typedef struct GenericAlternate {
* After visit_start_struct() succeeds, the caller may visit its
* members one after the other, passing the member's name and address
* within the struct. Finally, visit_end_struct() needs to be called
* to clean up, even if intermediate visits fail. See the examples
* above.
* with the same @obj to clean up, even if intermediate visits fail.
* See the examples above.
*
* FIXME Should this be named visit_start_object, since it is also
* used for QAPI unions, and maps to JSON objects?
Expand All @@ -267,12 +267,14 @@ void visit_check_struct(Visitor *v, Error **errp);
/*
* Complete an object visit started earlier.
*
* @obj must match what was passed to the paired visit_start_struct().
*
* Must be called after any successful use of visit_start_struct(),
* even if intermediate processing was skipped due to errors, to allow
* the backend to release any resources. Destroying the visitor early
* behaves as if this was implicitly called.
*/
void visit_end_struct(Visitor *v);
void visit_end_struct(Visitor *v, void **obj);


/*** Visiting lists ***/
Expand All @@ -299,8 +301,9 @@ void visit_end_struct(Visitor *v);
* 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.
* element. Finally, visit_end_list() needs to be called with the
* same @list to clean up, even if intermediate visits fail. See the
* examples above.
*/
void visit_start_list(Visitor *v, const char *name, GenericList **list,
size_t size, Error **errp);
Expand All @@ -324,12 +327,14 @@ GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size);
/*
* Complete a list visit started earlier.
*
* @list must match what was passed to the paired visit_start_list().
*
* Must be called after any successful use of visit_start_list(), even
* if intermediate processing was skipped due to errors, to allow the
* backend to release any resources. Destroying the visitor early
* behaves as if this was implicitly called.
*/
void visit_end_list(Visitor *v);
void visit_end_list(Visitor *v, void **list);


/*** Visiting alternates ***/
Expand All @@ -347,8 +352,9 @@ void visit_end_list(Visitor *v);
*
* If @promote_int, treat integers as QTYPE_FLOAT.
*
* If successful, this must be paired with visit_end_alternate() to
* clean up, even if visiting the contents of the alternate fails.
* If successful, this must be paired with visit_end_alternate() with
* the same @obj to clean up, even if visiting the contents of the
* alternate fails.
*/
void visit_start_alternate(Visitor *v, const char *name,
GenericAlternate **obj, size_t size,
Expand All @@ -357,15 +363,15 @@ void visit_start_alternate(Visitor *v, const char *name,
/*
* Finish visiting an alternate type.
*
* @obj must match what was passed to the paired visit_start_alternate().
*
* Must be called after any successful use of visit_start_alternate(),
* even if intermediate processing was skipped due to errors, to allow
* the backend to release any resources. Destroying the visitor early
* behaves as if this was implicitly called.
*
* TODO: Should all the visit_end_* interfaces take obj parameter, so
* that dealloc visitor need not track what was passed in visit_start?
*/
void visit_end_alternate(Visitor *v);
void visit_end_alternate(Visitor *v, void **obj);


/*** Other helpers ***/
Expand Down
4 changes: 2 additions & 2 deletions qapi/opts-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ opts_check_struct(Visitor *v, Error **errp)


static void
opts_end_struct(Visitor *v)
opts_end_struct(Visitor *v, void **obj)
{
OptsVisitor *ov = to_ov(v);

Expand Down Expand Up @@ -273,7 +273,7 @@ opts_next_list(Visitor *v, GenericList *tail, size_t size)


static void
opts_end_list(Visitor *v)
opts_end_list(Visitor *v, void **obj)
{
OptsVisitor *ov = to_ov(v);

Expand Down
47 changes: 3 additions & 44 deletions qapi/qapi-dealloc-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,18 @@
#include "qapi/qmp/types.h"
#include "qapi/visitor-impl.h"

typedef struct StackEntry
{
void *value;
QTAILQ_ENTRY(StackEntry) node;
} StackEntry;

struct QapiDeallocVisitor
{
Visitor visitor;
QTAILQ_HEAD(, StackEntry) stack;
};

static QapiDeallocVisitor *to_qov(Visitor *v)
{
return container_of(v, QapiDeallocVisitor, visitor);
}

static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
{
StackEntry *e = g_malloc0(sizeof(*e));

e->value = value;

QTAILQ_INSERT_HEAD(&qov->stack, e, node);
}

static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
{
StackEntry *e = QTAILQ_FIRST(&qov->stack);
QObject *value;
QTAILQ_REMOVE(&qov->stack, e, node);
value = e->value;
g_free(e);
return value;
}

static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
size_t unused, Error **errp)
{
QapiDeallocVisitor *qov = to_qov(v);
qapi_dealloc_push(qov, obj);
}

static void qapi_dealloc_end_struct(Visitor *v)
static void qapi_dealloc_end_struct(Visitor *v, void **obj)
{
QapiDeallocVisitor *qov = to_qov(v);
void **obj = qapi_dealloc_pop(qov);
if (obj) {
g_free(*obj);
}
Expand All @@ -75,14 +40,10 @@ static void qapi_dealloc_start_alternate(Visitor *v, const char *name,
GenericAlternate **obj, size_t size,
bool promote_int, Error **errp)
{
QapiDeallocVisitor *qov = to_qov(v);
qapi_dealloc_push(qov, obj);
}

static void qapi_dealloc_end_alternate(Visitor *v)
static void qapi_dealloc_end_alternate(Visitor *v, void **obj)
{
QapiDeallocVisitor *qov = to_qov(v);
void **obj = qapi_dealloc_pop(qov);
if (obj) {
g_free(*obj);
}
Expand All @@ -102,7 +63,7 @@ static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList *tail,
return next;
}

static void qapi_dealloc_end_list(Visitor *v)
static void qapi_dealloc_end_list(Visitor *v, void **obj)
{
}

Expand Down Expand Up @@ -178,7 +139,5 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
v->visitor.type_any = qapi_dealloc_type_anything;
v->visitor.type_null = qapi_dealloc_type_null;

QTAILQ_INIT(&v->stack);

return v;
}
12 changes: 6 additions & 6 deletions qapi/qapi-visit-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void visit_check_struct(Visitor *v, Error **errp)
}
}

void visit_end_struct(Visitor *v)
void visit_end_struct(Visitor *v, void **obj)
{
v->end_struct(v);
v->end_struct(v, obj);
}

void visit_start_list(Visitor *v, const char *name, GenericList **list,
Expand All @@ -67,9 +67,9 @@ GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
return v->next_list(v, tail, size);
}

void visit_end_list(Visitor *v)
void visit_end_list(Visitor *v, void **obj)
{
v->end_list(v);
v->end_list(v, obj);
}

void visit_start_alternate(Visitor *v, const char *name,
Expand All @@ -89,10 +89,10 @@ void visit_start_alternate(Visitor *v, const char *name,
error_propagate(errp, err);
}

void visit_end_alternate(Visitor *v)
void visit_end_alternate(Visitor *v, void **obj)
{
if (v->end_alternate) {
v->end_alternate(v);
v->end_alternate(v, obj);
}
}

Expand Down
Loading

0 comments on commit 1158bb2

Please sign in to comment.