Easier conversion from qapi to qobject
cover Easier conversion from qapi to qobject
I'm not sure if I'm posted this before, or if it is a cleanup I've just been sitting on locally. In all likelihood, it will miss tomorrow's freeze due to lack of review time, and thus be 2.11 material, but we'll see...
Eric Blake (5):
qapi: Add QAPI_TO_QOBJECT() convenience macro
nbd: Use simpler QAPI_TO_QOBJECT()
nfs: Use simpler QAPI_TO_QOBJECT()
qapi: Use simpler QAPI_TO_QOBJECT()
blockdev: Use simpler QAPI_TO_QOBJECT()
include/qapi/qobject-output-visitor.h | 19 +++++++++++++++++++ block/nbd.c | 8 +++----- block/nfs.c | 8 +++----- block/qapi.c | 7 +++---- blockdev.c | 13 ++++--------- qapi/qobject-output-visitor.c | 16 ++++++++++++++++ 6 files changed, 48 insertions(+), 23 deletions(-)
Signature · 1 line
2.13.3
PATCH 1/5 qapi: Add QAPI_TO_QOBJECT() convenience macro
We have several callers that want to convert a QAPI C type into a QObject; right now all of them have to copy the same boilerplate of creating a visitor. A convenience macro makes this paradigm easier to type.
--- include/qapi/qobject-output-visitor.h | 19 +++++++++++++++++++ qapi/qobject-output-visitor.c | 16 ++++++++++++++++ 2 files changed, 35 insertions(+)
include/qapi/qobject-output-visitor.h +19 -0
@@ -16,6 +16,8 @@#include "qapi/visitor.h" #include "qapi/qmp/qobject.h" +#include "qapi/error.h" +#include "qapi-visit.h" typedef struct QObjectOutputVisitor QObjectOutputVisitor ;
@@ -54,4 +56,21 @@ typedef struct QObjectOutputVisitor QObjectOutputVisitor;*/ Visitor *qobject_output_visitor_new (QObject **result); +QObject *qapi_to_qobject (const void *src, +void (*visit_type)(Visitor *,const char *, +void **,Error **), +Error **errp); + +/* +* Create a QObject from a QAPI object @src of the given @type. +* +* Not usable on QAPI scalars (integers, strings, enums), nor on a +* QAPI object that references the 'any' type. @src must not be NULL. +*/ +#define QAPI_TO_QOBJECT (type, src, err) \ + (qapi_to_qobject(1 ? (src) : (type *)NULL, \ + (void (*)(Visitor *, const char *, void**, \ + Error **))visit_type_ ## type, \ + err)) +#endif
qapi/qobject-output-visitor.c +16 -0
@@ -251,3 +251,19 @@ Visitor *qobject_output_visitor_new(QObject **result)return &v->visitor; } + +QObject *qapi_to_qobject (const void *src, +void (*visit_type)(Visitor *,const char *, +void **,Error **), +Error **errp) +{ +Visitor *v; +void *s = (void *) src;/* cast away const */ +QObject *dst =NULL ; + + v =qobject_output_visitor_new (&dst); +visit_type (v,NULL , &s, &error_abort); +visit_complete (v, &dst); +visit_free (v); +return dst; +}
Signature · 1 line
2.13.3
PATCH 2/5 nbd: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- block/nbd.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
block/nbd.c +3 -5
@@ -519,7 +519,6 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)BDRVNBDState *s = bs->opaque;QDict *opts =qdict_new ();QObject *saddr_qdict; -Visitor *ov;const char *host =NULL , *port =NULL , *path =NULL ;if (s->saddr->type ==SOCKET_ADDRESS_TYPE_INET ) {
@@ -548,10 +547,9 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) "nbd://%s:%s", host, port); } - ov =qobject_output_visitor_new (&saddr_qdict); -visit_type_SocketAddress (ov,NULL , &s->saddr, &error_abort); -visit_complete (ov, &saddr_qdict); -visit_free (ov); + saddr_qdict =QAPI_TO_QOBJECT (SocketAddress, s->saddr, &error_abort); +assert (qobject_type (saddr_qdict) ==QTYPE_QDICT ); +qdict_put_obj (opts,"server" , saddr_qdict);if (s->export) {
Signature · 1 line
2.13.3
PATCH 3/5 nfs: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- block/nfs.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
block/nfs.c +3 -5
@@ -819,7 +819,6 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)NFSClient *client = bs->opaque;QDict *opts =qdict_new ();QObject *server_qdict; -Visitor *ov;qdict_put_str (opts,"driver" ,"nfs" );
@@ -840,9 +839,9 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options) "nfs://%s%s", client->server->host, client->path); } - ov =qobject_output_visitor_new (&server_qdict); -visit_type_NFSServer (ov,NULL , &client->server, &error_abort); -visit_complete (ov, &server_qdict); + server_qdict =QAPI_TO_QOBJECT (NFSServer, client->server, &error_abort); +assert (qobject_type (server_qdict) ==QTYPE_QDICT ); +qdict_put_obj (opts,"server" , server_qdict);qdict_put_str (opts,"path" , client->path);
@@ -865,7 +864,6 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)qdict_put_int (opts,"debug" , client->debug); } -visit_free (ov);qdict_flatten (opts); bs->full_open_options = opts; }
Signature · 1 line
2.13.3
PATCH 4/5 qapi: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- block/qapi.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
block/qapi.c +3 -4
@@ -675,14 +675,13 @@ void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, ImageInfoSpecific *info_spec) {QObject *obj, *data; -Visitor *v =qobject_output_visitor_new (&obj); -visit_type_ImageInfoSpecific (v,NULL , &info_spec, &error_abort); -visit_complete (v, &obj); + obj =QAPI_TO_QOBJECT (ImageInfoSpecific, info_spec, &error_abort); + +assert (qobject_type (obj) ==QTYPE_QDICT ); data =qdict_get (qobject_to_qdict (obj),"data" );dump_qobject (func_fprintf, f,1 , data);qobject_decref (obj); -visit_free (v); } void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
Signature · 1 line
2.13.3
PATCH 5/5 blockdev: Use simpler QAPI_TO_QOBJECT()
In reply to Eric BlakeUse the new macro to avoid some boilerplate.
--- blockdev.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
blockdev.c +4 -9
@@ -3884,35 +3884,30 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp) {BlockDriverState *bs;QObject *obj; -Visitor *v =qobject_output_visitor_new (&obj);QDict *qdict;Error *local_err =NULL ; -visit_type_BlockdevOptions (v,NULL , &options, &local_err); + obj =QAPI_TO_QOBJECT (BlockdevOptions, options, &local_err);if (local_err) {error_propagate (errp, local_err); - goto fail; +return ; } -visit_complete (v, &obj); qdict =qobject_to_qdict (obj);qdict_flatten (qdict);if (!qdict_get_try_str (qdict,"node-name" )) {error_setg (errp,"'node-name' must be specified for the root node" ); - goto fail; +return ; } bs =bds_tree_init (qdict, errp);if (!bs) { - goto fail; +return ; }QTAILQ_INSERT_TAIL (&monitor_bdrv_states, bs, monitor_list); - -fail: -visit_free (v); }void qmp_blockdev_del (const char *node_name,Error **errp)
Signature · 1 line
2.13.3
Quoted 4 lines · Eric Blake
I'm not sure if I'm posted this before, or if it is a cleanup I've just been sitting on locally. In all likelihood, it will miss tomorrow's freeze due to lack of review time, and thus be 2.11 material, but we'll see...
Answering myself, it was part of my larger (mostly-unreviewed) attempt to rip out dynamic JSON parsing [1]. But it is independent enough that it warrants its own thread for separate review.
[1] https://lists.gnu.org/archive/html/qemu-devel/2016-11/msg05425.html
Quoted 8 lines
Eric Blake (5):
qapi: AddQAPI_TO_QOBJECT()convenience macro
nbd: Use simplerQAPI_TO_QOBJECT()
nfs: Use simplerQAPI_TO_QOBJECT()
qapi: Use simplerQAPI_TO_QOBJECT()
blockdev: Use simplerQAPI_TO_QOBJECT()
Quoted 7 lines
include/qapi/qobject-output-visitor.h | 19 +++++++++++++++++++ block/nbd.c | 8 +++----- block/nfs.c | 8 +++----- block/qapi.c | 7 +++---- blockdev.c | 13 ++++--------- qapi/qobject-output-visitor.c | 16 ++++++++++++++++ 6 files changed, 48 insertions(+), 23 deletions(-)
The diffstat is misleading because of the size of the additional interface and comments.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 5 lines · Eric Blake
Quoted 4 lines · Eric Blake
I'm not sure if I'm posted this before, or if it is a cleanup I've just been sitting on locally. In all likelihood, it will miss tomorrow's freeze due to lack of review time, and thus be 2.11 material, but we'll see...
I had a quick glance. Looks like a small improvement to me. No need to rush it into 2.10. I suspect some (all?) of the remaining qobject output visitor uses could also be converted.
Quoted 3 lines
Answering myself, it was part of my larger (mostly-unreviewed) attempt to rip out dynamic JSON parsing [1]. But it is independent enough that it warrants its own thread for separate review.
Yes, splitting it out makes sense. Thanks!
Quoted 1 line
[1] https://lists.gnu.org/archive/html/qemu-devel/2016-11/msg05425.html
[...]