Skip to content

Commit

Permalink
qapi: Split visit_end_struct() into pieces
Browse files Browse the repository at this point in the history
As mentioned in previous patches, we want to call visit_end_struct()
functions unconditionally, so that visitors can release resources
tied up since the matching visit_start_struct() without also having
to worry about error priority if more than one error occurs.

Even though error_propagate() can be safely used to ignore a second
error during cleanup caused by a first error, it is simpler if the
cleanup cannot set an error.  So, split out the error checking
portion (basically, input visitors checking for unvisited keys) into
a new function visit_check_struct(), which can be safely skipped if
any earlier errors are encountered, and leave the cleanup portion
(which never fails, but must be called unconditionally if
visit_start_struct() succeeded) in visit_end_struct().

Generated code in qapi-visit.c has diffs resembling:

|@@ -59,10 +59,12 @@ void visit_type_ACPIOSTInfo(Visitor *v,
|         goto out_obj;
|     }
|     visit_type_ACPIOSTInfo_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:

and in qapi-event.c:

@@ -47,7 +47,10 @@ void qapi_event_send_acpi_device_ost(ACP
|         goto out;
|     }
|     visit_type_q_obj_ACPI_DEVICE_OST_arg_members(v, &param, &err);
|-    visit_end_struct(v, err ? NULL : &err);
|+    if (!err) {
|+        visit_check_struct(v, &err);
|+    }
|+    visit_end_struct(v);
|     if (err) {
|         goto out;

Signed-off-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
[Conflict with a doc fixup resolved]
Signed-off-by: Markus Armbruster <[email protected]>
  • Loading branch information
ebblake authored and Markus Armbruster committed May 12, 2016
1 parent 56a6f02 commit 15c2f66
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 73 deletions.
14 changes: 8 additions & 6 deletions block/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ block_crypto_open_opts_init(QCryptoBlockFormat format,
OptsVisitor *ov;
QCryptoBlockOpenOptions *ret = NULL;
Error *local_err = NULL;
Error *end_err = NULL;

ret = g_new0(QCryptoBlockOpenOptions, 1);
ret->format = format;
Expand All @@ -219,9 +218,11 @@ block_crypto_open_opts_init(QCryptoBlockFormat format,
error_setg(&local_err, "Unsupported block format %d", format);
break;
}
if (!local_err) {
visit_check_struct(opts_get_visitor(ov), &local_err);
}

visit_end_struct(opts_get_visitor(ov), &end_err);
error_propagate(&local_err, end_err);
visit_end_struct(opts_get_visitor(ov));

out:
if (local_err) {
Expand All @@ -242,7 +243,6 @@ block_crypto_create_opts_init(QCryptoBlockFormat format,
OptsVisitor *ov;
QCryptoBlockCreateOptions *ret = NULL;
Error *local_err = NULL;
Error *end_err = NULL;

ret = g_new0(QCryptoBlockCreateOptions, 1);
ret->format = format;
Expand All @@ -265,9 +265,11 @@ block_crypto_create_opts_init(QCryptoBlockFormat format,
error_setg(&local_err, "Unsupported block format %d", format);
break;
}
if (!local_err) {
visit_check_struct(opts_get_visitor(ov), &local_err);
}

visit_end_struct(opts_get_visitor(ov), &end_err);
error_propagate(&local_err, end_err);
visit_end_struct(opts_get_visitor(ov));

out:
if (local_err) {
Expand Down
15 changes: 10 additions & 5 deletions docs/qapi-code-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,12 @@ Example:
goto out_obj;
}
visit_type_UserDefOne_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);
}
Expand Down Expand Up @@ -1007,7 +1009,10 @@ Example:
goto out;
}
visit_type_UserDefOneList(v, "arg1", &arg1, &err);
visit_end_struct(v, err ? NULL : &err);
if (!err) {
visit_check_struct(v, &err);
}
visit_end_struct(v);
if (err) {
goto out;
}
Expand All @@ -1026,7 +1031,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, NULL);
visit_end_struct(v);
qapi_dealloc_visitor_cleanup(qdv);
}

Expand Down
3 changes: 2 additions & 1 deletion hw/ppc/spapr_drc.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
case FDT_END_NODE:
/* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
g_assert(fdt_depth > 0);
visit_end_struct(v, &err);
visit_check_struct(v, &err);
visit_end_struct(v);
if (err) {
error_propagate(errp, err);
return;
Expand Down
15 changes: 8 additions & 7 deletions hw/virtio/virtio-balloon.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
if (err) {
break;
goto out_nested;
}
}
error_propagate(errp, err);
err = NULL;
visit_end_struct(v, &err);
visit_check_struct(v, &err);
out_nested:
visit_end_struct(v);

if (!err) {
visit_check_struct(v, &err);
}
out_end:
error_propagate(errp, err);
err = NULL;
visit_end_struct(v, &err);
visit_end_struct(v);
out:
error_propagate(errp, err);
}
Expand Down
5 changes: 4 additions & 1 deletion include/qapi/visitor-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ struct Visitor
void (*start_struct)(Visitor *v, const char *name, void **obj,
size_t size, Error **errp);

/* Optional; intended for input visitors */
void (*check_struct)(Visitor *v, Error **errp);

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

/* Must be set */
void (*start_list)(Visitor *v, const char *name, Error **errp);
Expand Down
21 changes: 16 additions & 5 deletions include/qapi/visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@
* }
* outlist:
* visit_end_list(v);
* if (!err) {
* visit_check_struct(v, &err);
* }
* outobj:
* error_propagate(errp, err);
* err = NULL;
* visit_end_struct(v, &err);
* visit_end_struct(v);
* out:
* error_propagate(errp, err);
* ...clean up v...
Expand Down Expand Up @@ -249,17 +250,27 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,
size_t size, Error **errp);

/*
* Complete an object visit started earlier.
* Prepare for completing an object visit.
*
* @errp obeys typical error usage, and reports failures such as
* unparsed keys remaining in the input stream.
*
* Should be called prior to visit_end_struct() if all other
* intermediate visit steps were successful, to allow the visitor one
* last chance to report errors. May be skipped on a cleanup path,
* where there is no need to check for further errors.
*/
void visit_check_struct(Visitor *v, Error **errp);

/*
* Complete an object visit started earlier.
*
* 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, Error **errp);
void visit_end_struct(Visitor *v);


/*** Visiting lists ***/
Expand Down
17 changes: 15 additions & 2 deletions qapi/opts-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ opts_start_struct(Visitor *v, const char *name, void **obj,


static void
opts_end_struct(Visitor *v, Error **errp)
opts_check_struct(Visitor *v, Error **errp)
{
OptsVisitor *ov = to_ov(v);
GHashTableIter iter;
GQueue *any;

if (--ov->depth > 0) {
if (ov->depth > 0) {
return;
}

Expand All @@ -177,6 +177,18 @@ opts_end_struct(Visitor *v, Error **errp)
first = g_queue_peek_head(any);
error_setg(errp, QERR_INVALID_PARAMETER, first->name);
}
}


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

if (--ov->depth > 0) {
return;
}

g_hash_table_destroy(ov->unprocessed_opts);
ov->unprocessed_opts = NULL;
if (ov->fake_id_opt) {
Expand Down Expand Up @@ -516,6 +528,7 @@ opts_visitor_new(const QemuOpts *opts)
ov->visitor.type = VISITOR_INPUT;

ov->visitor.start_struct = &opts_start_struct;
ov->visitor.check_struct = &opts_check_struct;
ov->visitor.end_struct = &opts_end_struct;

ov->visitor.start_list = &opts_start_list;
Expand Down
2 changes: 1 addition & 1 deletion qapi/qapi-dealloc-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
qapi_dealloc_push(qov, obj);
}

static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
static void qapi_dealloc_end_struct(Visitor *v)
{
QapiDeallocVisitor *qov = to_qov(v);
void **obj = qapi_dealloc_pop(qov);
Expand Down
11 changes: 9 additions & 2 deletions qapi/qapi-visit-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,
error_propagate(errp, err);
}

void visit_end_struct(Visitor *v, Error **errp)
void visit_check_struct(Visitor *v, Error **errp)
{
v->end_struct(v, errp);
if (v->check_struct) {
v->check_struct(v, errp);
}
}

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

void visit_start_list(Visitor *v, const char *name, Error **errp)
Expand Down
35 changes: 20 additions & 15 deletions qapi/qmp-input-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
}


static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
static void qmp_input_check_struct(Visitor *v, Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);
StackObject *tos = &qiv->stack[qiv->nb_stack - 1];

assert(qiv->nb_stack > 0);

if (qiv->strict) {
Expand All @@ -140,6 +142,20 @@ static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
}
}
}
}

static void qmp_input_pop(Visitor *v)
{
QmpInputVisitor *qiv = to_qiv(v);
StackObject *tos = &qiv->stack[qiv->nb_stack - 1];

assert(qiv->nb_stack > 0);

if (qiv->strict) {
GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
if (top_ht) {
g_hash_table_unref(top_ht);
}
tos->h = NULL;
Expand Down Expand Up @@ -175,12 +191,6 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
}
}

static void qmp_input_end_struct(Visitor *v, Error **errp)
{
QmpInputVisitor *qiv = to_qiv(v);

qmp_input_pop(qiv, errp);
}

static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
{
Expand Down Expand Up @@ -218,12 +228,6 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
return entry;
}

static void qmp_input_end_list(Visitor *v)
{
QmpInputVisitor *qiv = to_qiv(v);

qmp_input_pop(qiv, &error_abort);
}

static void qmp_input_start_alternate(Visitor *v, const char *name,
GenericAlternate **obj, size_t size,
Expand Down Expand Up @@ -383,10 +387,11 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict)

v->visitor.type = VISITOR_INPUT;
v->visitor.start_struct = qmp_input_start_struct;
v->visitor.end_struct = qmp_input_end_struct;
v->visitor.check_struct = qmp_input_check_struct;
v->visitor.end_struct = qmp_input_pop;
v->visitor.start_list = qmp_input_start_list;
v->visitor.next_list = qmp_input_next_list;
v->visitor.end_list = qmp_input_end_list;
v->visitor.end_list = qmp_input_pop;
v->visitor.start_alternate = qmp_input_start_alternate;
v->visitor.type_int64 = qmp_input_type_int64;
v->visitor.type_uint64 = qmp_input_type_uint64;
Expand Down
2 changes: 1 addition & 1 deletion qapi/qmp-output-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
qmp_output_push(qov, dict);
}

static void qmp_output_end_struct(Visitor *v, Error **errp)
static void qmp_output_end_struct(Visitor *v)
{
QmpOutputVisitor *qov = to_qov(v);
QObject *value = qmp_output_pop(qov);
Expand Down
5 changes: 2 additions & 3 deletions qom/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2036,10 +2036,9 @@ static void property_get_tm(Object *obj, Visitor *v, const char *name,
if (err) {
goto out_end;
}
visit_check_struct(v, &err);
out_end:
error_propagate(errp, err);
err = NULL;
visit_end_struct(v, errp);
visit_end_struct(v);
out:
error_propagate(errp, err);

Expand Down
23 changes: 10 additions & 13 deletions qom/object_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Object *user_creatable_add(const QDict *qdict,
char *type = NULL;
char *id = NULL;
Object *obj = NULL;
Error *local_err = NULL, *end_err = NULL;
Error *local_err = NULL;
QDict *pdict;

pdict = qdict_clone_shallow(qdict);
Expand All @@ -63,21 +63,15 @@ Object *user_creatable_add(const QDict *qdict,
if (local_err) {
goto out_visit;
}

obj = user_creatable_add_type(type, id, pdict, v, &local_err);
visit_check_struct(v, &local_err);
if (local_err) {
goto out_visit;
}

out_visit:
visit_end_struct(v, &end_err);
if (end_err) {
error_propagate(&local_err, end_err);
if (obj) {
user_creatable_del(id, NULL);
}
goto out;
}
obj = user_creatable_add_type(type, id, pdict, v, &local_err);

out_visit:
visit_end_struct(v);

out:
QDECREF(pdict);
Expand Down Expand Up @@ -130,7 +124,10 @@ Object *user_creatable_add_type(const char *type, const char *id,
break;
}
}
visit_end_struct(v, local_err ? NULL : &local_err);
if (!local_err) {
visit_check_struct(v, &local_err);
}
visit_end_struct(v);
if (local_err) {
goto out;
}
Expand Down
Loading

0 comments on commit 15c2f66

Please sign in to comment.