Add qapi-to-JSON and clone visitors
Subsystems (3)
v4 cover Add qapi-to-JSON and clone visitors
Hard prerequisites (for all patches to apply):
Markus' qjson movement:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg00471.html
Soft prerequisites (for valgrind to be happy with all touched tests):
My fix for memleak in range.h:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg02666.html
My fix for memleak in json-streamer.c:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg03186.html
Since v3:
Address review comments (lots from Markus, a few more from others) Drop patches 8-11/18 (qjson.h remains a one-off for migration) Drop patch 12/18 (done via nicer role reversal in 26/28) Defer Infinity/NaN rejection to later
Rearrange lots of patches; the clone visitor half (1-15) is simpler than the JSON visitor (16-28), if we need to subdivide the series Lots of new patches, in part implementing cleanups that make later patches simpler
The churn between revisions is pretty daunting; so I suspect we'll be needing a v5 as a result. But I'm really happy with how the role reversal of qobject_to_json() turned out, as well as how the polymorphic visit_complete() and visit_free() allowed me to hide knowledge of visitor subtypes from the rest of the tree. And working on this series let me plug a couple of other bugs
001/28:[----] [--] 'qapi: Rename (one) qjson.h to qobject-json.h' 002/28:[----] [--] 'qapi: Improve use of qmp/types.h' 003/28:[down] 'qemu-img: Don't leak errors when outputting JSON' 004/28:[0043] [FC] 'qapi: Add parameter to visit_end_*' 005/28:[down] 'qapi: Add new visit_free() function' 006/28:[down] 'opts-visitor: Favor new visit_free() function' 007/28:[down] 'string-input-visitor: Favor new visit_free() function' 008/28:[down] 'qmp-input-visitor: Favor new visit_free() function' 009/28:[down] 'string-output-visitor: Favor new visit_free() function' 010/28:[down] 'qmp-output-visitor: Favor new visit_free() function' 011/28:[down] 'tests: Factor out common code in qapi output tests' 012/28:[down] 'qapi: Add new visit_complete() function' 013/28:[0312] [FC] 'qapi: Add new clone visitor' 014/28:[0043] [FC] 'sockets: Use new QAPI cloning' 015/28:[0024] [FC] 'replay: Use new QAPI cloning' 016/28:[0028] [FC] 'qapi: Factor out JSON string escaping' 017/28:[0033] [FC] 'qapi: Factor out JSON number formatting' 018/28:[down] 'qapi: Add qstring_append_printf()' 019/28:[0030] [FC] 'qapi: Use qstring_append_chr() where appropriate' 020/28:[down] 'qstring: Add qstring_consume_str()' 021/28:[down] 'qstring: Add qstring_wrap_str()'
022/28:[down] 'qobject: Consolidate qobject_to_json() calls' 023/28:[down] 'tests: Test qobject_to_json() pretty formatting' 024/28:[down] 'qapi: Add JSON output visitor'
025/28:[0081] [FC] 'qapi: Support pretty printing in JSON output visitor' 026/28:[down] 'qobject: Implement qobject_to_json() atop JSON visitor' 027/28:[down] 'qapi: Add 'any' support to JSON output' 028/28:[0068] [FC] 'qemu-img: Use new JSON output formatter'
Eric Blake (28):
qapi: Rename (one) qjson.h to qobject-json.h
qapi: Improve use of qmp/types.h
qemu-img: Don't leak errors when outputting JSON
qapi: Add parameter to visit_end_*
qapi: Add new visit_free() function
opts-visitor: Favor new visit_free() function
string-input-visitor: Favor new visit_free() function
qmp-input-visitor: Favor new visit_free() function
string-output-visitor: Favor new visit_free() function
qmp-output-visitor: Favor new visit_free() function
tests: Factor out common code in qapi output tests
qapi: Add new visit_complete() function
qapi: Add new clone visitor
sockets: Use new QAPI cloning
replay: Use new QAPI cloning
qapi: Factor out JSON string escaping
qapi: Factor out JSON number formatting
qapi: Add qstring_append_printf()
qapi: Use qstring_append_chr() where appropriate
qstring: Add qstring_consume_str()
qstring: Add qstring_wrap_str()
qobject: Consolidate qobject_to_json() calls
tests: Test qobject_to_json() pretty formatting
qapi: Add JSON output visitor
qapi: Support pretty printing in JSON output visitor
qobject: Implement qobject_to_json() atop JSON visitor
qapi: Add 'any' support to JSON output
qemu-img: Use new JSON output formatter
include/qapi/visitor.h | 147 +++++---
include/qapi/visitor-impl.h | 24 +-
scripts/qapi-commands.py | 33 +-
scripts/qapi-event.py | 12 +-
scripts/qapi-types.py | 6 +-
scripts/qapi-visit.py | 8 +-
include/io/task.h | 2 +-
include/qapi/clone-visitor.h | 39 +++
include/qapi/dealloc-visitor.h | 5 +-
include/qapi/json-output-visitor.h | 29 ++
include/qapi/opts-visitor.h | 4 +-
include/qapi/qmp-input-visitor.h | 6 +-
include/qapi/qmp-output-visitor.h | 12 +-
include/qapi/qmp/{qjson.h => qobject-json.h} | 16 +-
include/qapi/qmp/qstring.h | 9 +-
include/qapi/qmp/types.h | 1 -
include/qapi/string-input-visitor.h | 5 +-
include/qapi/string-output-visitor.h | 11 +-
include/qemu/sockets.h | 4 -
qapi/qapi-visit-core.c | 47 ++-
balloon.c | 2 +-
block.c | 7 +-
block/archipelago.c | 8 +-
block/crypto.c | 30 +-
block/nbd.c | 2 +-
block/qapi.c | 9 +-
block/quorum.c | 2 +-
blockdev.c | 12 +-
blockjob.c | 2 +-
hmp.c | 19 +-
hw/acpi/core.c | 8 +-
hw/core/qdev.c | 2 +-
hw/misc/pvpanic.c | 2 +-
hw/net/virtio-net.c | 2 +-
hw/pci/pcie_aer.c | 1 +
hw/ppc/spapr_drc.c | 4 +-
hw/virtio/virtio-balloon.c | 4 +-
io/channel-socket.c | 9 +-
migration/qjson.c | 12 +-
monitor.c | 18 +-
net/net.c | 16 +-
numa.c | 6 +-
qapi/json-output-visitor.c | 211 ++++++++++++
qapi/opts-visitor.c | 37 +-
qapi/qapi-clone-visitor.c | 178 ++++++++++
qapi/qapi-dealloc-visitor.c | 59 +---
qapi/qmp-dispatch.c | 1 +
qapi/qmp-event.c | 2 +-
qapi/qmp-input-visitor.c | 18 +-
qapi/qmp-output-visitor.c | 37 +-
qapi/string-input-visitor.c | 20 +-
qapi/string-output-visitor.c | 29 +-
qemu-char.c | 5 +-
qemu-img.c | 66 ++--
qga/main.c | 7 +-
qmp.c | 9 +-
qobject/json-parser.c | 32 +-
qobject/qjson.c | 296 ----------------
qobject/qobject-json.c | 245 +++++++++++++
qobject/qobject.c | 7 +-
qobject/qstring.c | 74 +++-
qom/object.c | 61 ++--
qom/object_interfaces.c | 12 +-
qom/qom-qobject.c | 19 +-
replay/replay-input.c | 34 +-
target-s390x/kvm.c | 2 +-
tests/check-qnull.c | 17 +-
tests/{check-qjson.c => check-qobject-json.c} | 114 +++++--
tests/check-qstring.c | 55 ++-
tests/libqtest.c | 4 +-
tests/test-clone-visitor.c | 207 +++++++++++
tests/test-json-output-visitor.c | 475 ++++++++++++++++++++++++++
tests/test-opts-visitor.c | 9 +-
tests/test-qmp-commands.c | 8 +-
tests/test-qmp-input-strict.c | 13 +-
tests/test-qmp-input-visitor.c | 15 +-
tests/test-qmp-output-visitor.c | 97 ++----
tests/test-string-input-visitor.c | 22 +-
tests/test-string-output-visitor.c | 99 +++---
tests/test-visitor-serialization.c | 43 +--
ui/spice-core.c | 2 +-
util/qemu-sockets.c | 27 --
vl.c | 2 +-
MAINTAINERS | 2 +-
docs/qapi-code-gen.txt | 39 +--
qapi/Makefile.objs | 2 +-
qobject/Makefile.objs | 3 +-
tests/.gitignore | 4 +-
tests/Makefile | 15 +-
tests/qemu-iotests/043.out | 22 +-
90 files changed, 2237 insertions(+), 1117 deletions(-)
create mode 100644 include/qapi/clone-visitor.h
create mode 100644 include/qapi/json-output-visitor.h
rename include/qapi/qmp/{qjson.h => qobject-json.h} (51%)
create mode 100644 qapi/json-output-visitor.c
create mode 100644 qapi/qapi-clone-visitor.c
delete mode 100644 qobject/qjson.c
create mode 100644 qobject/qobject-json.c
rename tests/{check-qjson.c => check-qobject-json.c} (95%)
create mode 100644 tests/test-clone-visitor.c
create mode 100644 tests/test-json-output-visitor.cSignature · 1 line
2.5.5
PATCH v4 1/28 qapi: Rename (one) qjson.h to qobject-json.h
We have two different JSON visitors in the tree; and having both named 'qjson.h' can cause include confusion. Rename the qapi version.
Why did I pick that one? A later patch plans on deleting the top-level qjson.c once we have a native JSON output visitor; we could have renamed that one for less overall churn. On the other hand, all of the QObject subtypes have their own qFOO.c file, but qjson.c makes it sound like we have a QTYPE_JSON subclass of QObject; the new name of qobject-json makes it obvious that the file is used for conversions between QObject and JSON, and not a QObject subtype.
Kill trailing whitespace in the renamed tests/check-qobject-json.c to keep checkpatch.pl happy.
---
v4: no change
v3: rebase to master
v2: retitle, enhance commit message, rebase to master
---
include/qapi/qmp/{qjson.h => qobject-json.h} | 0
include/qapi/qmp/types.h | 2 +-
balloon.c | 2 +-
block.c | 2 +-
block/archipelago.c | 2 +-
block/nbd.c | 2 +-
block/quorum.c | 2 +-
blockjob.c | 2 +-
hw/core/qdev.c | 2 +-
hw/misc/pvpanic.c | 2 +-
hw/net/virtio-net.c | 2 +-
monitor.c | 2 +-
qapi/qmp-event.c | 2 +-
qemu-img.c | 2 +-
qga/main.c | 2 +-
qobject/{qjson.c => qobject-json.c} | 2 +-
target-s390x/kvm.c | 2 +-
tests/{check-qjson.c => check-qobject-json.c} | 14 +++++++-------
tests/libqtest.c | 2 +-
ui/spice-core.c | 2 +-
vl.c | 2 +-
MAINTAINERS | 2 +-
qobject/Makefile.objs | 3 ++-
tests/.gitignore | 2 +-
tests/Makefile | 8 ++++----
25 files changed, 34 insertions(+), 33 deletions(-)
rename include/qapi/qmp/{qjson.h => qobject-json.h} (100%)
rename qobject/{qjson.c => qobject-json.c} (99%)
rename tests/{check-qjson.c => check-qobject-json.c} (99%)include/qapi/qmp/qobject-json.h renamed from include/qapi/qmp/qjson.h +0 -0
include/qapi/qmp/types.h +1 -1
@@ -20,6 +20,6 @@#include "qapi/qmp/qstring.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qlist.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #endif /* QEMU_OBJECTS_H */
balloon.c +1 -1
@@ -32,7 +32,7 @@#include "trace.h" #include "qmp-commands.h" #include "qapi/qmp/qerror.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" static QEMUBalloonEvent *balloon_event_fn;static QEMUBalloonStatus *balloon_stat_fn;
block.c +1 -1
@@ -29,7 +29,7 @@#include "qemu/module.h" #include "qapi/qmp/qerror.h" #include "qapi/qmp/qbool.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "sysemu/block-backend.h" #include "sysemu/sysemu.h" #include "qemu/notify.h"
block/archipelago.c +1 -1
@@ -57,7 +57,7 @@#include "qemu/thread.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qstring.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qemu/atomic.h" #include <xseg/xseg.h>
block/nbd.c +1 -1
@@ -33,7 +33,7 @@#include "block/block_int.h" #include "qemu/module.h" #include "qapi/qmp/qdict.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qstring.h" #include "qemu/cutils.h"
block/quorum.c +1 -1
@@ -20,7 +20,7 @@#include "qapi/qmp/qdict.h" #include "qapi/qmp/qerror.h" #include "qapi/qmp/qint.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qapi/qmp/qlist.h" #include "qapi/qmp/qstring.h" #include "qapi-event.h"
blockjob.c +1 -1
@@ -31,7 +31,7 @@#include "block/block_int.h" #include "sysemu/block-backend.h" #include "qapi/qmp/qerror.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qemu/coroutine.h" #include "qmp-commands.h" #include "qemu/timer.h"
hw/core/qdev.c +1 -1
@@ -31,7 +31,7 @@#include "sysemu/sysemu.h" #include "qapi/qmp/qerror.h" #include "qapi/visitor.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qemu/error-report.h" #include "hw/hotplug.h" #include "hw/boards.h"
hw/misc/pvpanic.c +1 -1
@@ -14,7 +14,7 @@#include "qemu/osdep.h" #include "qapi/qmp/qobject.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "sysemu/sysemu.h" #include "qemu/log.h"
hw/net/virtio-net.c +1 -1
@@ -22,7 +22,7 @@#include "hw/virtio/virtio-net.h" #include "net/vhost_net.h" #include "hw/virtio/virtio-bus.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qapi-event.h" #include "hw/virtio/virtio-access.h"
monitor.c +1 -1
@@ -57,7 +57,7 @@#include "qapi/qmp/qlist.h" #include "qapi/qmp/qbool.h" #include "qapi/qmp/qstring.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qapi/qmp/json-streamer.h" #include "qapi/qmp/json-parser.h" #include <qom/object_interfaces.h>
qapi/qmp-event.c +1 -1
@@ -16,7 +16,7 @@#include "qemu-common.h" #include "qapi/qmp-event.h" #include "qapi/qmp/qstring.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" static QMPEventFuncEmit qmp_emit;
qemu-img.c +1 -1
@@ -26,7 +26,7 @@#include "qapi-visit.h" #include "qapi/qmp-output-visitor.h" #include "qapi/qmp/qerror.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qemu/cutils.h" #include "qemu/config-file.h" #include "qemu/option.h"
qga/main.c +1 -1
@@ -21,7 +21,7 @@#include "qapi/qmp/json-streamer.h" #include "qapi/qmp/json-parser.h" #include "qapi/qmp/qint.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qga/guest-agent-core.h" #include "qemu/module.h" #include "qapi/qmp/qerror.h"
qobject/qobject-json.c renamed from qobject/qjson.c +1 -1
@@ -15,7 +15,7 @@#include "qapi/qmp/json-lexer.h" #include "qapi/qmp/json-parser.h" #include "qapi/qmp/json-streamer.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qlist.h" #include "qapi/qmp/qbool.h"
target-s390x/kvm.c +1 -1
@@ -36,7 +36,7 @@#include "hw/hw.h" #include "cpu.h" #include "sysemu/device_tree.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "exec/gdbstub.h" #include "exec/address-spaces.h" #include "trace.h"
tests/check-qobject-json.c renamed from tests/check-qjson.c +7 -7
@@ -19,7 +19,7 @@#include "qapi/qmp/qlist.h" #include "qapi/qmp/qfloat.h" #include "qapi/qmp/qbool.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qemu-common.h"
@@ -64,7 +64,7 @@ static void escaped_string(void)g_assert (obj !=NULL );g_assert (qobject_type (obj) ==QTYPE_QSTRING ); - + str =qobject_to_qstring (obj); g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
@@ -99,7 +99,7 @@ static void simple_string(void)g_assert (obj !=NULL );g_assert (qobject_type (obj) ==QTYPE_QSTRING ); - + str =qobject_to_qstring (obj);g_assert (strcmp (qstring_get_str (str), test_cases[i].decoded) ==0 );
@@ -107,7 +107,7 @@ static void simple_string(void)g_assert (strcmp (qstring_get_str (str), test_cases[i].encoded) ==0 );qobject_decref (obj); - +QDECREF (str); } }
@@ -133,7 +133,7 @@ static void single_quote_string(void)g_assert (obj !=NULL );g_assert (qobject_type (obj) ==QTYPE_QSTRING ); - + str =qobject_to_qstring (obj);g_assert (strcmp (qstring_get_str (str), test_cases[i].decoded) ==0 );
@@ -881,7 +881,7 @@ static void vararg_string(void)g_assert (obj !=NULL );g_assert (qobject_type (obj) ==QTYPE_QSTRING ); - + str =qobject_to_qstring (obj);g_assert (strcmp (qstring_get_str (str), test_cases[i].decoded) ==0 );
@@ -1145,7 +1145,7 @@ static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs) helper.index =0 ; helper.objs = lhs->value.qlist; helper.result =1 ; - +qlist_iter (qobject_to_qlist (rhs), compare_helper, &helper);return helper.result;
tests/libqtest.c +1 -1
@@ -24,7 +24,7 @@#include "qapi/qmp/json-parser.h" #include "qapi/qmp/json-streamer.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #define MAX_IRQ 256#define SOCKET_TIMEOUT 5
ui/spice-core.c +1 -1
@@ -33,7 +33,7 @@#include "qapi/qmp/qint.h" #include "qapi/qmp/qbool.h" #include "qapi/qmp/qstring.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qemu/notify.h" #include "migration/migration.h" #include "hw/hw.h"
vl.c +1 -1
@@ -88,7 +88,7 @@ int main(int argc, char **argv)#include "audio/audio.h" #include "migration/migration.h" #include "sysemu/kvm.h" -#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qobject-json.h" #include "qemu/option.h" #include "qemu/config-file.h" #include "qemu-options.h"
MAINTAINERS +1 -1
@@ -1190,7 +1190,7 @@ X: include/qapi/qmp/dispatch.h F: tests/check-qdict.c F: tests/check-qfloat.c F: tests/check-qint.c -F: tests/check-qjson.c +F: tests/check-qobject-json.c F: tests/check-qlist.c F: tests/check-qstring.c T: git git://repo.or.cz/qemu/qmp-unstable.git queue/qmp
qobject/Makefile.objs +2 -1
@@ -1,2 +1,3 @@ util-obj-y = qnull.o qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o -util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o +util-obj-y += qobject-json.o qobject.o +util-obj-y += json-lexer.o json-streamer.o json-parser.o
tests/.gitignore +1 -1
@@ -1,9 +1,9 @@ check-qdict check-qfloat check-qint -check-qjson check-qlist check-qnull +check-qobject-json check-qstring check-qom-interface check-qom-proplist
tests/Makefile +4 -4
@@ -18,8 +18,8 @@ check-unit-y += tests/check-qlist$(EXESUF) gcov-files-check-qlist-y = qobject/qlist.c check-unit-y += tests/check-qnull$(EXESUF) gcov-files-check-qnull-y = qobject/qnull.c -check-unit-y += tests/check-qjson$(EXESUF) -gcov-files-check-qjson-y = qobject/qjson.c +check-unit-y += tests/check-qobject-json$(EXESUF) +gcov-files-check-qobject-json-y = qobject/qobject-json.c check-unit-y += tests/test-qmp-output-visitor$(EXESUF) gcov-files-test-qmp-output-visitor-y = qapi/qmp-output-visitor.c check-unit-y += tests/test-qmp-input-visitor$(EXESUF)
@@ -385,7 +385,7 @@ GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h \ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/check-qlist.o tests/check-qfloat.o tests/check-qnull.o \ - tests/check-qjson.o \ + tests/check-qobject-json.o \ tests/test-coroutine.o tests/test-string-output-visitor.o \ tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \ tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \
@@ -414,7 +414,7 @@ tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y) tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y) tests/check-qfloat$(EXESUF): tests/check-qfloat.o $(test-util-obj-y) tests/check-qnull$(EXESUF): tests/check-qnull.o $(test-util-obj-y) -tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y) +tests/check-qobject-json$(EXESUF): tests/check-qobject-json.o $(test-util-obj-y) tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y) tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y) tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y)
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 01/28] qapi: Rename (one) qjson.h to qobject-json.h
Quoted 3 lines · Eric Blake
We have two different JSON visitors in the tree; and having both named '
qjson.h' can cause include confusion. Rename the qapi version.
Commit 17b74b9 moved the other one to migration/. If you still want to rename this one, you'll need to update the commit message.
Quoted 2 lines
Why did I pick that one? A later patch plans on deleting the top-level
qjson.conce we have a native JSON output visitor; we
David objected to that in review of v3. We agreed to disagree, and let migration keep its own (incomplete) JSON visitor. That's why I moved it to migration/.
Quoted 12 lines
could have renamed that one for less overall churn. On the other hand, all of the QObject subtypes have their own
qFOO.cfile, butqjson.cmakes it sound like we have aQTYPE_JSONsubclass of QObject; the new name of qobject-json makes it obvious that the file is used for conversions between QObject and JSON, and not a QObject subtype.Kill trailing whitespace in the renamed
tests/check-qobject-json.cto keep checkpatch.pl happy.Signed-off-by Eric Blake <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>
PATCH v4 2/28 qapi: Improve use of qmp/types.h
In reply to Eric Blake'qobject-json.h' is not a QObject subtype; include this file directly in .c files that are using it, rather than abusing qmp/types.h for that purpose.
Meanwhile, for files that include a list of individual QObject subtypes, it's easier to just use qmp/types.h for that purpose.
---
v4: no change
v3: no change
v2: no change
--- include/qapi/qmp/types.h | 1 - hw/pci/pcie_aer.c | 1 + monitor.c | 6 +----- qapi/qmp-dispatch.c | 1 + qobject/json-parser.c | 7 +------ qobject/qobject-json.c | 6 +----- qobject/qobject.c | 7 +------ tests/check-qobject-json.c | 7 +------ tests/test-qmp-input-strict.c | 1 + tests/test-qmp-input-visitor.c | 1 + tests/test-qmp-output-visitor.c | 1 + tests/test-visitor-serialization.c | 1 + 12 files changed, 11 insertions(+), 29 deletions(-)
include/qapi/qmp/types.h +0 -1
@@ -20,6 +20,5 @@#include "qapi/qmp/qstring.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qlist.h" -#include "qapi/qmp/qobject-json.h" #endif /* QEMU_OBJECTS_H */
hw/pci/pcie_aer.c +1 -0
@@ -21,6 +21,7 @@#include "qemu/osdep.h" #include "sysemu/sysemu.h" #include "qapi/qmp/types.h" +#include "qapi/qmp/qobject-json.h" #include "monitor/monitor.h" #include "hw/pci/pci_bridge.h" #include "hw/pci/pcie.h"
monitor.c +1 -5
@@ -52,12 +52,8 @@#include "qemu/acl.h" #include "sysemu/tpm.h" #include "qapi/qmp/qerror.h" -#include "qapi/qmp/qint.h" -#include "qapi/qmp/qfloat.h" -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qbool.h" -#include "qapi/qmp/qstring.h" #include "qapi/qmp/qobject-json.h" +#include "qapi/qmp/types.h" #include "qapi/qmp/json-streamer.h" #include "qapi/qmp/json-parser.h" #include <qom/object_interfaces.h>
qapi/qmp-dispatch.c +1 -0
@@ -16,6 +16,7 @@#include "qapi/qmp/types.h" #include "qapi/qmp/dispatch.h" #include "qapi/qmp/json-parser.h" +#include "qapi/qmp/qobject-json.h" #include "qapi-types.h" #include "qapi/qmp/qerror.h"
qobject/json-parser.c +1 -6
@@ -14,12 +14,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "qemu-common.h" -#include "qapi/qmp/qstring.h" -#include "qapi/qmp/qint.h" -#include "qapi/qmp/qdict.h" -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qfloat.h" -#include "qapi/qmp/qbool.h" +#include "qapi/qmp/types.h" #include "qapi/qmp/json-parser.h" #include "qapi/qmp/json-lexer.h" #include "qapi/qmp/json-streamer.h"
qobject/qobject-json.c +1 -5
@@ -16,12 +16,8 @@#include "qapi/qmp/json-parser.h" #include "qapi/qmp/json-streamer.h" #include "qapi/qmp/qobject-json.h" -#include "qapi/qmp/qint.h" -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qbool.h" -#include "qapi/qmp/qfloat.h" -#include "qapi/qmp/qdict.h" #include "qemu/unicode.h" +#include "qapi/qmp/types.h" typedef struct JSONParsingState {
qobject/qobject.c +1 -6
@@ -9,12 +9,7 @@#include "qemu/osdep.h" #include "qemu-common.h" -#include "qapi/qmp/qbool.h" -#include "qapi/qmp/qdict.h" -#include "qapi/qmp/qfloat.h" -#include "qapi/qmp/qint.h" -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qstring.h" +#include "qapi/qmp/types.h" static void (*qdestroy[QTYPE__MAX ])(QObject *) = { [QTYPE_NONE ] =NULL ,/* No such object exists */
tests/check-qobject-json.c +1 -6
@@ -13,12 +13,7 @@#include "qemu/osdep.h" #include <glib.h> -#include "qapi/qmp/qstring.h" -#include "qapi/qmp/qint.h" -#include "qapi/qmp/qdict.h" -#include "qapi/qmp/qlist.h" -#include "qapi/qmp/qfloat.h" -#include "qapi/qmp/qbool.h" +#include "qapi/qmp/types.h" #include "qapi/qmp/qobject-json.h" #include "qemu-common.h"
tests/test-qmp-input-strict.c +1 -0
@@ -20,6 +20,7 @@#include "test-qapi-types.h" #include "test-qapi-visit.h" #include "qapi/qmp/types.h" +#include "qapi/qmp/qobject-json.h" #include "test-qmp-introspect.h" #include "qmp-introspect.h" #include "qapi-visit.h"
tests/test-qmp-input-visitor.c +1 -0
@@ -19,6 +19,7 @@#include "test-qapi-types.h" #include "test-qapi-visit.h" #include "qapi/qmp/types.h" +#include "qapi/qmp/qobject-json.h" typedef struct TestInputVisitorData { QObject *obj;
tests/test-qmp-output-visitor.c +1 -0
@@ -19,6 +19,7 @@#include "test-qapi-types.h" #include "test-qapi-visit.h" #include "qapi/qmp/types.h" +#include "qapi/qmp/qobject-json.h" typedef struct TestOutputVisitorData { QmpOutputVisitor *qov;
tests/test-visitor-serialization.c +1 -0
@@ -20,6 +20,7 @@#include "test-qapi-visit.h" #include "qapi/error.h" #include "qapi/qmp/types.h" +#include "qapi/qmp/qobject-json.h" #include "qapi/qmp-input-visitor.h" #include "qapi/qmp-output-visitor.h" #include "qapi/string-input-visitor.h"
Signature · 1 line
2.5.5
PATCH v4 3/28 qemu-img: Don't leak errors when outputting JSON
In reply to Eric BlakeIf our JSON output ever encounters an error, we would just silently leak the local variable. Instead, assert that our usage won't fail.
---
v4: new patch (split out from v3 14/18)
--- qemu-img.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
qemu-img.c +4 -6
@@ -479,12 +479,11 @@ fail:static void dump_json_image_check (ImageCheck *check,bool quiet) { -Error *local_err =NULL ;QString *str;QmpOutputVisitor *ov =qmp_output_visitor_new ();QObject *obj;visit_type_ImageCheck (qmp_output_get_visitor (ov),NULL , &check, - &local_err); + &error_abort); obj =qmp_output_get_qobject (ov); str =qobject_to_json_pretty (obj);assert (str !=NULL );
@@ -2170,12 +2169,11 @@ static void dump_snapshots(BlockDriverState *bs)static void dump_json_image_info_list (ImageInfoList *list) { -Error *local_err =NULL ;QString *str;QmpOutputVisitor *ov =qmp_output_visitor_new ();QObject *obj;visit_type_ImageInfoList (qmp_output_get_visitor (ov),NULL , &list, - &local_err); + &error_abort); obj =qmp_output_get_qobject (ov); str =qobject_to_json_pretty (obj);assert (str !=NULL );
@@ -2187,11 +2185,11 @@ static void dump_json_image_info_list(ImageInfoList *list)static void dump_json_image_info (ImageInfo *info) { -Error *local_err =NULL ;QString *str;QmpOutputVisitor *ov =qmp_output_visitor_new ();QObject *obj; -visit_type_ImageInfo (qmp_output_get_visitor (ov),NULL , &info, &local_err); +visit_type_ImageInfo (qmp_output_get_visitor (ov),NULL , &info, + &error_abort); obj =qmp_output_get_qobject (ov); str =qobject_to_json_pretty (obj);assert (str !=NULL );
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 03/28] qemu-img: Don't leak errors when outputting JSON
Quoted 2 lines · Eric Blake
If our JSON output ever encounters an error, we would just silently leak the local variable. Instead, assert that our usage won't fail.
Suggest "leak the error object". Can do on commit.
Quoted 1 line
Signed-off-by Eric Blake <[email protected]>
PATCH v4 4/28 qapi: Add parameter to visit_end_*
In reply to Eric BlakeRather 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.
---
v4: hoist earlier in series, document why void** is used, update docs/
v3: new patch
--- include/qapi/visitor.h | 32 ++++++++++++++++------------ include/qapi/visitor-impl.h | 6 +++--- scripts/qapi-commands.py | 4 ++-- scripts/qapi-event.py | 2 +- scripts/qapi-visit.py | 8 +++---- qapi/qapi-visit-core.c | 12 +++++------ block/crypto.c | 4 ++-- hw/ppc/spapr_drc.c | 4 ++-- hw/virtio/virtio-balloon.c | 4 ++-- qapi/opts-visitor.c | 4 ++-- qapi/qapi-dealloc-visitor.c | 47 +++-------------------------------------- qapi/qmp-input-visitor.c | 2 +- qapi/qmp-output-visitor.c | 4 ++-- qapi/string-input-visitor.c | 2 +- qapi/string-output-visitor.c | 2 +- qom/object.c | 2 +- qom/object_interfaces.c | 4 ++-- tests/test-qmp-input-visitor.c | 2 +- tests/test-qmp-output-visitor.c | 2 +- docs/qapi-code-gen.txt | 8 +++---- 20 files changed, 60 insertions(+), 95 deletions(-)
include/qapi/visitor.h +19 -13
@@ -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...
@@ -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?
@@ -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 ***/
@@ -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);
@@ -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 ***/
@@ -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,
@@ -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 ***/
include/qapi/visitor-impl.h +3 -3
@@ -47,7 +47,7 @@ struct Visitorvoid (*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. */
@@ -58,7 +58,7 @@ struct VisitorGenericList *(*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. */
@@ -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,
scripts/qapi-commands.py +2 -2
@@ -129,7 +129,7 @@ def gen_marshal(name, arg_type, ret_type): if (!err) { visit_check_struct(v, &err); } - visit_end_struct(v); + visit_end_struct(v, NULL); if (err) { goto out; }
@@ -160,7 +160,7 @@ out: v = qapi_dealloc_get_visitor(qdv); visit_start_struct(v, NULL, NULL, 0, NULL); visit_type_%(c_name)s_members(v, &arg, NULL); - visit_end_struct(v); + visit_end_struct(v, NULL); qapi_dealloc_visitor_cleanup(qdv); ''', c_name=arg_type.c_name())
scripts/qapi-event.py +1 -1
@@ -101,7 +101,7 @@ def gen_event_send(name, arg_type): if (!err) { visit_check_struct(v, &err); } - visit_end_struct(v); + visit_end_struct(v, NULL); if (err) { goto out; }
scripts/qapi-visit.py +4 -4
@@ -129,7 +129,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error } } - visit_end_list(v); + visit_end_list(v, (void **)obj); if (err && visit_is_input(v)) { qapi_free_%(c_name)s(*obj); *obj = NULL;
@@ -191,7 +191,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error if (!err) { visit_check_struct(v, &err); } - visit_end_struct(v); + visit_end_struct(v, NULL); ''', c_type=var.type.c_name(), c_name=c_name(var.name))
@@ -210,7 +210,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error error_setg(&err, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", "%(name)s"); } - visit_end_alternate(v); + visit_end_alternate(v, (void **)obj); if (err && visit_is_input(v)) { qapi_free_%(c_name)s(*obj); *obj = NULL;
@@ -244,7 +244,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error } 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_%(c_name)s(*obj); *obj = NULL;
qapi/qapi-visit-core.c +6 -6
@@ -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,
@@ -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,
@@ -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); } }
block/crypto.c +2 -2
@@ -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) {
@@ -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) {
hw/ppc/spapr_drc.c +2 -2
@@ -298,7 +298,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 ;
@@ -321,7 +321,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 :
hw/virtio/virtio-balloon.c +2 -2
@@ -143,13 +143,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); }
qapi/opts-visitor.c +2 -2
@@ -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);
@@ -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);
qapi/qapi-dealloc-visitor.c +3 -44
84 lines not on this page · raw message
qapi/qmp-input-visitor.c +1 -1
@@ -145,7 +145,7 @@ static void qmp_input_check_struct(Visitor *v, Error **errp) } } -static void qmp_input_pop (Visitor *v) +static void qmp_input_pop (Visitor *v,void **obj) {QmpInputVisitor *qiv =to_qiv (v);StackObject *tos = &qiv->stack[qiv->nb_stack -1 ];
qapi/qmp-output-visitor.c +2 -2
@@ -107,7 +107,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) +static void qmp_output_end_struct (Visitor *v,void **obj) {QmpOutputVisitor *qov =to_qov (v);QObject *value =qmp_output_pop (qov);
@@ -131,7 +131,7 @@ static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,return tail->next; } -static void qmp_output_end_list (Visitor *v) +static void qmp_output_end_list (Visitor *v,void **end) {QmpOutputVisitor *qov =to_qov (v);QObject *value =qmp_output_pop (qov);
qapi/string-input-visitor.c +1 -1
@@ -172,7 +172,7 @@ static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)return tail->next; } -static void end_list (Visitor *v) +static void end_list (Visitor *v,void **obj) { }
qapi/string-output-visitor.c +1 -1
@@ -291,7 +291,7 @@ static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)return ret; } -static void end_list (Visitor *v) +static void end_list (Visitor *v,void **obj) {StringOutputVisitor *sov =to_sov (v);
qom/object.c +1 -1
@@ -2038,7 +2038,7 @@ static void property_get_tm(Object *obj, Visitor *v, const char *name, }visit_check_struct (v, &err); out_end: -visit_end_struct (v); +visit_end_struct (v,NULL ); out:error_propagate (errp, err);
qom/object_interfaces.c +2 -2
@@ -71,7 +71,7 @@ Object *user_creatable_add(const QDict *qdict, obj =user_creatable_add_type (type, id, pdict, v, &local_err); out_visit: -visit_end_struct (v); +visit_end_struct (v,NULL ); out:QDECREF (pdict);
@@ -127,7 +127,7 @@ Object *user_creatable_add_type(const char *type, const char *id,if (!local_err) {visit_check_struct (v, &local_err); } -visit_end_struct (v); +visit_end_struct (v,NULL );if (local_err) { goto out; }
tests/test-qmp-input-visitor.c +1 -1
@@ -305,7 +305,7 @@ static void test_visitor_in_null(TestInputVisitorData *data,visit_type_null (v,"b" , &err);error_free_or_abort (&err);visit_check_struct (v, &error_abort); -visit_end_struct (v); +visit_end_struct (v,NULL ); } static void test_visitor_in_union_flat(TestInputVisitorData *data,
tests/test-qmp-output-visitor.c +1 -1
@@ -500,7 +500,7 @@ static void test_visitor_out_null(TestOutputVisitorData *data,visit_start_struct (data->ov,NULL ,NULL ,0 , &error_abort);visit_type_null (data->ov,"a" , &error_abort);visit_check_struct (data->ov, &error_abort); -visit_end_struct (data->ov); +visit_end_struct (data->ov,NULL ); arg =qmp_output_get_qobject (data->qov);g_assert (qobject_type (arg) ==QTYPE_QDICT ); qdict =qobject_to_qdict (arg);
docs/qapi-code-gen.txt +4 -4
@@ -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;
@@ -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;
@@ -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; }
@@ -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); }
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 04/28] qapi: Add parameter to visit_end_*
Quoted 19 lines · Eric Blake
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 callvisit_start, while using void** lets us usevisit_endwithout a cast. Also, an upcoming patch will add a clone visitor that wants to use the same implementation for all threevisit_endcallbacks, which is made easier if all three share the same signature.Signed-off-by Eric Blake <[email protected]>
[...]
qapi/qmp-input-visitor.c static void qmp_input_check_struct(Visitor *v, Error **errp) · line 151
Quoted 13 lines
qapi/qmp-input-visitor.c +1
-1@@ -145,7 +145,7 @@ static void qmp_input_check_struct(Visitor *v, Error **errp) } } -static void qmp_input_pop (Visitor *v) +static void qmp_input_pop (Visitor *v,void **obj) {QmpInputVisitor *qiv =to_qiv (v);StackObject *tos = &qiv->stack[qiv->nb_stack -1 ];
You could assert @obj matches tos->obj. Same for the other visitors that still need a stack. Adding a stack to the ones that don't just for the assertion seems excessive, though.
[...]
Re: [Qemu-devel] [PATCH v4 04/28] qapi: Add parameter to visit_end_*
Quoted 30 lines · Markus Armbruster
Quoted 19 lines · Eric Blake
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 callvisit_start, while using void** lets us usevisit_endwithout a cast. Also, an upcoming patch will add a clone visitor that wants to use the same implementation for all threevisit_endcallbacks, which is made easier if all three share the same signature.Signed-off-by Eric Blake <[email protected]>[...]
Quoted 8 lines
qapi/qmp-input-visitor.c +0
-0@@ -145,7 +145,7 @@ static void qmp_input_check_struct(Visitor *v, Error **errp) } }
Quoted 9 lines
qapi/qmp-input-visitor.c static void qmp_input_check_struct(Visitor *v, Error **errp) · line 151
Quoted 5 lines
qapi/qmp-input-visitor.c +1
-1-static void qmp_input_pop (Visitor *v) +static void qmp_input_pop (Visitor *v,void **obj) {QmpInputVisitor *qiv =to_qiv (v);StackObject *tos = &qiv->stack[qiv->nb_stack -1 ];You could assert @obj matches
tos->obj. Same for the other visitors that still need a stack. Adding a stack to the ones that don't just for the assertion seems excessive, though.
At this point, only the QMP visitors track a stack (the OptsVisitor does not, and we just got rid of the dealloc visitor stack); but since the string visitors only support a top-level visit with no struct or nested list, those can also support an assert. That makes 4 of the 6 visitors at this stage in the series; and only 4/8 when the clone and json visitors are added. I'll go ahead and add it in, though.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v4 5/28 qapi: Add new visit_free() function
In reply to Eric BlakeMaking each visitor provide its own (awkwardly-named) FOO_cleanup() is unusual, when we can instead have a polymorphic visit_free() interface.
The dealloc visitor is the first one converted to completely use the new entry point, since only generated code and the testsuite were using it. Diffs to the generated code look like:
| void qapi_free_ACPIOSTInfo(ACPIOSTInfo *obj)
| {
|- QapiDeallocVisitor *qdv;
| Visitor *v;
|
| if (!obj) {
| return;
| }
|
|- qdv = qapi_dealloc_visitor_new();
|- v = qapi_dealloc_get_visitor(qdv);
|+ v = qapi_dealloc_visitor_new();
| visit_type_ACPIOSTInfo(v, NULL, &obj, NULL);
|- qapi_dealloc_visitor_cleanup(qdv);
|+ visit_free(v);
|}---
v4: new patch, inspired by review of v3 7/18
--- include/qapi/visitor.h | 37 ++++++++++++++++++++++++++++++++++--- include/qapi/visitor-impl.h | 3 +++ scripts/qapi-commands.py | 16 ++++++---------- scripts/qapi-event.py | 2 +- scripts/qapi-types.py | 6 ++---- include/qapi/dealloc-visitor.h | 5 +---- qapi/qapi-visit-core.c | 7 +++++++ qapi/opts-visitor.c | 9 +++++++++ qapi/qapi-dealloc-visitor.c | 12 ++++-------- qapi/qmp-input-visitor.c | 7 +++++++ qapi/qmp-output-visitor.c | 7 +++++++ qapi/string-input-visitor.c | 7 +++++++ qapi/string-output-visitor.c | 7 +++++++ tests/test-visitor-serialization.c | 6 +++--- docs/qapi-code-gen.txt | 28 ++++++++++------------------ 15 files changed, 108 insertions(+), 51 deletions(-)
include/qapi/visitor.h +34 -3
@@ -37,6 +37,24 @@* implemented by each visitor, and docs/qapi-code-gen.txt for more * about the QAPI code generator. * +* All of the visitors are created via: +* +* Type *subtype_visitor_new(parameters...); +* +* where Type is either directly 'Visitor *', or is a subtype that can +* be trivially upcast to Visitor * via another function: +* +* Visitor *subtype_get_visitor(SubtypeVisitor *); +* +* A visitor should be used for exactly one top-level visit_type_FOO() +* or virtual walk, then passed to visit_free() to clean up resources. +* It is okay to free the visitor without completing the visit, if +* some other error is detected in the meantime. Output visitors +* provide an additional function, for collecting the final results of +* a successful visit: string_output_get_string() and +* qmp_output_get_qobject(); this collection function should not be +* called if any errors were reported during the visit. +* * All QAPI types have a corresponding function with a signature * roughly compatible with this: *
@@ -222,6 +240,19 @@ typedef struct GenericAlternate {char padding[]; } GenericAlternate; +/*** Visitor cleanup ***/ + +/* +* Free @v and any resources it has tied up. +* +* May be called whether or not the visit has been successfully +* completed, but should not be called until a top-level +* visit_type_FOO() or visit_start_ITEM() has been performed on the +* visitor. Safe if @v is NULL. +*/ +void visit_free (Visitor *v); + +/*** Visiting structures ***/ /*
@@ -272,7 +303,7 @@ void visit_check_struct(Visitor *v, Error **errp);* 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. +* with visit_free() behaves as if this was implicitly called. */ void visit_end_struct (Visitor *v,void **obj);
@@ -332,7 +363,7 @@ GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size);* 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. +* with visit_free() behaves as if this was implicitly called. */ void visit_end_list (Visitor *v,void **list);
@@ -368,7 +399,7 @@ void visit_start_alternate(Visitor *v, const char *name,* 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. +* with visit_free() behaves as if this was implicitly called. * */ void visit_end_alternate (Visitor *v,void **obj);
include/qapi/visitor-impl.h +3 -0
@@ -104,6 +104,9 @@ struct Visitor/* Must be set */ VisitorType type; + +/* Must be set */ + void (*free)(Visitor *v); };#endif
scripts/qapi-commands.py +6 -10
@@ -62,7 +62,6 @@ static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, { Error *err = NULL; QmpOutputVisitor *qov = qmp_output_visitor_new(); - QapiDeallocVisitor *qdv; Visitor *v; v = qmp_output_get_visitor(qov);
@@ -74,11 +73,10 @@ static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, out: error_propagate(errp, err); - qmp_output_visitor_cleanup(qov); - qdv = qapi_dealloc_visitor_new(); - v = qapi_dealloc_get_visitor(qdv); + visit_free(v); + v = qapi_dealloc_visitor_new(); visit_type_%(c_name)s(v, "unused", &ret_in, NULL); - qapi_dealloc_visitor_cleanup(qdv); + visit_free(v); } ''', c_type=ret_type.c_type(), c_name=ret_type.c_name())
@@ -116,7 +114,6 @@ def gen_marshal(name, arg_type, ret_type): if arg_type and arg_type.members: ret += mcgen(''' QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true); - QapiDeallocVisitor *qdv; Visitor *v; %(c_name)s arg = {0};
@@ -155,13 +152,12 @@ out: ''') if arg_type and arg_type.members: ret += mcgen(''' - qmp_input_visitor_cleanup(qiv); - qdv = qapi_dealloc_visitor_new(); - v = qapi_dealloc_get_visitor(qdv); + visit_free(v); + v = qapi_dealloc_visitor_new(); visit_start_struct(v, NULL, NULL, 0, NULL); visit_type_%(c_name)s_members(v, &arg, NULL); visit_end_struct(v, NULL); - qapi_dealloc_visitor_cleanup(qdv); + visit_free(v); ''', c_name=arg_type.c_name())
scripts/qapi-event.py +1 -1
@@ -119,7 +119,7 @@ def gen_event_send(name, arg_type): if arg_type and arg_type.members: ret += mcgen(''' out: - qmp_output_visitor_cleanup(qov); + visit_free(v); ''') ret += mcgen(''' error_propagate(errp, err);
scripts/qapi-types.py +2 -4
@@ -150,17 +150,15 @@ def gen_type_cleanup(name): void qapi_free_%(c_name)s(%(c_name)s *obj) { - QapiDeallocVisitor *qdv; Visitor *v; if (!obj) { return; } - qdv = qapi_dealloc_visitor_new(); - v = qapi_dealloc_get_visitor(qdv); + v = qapi_dealloc_visitor_new(); visit_type_%(c_name)s(v, NULL, &obj, NULL); - qapi_dealloc_visitor_cleanup(qdv); + visit_free(v); } ''', c_name=c_name(name))
include/qapi/dealloc-visitor.h +1 -4
@@ -23,9 +23,6 @@ typedef struct QapiDeallocVisitor QapiDeallocVisitor;* qapi_free_FOO() functions, and is the only visitor designed to work * correctly in the face of a partially-constructed QAPI tree. */ -QapiDeallocVisitor *qapi_dealloc_visitor_new (void ); -void qapi_dealloc_visitor_cleanup (QapiDeallocVisitor *d); - -Visitor *qapi_dealloc_get_visitor (QapiDeallocVisitor *v); +Visitor *qapi_dealloc_visitor_new (void );#endif
qapi/qapi-visit-core.c +7 -0
@@ -20,6 +20,13 @@#include "qapi/visitor.h" #include "qapi/visitor-impl.h" +void visit_free (Visitor *v) +{ +if (v) { + v->free (v); + } +} +void visit_start_struct (Visitor *v,const char *name,void **obj,size_t size,Error **errp) {
qapi/opts-visitor.c +9 -0
@@ -513,6 +513,14 @@ opts_optional(Visitor *v, const char *name, bool *present) } +static void +opts_free (Visitor *v) +{ +OptsVisitor *ov =to_ov (v); +opts_visitor_cleanup (ov); +} + +OptsVisitor *opts_visitor_new (const QemuOpts *opts) {
@@ -540,6 +548,7 @@ opts_visitor_new(const QemuOpts *opts)* skip some mandatory methods... */ ov->visitor.optional = &opts_optional; + ov->visitor.free = opts_free; ov->opts_root = opts;
qapi/qapi-dealloc-visitor.c +4 -8
@@ -107,17 +107,12 @@ static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp) { } -Visitor *qapi_dealloc_get_visitor (QapiDeallocVisitor *v) -{ -return &v->visitor; -} - -void qapi_dealloc_visitor_cleanup (QapiDeallocVisitor *v) +static void qapi_dealloc_free (Visitor *v) {g_free (v); } -QapiDeallocVisitor *qapi_dealloc_visitor_new (void ) +Visitor *qapi_dealloc_visitor_new (void ) {QapiDeallocVisitor *v;
@@ -138,6 +133,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void) v->visitor.type_number = qapi_dealloc_type_number; v->visitor.type_any = qapi_dealloc_type_anything; v->visitor.type_null = qapi_dealloc_type_null; + v->visitor.free = qapi_dealloc_free; -return v; +return &v->visitor; }
qapi/qmp-input-visitor.c +7 -0
@@ -375,6 +375,12 @@ Visitor *qmp_input_get_visitor(QmpInputVisitor *v)return &v->visitor; } +static void qmp_input_free (Visitor *v) +{ +QmpInputVisitor *qiv =to_qiv (v); +qmp_input_visitor_cleanup (qiv); +} +void qmp_input_visitor_cleanup (QmpInputVisitor *v) {qobject_decref (v->root);
@@ -403,6 +409,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict) v->visitor.type_any = qmp_input_type_any; v->visitor.type_null = qmp_input_type_null; v->visitor.optional = qmp_input_optional; + v->visitor.free = qmp_input_free; v->strict = strict; v->root = obj;
qapi/qmp-output-visitor.c +7 -0
@@ -209,6 +209,12 @@ Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)return &v->visitor; } +static void qmp_output_free (Visitor *v) +{ +QmpOutputVisitor *qov =to_qov (v); +qmp_output_visitor_cleanup (qov); +} +void qmp_output_visitor_cleanup (QmpOutputVisitor *v) {QStackEntry *e, *tmp;
@@ -241,6 +247,7 @@ QmpOutputVisitor *qmp_output_visitor_new(void) v->visitor.type_number = qmp_output_type_number; v->visitor.type_any = qmp_output_type_any; v->visitor.type_null = qmp_output_type_null; + v->visitor.free = qmp_output_free;QTAILQ_INIT (&v->stack);
qapi/string-input-visitor.c +7 -0
@@ -330,6 +330,12 @@ Visitor *string_input_get_visitor(StringInputVisitor *v)return &v->visitor; } +static void string_input_free (Visitor *v) +{ +StringInputVisitor *siv =to_siv (v); +string_input_visitor_cleanup (siv); +} +void string_input_visitor_cleanup (StringInputVisitor *v) {g_list_foreach (v->ranges, free_range,NULL );
@@ -354,6 +360,7 @@ StringInputVisitor *string_input_visitor_new(const char *str) v->visitor.next_list = next_list; v->visitor.end_list = end_list; v->visitor.optional = parse_optional; + v->visitor.free = string_input_free; v->string = str;return v;
qapi/string-output-visitor.c +7 -0
@@ -319,6 +319,12 @@ static void free_range(void *range, void *dummy)g_free (range); } +static void string_output_free (Visitor *v) +{ +StringOutputVisitor *sov =to_sov (v); +string_output_visitor_cleanup (sov); +} +void string_output_visitor_cleanup (StringOutputVisitor *sov) {if (sov->string) {
@@ -348,6 +354,7 @@ StringOutputVisitor *string_output_visitor_new(bool human) v->visitor.start_list = start_list; v->visitor.next_list = next_list; v->visitor.end_list = end_list; + v->visitor.free = string_output_free;return v; }
tests/test-visitor-serialization.c +3 -3
@@ -90,11 +90,11 @@ typedef void (*VisitorFunc)(Visitor *v, void **native, Error **errp);static void dealloc_helper (void *native_in,VisitorFunc visit,Error **errp) { -QapiDeallocVisitor *qdv =qapi_dealloc_visitor_new (); +Visitor *v =qapi_dealloc_visitor_new (); -visit (qapi_dealloc_get_visitor (qdv), &native_in, errp); +visit (v, &native_in, errp); -qapi_dealloc_visitor_cleanup (qdv); +visit_free (v); }static void visit_primitive_type (Visitor *v,void **native,Error **errp)
docs/qapi-code-gen.txt +10 -18
@@ -802,32 +802,28 @@ Example: void qapi_free_UserDefOne(UserDefOne *obj) { - QapiDeallocVisitor *qdv; Visitor *v; if (!obj) { return; } - qdv = qapi_dealloc_visitor_new(); - v = qapi_dealloc_get_visitor(qdv); + v = qapi_dealloc_visitor_new(); visit_type_UserDefOne(v, NULL, &obj, NULL); - qapi_dealloc_visitor_cleanup(qdv); + visit_free(v); } void qapi_free_UserDefOneList(UserDefOneList *obj) { - QapiDeallocVisitor *qdv; Visitor *v; if (!obj) { return; } - qdv = qapi_dealloc_visitor_new(); - v = qapi_dealloc_get_visitor(qdv); + v = qapi_dealloc_visitor_new(); visit_type_UserDefOneList(v, NULL, &obj, NULL); - qapi_dealloc_visitor_cleanup(qdv); + visit_free(v); } === scripts/qapi-visit.py ===
@@ -985,7 +981,6 @@ Example: { Error *err = NULL; QmpOutputVisitor *qov = qmp_output_visitor_new(); - QapiDeallocVisitor *qdv; Visitor *v; v = qmp_output_get_visitor(qov);
@@ -997,11 +992,10 @@ Example: out: error_propagate(errp, err); - qmp_output_visitor_cleanup(qov); - qdv = qapi_dealloc_visitor_new(); - v = qapi_dealloc_get_visitor(qdv); + visit_free(v); + v = qapi_dealloc_visitor_new(); visit_type_UserDefOne(v, "unused", &ret_in, NULL); - qapi_dealloc_visitor_cleanup(qdv); + visit_free(v); } static void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp)
@@ -1009,7 +1003,6 @@ Example: Error *err = NULL; UserDefOne *retval; QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true); - QapiDeallocVisitor *qdv; Visitor *v; UserDefOneList *arg1 = NULL;
@@ -1036,13 +1029,12 @@ Example: out: error_propagate(errp, err); - qmp_input_visitor_cleanup(qiv); - qdv = qapi_dealloc_visitor_new(); - v = qapi_dealloc_get_visitor(qdv); + visit_free(v); + v = qapi_dealloc_visitor_new(); visit_start_struct(v, NULL, NULL, 0, NULL); visit_type_UserDefOneList(v, "arg1", &arg1, NULL); visit_end_struct(v, NULL); - qapi_dealloc_visitor_cleanup(qdv); + visit_free(v); } static void qmp_init_marshal(void)
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 05/28] qapi: Add new visit_free() function
Quoted 8 lines · Eric Blake
Making each visitor provide its own (awkwardly-named)
FOO_cleanup()is unusual, when we can instead have a polymorphicvisit_free()interface.The dealloc visitor is the first one converted to completely use the new entry point, since only generated code and the testsuite were using it. Diffs to the generated code look like:
Quoted 12 lines
| void qapi_free_ACPIOSTInfo(ACPIOSTInfo *obj)
| {
|- QapiDeallocVisitor *qdv;
| Visitor *v;
|
| if (!obj) {
| return;
| }
|
|- qdv = qapi_dealloc_visitor_new();
|- v = qapi_dealloc_get_visitor(qdv);
|+ v = qapi_dealloc_visitor_new();At this point, I wonder what this change has to do with the new visit_free(). It turns out that...
Quoted 2 lines
| visit_type_ACPIOSTInfo(v, NULL, &obj, NULL); |- qapi_dealloc_visitor_cleanup(qdv);
... this call is the only use of qdv, so you're eliminating it.
Next, I wonder whether this is worthwhile, as it creates an inconsistency with the other visitors. Peeking ahead, I see the inconsistency is temporary: you're eliminating the other _get_visitor() later in this series. Okay, but perhaps you can explain your intentions further up in the commit message.
Quoted 4 lines
|+
visit_free(v);
|}Signed-off-by Eric Blake <[email protected]>
[...]
qapi/opts-visitor.c opts_optional(Visitor *v, const char *name, bool *present) · line 520
Quoted 13 lines
qapi/opts-visitor.c +5
-0@@ -513,6 +513,14 @@ opts_optional(Visitor *v, const char *name, bool *present) } +static void +opts_free (Visitor *v) +{ +OptsVisitor *ov =to_ov (v); +opts_visitor_cleanup (ov);
I'd eliminate the variable:
opts_visitor_cleanup(to_ov(v));
Hmm, you put it to use when you inline opts_visitor_cleanup() in the next patch. Doesn't matter then.
Quoted 21 lines
qapi/opts-visitor.c +4
-0+} + +OptsVisitor *opts_visitor_new (const QemuOpts *opts) {@@ -540,6 +548,7 @@ opts_visitor_new(const QemuOpts *opts)* skip some mandatory methods... */ ov->visitor.optional = &opts_optional; + ov->visitor.free = opts_free; ov->opts_root = opts;qapi/qapi-dealloc-visitor.c +0
-0@@ -107,17 +107,12 @@ static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp) { }
qapi/qapi-dealloc-visitor.c static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp) · line 112
Quoted 10 lines
qapi/qapi-dealloc-visitor.c +1
-6-Visitor *qapi_dealloc_get_visitor (QapiDeallocVisitor *v) -{ -return &v->visitor; -} - -void qapi_dealloc_visitor_cleanup (QapiDeallocVisitor *v) +static void qapi_dealloc_free (Visitor *v) {g_free (v);
Uh, shouldn't this be g_free(v, QapiDeallocVisitor, visitor)? That way, we don't assume that visitor is QapiDeallocVisitor's first member.
Quoted 21 lines
qapi/qapi-dealloc-visitor.c +3
-2} -QapiDeallocVisitor *qapi_dealloc_visitor_new (void ) +Visitor *qapi_dealloc_visitor_new (void ) {QapiDeallocVisitor *v;@@ -138,6 +133,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void) v->visitor.type_number = qapi_dealloc_type_number; v->visitor.type_any = qapi_dealloc_type_anything; v->visitor.type_null = qapi_dealloc_type_null; + v->visitor.free = qapi_dealloc_free; -return v; +return &v->visitor; }qapi/qmp-input-visitor.c +0
-0
qapi/qmp-input-visitor.c Visitor *qmp_input_get_visitor(QmpInputVisitor *v) · line 383
Quoted 9 lines
qapi/qmp-input-visitor.c +6
-0@@ -375,6 +375,12 @@ Visitor *qmp_input_get_visitor(QmpInputVisitor *v)return &v->visitor; } +static void qmp_input_free (Visitor *v) +{ +QmpInputVisitor *qiv =to_qiv (v); +qmp_input_visitor_cleanup (qiv); +} +
Like for opts_free(), the variable becomes useful when qmp_input_visitor_cleanup() gets inlined in a later patch.
Same for the other free methods.
Quoted 11 lines
qapi/qmp-input-visitor.c +1
-0void qmp_input_visitor_cleanup (QmpInputVisitor *v) {qobject_decref (v->root);@@ -403,6 +409,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict) v->visitor.type_any = qmp_input_type_any; v->visitor.type_null = qmp_input_type_null; v->visitor.optional = qmp_input_optional; + v->visitor.free = qmp_input_free; v->strict = strict; v->root = obj;
[...]
Re: [Qemu-devel] [PATCH v4 05/28] qapi: Add new visit_free() function
Quoted 1 line · Markus Armbruster
Eric Blake <[email protected]> writes:
[...]
Quoted 8 lines
Quoted 8 lines
qapi/qapi-dealloc-visitor.c +0
-0@@ -107,17 +107,12 @@ static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp) { }
Quoted 12 lines
qapi/qapi-dealloc-visitor.c static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp) · line 112
Quoted 9 lines
qapi/qapi-dealloc-visitor.c +1
-6-Visitor *qapi_dealloc_get_visitor (QapiDeallocVisitor *v) -{ -return &v->visitor; -} - -void qapi_dealloc_visitor_cleanup (QapiDeallocVisitor *v) +static void qapi_dealloc_free (Visitor *v) {g_free (v);Uh, shouldn't this be
g_free(v, QapiDeallocVisitor, visitor)? That way, we don't assume that visitor is QapiDeallocVisitor's first member.
I mean g_free(container_of(v, QapiDeallocVisitor, visitor), of course.
Quoted 8 lines
Quoted 8 lines
qapi/qapi-dealloc-visitor.c +1
-1} -QapiDeallocVisitor *qapi_dealloc_visitor_new (void ) +Visitor *qapi_dealloc_visitor_new (void ) {QapiDeallocVisitor *v;
[...]
PATCH v4 6/28 opts-visitor: Favor new visit_free() function
In reply to Eric BlakeNow that we have a polymorphic visit_free(), we no longer need opts_visitor_cleanup(); which in turn means we no longer need to return a subtype from opts_visitor_new() nor a public upcast function.
---
v4: new patch
--- include/qapi/opts-visitor.h | 4 +--- block/crypto.c | 30 ++++++++++++++---------------- hmp.c | 8 ++++---- hw/acpi/core.c | 8 ++++---- net/net.c | 5 ++--- numa.c | 6 +++--- qapi/opts-visitor.c | 26 ++++++-------------------- qom/object_interfaces.c | 8 ++++---- tests/test-opts-visitor.c | 9 ++++----- 9 files changed, 42 insertions(+), 62 deletions(-)
include/qapi/opts-visitor.h +1 -3
@@ -35,8 +35,6 @@ typedef struct OptsVisitor OptsVisitor;* QTypes. It also requires a non-null list argument to * visit_start_list(). */ -OptsVisitor *opts_visitor_new (const QemuOpts *opts); -void opts_visitor_cleanup (OptsVisitor *nv); -Visitor *opts_get_visitor (OptsVisitor *nv); +Visitor *opts_visitor_new (const QemuOpts *opts);#endif
block/crypto.c +14 -16
@@ -193,17 +193,16 @@ block_crypto_open_opts_init(QCryptoBlockFormat format, QemuOpts *opts, Error **errp) { -OptsVisitor *ov; +Visitor *v;QCryptoBlockOpenOptions *ret =NULL ;Error *local_err =NULL ; ret =g_new0 (QCryptoBlockOpenOptions,1 ); ret->format = format; - ov =opts_visitor_new (opts); + v =opts_visitor_new (opts); -visit_start_struct (opts_get_visitor (ov), -NULL ,NULL ,0 , &local_err); +visit_start_struct (v,NULL ,NULL ,0 , &local_err);if (local_err) { goto out; }
@@ -211,7 +210,7 @@ block_crypto_open_opts_init(QCryptoBlockFormat format,switch (format) {case Q_CRYPTO_BLOCK_FORMAT_LUKS :visit_type_QCryptoBlockOptionsLUKS_members ( -opts_get_visitor (ov), &ret->u.luks, &local_err); + v, &ret->u.luks, &local_err);break ;default :
@@ -219,10 +218,10 @@ block_crypto_open_opts_init(QCryptoBlockFormat format,break ; }if (!local_err) { -visit_check_struct (opts_get_visitor (ov), &local_err); +visit_check_struct (v, &local_err); } -visit_end_struct (opts_get_visitor (ov),NULL ); +visit_end_struct (v,NULL ); out:if (local_err) {
@@ -230,7 +229,7 @@ block_crypto_open_opts_init(QCryptoBlockFormat format,qapi_free_QCryptoBlockOpenOptions (ret); ret =NULL ; } -opts_visitor_cleanup (ov); +visit_free (v);return ret; }
@@ -240,17 +239,16 @@ block_crypto_create_opts_init(QCryptoBlockFormat format, QemuOpts *opts, Error **errp) { -OptsVisitor *ov; +Visitor *v;QCryptoBlockCreateOptions *ret =NULL ;Error *local_err =NULL ; ret =g_new0 (QCryptoBlockCreateOptions,1 ); ret->format = format; - ov =opts_visitor_new (opts); + v =opts_visitor_new (opts); -visit_start_struct (opts_get_visitor (ov), -NULL ,NULL ,0 , &local_err); +visit_start_struct (v,NULL ,NULL ,0 , &local_err);if (local_err) { goto out; }
@@ -258,7 +256,7 @@ block_crypto_create_opts_init(QCryptoBlockFormat format,switch (format) {case Q_CRYPTO_BLOCK_FORMAT_LUKS :visit_type_QCryptoBlockCreateOptionsLUKS_members ( -opts_get_visitor (ov), &ret->u.luks, &local_err); + v, &ret->u.luks, &local_err);break ;default :
@@ -266,10 +264,10 @@ block_crypto_create_opts_init(QCryptoBlockFormat format,break ; }if (!local_err) { -visit_check_struct (opts_get_visitor (ov), &local_err); +visit_check_struct (v, &local_err); } -visit_end_struct (opts_get_visitor (ov),NULL ); +visit_end_struct (v,NULL ); out:if (local_err) {
@@ -277,7 +275,7 @@ block_crypto_create_opts_init(QCryptoBlockFormat format,qapi_free_QCryptoBlockCreateOptions (ret); ret =NULL ; } -opts_visitor_cleanup (ov); +visit_free (v);return ret; }
hmp.c +4 -4
@@ -1675,7 +1675,7 @@ void hmp_object_add(Monitor *mon, const QDict *qdict) {Error *err =NULL ;QemuOpts *opts; -OptsVisitor *ov; +Visitor *v;Object *obj =NULL ; opts =qemu_opts_from_qdict (qemu_find_opts ("object" ), qdict, &err);
@@ -1684,9 +1684,9 @@ void hmp_object_add(Monitor *mon, const QDict *qdict)return ; } - ov =opts_visitor_new (opts); - obj =user_creatable_add (qdict,opts_get_visitor (ov), &err); -opts_visitor_cleanup (ov); + v =opts_visitor_new (opts); + obj =user_creatable_add (qdict, v, &err); +visit_free (v);qemu_opts_del (opts);if (err) {
hw/acpi/core.c +4 -4
@@ -239,11 +239,11 @@ void acpi_table_add(const QemuOpts *opts, Error **errp)char unsigned *blob =NULL ; { -OptsVisitor *ov; +Visitor *v; - ov =opts_visitor_new (opts); -visit_type_AcpiTableOptions (opts_get_visitor (ov),NULL , &hdrs, &err); -opts_visitor_cleanup (ov); + v =opts_visitor_new (opts); +visit_type_AcpiTableOptions (v,NULL , &hdrs, &err); +visit_free (v); }if (err) {
net/net.c +2 -3
@@ -1026,8 +1026,7 @@ int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)void *object =NULL ;Error *err =NULL ;int ret =-1 ; -OptsVisitor *ov =opts_visitor_new (opts); -Visitor *v =opts_get_visitor (ov); +Visitor *v =opts_visitor_new (opts); {/* Parse convenience option format ip6-net=fec0::0[/64] */
@@ -1077,7 +1076,7 @@ int net_client_init(QemuOpts *opts, int is_netdev, Error **errp) }error_propagate (errp, err); -opts_visitor_cleanup (ov); +visit_free (v);return ret; }
numa.c +3 -3
@@ -217,9 +217,9 @@ static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)Error *err =NULL ; { -OptsVisitor *ov =opts_visitor_new (opts); -visit_type_NumaOptions (opts_get_visitor (ov),NULL , &object, &err); -opts_visitor_cleanup (ov); +Visitor *v =opts_visitor_new (opts); +visit_type_NumaOptions (v,NULL , &object, &err); +visit_free (v); }if (err) {
qapi/opts-visitor.c +6 -20
@@ -517,11 +517,15 @@ static void opts_free(Visitor *v) {OptsVisitor *ov =to_ov (v); -opts_visitor_cleanup (ov); +if (ov->unprocessed_opts !=NULL ) { +g_hash_table_destroy (ov->unprocessed_opts); + } +g_free (ov->fake_id_opt); +g_free (ov); } -OptsVisitor * +Visitor *opts_visitor_new (const QemuOpts *opts) {OptsVisitor *ov;
@@ -552,23 +556,5 @@ opts_visitor_new(const QemuOpts *opts) ov->opts_root = opts; -return ov; -} - - -void -opts_visitor_cleanup (OptsVisitor *ov) -{ -if (ov->unprocessed_opts !=NULL ) { -g_hash_table_destroy (ov->unprocessed_opts); - } -g_free (ov->fake_id_opt); -g_free (ov); -} - - -Visitor * -opts_get_visitor (OptsVisitor *ov) -{return &ov->visitor; }
qom/object_interfaces.c +4 -4
@@ -156,15 +156,15 @@ out:Object *user_creatable_add_opts (QemuOpts *opts,Error **errp) { -OptsVisitor *ov; +Visitor *v;QDict *pdict;Object *obj =NULL ; - ov =opts_visitor_new (opts); + v =opts_visitor_new (opts); pdict =qemu_opts_to_qdict (opts,NULL ); - obj =user_creatable_add (pdict,opts_get_visitor (ov), errp); -opts_visitor_cleanup (ov); + obj =user_creatable_add (pdict, v, errp); +visit_free (v);QDECREF (pdict);return obj; }
tests/test-opts-visitor.c +4 -5
@@ -38,16 +38,15 @@ setup_fixture(OptsVisitorFixture *f, gconstpointer test_data) {const char *opts_string = test_data;QemuOpts *opts; -OptsVisitor *ov; +Visitor *v; opts =qemu_opts_parse (qemu_find_opts ("userdef" ), opts_string, false,NULL );g_assert (opts !=NULL ); - ov =opts_visitor_new (opts); -visit_type_UserDefOptions (opts_get_visitor (ov),NULL , &f->userdef, - &f->err); -opts_visitor_cleanup (ov); + v =opts_visitor_new (opts); +visit_type_UserDefOptions (v,NULL , &f->userdef, &f->err); +visit_free (v);qemu_opts_del (opts); }
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 06/28] opts-visitor: Favor new visit_free() function
Quoted 6 lines · Eric Blake
Now that we have a polymorphic
visit_free(), we no longer needopts_visitor_cleanup(); which in turn means we no longer need to return a subtype fromopts_visitor_new()nor a public upcast function.Signed-off-by Eric Blake <[email protected]>
[...]
qapi/opts-visitor.c static void · line 520
Quoted 9 lines
qapi/opts-visitor.c +0
-1@@ -517,11 +517,15 @@ static void opts_free(Visitor *v) {OptsVisitor *ov =to_ov (v); -opts_visitor_cleanup (ov);
Blank line between declaration and statements, please.
Quoted 13 lines
qapi/opts-visitor.c +6
-1+if (ov->unprocessed_opts !=NULL ) { +g_hash_table_destroy (ov->unprocessed_opts); + } +g_free (ov->fake_id_opt); +g_free (ov); } -OptsVisitor * +Visitor *opts_visitor_new (const QemuOpts *opts) {OptsVisitor *ov;
[...]
PATCH v4 7/28 string-input-visitor: Favor new visit_free() function
In reply to Eric BlakeNow that we have a polymorphic visit_free(), we no longer need string_input_visitor_cleanup(); which in turn means we no longer need to return a subtype from string_input_visitor_new() nor a public upcast function.
---
v4: new patch
--- include/qapi/string-input-visitor.h | 5 +---- qapi/string-input-visitor.c | 19 +++++-------------- qom/object.c | 25 +++++++++++-------------- tests/test-string-input-visitor.c | 22 +++++++--------------- tests/test-visitor-serialization.c | 6 +++--- 5 files changed, 27 insertions(+), 50 deletions(-)
include/qapi/string-input-visitor.h +1 -4
@@ -22,9 +22,6 @@ typedef struct StringInputVisitor StringInputVisitor;* 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); - -Visitor *string_input_get_visitor (StringInputVisitor *v); +Visitor *string_input_visitor_new (const char *str);#endif
qapi/string-input-visitor.c +5 -14
@@ -325,25 +325,16 @@ static void parse_optional(Visitor *v, const char *name, bool *present) *present = true; } -Visitor *string_input_get_visitor (StringInputVisitor *v) -{ -return &v->visitor; -} -static void string_input_free (Visitor *v) {StringInputVisitor *siv =to_siv (v); -string_input_visitor_cleanup (siv); -} -void string_input_visitor_cleanup (StringInputVisitor *v) -{ -g_list_foreach (v->ranges, free_range,NULL ); -g_list_free (v->ranges); -g_free (v); +g_list_foreach (siv->ranges, free_range,NULL ); +g_list_free (siv->ranges); +g_free (siv); } -StringInputVisitor *string_input_visitor_new (const char *str) +Visitor *string_input_visitor_new (const char *str) {StringInputVisitor *v;
@@ -363,5 +354,5 @@ StringInputVisitor *string_input_visitor_new(const char *str) v->visitor.free = string_input_free; v->string = str; -return v; +return &v->visitor; }
qom/object.c +11 -14
@@ -1216,7 +1216,7 @@ int object_property_get_enum(Object *obj, const char *name, {Error *err =NULL ;StringOutputVisitor *sov; -StringInputVisitor *siv; +Visitor *v;char *str;int ret;ObjectProperty *prop =object_property_find (obj, name, errp);
@@ -1243,13 +1243,12 @@ int object_property_get_enum(Object *obj, const char *name,return 0 ; } str =string_output_get_string (sov); - siv =string_input_visitor_new (str);string_output_visitor_cleanup (sov); -visit_type_enum (string_input_get_visitor (siv), name, &ret, - enumprop->strings, errp); + v =string_input_visitor_new (str); +visit_type_enum (v, name, &ret, enumprop->strings, errp);g_free (str); -string_input_visitor_cleanup (siv); +visit_free (v);return ret; }
@@ -1259,7 +1258,7 @@ void object_property_get_uint16List(Object *obj, const char *name, {Error *err =NULL ;StringOutputVisitor *ov; -StringInputVisitor *iv; +Visitor *v;char *str; ov =string_output_visitor_new (false);
@@ -1270,11 +1269,11 @@ void object_property_get_uint16List(Object *obj, const char *name, goto out; } str =string_output_get_string (ov); - iv =string_input_visitor_new (str); -visit_type_uint16List (string_input_get_visitor (iv),NULL , list, errp); + v =string_input_visitor_new (str); +visit_type_uint16List (v,NULL , list, errp);g_free (str); -string_input_visitor_cleanup (iv); +visit_free (v); out:string_output_visitor_cleanup (ov); }
@@ -1282,11 +1281,9 @@ out:void object_property_parse (Object *obj,const char *string,const char *name,Error **errp) { -StringInputVisitor *siv; - siv =string_input_visitor_new (string); -object_property_set (obj,string_input_get_visitor (siv), name, errp); - -string_input_visitor_cleanup (siv); +Visitor *v =string_input_visitor_new (string); +object_property_set (obj, v, name, errp); +visit_free (v); } char *object_property_print(Object *obj, const char *name, bool human,
tests/test-string-input-visitor.c +7 -15
@@ -21,15 +21,15 @@#include "qapi/qmp/types.h" typedef struct TestInputVisitorData { -StringInputVisitor *siv; +Visitor *v; }TestInputVisitorData ;static void visitor_input_teardown (TestInputVisitorData *data,const void *unused) { -if (data->siv) { -string_input_visitor_cleanup (data->siv); - data->siv =NULL ; +if (data->v) { +visit_free (data->v); + data->v =NULL ; } }
@@ -40,15 +40,9 @@ staticVisitor *visitor_input_test_init (TestInputVisitorData *data,const char *string) { -Visitor *v; - - data->siv =string_input_visitor_new (string); -g_assert (data->siv !=NULL ); - - v =string_input_get_visitor (data->siv); -g_assert (v !=NULL ); - -return v; + data->v =string_input_visitor_new (string); +g_assert (data->v); +return data->v; } static void test_visitor_in_int(TestInputVisitorData *data,
@@ -200,8 +194,6 @@ static void test_visitor_in_enum(TestInputVisitorData *data,visitor_input_teardown (data,NULL ); } - - data->siv =NULL ; }/* Try to crash the visitors */
tests/test-visitor-serialization.c +3 -3
@@ -1057,7 +1057,7 @@ static void qmp_cleanup(void *datap)typedef struct StringSerializeData {char *string;StringOutputVisitor *sov; -StringInputVisitor *siv; +Visitor *siv; }StringSerializeData ; static void string_serialize(void *native_in, void **datap,
@@ -1077,7 +1077,7 @@ static void string_deserialize(void **native_out, void *datap, d->string =string_output_get_string (d->sov); d->siv =string_input_visitor_new (d->string); -visit (string_input_get_visitor (d->siv), native_out, errp); +visit (d->siv, native_out, errp); }static void string_cleanup (void *datap)
@@ -1085,7 +1085,7 @@ static void string_cleanup(void *datap)StringSerializeData *d = datap;string_output_visitor_cleanup (d->sov); -string_input_visitor_cleanup (d->siv); +visit_free (d->siv);g_free (d->string);g_free (d); }
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 07/28] string-input-visitor: Favor new visit_free() function
Quoted 6 lines · Eric Blake
Now that we have a polymorphic
visit_free(), we no longer needstring_input_visitor_cleanup(); which in turn means we no longer need to return a subtype fromstring_input_visitor_new()nor a public upcast function.Signed-off-by Eric Blake <[email protected]>
[...]
Quoted 55 lines
qom/object.c +8
-9@@ -1216,7 +1216,7 @@ int object_property_get_enum(Object *obj, const char *name, {Error *err =NULL ;StringOutputVisitor *sov; -StringInputVisitor *siv; +Visitor *v;char *str;int ret;ObjectProperty *prop =object_property_find (obj, name, errp);@@ -1243,13 +1243,12 @@ int object_property_get_enum(Object *obj, const char *name,return 0 ; } str =string_output_get_string (sov); - siv =string_input_visitor_new (str);string_output_visitor_cleanup (sov); -visit_type_enum (string_input_get_visitor (siv), name, &ret, - enumprop->strings, errp); + v =string_input_visitor_new (str); +visit_type_enum (v, name, &ret, enumprop->strings, errp);g_free (str); -string_input_visitor_cleanup (siv); +visit_free (v);return ret; }@@ -1259,7 +1258,7 @@ void object_property_get_uint16List(Object *obj, const char *name, {Error *err =NULL ;StringOutputVisitor *ov; -StringInputVisitor *iv; +Visitor *v;char *str; ov =string_output_visitor_new (false);@@ -1270,11 +1269,11 @@ void object_property_get_uint16List(Object *obj, const char *name, goto out; } str =string_output_get_string (ov); - iv =string_input_visitor_new (str); -visit_type_uint16List (string_input_get_visitor (iv),NULL , list, errp); + v =string_input_visitor_new (str); +visit_type_uint16List (v,NULL , list, errp);g_free (str); -string_input_visitor_cleanup (iv); +visit_free (v); out:string_output_visitor_cleanup (ov); }
qom/object.c out: · line 1284
Quoted 9 lines
qom/object.c +1
-5@@ -1282,11 +1281,9 @@ out:void object_property_parse (Object *obj,const char *string,const char *name,Error **errp) { -StringInputVisitor *siv; - siv =string_input_visitor_new (string); -object_property_set (obj,string_input_get_visitor (siv), name, errp); - -string_input_visitor_cleanup (siv); +Visitor *v =string_input_visitor_new (string);
Blank line between declaration and statements, please.
Quoted 5 lines
qom/object.c +2
-0+object_property_set (obj, v, name, errp); +visit_free (v); } char *object_property_print(Object *obj, const char *name, bool human,
[...]
PATCH v4 8/28 qmp-input-visitor: Favor new visit_free() function
In reply to Eric BlakeNow that we have a polymorphic visit_free(), we no longer need qmp_input_visitor_cleanup(); which in turn means we no longer need to return a subtype from qmp_input_visitor_new() nor a public upcast function.
Generated code changes to qmp-marshal look like:
|@@ -52,11 +52,10 @@ void qmp_marshal_add_fd(QDict *args, QOb
| {
| Error *err = NULL;
| AddfdInfo *retval;
|- QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
| Visitor *v;
| q_obj_add_fd_arg arg = {0};
|
|- v = qmp_input_get_visitor(qiv);
|+ v = qmp_input_visitor_new(QOBJECT(args), true);
| visit_start_struct(v, NULL, NULL, 0, &err);
| if (err) {
| goto out;---
v4: new patch
--- scripts/qapi-commands.py | 3 +-- include/qapi/qmp-input-visitor.h | 6 +----- qapi/qmp-input-visitor.c | 17 ++++------------- qmp.c | 9 ++++----- qom/qom-qobject.c | 9 ++++----- replay/replay-input.c | 6 ++---- tests/check-qnull.c | 6 +++--- tests/test-qmp-commands.c | 8 ++++---- tests/test-qmp-input-strict.c | 12 +++--------- tests/test-qmp-input-visitor.c | 12 +++--------- tests/test-visitor-serialization.c | 6 +++--- util/qemu-sockets.c | 6 ++---- docs/qapi-code-gen.txt | 3 +-- 13 files changed, 35 insertions(+), 68 deletions(-)
scripts/qapi-commands.py +1 -2
@@ -113,11 +113,10 @@ def gen_marshal(name, arg_type, ret_type): if arg_type and arg_type.members: ret += mcgen(''' - QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true); Visitor *v; %(c_name)s arg = {0}; - v = qmp_input_get_visitor(qiv); + v = qmp_input_visitor_new(QOBJECT(args), true); visit_start_struct(v, NULL, NULL, 0, &err); if (err) { goto out;
include/qapi/qmp-input-visitor.h +1 -5
@@ -25,10 +25,6 @@ typedef struct QmpInputVisitor QmpInputVisitor;* Set @strict to reject a parse that doesn't consume all keys of a * dictionary; otherwise excess input is ignored. */ -QmpInputVisitor *qmp_input_visitor_new (QObject *obj,bool strict); - -void qmp_input_visitor_cleanup (QmpInputVisitor *v); - -Visitor *qmp_input_get_visitor (QmpInputVisitor *v); +Visitor *qmp_input_visitor_new (QObject *obj,bool strict);#endif
qapi/qmp-input-visitor.c +4 -13
@@ -370,24 +370,15 @@ static void qmp_input_optional(Visitor *v, const char *name, bool *present) *present = true; } -Visitor *qmp_input_get_visitor (QmpInputVisitor *v) -{ -return &v->visitor; -} -static void qmp_input_free (Visitor *v) {QmpInputVisitor *qiv =to_qiv (v); -qmp_input_visitor_cleanup (qiv); -} -void qmp_input_visitor_cleanup (QmpInputVisitor *v) -{ -qobject_decref (v->root); -g_free (v); +qobject_decref (qiv->root); +g_free (qiv); } -QmpInputVisitor *qmp_input_visitor_new (QObject *obj,bool strict) +Visitor *qmp_input_visitor_new (QObject *obj,bool strict) {QmpInputVisitor *v;
@@ -415,5 +406,5 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict) v->root = obj;qobject_incref (obj); -return v; +return &v->visitor; }
qmp.c +4 -5
@@ -652,7 +652,7 @@ void qmp_object_add(const char *type, const char *id, bool has_props, QObject *props, Error **errp) { const QDict *pdict = NULL; -QmpInputVisitor *qiv; +Visitor *v;Object *obj;if (props) {
@@ -663,10 +663,9 @@ void qmp_object_add(const char *type, const char *id, } } - qiv =qmp_input_visitor_new (props, true); - obj =user_creatable_add_type (type, id, pdict, -qmp_input_get_visitor (qiv), errp); -qmp_input_visitor_cleanup (qiv); + v =qmp_input_visitor_new (props, true); + obj =user_creatable_add_type (type, id, pdict, v, errp); +visit_free (v);if (obj) {object_unref (obj); }
qom/qom-qobject.c +4 -5
@@ -21,12 +21,11 @@void object_property_set_qobject (Object *obj,QObject *value,const char *name,Error **errp) { -QmpInputVisitor *qiv; +Visitor *v;/* TODO: Should we reject, rather than ignore, excess input? */ - qiv =qmp_input_visitor_new (value, false); -object_property_set (obj,qmp_input_get_visitor (qiv), name, errp); - -qmp_input_visitor_cleanup (qiv); + v =qmp_input_visitor_new (value, false); +object_property_set (obj, v, name, errp); +visit_free (v); } QObject *object_property_get_qobject(Object *obj, const char *name,
replay/replay-input.c +2 -4
@@ -23,7 +23,6 @@static InputEvent *qapi_clone_InputEvent (InputEvent *src) {QmpOutputVisitor *qov; -QmpInputVisitor *qiv;Visitor *ov, *iv;QObject *obj;InputEvent *dst =NULL ;
@@ -37,10 +36,9 @@ static InputEvent *qapi_clone_InputEvent(InputEvent *src)return NULL ; } - qiv =qmp_input_visitor_new (obj, true); - iv =qmp_input_get_visitor (qiv); + iv =qmp_input_visitor_new (obj, true);visit_type_InputEvent (iv,NULL , &dst, &error_abort); -qmp_input_visitor_cleanup (qiv); +visit_free (iv);qobject_decref (obj);return dst;
tests/check-qnull.c +3 -3
@@ -39,7 +39,7 @@ static void qnull_visit_test(void) {QObject *obj;QmpOutputVisitor *qov; -QmpInputVisitor *qiv; +Visitor *qiv;/* * Most tests of interactions between QObject and visitors are in
@@ -51,8 +51,8 @@ static void qnull_visit_test(void) obj =qnull (); qiv =qmp_input_visitor_new (obj, true);qobject_decref (obj); -visit_type_null (qmp_input_get_visitor (qiv),NULL , &error_abort); -qmp_input_visitor_cleanup (qiv); +visit_type_null (qiv,NULL , &error_abort); +visit_free (qiv); qov =qmp_output_visitor_new ();visit_type_null (qmp_output_get_visitor (qov),NULL , &error_abort);
tests/test-qmp-commands.c +4 -4
@@ -217,14 +217,14 @@ static void test_dealloc_partial(void)/* create partial object */ {QDict *ud2_dict; -QmpInputVisitor *qiv; +Visitor *v; ud2_dict =qdict_new ();qdict_put_obj (ud2_dict,"string0" ,QOBJECT (qstring_from_str (text))); - qiv =qmp_input_visitor_new (QOBJECT (ud2_dict), true); -visit_type_UserDefTwo (qmp_input_get_visitor (qiv),NULL , &ud2, &err); -qmp_input_visitor_cleanup (qiv); + v =qmp_input_visitor_new (QOBJECT (ud2_dict), true); +visit_type_UserDefTwo (v,NULL , &ud2, &err); +visit_free (v);QDECREF (ud2_dict); }
tests/test-qmp-input-strict.c +3 -9
@@ -27,7 +27,7 @@typedef struct TestInputVisitorData {QObject *obj; -QmpInputVisitor *qiv; +Visitor *qiv; }TestInputVisitorData ; static void validate_teardown(TestInputVisitorData *data,
@@ -37,7 +37,7 @@ static void validate_teardown(TestInputVisitorData *data, data->obj =NULL ;if (data->qiv) { -qmp_input_visitor_cleanup (data->qiv); +visit_free (data->qiv); data->qiv =NULL ; } }
@@ -49,8 +49,6 @@ static Visitor *validate_test_init_internal(TestInputVisitorData *data,const char *json_string, va_list *ap) { - Visitor *v; -validate_teardown (data ,NULL ); data->obj =qobject_from_jsonv (json_string, ap);
@@ -58,11 +56,7 @@ static Visitor *validate_test_init_internal(TestInputVisitorData *data, data->qiv =qmp_input_visitor_new (data->obj, true);g_assert (data->qiv); - - v =qmp_input_get_visitor (data->qiv); -g_assert (v); - -return v; +return data->qiv; } static GCC_FMT_ATTR(2, 3)
tests/test-qmp-input-visitor.c +3 -9
@@ -23,7 +23,7 @@typedef struct TestInputVisitorData {QObject *obj; -QmpInputVisitor *qiv; +Visitor *qiv; }TestInputVisitorData ; static void visitor_input_teardown(TestInputVisitorData *data,
@@ -33,7 +33,7 @@ static void visitor_input_teardown(TestInputVisitorData *data, data->obj =NULL ;if (data->qiv) { -qmp_input_visitor_cleanup (data->qiv); +visit_free (data->qiv); data->qiv =NULL ; } }
@@ -45,8 +45,6 @@ static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,const char *json_string, va_list *ap) { - Visitor *v; -visitor_input_teardown (data ,NULL ); data->obj =qobject_from_jsonv (json_string, ap);
@@ -54,11 +52,7 @@ static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data, data->qiv =qmp_input_visitor_new (data->obj, false);g_assert (data->qiv); - - v =qmp_input_get_visitor (data->qiv); -g_assert (v); - -return v; +return data->qiv; } static GCC_FMT_ATTR(2, 3)
tests/test-visitor-serialization.c +3 -3
@@ -1014,7 +1014,7 @@ static PrimitiveType pt_values[] = {typedef struct QmpSerializeData {QmpOutputVisitor *qov; -QmpInputVisitor *qiv; +Visitor *qiv; }QmpSerializeData ; static void qmp_serialize(void *native_in, void **datap,
@@ -1042,14 +1042,14 @@ static void qmp_deserialize(void **native_out, void *datap, d->qiv =qmp_input_visitor_new (obj, true);qobject_decref (obj_orig);qobject_decref (obj); -visit (qmp_input_get_visitor (d->qiv), native_out, errp); +visit (d->qiv, native_out, errp); }static void qmp_cleanup (void *datap) {QmpSerializeData *d = datap;qmp_output_visitor_cleanup (d->qov); -qmp_input_visitor_cleanup (d->qiv); +visit_free (d->qiv);g_free (d); }
util/qemu-sockets.c +2 -4
@@ -1130,7 +1130,6 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest, SocketAddress *src) {QmpOutputVisitor *qov; -QmpInputVisitor *qiv;Visitor *ov, *iv;QObject *obj;
@@ -1145,9 +1144,8 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest,return ; } - qiv =qmp_input_visitor_new (obj, true); - iv =qmp_input_get_visitor (qiv); + iv =qmp_input_visitor_new (obj, true);visit_type_SocketAddress (iv,NULL , p_dest, &error_abort); -qmp_input_visitor_cleanup (qiv); +visit_free (iv);qobject_decref (obj); }
docs/qapi-code-gen.txt +1 -2
@@ -1002,11 +1002,10 @@ Example: { Error *err = NULL; UserDefOne *retval; - QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true); Visitor *v; UserDefOneList *arg1 = NULL; - v = qmp_input_get_visitor(qiv); + v = qmp_input_visitor_new(QOBJECT(args), true); visit_start_struct(v, NULL, NULL, 0, &err); if (err) { goto out;
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 08/28] qmp-input-visitor: Favor new visit_free() function
Quoted 6 lines · Eric Blake
Now that we have a polymorphic
visit_free(), we no longer needqmp_input_visitor_cleanup(); which in turn means we no longer need to return a subtype fromqmp_input_visitor_new()nor a public upcast function.Generated code changes to qmp-marshal look like:
qmp-marshal.c
Quoted 16 lines
|@@ -52,11 +52,10 @@ void qmp_marshal_add_fd(QDict *args, QOb | { | Error *err = NULL; | AddfdInfo *retval; |- QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true); | Visitor *v; | q_obj_add_fd_arg arg = {0}; | |- v = qmp_input_get_visitor(qiv); |+ v = qmp_input_visitor_new(QOBJECT(args), true); | visit_start_struct(v, NULL, NULL, 0, &err); | if (err) { | goto out;Signed-off-by Eric Blake <[email protected]>
[...]
tests/check-qnull.c static void qnull_visit_test(void) · line 42
Quoted 10 lines
tests/check-qnull.c +1
-1@@ -39,7 +39,7 @@ static void qnull_visit_test(void) {QObject *obj;QmpOutputVisitor *qov; -QmpInputVisitor *qiv; +Visitor *qiv;
Call it v instead of qiv? You do that elsewhere... Your choice.
Quoted 14 lines
tests/check-qnull.c +2
-2/* * Most tests of interactions between QObject and visitors are in @@ -51,8 +51,8 @@ static void qnull_visit_test(void) obj =qnull (); qiv =qmp_input_visitor_new (obj, true);qobject_decref (obj); -visit_type_null (qmp_input_get_visitor (qiv),NULL , &error_abort); -qmp_input_visitor_cleanup (qiv); +visit_type_null (qiv,NULL , &error_abort); +visit_free (qiv); qov =qmp_output_visitor_new ();visit_type_null (qmp_output_get_visitor (qov),NULL , &error_abort);
[...]
PATCH v4 9/28 string-output-visitor: Favor new visit_free() function
In reply to Eric BlakeNow that we have a polymorphic visit_free(), we no longer need string_output_visitor_cleanup(); however, we still need to expose the subtype for string_output_get_string().
---
v4: new patch
--- include/qapi/string-output-visitor.h | 1 - hmp.c | 2 +- net/net.c | 2 +- qapi/string-output-visitor.c | 4 ---- qom/object.c | 11 ++++++----- tests/test-string-output-visitor.c | 2 +- tests/test-visitor-serialization.c | 4 ++-- 7 files changed, 11 insertions(+), 15 deletions(-)
include/qapi/string-output-visitor.h +0 -1
@@ -23,7 +23,6 @@ typedef struct StringOutputVisitor StringOutputVisitor;* requires a non-null list argument to visit_start_list(). */ StringOutputVisitor *string_output_visitor_new (bool human); -void string_output_visitor_cleanup (StringOutputVisitor *v);char *string_output_get_string (StringOutputVisitor *v);Visitor *string_output_get_visitor (StringOutputVisitor *v);
hmp.c +1 -1
@@ -1954,7 +1954,7 @@ void hmp_info_memdev(Monitor *mon, const QDict *qdict)monitor_printf (mon," host nodes: %s\n" , str);g_free (str); -string_output_visitor_cleanup (ov); +visit_free (string_output_get_visitor (ov)); m = m->next; i++; }
net/net.c +1 -1
@@ -1212,7 +1212,7 @@ static void netfilter_print_info(Monitor *mon, NetFilterState *nf)object_property_get (OBJECT (nf),string_output_get_visitor (ov), prop->name,NULL ); str =string_output_get_string (ov); -string_output_visitor_cleanup (ov); +visit_free (string_output_get_visitor (ov));monitor_printf (mon,",%s=%s" , prop->name, str);g_free (str); }
qapi/string-output-visitor.c +0 -4
@@ -322,11 +322,7 @@ static void free_range(void *range, void *dummy)static void string_output_free (Visitor *v) {StringOutputVisitor *sov =to_sov (v); -string_output_visitor_cleanup (sov); -} -void string_output_visitor_cleanup (StringOutputVisitor *sov) -{if (sov->string) {g_string_free (sov->string, true); }
qom/object.c +6 -5
@@ -1236,14 +1236,15 @@ int object_property_get_enum(Object *obj, const char *name, enumprop = prop->opaque; sov =string_output_visitor_new (false); -object_property_get (obj,string_output_get_visitor (sov), name, &err); + v =string_output_get_visitor (sov); +object_property_get (obj, v, name, &err);if (err) {error_propagate (errp, err); -string_output_visitor_cleanup (sov); +visit_free (v);return 0 ; } str =string_output_get_string (sov); -string_output_visitor_cleanup (sov); +visit_free (v); v =string_input_visitor_new (str);visit_type_enum (v, name, &ret, enumprop->strings, errp);
@@ -1275,7 +1276,7 @@ void object_property_get_uint16List(Object *obj, const char *name,g_free (str);visit_free (v); out: -string_output_visitor_cleanup (ov); +visit_free (string_output_get_visitor (ov)); } void object_property_parse(Object *obj, const char *string,
@@ -1303,7 +1304,7 @@ char *object_property_print(Object *obj, const char *name, bool human, string =string_output_get_string (sov); out: -string_output_visitor_cleanup (sov); +visit_free (string_output_get_visitor (sov));return string; }
tests/test-string-output-visitor.c +1 -1
@@ -51,7 +51,7 @@ static void visitor_output_setup_human(TestOutputVisitorData *data,static void visitor_output_teardown (TestOutputVisitorData *data,const void *unused) { -string_output_visitor_cleanup (data->sov); +visit_free (data->ov); data->sov =NULL ; data->ov =NULL ; }
tests/test-visitor-serialization.c +2 -2
@@ -1048,7 +1048,7 @@ static void qmp_deserialize(void **native_out, void *datap,static void qmp_cleanup (void *datap) {QmpSerializeData *d = datap; -qmp_output_visitor_cleanup (d->qov); +visit_free (qmp_output_get_visitor (d->qov));visit_free (d->qiv);g_free (d);
@@ -1084,7 +1084,7 @@ static void string_cleanup(void *datap) {StringSerializeData *d = datap; -string_output_visitor_cleanup (d->sov); +visit_free (string_output_get_visitor (d->sov));visit_free (d->siv);g_free (d->string);g_free (d);
Signature · 1 line
2.5.5
PATCH v4 10/28 qmp-output-visitor: Favor new visit_free() function
In reply to Eric BlakeNow that we have a polymorphic visit_free(), we no longer need qmp_output_visitor_cleanup(); however, we still need to expose the subtype for qmp_output_get_qobject().
---
v4: new patch
--- include/qapi/qmp-output-visitor.h | 1 - block/qapi.c | 2 +- blockdev.c | 2 +- qapi/qmp-output-visitor.c | 13 ++++--------- qemu-img.c | 6 +++--- qom/qom-qobject.c | 2 +- replay/replay-input.c | 2 +- tests/check-qnull.c | 2 +- tests/test-qmp-output-visitor.c | 2 +- util/qemu-sockets.c | 2 +- 10 files changed, 14 insertions(+), 20 deletions(-)
include/qapi/qmp-output-visitor.h +0 -1
@@ -20,7 +20,6 @@typedef struct QmpOutputVisitor QmpOutputVisitor ;QmpOutputVisitor *qmp_output_visitor_new (void ); -void qmp_output_visitor_cleanup (QmpOutputVisitor *v);QObject *qmp_output_get_qobject (QmpOutputVisitor *v);Visitor *qmp_output_get_visitor (QmpOutputVisitor *v);
block/qapi.c +1 -1
@@ -699,7 +699,7 @@ void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,assert (qobject_type (obj) ==QTYPE_QDICT ); data =qdict_get (qobject_to_qdict (obj),"data" );dump_qobject (func_fprintf, f,1 , data); -qmp_output_visitor_cleanup (ov); +visit_free (qmp_output_get_visitor (ov)); } void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
blockdev.c +1 -1
@@ -4009,7 +4009,7 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp) } fail: -qmp_output_visitor_cleanup (ov); +visit_free (qmp_output_get_visitor (ov)); } void qmp_x_blockdev_del(bool has_id, const char *id,
qapi/qmp-output-visitor.c +4 -9
@@ -212,20 +212,15 @@ Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)static void qmp_output_free (Visitor *v) {QmpOutputVisitor *qov =to_qov (v); -qmp_output_visitor_cleanup (qov); -} - -void qmp_output_visitor_cleanup (QmpOutputVisitor *v) -{QStackEntry *e, *tmp; - QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) { -QTAILQ_REMOVE (&v->stack, e, node); + QTAILQ_FOREACH_SAFE(e, &qov->stack, node, tmp) { +QTAILQ_REMOVE (&qov->stack, e, node);g_free (e); } -qobject_decref (v->root); -g_free (v); +qobject_decref (qov->root); +g_free (qov); }QmpOutputVisitor *qmp_output_visitor_new (void )
qemu-img.c +3 -3
@@ -489,7 +489,7 @@ static void dump_json_image_check(ImageCheck *check, bool quiet)assert (str !=NULL );qprintf (quiet,"%s\n" ,qstring_get_str (str));qobject_decref (obj); -qmp_output_visitor_cleanup (ov); +visit_free (qmp_output_get_visitor (ov));QDECREF (str); }
@@ -2179,7 +2179,7 @@ static void dump_json_image_info_list(ImageInfoList *list)assert (str !=NULL );printf ("%s\n" ,qstring_get_str (str));qobject_decref (obj); -qmp_output_visitor_cleanup (ov); +visit_free (qmp_output_get_visitor (ov));QDECREF (str); }
@@ -2195,7 +2195,7 @@ static void dump_json_image_info(ImageInfo *info)assert (str !=NULL );printf ("%s\n" ,qstring_get_str (str));qobject_decref (obj); -qmp_output_visitor_cleanup (ov); +visit_free (qmp_output_get_visitor (ov));QDECREF (str); }
qom/qom-qobject.c +1 -1
@@ -41,6 +41,6 @@ QObject *object_property_get_qobject(Object *obj, const char *name, ret =qmp_output_get_qobject (qov); }error_propagate (errp, local_err); -qmp_output_visitor_cleanup (qov); +visit_free (qmp_output_get_visitor (qov));return ret; }
replay/replay-input.c +1 -1
@@ -31,7 +31,7 @@ static InputEvent *qapi_clone_InputEvent(InputEvent *src) ov =qmp_output_get_visitor (qov);visit_type_InputEvent (ov,NULL , &src, &error_abort); obj =qmp_output_get_qobject (qov); -qmp_output_visitor_cleanup (qov); +visit_free (ov);if (!obj) {return NULL ; }
tests/check-qnull.c +1 -1
@@ -59,7 +59,7 @@ static void qnull_visit_test(void) obj =qmp_output_get_qobject (qov);g_assert (obj == &qnull_);qobject_decref (obj); -qmp_output_visitor_cleanup (qov); +visit_free (qmp_output_get_visitor (qov));g_assert (qnull_.refcnt ==1 ); }
tests/test-qmp-output-visitor.c +1 -1
@@ -39,7 +39,7 @@ static void visitor_output_setup(TestOutputVisitorData *data,static void visitor_output_teardown (TestOutputVisitorData *data,const void *unused) { -qmp_output_visitor_cleanup (data->qov); +visit_free (data->ov); data->qov =NULL ; data->ov =NULL ; }
util/qemu-sockets.c +1 -1
@@ -1139,7 +1139,7 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest, ov =qmp_output_get_visitor (qov);visit_type_SocketAddress (ov,NULL , &src, &error_abort); obj =qmp_output_get_qobject (qov); -qmp_output_visitor_cleanup (qov); +visit_free (ov);if (!obj) {return ; }
Signature · 1 line
2.5.5
PATCH v4 11/28 tests: Factor out common code in qapi output tests
In reply to Eric BlakeCreate a new visitor_get() function to capture common actions taken in collecting output from an output visitor, to make it easier to refactor the output visitors in a later patch. While making test improvements, also use &error_abort and error_free_or_abort() in more places, use the generated qapi_free_intList() instead of open-coding it, reduce the scope of some variables, and copy the visitor_reset() concept from the qmp-output test to the string-output test.
---
v4: new patch
--- tests/test-qmp-output-visitor.c | 78 +++++++++++------------------- tests/test-string-output-visitor.c | 97 ++++++++++++++++++-------------------- 2 files changed, 72 insertions(+), 103 deletions(-)
tests/test-qmp-output-visitor.c +27 -51
@@ -24,6 +24,7 @@typedef struct TestOutputVisitorData {QmpOutputVisitor *qov;Visitor *ov; +QObject *obj; }TestOutputVisitorData ; static void visitor_output_setup(TestOutputVisitorData *data,
@@ -42,6 +43,15 @@ static void visitor_output_teardown(TestOutputVisitorData *data,visit_free (data->ov); data->qov =NULL ; data->ov =NULL ; +qobject_decref (data->obj); + data->obj =NULL ; +} + +static QObject *visitor_get (TestOutputVisitorData *data) +{ + data->obj =qmp_output_get_qobject (data->qov); +g_assert (data->obj); +return data->obj; }static void visitor_reset (TestOutputVisitorData *data)
@@ -58,12 +68,9 @@ static void test_visitor_out_int(TestOutputVisitorData *data,visit_type_int (data->ov,NULL , &value, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QINT ); g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value); - -qobject_decref (obj); } static void test_visitor_out_bool(TestOutputVisitorData *data,
@@ -74,12 +81,9 @@ static void test_visitor_out_bool(TestOutputVisitorData *data,visit_type_bool (data->ov,NULL , &value, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QBOOL );g_assert (qbool_get_bool (qobject_to_qbool (obj)) == value); - -qobject_decref (obj); } static void test_visitor_out_number(TestOutputVisitorData *data,
@@ -90,12 +94,9 @@ static void test_visitor_out_number(TestOutputVisitorData *data,visit_type_number (data->ov,NULL , &value, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QFLOAT );g_assert (qfloat_get_double (qobject_to_qfloat (obj)) == value); - -qobject_decref (obj); } static void test_visitor_out_string(TestOutputVisitorData *data,
@@ -106,12 +107,9 @@ static void test_visitor_out_string(TestOutputVisitorData *data,visit_type_str (data->ov,NULL , &string, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); - -qobject_decref (obj); } static void test_visitor_out_no_string(TestOutputVisitorData *data,
@@ -123,12 +121,9 @@ static void test_visitor_out_no_string(TestOutputVisitorData *data,/* A null string should return "" */ visit_type_str (data->ov,NULL , &string, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, ""); - -qobject_decref (obj); } static void test_visitor_out_enum(TestOutputVisitorData *data,
@@ -140,12 +135,10 @@ static void test_visitor_out_enum(TestOutputVisitorData *data,for (i =0 ; i <ENUM_ONE__MAX ; i++) {visit_type_EnumOne (data->ov,"unused" , &i, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, EnumOne_lookup[i]); -qobject_decref (obj);visitor_reset (data); } }
@@ -178,8 +171,7 @@ static void test_visitor_out_struct(TestOutputVisitorData *data,visit_type_TestStruct (data->ov,NULL , &p, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QDICT ); qdict =qobject_to_qdict (obj);
@@ -187,8 +179,6 @@ static void test_visitor_out_struct(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42); g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false); g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo"); - -QDECREF (qdict); } static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
@@ -223,8 +213,7 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data,visit_type_UserDefTwo (data->ov,"unused" , &ud2, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QDICT ); qdict =qobject_to_qdict (obj);
@@ -251,7 +240,6 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); -QDECREF (qdict);qapi_free_UserDefTwo (ud2); }
@@ -303,8 +291,7 @@ static void test_visitor_out_list(TestOutputVisitorData *data,visit_type_TestStructList (data->ov,NULL , &head, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QLIST ); qlist =qobject_to_qlist (obj);
@@ -325,7 +312,6 @@ static void test_visitor_out_list(TestOutputVisitorData *data, } g_assert_cmpint(i, ==, max_items); -QDECREF (qlist);qapi_free_TestStructList (head); }
@@ -369,11 +355,9 @@ static void test_visitor_out_any(TestOutputVisitorData *data, qobj =QOBJECT (qint_from_int (-42 ));visit_type_any (data->ov,NULL , &qobj, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QINT ); g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42); -qobject_decref (obj);qobject_decref (qobj);visitor_reset (data);
@@ -383,9 +367,8 @@ static void test_visitor_out_any(TestOutputVisitorData *data,qdict_put (qdict,"string" ,qstring_from_str ("foo" )); qobj =QOBJECT (qdict);visit_type_any (data->ov,NULL , &qobj, &error_abort); + obj =visitor_get (data);qobject_decref (qobj); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); qdict =qobject_to_qdict (obj);g_assert (qdict); qobj =qdict_get (qdict,"integer" );
@@ -403,7 +386,6 @@ static void test_visitor_out_any(TestOutputVisitorData *data, qstring =qobject_to_qstring (qobj);g_assert (qstring); g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); -qobject_decref (obj); } static void test_visitor_out_union_flat(TestOutputVisitorData *data,
@@ -419,7 +401,7 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data, tmp->u.value1.boolean = true;visit_type_UserDefFlatUnion (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QDICT ); qdict =qobject_to_qdict (arg);
@@ -430,7 +412,6 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);qapi_free_UserDefFlatUnion (tmp); -QDECREF (qdict); } static void test_visitor_out_alternate(TestOutputVisitorData *data,
@@ -445,13 +426,12 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.i =42 ;visit_type_UserDefAlternate (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QINT ); g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);qapi_free_UserDefAlternate (tmp); -qobject_decref (arg);visitor_reset (data); tmp =g_new0 (UserDefAlternate,1 );
@@ -459,13 +439,12 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.s =g_strdup ("hello" );visit_type_UserDefAlternate (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");qapi_free_UserDefAlternate (tmp); -qobject_decref (arg);visitor_reset (data); tmp =g_new0 (UserDefAlternate,1 );
@@ -476,7 +455,7 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.udfu.u.value1.boolean = true;visit_type_UserDefAlternate (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data); g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT); qdict =qobject_to_qdict (arg);
@@ -487,7 +466,6 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);qapi_free_UserDefAlternate (tmp); -qobject_decref (arg); } static void test_visitor_out_null(TestOutputVisitorData *data,
@@ -501,14 +479,13 @@ static void test_visitor_out_null(TestOutputVisitorData *data,visit_type_null (data->ov,"a" , &error_abort);visit_check_struct (data->ov, &error_abort);visit_end_struct (data->ov,NULL ); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QDICT ); qdict =qobject_to_qdict (arg); g_assert_cmpint(qdict_size(qdict), ==, 1); nil =qdict_get (qdict,"a" );g_assert (nil);g_assert (qobject_type (nil) ==QTYPE_QNULL ); -qobject_decref (arg); }static void init_native_list (UserDefNativeListUnion *cvalue)
@@ -739,10 +716,9 @@ static void test_native_list(TestOutputVisitorData *data,visit_type_UserDefNativeListUnion (data->ov,NULL , &cvalue, &error_abort); - obj =qmp_output_get_qobject (data->qov); + obj =visitor_get (data);check_native_list (obj, cvalue->type);qapi_free_UserDefNativeListUnion (cvalue); -qobject_decref (obj); } static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
tests/test-string-output-visitor.c +45 -52
204 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 11/28] tests: Factor out common code in qapi output tests
Quoted 8 lines · Eric Blake
Create a new
visitor_get()function to capture common actions taken in collecting output from an output visitor, to make it easier to refactor the output visitors in a later patch. While making test improvements, also use&error_abortanderror_free_or_abort()in more places, use the generatedqapi_free_intList()instead of open-coding it, reduce the scope of some variables, and copy thevisitor_reset()concept from the qmp-output test to the string-output test.
Sounds like this would be easier to review if split into parts.
Quoted 190 lines
Signed-off-by Eric Blake <[email protected]>---
v4: new patch--- tests/test-qmp-output-visitor.c | 78 +++++++++++------------------- tests/test-string-output-visitor.c | 97 ++++++++++++++++++-------------------- 2 files changed, 72 insertions(+), 103 deletions(-)tests/test-qmp-output-visitor.c +20
-36@@ -24,6 +24,7 @@typedef struct TestOutputVisitorData {QmpOutputVisitor *qov;Visitor *ov; +QObject *obj; }TestOutputVisitorData ; static void visitor_output_setup(TestOutputVisitorData *data,@@ -42,6 +43,15 @@ static void visitor_output_teardown(TestOutputVisitorData *data,visit_free (data->ov); data->qov =NULL ; data->ov =NULL ; +qobject_decref (data->obj); + data->obj =NULL ; +} + +static QObject *visitor_get (TestOutputVisitorData *data) +{ + data->obj =qmp_output_get_qobject (data->qov); +g_assert (data->obj); +return data->obj; }static void visitor_reset (TestOutputVisitorData *data)@@ -58,12 +68,9 @@ static void test_visitor_out_int(TestOutputVisitorData *data,visit_type_int (data->ov,NULL , &value, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QINT ); g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value); - -qobject_decref (obj); } static void test_visitor_out_bool(TestOutputVisitorData *data,@@ -74,12 +81,9 @@ static void test_visitor_out_bool(TestOutputVisitorData *data,visit_type_bool (data->ov,NULL , &value, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QBOOL );g_assert (qbool_get_bool (qobject_to_qbool (obj)) == value); - -qobject_decref (obj); } static void test_visitor_out_number(TestOutputVisitorData *data,@@ -90,12 +94,9 @@ static void test_visitor_out_number(TestOutputVisitorData *data,visit_type_number (data->ov,NULL , &value, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QFLOAT );g_assert (qfloat_get_double (qobject_to_qfloat (obj)) == value); - -qobject_decref (obj); } static void test_visitor_out_string(TestOutputVisitorData *data,@@ -106,12 +107,9 @@ static void test_visitor_out_string(TestOutputVisitorData *data,visit_type_str (data->ov,NULL , &string, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); - -qobject_decref (obj); } static void test_visitor_out_no_string(TestOutputVisitorData *data,@@ -123,12 +121,9 @@ static void test_visitor_out_no_string(TestOutputVisitorData *data,/* A null string should return "" */ visit_type_str (data->ov,NULL , &string, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, ""); - -qobject_decref (obj); } static void test_visitor_out_enum(TestOutputVisitorData *data,@@ -140,12 +135,10 @@ static void test_visitor_out_enum(TestOutputVisitorData *data,for (i =0 ; i <ENUM_ONE__MAX ; i++) {visit_type_EnumOne (data->ov,"unused" , &i, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, EnumOne_lookup[i]); -qobject_decref (obj);visitor_reset (data); } }@@ -178,8 +171,7 @@ static void test_visitor_out_struct(TestOutputVisitorData *data,visit_type_TestStruct (data->ov,NULL , &p, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QDICT ); qdict =qobject_to_qdict (obj);@@ -187,8 +179,6 @@ static void test_visitor_out_struct(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42); g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false); g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo"); - -QDECREF (qdict); } static void test_visitor_out_struct_nested(TestOutputVisitorData *data,@@ -223,8 +213,7 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data,visit_type_UserDefTwo (data->ov,"unused" , &ud2, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QDICT ); qdict =qobject_to_qdict (obj);@@ -251,7 +240,6 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); -QDECREF (qdict);qapi_free_UserDefTwo (ud2); }@@ -303,8 +291,7 @@ static void test_visitor_out_list(TestOutputVisitorData *data,visit_type_TestStructList (data->ov,NULL , &head, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QLIST ); qlist =qobject_to_qlist (obj);@@ -325,7 +312,6 @@ static void test_visitor_out_list(TestOutputVisitorData *data, } g_assert_cmpint(i, ==, max_items); -QDECREF (qlist);qapi_free_TestStructList (head); }@@ -369,11 +355,9 @@ static void test_visitor_out_any(TestOutputVisitorData *data, qobj =QOBJECT (qint_from_int (-42 ));visit_type_any (data->ov,NULL , &qobj, &error_abort); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL ); + obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QINT ); g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42); -qobject_decref (obj);qobject_decref (qobj);visitor_reset (data);
tests/test-qmp-output-visitor.c static void test_visitor_out_any(TestOutputVisitorData *data, · line 370
Quoted 4 lines
tests/test-qmp-output-visitor.c +1
-0@@ -383,9 +367,8 @@ static void test_visitor_out_any(TestOutputVisitorData *data,qdict_put (qdict,"string" ,qstring_from_str ("foo" )); qobj =QOBJECT (qdict);visit_type_any (data->ov,NULL , &qobj, &error_abort); + obj =visitor_get (data);
Any particular reason to put this here...
tests/test-qmp-output-visitor.c static void test_visitor_out_any(TestOutputVisitorData *data, · line 388
Quoted 3 lines
tests/test-qmp-output-visitor.c +0
-2qobject_decref (qobj); - obj =qmp_output_get_qobject (data->qov); -g_assert (obj !=NULL );
... rather than here?
Quoted 311 lines
tests/test-qmp-output-visitor.c +6
-13qdict =qobject_to_qdict (obj);g_assert (qdict); qobj =qdict_get (qdict,"integer" );@@ -403,7 +386,6 @@ static void test_visitor_out_any(TestOutputVisitorData *data, qstring =qobject_to_qstring (qobj);g_assert (qstring); g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); -qobject_decref (obj); } static void test_visitor_out_union_flat(TestOutputVisitorData *data,@@ -419,7 +401,7 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data, tmp->u.value1.boolean = true;visit_type_UserDefFlatUnion (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QDICT ); qdict =qobject_to_qdict (arg);@@ -430,7 +412,6 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);qapi_free_UserDefFlatUnion (tmp); -QDECREF (qdict); } static void test_visitor_out_alternate(TestOutputVisitorData *data,@@ -445,13 +426,12 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.i =42 ;visit_type_UserDefAlternate (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QINT ); g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);qapi_free_UserDefAlternate (tmp); -qobject_decref (arg);visitor_reset (data); tmp =g_new0 (UserDefAlternate,1 );@@ -459,13 +439,12 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.s =g_strdup ("hello" );visit_type_UserDefAlternate (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");qapi_free_UserDefAlternate (tmp); -qobject_decref (arg);visitor_reset (data); tmp =g_new0 (UserDefAlternate,1 );@@ -476,7 +455,7 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, tmp->u.udfu.u.value1.boolean = true;visit_type_UserDefAlternate (data->ov,NULL , &tmp, &error_abort); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data); g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT); qdict =qobject_to_qdict (arg);@@ -487,7 +466,6 @@ static void test_visitor_out_alternate(TestOutputVisitorData *data, g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);qapi_free_UserDefAlternate (tmp); -qobject_decref (arg); } static void test_visitor_out_null(TestOutputVisitorData *data,@@ -501,14 +479,13 @@ static void test_visitor_out_null(TestOutputVisitorData *data,visit_type_null (data->ov,"a" , &error_abort);visit_check_struct (data->ov, &error_abort);visit_end_struct (data->ov,NULL ); - arg =qmp_output_get_qobject (data->qov); + arg =visitor_get (data);g_assert (qobject_type (arg) ==QTYPE_QDICT ); qdict =qobject_to_qdict (arg); g_assert_cmpint(qdict_size(qdict), ==, 1); nil =qdict_get (qdict,"a" );g_assert (nil);g_assert (qobject_type (nil) ==QTYPE_QNULL ); -qobject_decref (arg); }static void init_native_list (UserDefNativeListUnion *cvalue)@@ -739,10 +716,9 @@ static void test_native_list(TestOutputVisitorData *data,visit_type_UserDefNativeListUnion (data->ov,NULL , &cvalue, &error_abort); - obj =qmp_output_get_qobject (data->qov); + obj =visitor_get (data);check_native_list (obj, cvalue->type);qapi_free_UserDefNativeListUnion (cvalue); -qobject_decref (obj); } static void test_visitor_out_native_list_int(TestOutputVisitorData *data,tests/test-string-output-visitor.c +44
-50@@ -23,29 +23,30 @@typedef struct TestOutputVisitorData {StringOutputVisitor *sov;Visitor *ov; +char *str;bool human; }TestOutputVisitorData ; +static void visitor_output_setup_internal (TestOutputVisitorData *data, +bool human) +{ + data->human = human; + data->sov =string_output_visitor_new (human); +g_assert (data->sov); + data->ov =string_output_get_visitor (data->sov); +g_assert (data->ov); +} +static void visitor_output_setup (TestOutputVisitorData *data,const void *unused) { - data->human = false; - data->sov =string_output_visitor_new (data->human); -g_assert (data->sov !=NULL ); - - data->ov =string_output_get_visitor (data->sov); -g_assert (data->ov !=NULL ); +return visitor_output_setup_internal (data, false); }static void visitor_output_setup_human (TestOutputVisitorData *data,const void *unused) { - data->human = true; - data->sov =string_output_visitor_new (data->human); -g_assert (data->sov !=NULL ); - - data->ov =string_output_get_visitor (data->sov); -g_assert (data->ov !=NULL ); +return visitor_output_setup_internal (data, true); } static void visitor_output_teardown(TestOutputVisitorData *data,@@ -54,6 +55,23 @@ static void visitor_output_teardown(TestOutputVisitorData *data,visit_free (data->ov); data->sov =NULL ; data->ov =NULL ; +g_free (data->str); + data->str =NULL ; +} + +static char *visitor_get (TestOutputVisitorData *data) +{ + data->str =string_output_get_string (data->sov); +g_assert (data->str); +return data->str; +} + +static void visitor_reset (TestOutputVisitorData *data) +{ +bool human = data->human; + +visitor_output_teardown (data,NULL ); +visitor_output_setup_internal (data, human); } static void test_visitor_out_int(TestOutputVisitorData *data,@@ -66,14 +84,12 @@ static void test_visitor_out_int(TestOutputVisitorData *data,visit_type_int (data->ov,NULL , &value, &err);g_assert (!err); - str =string_output_get_string (data->sov); -g_assert (str !=NULL ); + str =visitor_get (data);if (data->human) { g_assert_cmpstr(str, ==, "42 (0x2a)"); }else { g_assert_cmpstr(str, ==, "42"); } -g_free (str); } static void test_visitor_out_intList(TestOutputVisitorData *data,@@ -95,8 +111,7 @@ static void test_visitor_out_intList(TestOutputVisitorData *data,visit_type_intList (data->ov,NULL , &list, &err);g_assert (err ==NULL ); - str =string_output_get_string (data->sov); -g_assert (str !=NULL ); + str =visitor_get (data);if (data->human) { g_assert_cmpstr(str, ==, "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "@@ -106,13 +121,7 @@ static void test_visitor_out_intList(TestOutputVisitorData *data, g_assert_cmpstr(str, ==,"0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807" ); } -g_free (str); -while (list) { -intList *tmp2; - tmp2 = list->next; -g_free (list); - list = tmp2; - } +qapi_free_intList (list); } static void test_visitor_out_bool(TestOutputVisitorData *data,@@ -125,10 +134,8 @@ static void test_visitor_out_bool(TestOutputVisitorData *data,visit_type_bool (data->ov,NULL , &value, &err);g_assert (!err); - str =string_output_get_string (data->sov); -g_assert (str !=NULL ); + str =visitor_get (data); g_assert_cmpstr(str, ==, "true"); -g_free (str); } static void test_visitor_out_number(TestOutputVisitorData *data,@@ -141,10 +148,8 @@ static void test_visitor_out_number(TestOutputVisitorData *data,visit_type_number (data->ov,NULL , &value, &err);g_assert (!err); - str =string_output_get_string (data->sov); -g_assert (str !=NULL ); + str =visitor_get (data); g_assert_cmpstr(str, ==, "3.140000"); -g_free (str); } static void test_visitor_out_string(TestOutputVisitorData *data,@@ -158,61 +163,50 @@ static void test_visitor_out_string(TestOutputVisitorData *data,visit_type_str (data->ov,NULL , &string, &err);g_assert (!err); - str =string_output_get_string (data->sov); -g_assert (str !=NULL ); + str =visitor_get (data);if (data->human) { g_assert_cmpstr(str, ==, string_human); }else { g_assert_cmpstr(str, ==, string); } -g_free (str); }static void test_visitor_out_no_string (TestOutputVisitorData *data,const void *unused) {char *string =NULL ; -Error *err =NULL ;char *str;/* A null string should return "" */ -visit_type_str (data->ov,NULL , &string, &err); -g_assert (!err); +visit_type_str (data->ov,NULL , &string, &error_abort); - str =string_output_get_string (data->sov); -g_assert (str !=NULL ); + str =visitor_get (data);if (data->human) { g_assert_cmpstr(str, ==, "<null>"); }else { g_assert_cmpstr(str, ==, ""); } -g_free (str); }static void test_visitor_out_enum (TestOutputVisitorData *data,const void *unused) { -Error *err =NULL ;char *str;EnumOne i;for (i =0 ; i <ENUM_ONE__MAX ; i++) { -char *str_human; +visit_type_EnumOne (data->ov,"unused" , &i, &error_abort); -visit_type_EnumOne (data->ov,"unused" , &i, &err); -g_assert (!err); - - str_human =g_strdup_printf ("\"%s\"" , EnumOne_lookup[i]); - - str =string_output_get_string (data->sov); -g_assert (str !=NULL ); + str =visitor_get (data);if (data->human) { +char *str_human =g_strdup_printf ("\"%s\"" , EnumOne_lookup[i]); + g_assert_cmpstr(str, ==, str_human); +g_free (str_human); }else { g_assert_cmpstr(str, ==, EnumOne_lookup[i]); } -g_free (str_human); -g_free (str); +visitor_reset (data); } }
tests/test-string-output-visitor.c static void test_visitor_out_enum_errors(TestOutputVisitorData *data, · line 224
Quoted 8 lines
tests/test-string-output-visitor.c +1
-2@@ -225,8 +219,7 @@ static void test_visitor_out_enum_errors(TestOutputVisitorData *data,for (i =0 ; i <ARRAY_SIZE (bad_values) ; i++) { err =NULL ;visit_type_EnumOne (data->ov,"unused" , &bad_values[i], &err); -g_assert (err); -error_free (err); +error_free_or_abort (&err); } }
All right, this wasn't too bad. I'd still split it up, but I'm a neat freak in that regard. Your choice.
PATCH v4 12/28 qapi: Add new visit_complete() function
In reply to Eric BlakeMaking each output visitor provide its own output collection function was the only remaining reason for exposing visitor sub-types to the rest of the code base. Add a polymorphic visit_complete() function which is a no-op for input visitors, and which populates an opaque pointer for output visitors. For maximum type-safety, also add a parameter to the output visitor constructors with a type-correct version of the output pointer, and assert that the two uses match.
This approach was considered superior to either passing the output parameter only during construction (action at a distance during visit_free() feels awkward) or only during visit_complete() (defeating type safety makes it easier to use incorrectly).
Most callers were function-local, and therefore a mechanical conversion; the testsuite was a bit trickier, but the previous cleanup patch minimized the churn here.
Generated code is simplified as follows for events:
|@@ -26,7 +26,7 @@ void qapi_event_send_acpi_device_ost(ACP
| QDict *qmp;
| Error *err = NULL;
| QMPEventFuncEmit emit;
|- QmpOutputVisitor *qov;
|+ QObject *obj;
| Visitor *v;
| q_obj_ACPI_DEVICE_OST_arg param = {
| info
|@@ -39,8 +39,7 @@ void qapi_event_send_acpi_device_ost(ACP
|
| qmp = qmp_event_build_dict("ACPI_DEVICE_OST");
|
|- qov = qmp_output_visitor_new();
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(&obj);
|
| visit_start_struct(v, "ACPI_DEVICE_OST", NULL, 0, &err);
| if (err) {
|@@ -55,7 +54,8 @@ void qapi_event_send_acpi_device_ost(ACP
| goto out;
| }
|
|- qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
|+ visit_complete(v, &obj);
|+ qdict_put_obj(qmp, "data", obj);
| emit(QAPI_EVENT_ACPI_DEVICE_OST, qmp, &err);and for commands:
| {
| Error *err = NULL;
|- QmpOutputVisitor *qov = qmp_output_visitor_new();
| Visitor *v;
|
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(ret_out);
| visit_type_AddfdInfo(v, "unused", &ret_in, &err);
|- if (err) {
|- goto out;
|+ if (!err) {
|+ visit_complete(v, ret_out);
| }
|- *ret_out = qmp_output_get_qobject(qov);
|-
|-out:
| error_propagate(errp, err);---
v4: new patch, inspired by review of v3 7/18
--- include/qapi/visitor.h | 52 +++++++++++++++++++++--------------- include/qapi/visitor-impl.h | 3 +++ scripts/qapi-commands.py | 10 +++---- scripts/qapi-event.py | 8 +++--- include/qapi/qmp-output-visitor.h | 11 +++++--- include/qapi/string-output-visitor.h | 10 ++++--- qapi/qapi-visit-core.c | 10 +++++++ block/qapi.c | 9 +++---- blockdev.c | 9 +++---- hmp.c | 11 ++++---- net/net.c | 11 ++++---- qapi/qmp-output-visitor.c | 21 ++++++++------- qapi/string-output-visitor.c | 22 ++++++++------- qemu-img.c | 30 ++++++++++----------- qom/object.c | 28 +++++++++---------- qom/qom-qobject.c | 10 +++---- replay/replay-input.c | 6 ++--- tests/check-qnull.c | 17 ++++++------ tests/test-qmp-output-visitor.c | 11 +++----- tests/test-string-output-visitor.c | 8 ++---- tests/test-visitor-serialization.c | 22 ++++++++------- util/qemu-sockets.c | 6 ++--- 22 files changed, 167 insertions(+), 158 deletions(-)
include/qapi/visitor.h +31 -21
@@ -39,21 +39,15 @@* * All of the visitors are created via: * -* Type *subtype_visitor_new(parameters...); -* -* where Type is either directly 'Visitor *', or is a subtype that can -* be trivially upcast to Visitor * via another function: -* -* Visitor *subtype_get_visitor(SubtypeVisitor *); +* Visitor *subtype_visitor_new(parameters...); * * A visitor should be used for exactly one top-level visit_type_FOO() -* or virtual walk, then passed to visit_free() to clean up resources. -* It is okay to free the visitor without completing the visit, if -* some other error is detected in the meantime. Output visitors -* provide an additional function, for collecting the final results of -* a successful visit: string_output_get_string() and -* qmp_output_get_qobject(); this collection function should not be -* called if any errors were reported during the visit. +* or virtual walk; if that is successful, the caller can optionally +* call visit_complete() (most useful for output visits). Then, +* regardless of success or failure, the user should call visit_free() +* to clean up resources. It is okay to free the visitor without +* completing the visit, if some other error is detected in the +* meantime. * * All QAPI types have a corresponding function with a signature * roughly compatible with this:
@@ -123,14 +117,14 @@* Error *err = NULL; * Visitor *v; * -* v = ...obtain input visitor... +* v = FOO_visitor_new(...); * visit_type_Foo(v, NULL, &f, &err); * if (err) { * ...handle error... * } else { * ...use f... * } -* ...clean up v... +* visit_free(v); * qapi_free_Foo(f); * </example> *
@@ -140,7 +134,7 @@* Error *err = NULL; * Visitor *v; * -* v = ...obtain input visitor... +* v = FOO_visitor_new(...); * visit_type_FooList(v, NULL, &l, &err); * if (err) { * ...handle error...
@@ -149,7 +143,7 @@* ...use l->value... * } * } -* ...clean up v... +* visit_free(v); * qapi_free_FooList(l); * </example> *
@@ -159,13 +153,17 @@* Foo *f = ...obtain populated object... * Error *err = NULL; * Visitor *v; +* Type *result; * -* v = ...obtain output visitor... +* v = FOO_visitor_new(..., &result); * visit_type_Foo(v, NULL, &f, &err); * if (err) { * ...handle error... +* } else { +* visit_complete(v, &result); +* ...use result... * } -* ...clean up v... +* visit_free(v); * </example> * * When visiting a real QAPI struct, this file provides several
@@ -191,7 +189,7 @@* Error *err = NULL; * int value; * -* v = ...obtain visitor... +* v = FOO_visitor_new(...); * visit_start_struct(v, NULL, NULL, 0, &err); * if (err) { * goto out;
@@ -219,7 +217,7 @@* visit_end_struct(v, NULL); * out: * error_propagate(errp, err); -* ...clean up v... +* visit_free(v); * </example> */
@@ -243,6 +241,18 @@ typedef struct GenericAlternate {/*** Visitor cleanup ***/ /* +* Complete the visit, collecting any output. +* +* May only be called after a successful top-level visit_type_FOO() or +* visit_end_ITEM(), and marks the end of the visit. The @opaque +* pointer should match the output parameter passed to the +* subtype_visitor_new() used to create an output visitor, or NULL for +* any other visitor. Needed for output visitors, but may also be +* called with other visitors. +*/ +void visit_complete (Visitor *v,void *opaque); + +/* * Free @v and any resources it has tied up. * * May be called whether or not the visit has been successfully
include/qapi/visitor-impl.h +3 -0
@@ -105,6 +105,9 @@ struct Visitor/* Must be set */ VisitorType type; +/* Must be set for output visitors, optional otherwise. */ +void (*complete)(Visitor *v,void *opaque); +/* Must be set */ void (*free)(Visitor *v); };
scripts/qapi-commands.py +3 -7
@@ -61,17 +61,13 @@ def gen_marshal_output(ret_type): static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp) { Error *err = NULL; - QmpOutputVisitor *qov = qmp_output_visitor_new(); Visitor *v; - v = qmp_output_get_visitor(qov); + v = qmp_output_visitor_new(ret_out); visit_type_%(c_name)s(v, "unused", &ret_in, &err); - if (err) { - goto out; + if (!err) { + visit_complete(v, ret_out); } - *ret_out = qmp_output_get_qobject(qov); - -out: error_propagate(errp, err); visit_free(v); v = qapi_dealloc_visitor_new();
scripts/qapi-event.py +4 -4
@@ -71,7 +71,7 @@ def gen_event_send(name, arg_type): if arg_type and arg_type.members: ret += mcgen(''' - QmpOutputVisitor *qov; + QObject *obj; Visitor *v; ''') ret += gen_param_var(arg_type)
@@ -90,8 +90,7 @@ def gen_event_send(name, arg_type): if arg_type and arg_type.members: ret += mcgen(''' - qov = qmp_output_visitor_new(); - v = qmp_output_get_visitor(qov); + v = qmp_output_visitor_new(&obj); visit_start_struct(v, "%(name)s", NULL, 0, &err); if (err) {
@@ -106,7 +105,8 @@ def gen_event_send(name, arg_type): goto out; } - qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov)); + visit_complete(v, &obj); + qdict_put_obj(qmp, "data", obj); ''', name=name, c_name=arg_type.c_name())
include/qapi/qmp-output-visitor.h +7 -4
@@ -19,9 +19,12 @@typedef struct QmpOutputVisitor QmpOutputVisitor ; -QmpOutputVisitor *qmp_output_visitor_new (void ); - -QObject *qmp_output_get_qobject (QmpOutputVisitor *v); -Visitor *qmp_output_get_visitor (QmpOutputVisitor *v); +/* +* Create a new QMP output visitor. +* +* If everything else succeeds, pass @result to visit_complete() to +* collect the result of the visit. +*/ +Visitor *qmp_output_visitor_new (QObject **result);#endif
include/qapi/string-output-visitor.h +6 -4
@@ -18,13 +18,15 @@typedef struct StringOutputVisitor StringOutputVisitor ;/* +* Create a new string output visitor. +* +* If everything else succeeds, pass @result to visit_complete() to +* collect the result of the visit. +* * The string output visitor does not implement support for visiting * QAPI structs, alternates, null, or arbitrary QTypes. It also * requires a non-null list argument to visit_start_list(). */ -StringOutputVisitor *string_output_visitor_new (bool human); - -char *string_output_get_string (StringOutputVisitor *v); -Visitor *string_output_get_visitor (StringOutputVisitor *v); +Visitor *string_output_visitor_new (bool human,char **result);#endif
qapi/qapi-visit-core.c +10 -0
@@ -20,6 +20,16 @@#include "qapi/visitor.h" #include "qapi/visitor-impl.h" +void visit_complete (Visitor *v,void *opaque) +{ +if (v->type ==VISITOR_OUTPUT ) { +assert (v->complete); + } +if (v->complete) { + v->complete (v, opaque); + } +} +void visit_free (Visitor *v) {if (v) {
block/qapi.c +4 -5
@@ -690,16 +690,15 @@ static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,void bdrv_image_info_specific_dump (fprintf_function func_fprintf,void *f,ImageInfoSpecific *info_spec) { -QmpOutputVisitor *ov =qmp_output_visitor_new ();QObject *obj, *data; +Visitor *v =qmp_output_visitor_new (&obj); -visit_type_ImageInfoSpecific (qmp_output_get_visitor (ov),NULL , &info_spec, - &error_abort); - obj =qmp_output_get_qobject (ov); +visit_type_ImageInfoSpecific (v,NULL , &info_spec, &error_abort); +visit_complete (v, &obj);assert (qobject_type (obj) ==QTYPE_QDICT ); data =qdict_get (qobject_to_qdict (obj),"data" );dump_qobject (func_fprintf, f,1 , data); -visit_free (qmp_output_get_visitor (ov)); +visit_free (v); } void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
blockdev.c +4 -5
@@ -3939,10 +3939,10 @@ out:void qmp_blockdev_add (BlockdevOptions *options,Error **errp) { -QmpOutputVisitor *ov =qmp_output_visitor_new ();BlockDriverState *bs;BlockBackend *blk =NULL ;QObject *obj; +Visitor *v =qmp_output_visitor_new (&obj);QDict *qdict;Error *local_err =NULL ;
@@ -3961,14 +3961,13 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp) } } -visit_type_BlockdevOptions (qmp_output_get_visitor (ov),NULL , &options, - &local_err); +visit_type_BlockdevOptions (v,NULL , &options, &local_err);if (local_err) {error_propagate (errp, local_err); goto fail; } - obj =qmp_output_get_qobject (ov); +visit_complete (v, &obj); qdict =qobject_to_qdict (obj);qdict_flatten (qdict);
@@ -4009,7 +4008,7 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp) } fail: -visit_free (qmp_output_get_visitor (ov)); +visit_free (v); } void qmp_x_blockdev_del(bool has_id, const char *id,
hmp.c +5 -6
@@ -1931,15 +1931,14 @@ void hmp_info_memdev(Monitor *mon, const QDict *qdict)Error *err =NULL ;MemdevList *memdev_list =qmp_query_memdev (&err);MemdevList *m = memdev_list; -StringOutputVisitor *ov; +Visitor *v;char *str;int i =0 ;while (m) { - ov =string_output_visitor_new (false); -visit_type_uint16List (string_output_get_visitor (ov),NULL , - &m->value->host_nodes,NULL ); + v =string_output_visitor_new (false, &str); +visit_type_uint16List (v,NULL , &m->value->host_nodes,NULL );monitor_printf (mon,"memory backend: %d\n" , i);monitor_printf (mon," size: %" PRId64"\n" , m->value->size); monitor_printf(mon, " merge: %s\n",
@@ -1950,11 +1949,11 @@ void hmp_info_memdev(Monitor *mon, const QDict *qdict) m->value->prealloc ? "true" : "false");monitor_printf (mon," policy: %s\n" , HostMemPolicy_lookup[m->value->policy]); - str =string_output_get_string (ov); +visit_complete (v, &str);monitor_printf (mon," host nodes: %s\n" , str);g_free (str); -visit_free (string_output_get_visitor (ov)); +visit_free (v); m = m->next; i++; }
net/net.c +5 -6
@@ -1200,7 +1200,7 @@ static void netfilter_print_info(Monitor *mon, NetFilterState *nf)char *str;ObjectProperty *prop;ObjectPropertyIterator iter; -StringOutputVisitor *ov; +Visitor *v;/* generate info str */ object_property_iter_init (&iter,OBJECT (nf));
@@ -1208,11 +1208,10 @@ static void netfilter_print_info(Monitor *mon, NetFilterState *nf)if (!strcmp (prop->name,"type" )) {continue ; } - ov =string_output_visitor_new (false); -object_property_get (OBJECT (nf),string_output_get_visitor (ov), - prop->name,NULL ); - str =string_output_get_string (ov); -visit_free (string_output_get_visitor (ov)); + v =string_output_visitor_new (false, &str); +object_property_get (OBJECT (nf), v, prop->name,NULL ); +visit_complete (v, &str); +visit_free (v);monitor_printf (mon,",%s=%s" , prop->name, str);g_free (str); }
qapi/qmp-output-visitor.c +12 -9
@@ -32,6 +32,7 @@ struct QmpOutputVisitorVisitor visitor;QStack stack;/* Stack of containers that haven't yet been finished */ QObject *root;/* Root of the output visit */ +QObject **result;/* User's storage location for result */ };#define qmp_output_add (qov, name, value) \
@@ -195,18 +196,17 @@ static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)/* Finish building, and return the root object. * The root object is never null. The caller becomes the object's * owner, and should use qobject_decref() when done with it. */ -QObject *qmp_output_get_qobject (QmpOutputVisitor *qov) +static void qmp_output_complete (Visitor *v,void *opaque) { +QmpOutputVisitor *qov =to_qov (v); +/* A visit must have occurred, with each start paired with end. */ assert (qov->root &&QTAILQ_EMPTY (&qov->stack)); +assert (opaque == qov->result);qobject_incref (qov->root); -return qov->root; -} - -Visitor *qmp_output_get_visitor (QmpOutputVisitor *v) -{ -return &v->visitor; + *qov->result = qov->root; + qov->result =NULL ; }static void qmp_output_free (Visitor *v)
@@ -223,7 +223,7 @@ static void qmp_output_free(Visitor *v)g_free (qov); } -QmpOutputVisitor *qmp_output_visitor_new (void ) +Visitor *qmp_output_visitor_new (QObject **result) {QmpOutputVisitor *v;
@@ -242,9 +242,12 @@ QmpOutputVisitor *qmp_output_visitor_new(void) v->visitor.type_number = qmp_output_type_number; v->visitor.type_any = qmp_output_type_any; v->visitor.type_null = qmp_output_type_null; + v->visitor.complete = qmp_output_complete; v->visitor.free = qmp_output_free;QTAILQ_INIT (&v->stack); + *result =NULL ; + v->result = result; -return v; +return &v->visitor; }
qapi/string-output-visitor.c +12 -10
@@ -58,6 +58,7 @@ struct StringOutputVisitorVisitor visitor;bool human;GString *string; +char **result;ListMode list_mode;union {int64_t s;
@@ -302,16 +303,13 @@ static void end_list(Visitor *v, void **obj) sov->list_mode =LM_NONE ; } -char *string_output_get_string (StringOutputVisitor *sov) +static void string_output_complete (Visitor *v,void *opaque) { -char *string =g_string_free (sov->string, false); +StringOutputVisitor *sov =to_sov (v); + +assert (opaque == sov->result); + *sov->result =g_string_free (sov->string, false); sov->string =NULL ; -return string; -} - -Visitor *string_output_get_visitor (StringOutputVisitor *sov) -{ -return &sov->visitor; }static void free_range (void *range,void *dummy)
@@ -332,7 +330,7 @@ static void string_output_free(Visitor *v)g_free (sov); } -StringOutputVisitor *string_output_visitor_new (bool human) +Visitor *string_output_visitor_new (bool human,char **result) {StringOutputVisitor *v;
@@ -340,6 +338,9 @@ StringOutputVisitor *string_output_visitor_new(bool human) v->string =g_string_new (NULL ); v->human = human; + v->result = result; + *result =NULL ; + v->visitor.type =VISITOR_OUTPUT ; v->visitor.type_int64 = print_type_int64; v->visitor.type_uint64 = print_type_uint64;
@@ -350,7 +351,8 @@ StringOutputVisitor *string_output_visitor_new(bool human) v->visitor.start_list = start_list; v->visitor.next_list = next_list; v->visitor.end_list = end_list; + v->visitor.complete = string_output_complete; v->visitor.free = string_output_free; -return v; +return &v->visitor; }
qemu-img.c +15 -15
@@ -480,16 +480,16 @@ fail:static void dump_json_image_check (ImageCheck *check,bool quiet) {QString *str; -QmpOutputVisitor *ov =qmp_output_visitor_new ();QObject *obj; -visit_type_ImageCheck (qmp_output_get_visitor (ov),NULL , &check, - &error_abort); - obj =qmp_output_get_qobject (ov); +Visitor *v =qmp_output_visitor_new (&obj); + +visit_type_ImageCheck (v,NULL , &check, &error_abort); +visit_complete (v, &obj); str =qobject_to_json_pretty (obj);assert (str !=NULL );qprintf (quiet,"%s\n" ,qstring_get_str (str));qobject_decref (obj); -visit_free (qmp_output_get_visitor (ov)); +visit_free (v);QDECREF (str); }
@@ -2170,32 +2170,32 @@ static void dump_snapshots(BlockDriverState *bs)static void dump_json_image_info_list (ImageInfoList *list) {QString *str; -QmpOutputVisitor *ov =qmp_output_visitor_new ();QObject *obj; -visit_type_ImageInfoList (qmp_output_get_visitor (ov),NULL , &list, - &error_abort); - obj =qmp_output_get_qobject (ov); +Visitor *v =qmp_output_visitor_new (&obj); + +visit_type_ImageInfoList (v,NULL , &list, &error_abort); +visit_complete (v, &obj); str =qobject_to_json_pretty (obj);assert (str !=NULL );printf ("%s\n" ,qstring_get_str (str));qobject_decref (obj); -visit_free (qmp_output_get_visitor (ov)); +visit_free (v);QDECREF (str); }static void dump_json_image_info (ImageInfo *info) {QString *str; -QmpOutputVisitor *ov =qmp_output_visitor_new ();QObject *obj; -visit_type_ImageInfo (qmp_output_get_visitor (ov),NULL , &info, - &error_abort); - obj =qmp_output_get_qobject (ov); +Visitor *v =qmp_output_visitor_new (&obj); + +visit_type_ImageInfo (v,NULL , &info, &error_abort); +visit_complete (v, &obj); str =qobject_to_json_pretty (obj);assert (str !=NULL );printf ("%s\n" ,qstring_get_str (str));qobject_decref (obj); -visit_free (qmp_output_get_visitor (ov)); +visit_free (v);QDECREF (str); }
qom/object.c +12 -16
@@ -1215,7 +1215,6 @@ int object_property_get_enum(Object *obj, const char *name, const char *typename, Error **errp) { Error *err = NULL; -StringOutputVisitor *sov;Visitor *v;char *str;int ret;
@@ -1235,15 +1234,14 @@ int object_property_get_enum(Object *obj, const char *name, enumprop = prop->opaque; - sov =string_output_visitor_new (false); - v =string_output_get_visitor (sov); + v =string_output_visitor_new (false, &str);object_property_get (obj, v, name, &err);if (err) {error_propagate (errp, err);visit_free (v);return 0 ; } - str =string_output_get_string (sov); +visit_complete (v, &str);visit_free (v); v =string_input_visitor_new (str);visit_type_enum (v, name, &ret, enumprop->strings, errp);
@@ -1258,25 +1256,23 @@ void object_property_get_uint16List(Object *obj, const char *name, uint16List **list, Error **errp) {Error *err =NULL ; -StringOutputVisitor *ov;Visitor *v;char *str; - ov =string_output_visitor_new (false); -object_property_get (obj,string_output_get_visitor (ov), - name, &err); + v =string_output_visitor_new (false, &str); +object_property_get (obj, v, name, &err);if (err) {error_propagate (errp, err); goto out; } - str =string_output_get_string (ov); +visit_complete (v, &str); +visit_free (v); v =string_input_visitor_new (str);visit_type_uint16List (v,NULL , list, errp);g_free (str); -visit_free (v); out: -visit_free (string_output_get_visitor (ov)); +visit_free (v); } void object_property_parse(Object *obj, const char *string,
@@ -1290,21 +1286,21 @@ void object_property_parse(Object *obj, const char *string,char *object_property_print (Object *obj,const char *name,bool human,Error **errp) { -StringOutputVisitor *sov; +Visitor *v;char *string =NULL ;Error *local_err =NULL ; - sov =string_output_visitor_new (human); -object_property_get (obj,string_output_get_visitor (sov), name, &local_err); + v =string_output_visitor_new (human, &string); +object_property_get (obj, v, name, &local_err);if (local_err) {error_propagate (errp, local_err); goto out; } - string =string_output_get_string (sov); +visit_complete (v, &string); out: -visit_free (string_output_get_visitor (sov)); +visit_free (v);return string; }
qom/qom-qobject.c +5 -5
@@ -33,14 +33,14 @@ QObject *object_property_get_qobject(Object *obj, const char *name, {QObject *ret =NULL ;Error *local_err =NULL ; -QmpOutputVisitor *qov; +Visitor *v; - qov =qmp_output_visitor_new (); -object_property_get (obj,qmp_output_get_visitor (qov), name, &local_err); + v =qmp_output_visitor_new (&ret); +object_property_get (obj, v, name, &local_err);if (!local_err) { - ret =qmp_output_get_qobject (qov); +visit_complete (v, &ret); }error_propagate (errp, local_err); -visit_free (qmp_output_get_visitor (qov)); +visit_free (v);return ret; }
replay/replay-input.c +2 -4
@@ -22,15 +22,13 @@static InputEvent *qapi_clone_InputEvent (InputEvent *src) { -QmpOutputVisitor *qov;Visitor *ov, *iv;QObject *obj;InputEvent *dst =NULL ; - qov =qmp_output_visitor_new (); - ov =qmp_output_get_visitor (qov); + ov =qmp_output_visitor_new (&obj);visit_type_InputEvent (ov,NULL , &src, &error_abort); - obj =qmp_output_get_qobject (qov); +visit_complete (ov, &obj);visit_free (ov);if (!obj) {return NULL ;
tests/check-qnull.c +8 -9
@@ -38,8 +38,7 @@ static void qnull_ref_test(void)static void qnull_visit_test (void ) {QObject *obj; -QmpOutputVisitor *qov; -Visitor *qiv; +Visitor *v;/* * Most tests of interactions between QObject and visitors are in
@@ -49,17 +48,17 @@ static void qnull_visit_test(void)g_assert (qnull_.refcnt ==1 ); obj =qnull (); - qiv =qmp_input_visitor_new (obj, true); + v =qmp_input_visitor_new (obj, true);qobject_decref (obj); -visit_type_null (qiv,NULL , &error_abort); -visit_free (qiv); +visit_type_null (v,NULL , &error_abort); +visit_free (v); - qov =qmp_output_visitor_new (); -visit_type_null (qmp_output_get_visitor (qov),NULL , &error_abort); - obj =qmp_output_get_qobject (qov); + v =qmp_output_visitor_new (&obj); +visit_type_null (v,NULL , &error_abort); +visit_complete (v, &obj);g_assert (obj == &qnull_);qobject_decref (obj); -visit_free (qmp_output_get_visitor (qov)); +visit_free (v);g_assert (qnull_.refcnt ==1 ); }
tests/test-qmp-output-visitor.c +3 -8
@@ -22,7 +22,6 @@#include "qapi/qmp/qobject-json.h" typedef struct TestOutputVisitorData { -QmpOutputVisitor *qov;Visitor *ov;QObject *obj; }TestOutputVisitorData ;
@@ -30,18 +29,14 @@ typedef struct TestOutputVisitorData {static void visitor_output_setup (TestOutputVisitorData *data,const void *unused) { - data->qov =qmp_output_visitor_new (); -g_assert (data->qov !=NULL ); - - data->ov =qmp_output_get_visitor (data->qov); -g_assert (data->ov !=NULL ); + data->ov =qmp_output_visitor_new (&data->obj); +g_assert (data->ov); }static void visitor_output_teardown (TestOutputVisitorData *data,const void *unused) {visit_free (data->ov); - data->qov =NULL ; data->ov =NULL ;qobject_decref (data->obj); data->obj =NULL ;
@@ -49,7 +44,7 @@ static void visitor_output_teardown(TestOutputVisitorData *data,static QObject *visitor_get (TestOutputVisitorData *data) { - data->obj =qmp_output_get_qobject (data->qov); +visit_complete (data->ov, &data->obj);g_assert (data->obj);return data->obj; }
tests/test-string-output-visitor.c +2 -6
@@ -21,7 +21,6 @@#include "qapi/qmp/types.h" typedef struct TestOutputVisitorData { - StringOutputVisitor *sov; Visitor *ov; char *str; bool human;
@@ -31,9 +30,7 @@ static void visitor_output_setup_internal(TestOutputVisitorData *data, bool human) { data->human = human; - data->sov =string_output_visitor_new (human); -g_assert (data->sov); - data->ov =string_output_get_visitor (data->sov); + data->ov =string_output_visitor_new (human, &data->str);g_assert (data->ov); }
@@ -53,7 +50,6 @@ static void visitor_output_teardown(TestOutputVisitorData *data, const void *unused) {visit_free (data->ov); - data->sov =NULL ; data->ov =NULL ;g_free (data->str); data->str =NULL ;
@@ -61,7 +57,7 @@ static void visitor_output_teardown(TestOutputVisitorData *data,static char *visitor_get (TestOutputVisitorData *data) { - data->str =string_output_get_string (data->sov); +visit_complete (data->ov, &data->str);g_assert (data->str);return data->str; }
tests/test-visitor-serialization.c +12 -10
@@ -1013,7 +1013,8 @@ static PrimitiveType pt_values[] = {/* visitor-specific op implementations */ typedef struct QmpSerializeData { -QmpOutputVisitor *qov; +Visitor *qov; +QObject *obj;Visitor *qiv; }QmpSerializeData ;
@@ -1022,8 +1023,8 @@ static void qmp_serialize(void *native_in, void **datap, {QmpSerializeData *d =g_malloc0 (sizeof (*d)); - d->qov =qmp_output_visitor_new (); -visit (qmp_output_get_visitor (d->qov), &native_in, errp); + d->qov =qmp_output_visitor_new (&d->obj); +visit (d->qov, &native_in, errp); *datap = d; }
@@ -1034,7 +1035,8 @@ static void qmp_deserialize(void **native_out, void *datap,QString *output_json;QObject *obj_orig, *obj; - obj_orig =qmp_output_get_qobject (d->qov); +visit_complete (d->qov, &d->obj); + obj_orig = d->obj; output_json =qobject_to_json (obj_orig); obj =qobject_from_json (qstring_get_str (output_json));
@@ -1048,7 +1050,7 @@ static void qmp_deserialize(void **native_out, void *datap,static void qmp_cleanup (void *datap) {QmpSerializeData *d = datap; -visit_free (qmp_output_get_visitor (d->qov)); +visit_free (d->qov);visit_free (d->qiv);g_free (d);
@@ -1056,7 +1058,7 @@ static void qmp_cleanup(void *datap)typedef struct StringSerializeData {char *string; -StringOutputVisitor *sov; +Visitor *sov;Visitor *siv; }StringSerializeData ;
@@ -1065,8 +1067,8 @@ static void string_serialize(void *native_in, void **datap, {StringSerializeData *d =g_malloc0 (sizeof (*d)); - d->sov =string_output_visitor_new (false); -visit (string_output_get_visitor (d->sov), &native_in, errp); + d->sov =string_output_visitor_new (false, &d->string); +visit (d->sov, &native_in, errp); *datap = d; }
@@ -1075,7 +1077,7 @@ static void string_deserialize(void **native_out, void *datap, {StringSerializeData *d = datap; - d->string =string_output_get_string (d->sov); +visit_complete (d->sov, &d->string); d->siv =string_input_visitor_new (d->string);visit (d->siv, native_out, errp); }
@@ -1084,7 +1086,7 @@ static void string_cleanup(void *datap) {StringSerializeData *d = datap; -visit_free (string_output_get_visitor (d->sov)); +visit_free (d->sov);visit_free (d->siv);g_free (d->string);g_free (d);
util/qemu-sockets.c +2 -4
@@ -1129,16 +1129,14 @@ SocketAddress *socket_remote_address(int fd, Error **errp)void qapi_copy_SocketAddress (SocketAddress **p_dest,SocketAddress *src) { -QmpOutputVisitor *qov;Visitor *ov, *iv;QObject *obj; *p_dest =NULL ; - qov =qmp_output_visitor_new (); - ov =qmp_output_get_visitor (qov); + ov =qmp_output_visitor_new (&obj);visit_type_SocketAddress (ov,NULL , &src, &error_abort); - obj =qmp_output_get_qobject (qov); +visit_complete (ov, &obj);visit_free (ov);if (!obj) {return ;
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 12/28] qapi: Add new visit_complete() function
Quoted 9 lines · Eric Blake
Making each output visitor provide its own output collection function was the only remaining reason for exposing visitor sub-types to the rest of the code base. Add a polymorphic
visit_complete()function which is a no-op for input visitors, and which populates an opaque pointer for output visitors. For maximum type-safety, also add a parameter to the output visitor constructors with a type-correct version of the output pointer, and assert that the two uses match.
Quoted 4 lines
This approach was considered superior to either passing the output parameter only during construction (action at a distance during
visit_free()feels awkward) or only duringvisit_complete()(defeating type safety makes it easier to use incorrectly).
Reasonable.
Quoted 99 lines
Most callers were function-local, and therefore a mechanical conversion; the testsuite was a bit trickier, but the previous cleanup patch minimized the churn here.
Generated code is simplified as follows for events:
|@@ -26,7 +26,7 @@ void qapi_event_send_acpi_device_ost(ACP | QDict *qmp; | Error *err = NULL; | QMPEventFuncEmit emit; |- QmpOutputVisitor *qov; |+ QObject *obj; | Visitor *v; | q_obj_ACPI_DEVICE_OST_arg param = { | info |@@ -39,8 +39,7 @@ void qapi_event_send_acpi_device_ost(ACP | | qmp = qmp_event_build_dict("ACPI_DEVICE_OST"); | |- qov = qmp_output_visitor_new(); |- v = qmp_output_get_visitor(qov); |+ v = qmp_output_visitor_new(&obj); | | visit_start_struct(v, "ACPI_DEVICE_OST", NULL, 0, &err); | if (err) { |@@ -55,7 +54,8 @@ void qapi_event_send_acpi_device_ost(ACP | goto out; | } | |- qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov)); |+ visit_complete(v, &obj); |+ qdict_put_obj(qmp, "data", obj); | emit(QAPI_EVENT_ACPI_DEVICE_OST, qmp, &err);and for commands:
| { | Error *err = NULL; |- QmpOutputVisitor *qov = qmp_output_visitor_new(); | Visitor *v; | |- v = qmp_output_get_visitor(qov); |+ v = qmp_output_visitor_new(ret_out); | visit_type_AddfdInfo(v, "unused", &ret_in, &err); |- if (err) { |- goto out; |+ if (!err) { |+ visit_complete(v, ret_out); | } |- *ret_out = qmp_output_get_qobject(qov); |- |-out: | error_propagate(errp, err);Signed-off-by Eric Blake <[email protected]>---
v4: new patch, inspired by review of v3 7/18--- include/qapi/visitor.h | 52 +++++++++++++++++++++--------------- include/qapi/visitor-impl.h | 3 +++ scripts/qapi-commands.py | 10 +++---- scripts/qapi-event.py | 8 +++--- include/qapi/qmp-output-visitor.h | 11 +++++--- include/qapi/string-output-visitor.h | 10 ++++--- qapi/qapi-visit-core.c | 10 +++++++ block/qapi.c | 9 +++---- blockdev.c | 9 +++---- hmp.c | 11 ++++---- net/net.c | 11 ++++---- qapi/qmp-output-visitor.c | 21 ++++++++------- qapi/string-output-visitor.c | 22 ++++++++------- qemu-img.c | 30 ++++++++++----------- qom/object.c | 28 +++++++++---------- qom/qom-qobject.c | 10 +++---- replay/replay-input.c | 6 ++--- tests/check-qnull.c | 17 ++++++------ tests/test-qmp-output-visitor.c | 11 +++----- tests/test-string-output-visitor.c | 8 ++---- tests/test-visitor-serialization.c | 22 ++++++++------- util/qemu-sockets.c | 6 ++--- 22 files changed, 167 insertions(+), 158 deletions(-)include/qapi/visitor.h +1
-6@@ -39,21 +39,15 @@* * All of the visitors are created via: * -* Type *subtype_visitor_new(parameters...); -* -* where Type is either directly 'Visitor *', or is a subtype that can -* be trivially upcast to Visitor * via another function: -* -* Visitor *subtype_get_visitor(SubtypeVisitor *); +* Visitor *subtype_visitor_new(parameters...); *
include/qapi/visitor.h line 46
Quoted 10 lines
include/qapi/visitor.h +2
-7* A visitor should be used for exactly one top-level visit_type_FOO() -* or virtual walk, then passed to visit_free() to clean up resources. -* It is okay to free the visitor without completing the visit, if -* some other error is detected in the meantime. Output visitors -* provide an additional function, for collecting the final results of -* a successful visit: string_output_get_string() and -* qmp_output_get_qobject(); this collection function should not be -* called if any errors were reported during the visit. +* or virtual walk; if that is successful, the caller can optionally +* call visit_complete() (most useful for output visits). Then,
Well, it's mostly useless for any other visits, isn't it?
Quoted 99 lines
include/qapi/visitor.h +28
-8+* regardless of success or failure, the user should call visit_free() +* to clean up resources. It is okay to free the visitor without +* completing the visit, if some other error is detected in the +* meantime. * * All QAPI types have a corresponding function with a signature * roughly compatible with this: @@ -123,14 +117,14 @@* Error *err = NULL; * Visitor *v; * -* v = ...obtain input visitor... +* v = FOO_visitor_new(...); * visit_type_Foo(v, NULL, &f, &err); * if (err) { * ...handle error... * } else { * ...use f... * } -* ...clean up v... +* visit_free(v); * qapi_free_Foo(f); * </example> * @@ -140,7 +134,7 @@* Error *err = NULL; * Visitor *v; * -* v = ...obtain input visitor... +* v = FOO_visitor_new(...); * visit_type_FooList(v, NULL, &l, &err); * if (err) { * ...handle error... @@ -149,7 +143,7 @@* ...use l->value... * } * } -* ...clean up v... +* visit_free(v); * qapi_free_FooList(l); * </example> * @@ -159,13 +153,17 @@* Foo *f = ...obtain populated object... * Error *err = NULL; * Visitor *v; +* Type *result; * -* v = ...obtain output visitor... +* v = FOO_visitor_new(..., &result); * visit_type_Foo(v, NULL, &f, &err); * if (err) { * ...handle error... +* } else { +* visit_complete(v, &result); +* ...use result... * } -* ...clean up v... +* visit_free(v); * </example> * * When visiting a real QAPI struct, this file provides several @@ -191,7 +189,7 @@* Error *err = NULL; * int value; * -* v = ...obtain visitor... +* v = FOO_visitor_new(...); * visit_start_struct(v, NULL, NULL, 0, &err); * if (err) { * goto out; @@ -219,7 +217,7 @@* visit_end_struct(v, NULL); * out: * error_propagate(errp, err); -* ...clean up v... +* visit_free(v); * </example> */ @@ -243,6 +241,18 @@ typedef struct GenericAlternate {/*** Visitor cleanup ***/ /* +* Complete the visit, collecting any output. +* +* May only be called after a successful top-level visit_type_FOO() or +* visit_end_ITEM(), and marks the end of the visit. The @opaque +* pointer should match the output parameter passed to the +* subtype_visitor_new() used to create an output visitor, or NULL for +* any other visitor. Needed for output visitors, but may also be +* called with other visitors. +*/ +void visit_complete (Visitor *v,void *opaque); + +/* * Free @v and any resources it has tied up. * * May be called whether or not the visit has been successfully
[...]
Quoted 11 lines
include/qapi/qmp-output-visitor.h +0
-3@@ -19,9 +19,12 @@typedef struct QmpOutputVisitor QmpOutputVisitor ; -QmpOutputVisitor *qmp_output_visitor_new (void ); - -QObject *qmp_output_get_qobject (QmpOutputVisitor *v);
include/qapi/qmp-output-visitor.h line 30
Quoted 10 lines
include/qapi/qmp-output-visitor.h +7
-1-Visitor *qmp_output_get_visitor (QmpOutputVisitor *v); +/* +* Create a new QMP output visitor. +* +* If everything else succeeds, pass @result to visit_complete() to +* collect the result of the visit. +*/ +Visitor *qmp_output_visitor_new (QObject **result);#endif
One qmp_output_get_visitor() left behind in docs/qapi-code-gen.txt.
include/qapi/string-output-visitor.h line 25
Quoted 13 lines
include/qapi/string-output-visitor.h +5
-0@@ -18,13 +18,15 @@typedef struct StringOutputVisitor StringOutputVisitor ;/* +* Create a new string output visitor. +* +* If everything else succeeds, pass @result to visit_complete() to +* collect the result of the visit. +*
Document @human while we're here?
Quoted 16 lines
include/qapi/string-output-visitor.h +1
-4* The string output visitor does not implement support for visiting * QAPI structs, alternates, null, or arbitrary QTypes. It also * requires a non-null list argument to visit_start_list(). */ -StringOutputVisitor *string_output_visitor_new (bool human); - -char *string_output_get_string (StringOutputVisitor *v); -Visitor *string_output_get_visitor (StringOutputVisitor *v); +Visitor *string_output_visitor_new (bool human,char **result);#endif qapi/qapi-visit-core.c +0
-0
qapi/qapi-visit-core.c line 27
Quoted 8 lines
qapi/qapi-visit-core.c +5
-0@@ -20,6 +20,16 @@#include "qapi/visitor.h" #include "qapi/visitor-impl.h" +void visit_complete (Visitor *v,void *opaque) +{ +if (v->type ==VISITOR_OUTPUT ) { +assert (v->complete); + }
assert(v->type != VISITOR_OUTPUT || v->complete);
Quoted 8 lines
qapi/qapi-visit-core.c +5
-0+if (v->complete) { + v->complete (v, opaque); + } +} +void visit_free (Visitor *v) {if (v) {
[...]
Quoted 24 lines
qapi/qmp-output-visitor.c +5
-1@@ -32,6 +32,7 @@ struct QmpOutputVisitorVisitor visitor;QStack stack;/* Stack of containers that haven't yet been finished */ QObject *root;/* Root of the output visit */ +QObject **result;/* User's storage location for result */ };#define qmp_output_add (qov, name, value) \@@ -195,18 +196,17 @@ static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)/* Finish building, and return the root object. * The root object is never null. The caller becomes the object's * owner, and should use qobject_decref() when done with it. */ -QObject *qmp_output_get_qobject (QmpOutputVisitor *qov) +static void qmp_output_complete (Visitor *v,void *opaque) { +QmpOutputVisitor *qov =to_qov (v); +/* A visit must have occurred, with each start paired with end. */ assert (qov->root &&QTAILQ_EMPTY (&qov->stack)); +assert (opaque == qov->result);
qapi/qmp-output-visitor.c static void qmp_output_type_null(Visitor *v, const char *name, Error **errp) · line 209
Quoted 10 lines
qapi/qmp-output-visitor.c +2
-6qobject_incref (qov->root); -return qov->root; -} - -Visitor *qmp_output_get_visitor (QmpOutputVisitor *v) -{ -return &v->visitor; + *qov->result = qov->root; + qov->result =NULL ;
Why zap qov->result?
If we want to do that, we better spell out that visit_complete() may be called at most once.
I like idempotent functions... but see string_output_complete() below.
Quoted 41 lines
qapi/qmp-output-visitor.c +5
-2}static void qmp_output_free (Visitor *v)@@ -223,7 +223,7 @@ static void qmp_output_free(Visitor *v)g_free (qov); } -QmpOutputVisitor *qmp_output_visitor_new (void ) +Visitor *qmp_output_visitor_new (QObject **result) {QmpOutputVisitor *v;@@ -242,9 +242,12 @@ QmpOutputVisitor *qmp_output_visitor_new(void) v->visitor.type_number = qmp_output_type_number; v->visitor.type_any = qmp_output_type_any; v->visitor.type_null = qmp_output_type_null; + v->visitor.complete = qmp_output_complete; v->visitor.free = qmp_output_free;QTAILQ_INIT (&v->stack); + *result =NULL ; + v->result = result; -return v; +return &v->visitor; }qapi/string-output-visitor.c +1
-0@@ -58,6 +58,7 @@ struct StringOutputVisitorVisitor visitor;bool human;GString *string; +char **result;ListMode list_mode;union {int64_t s;@@ -302,16 +303,13 @@ static void end_list(Visitor *v, void **obj) sov->list_mode =LM_NONE ; }
qapi/string-output-visitor.c static void end_list(Visitor *v, void **obj) · line 312
Quoted 10 lines
qapi/string-output-visitor.c +5
-2-char *string_output_get_string (StringOutputVisitor *sov) +static void string_output_complete (Visitor *v,void *opaque) { -char *string =g_string_free (sov->string, false); +StringOutputVisitor *sov =to_sov (v); + +assert (opaque == sov->result); + *sov->result =g_string_free (sov->string, false); sov->string =NULL ;
Why zap sov->string?
I guess it lets you transfer ownership of the string inside the GString without copying it. An idempotent string_output_complete() would have to copy. Okay, I can buy that argument for making it non-idempotent.
Quoted 29 lines
qapi/string-output-visitor.c +4
-7-return string; -} - -Visitor *string_output_get_visitor (StringOutputVisitor *sov) -{ -return &sov->visitor; }static void free_range (void *range,void *dummy)@@ -332,7 +330,7 @@ static void string_output_free(Visitor *v)g_free (sov); } -StringOutputVisitor *string_output_visitor_new (bool human) +Visitor *string_output_visitor_new (bool human,char **result) {StringOutputVisitor *v;@@ -340,6 +338,9 @@ StringOutputVisitor *string_output_visitor_new(bool human) v->string =g_string_new (NULL ); v->human = human; + v->result = result; + *result =NULL ; + v->visitor.type =VISITOR_OUTPUT ; v->visitor.type_int64 = print_type_int64; v->visitor.type_uint64 = print_type_uint64;
qapi/string-output-visitor.c StringOutputVisitor *string_output_visitor_new(bool human) · line 358
Quoted 9 lines
qapi/string-output-visitor.c +2
-1@@ -350,7 +351,8 @@ StringOutputVisitor *string_output_visitor_new(bool human) v->visitor.start_list = start_list; v->visitor.next_list = next_list; v->visitor.end_list = end_list; + v->visitor.complete = string_output_complete; v->visitor.free = string_output_free; -return v; +return &v->visitor; }
[...]
I rather like how PATCH 05-12 reduce boilerplate in visitor usage.
PATCH v4 13/28 qapi: Add new clone visitor
In reply to Eric BlakeWe have a couple places in the code base that want to deep-clone one QAPI object into another, and they were resorting to serializing the struct out to QObject then reparsing it. A much more efficient version can be done by adding a new clone visitor.
Since cloning is still relatively uncommon, expose the use of the new visitor via a QAPI_CLONE() macro that takes care of type-punning the underlying function pointer, rather than generating lots of unused functions for types that won't be cloned. And yes, we're relying on the compiler treating all pointers equally, even though a strict C program cannot portably do so - but we're not the first one in the qemu code base to expect it to work (hello, glib!).
Note that we can only clone objects (including alternates) and lists, not built-ins or enums. The visitor core hides integer width from the actual visitor (since commit 04e070d), and as long as that's the case, we can't clone top-level integers. Then again, those can always be cloned by direct copy, since they are not objects with deep pointers, so it's no real loss. And restricting cloning to just objects and lists is cleaner than restricting it to non-integers. As such, I documented that the clone visitor is for direct use only by code internal to QAPI.
Scalars not at the root of the clone copy just fine, by virtue of a g_memdup() each time we push another struct onto the stack.
Cloning an 'any' type could be possible by incrementing the QObject refcnt, but it's not obvious whether that is better than implementing a QObject deep clone. So for now, we document it as unsupported, and intentionally omit the .type_any() callback to let a developer know their usage needs implementation.
The choice of adding a fourth visitor type deserves some explanation. On the surface, the clone visitor is mostly an input visitor (it takes arbitrary input - in this case, another QAPI object - and creates a new QAPI object during the course of the visit). But ever since commit da72ab0 consolidated enum visits based on the visitor type, using VISITOR_INPUT would cause us to run visit_type_str(), even though for cloning there is nothing to do (we just copy the enum value across, without regards to its mapping
to strings). Also, since our input happens to be a QAPI object,
we can also satisfy the internal checks for VISITOR_OUTPUT. So in the end, I settled with a new VISITOR_CLONE, and chose its value such that many internal checks can use 'v->type & mask', sticking to 'v->type == value' where the difference matters.
Add testsuite coverage for several different clone situations, to ensure that the code is working. I also tested that valgrind was happy with the test.
---
v4: hoist earlier in series, drop wasted generated functions and replace it with QAPI_CLONE() macro, drop 'any' support, tweak core assertion checking, internal improve commit message
v3: new patch
--- include/qapi/visitor.h | 33 ++++--- include/qapi/visitor-impl.h | 12 +-- include/qapi/clone-visitor.h | 37 ++++++++ qapi/qapi-visit-core.c | 18 ++-- qapi/qapi-clone-visitor.c | 178 +++++++++++++++++++++++++++++++++++++ tests/test-clone-visitor.c | 207 +++++++++++++++++++++++++++++++++++++++++++ qapi/Makefile.objs | 2 +- tests/.gitignore | 1 + tests/Makefile | 4 + 9 files changed, 466 insertions(+), 26 deletions(-) create mode 100644 include/qapi/clone-visitor.h create mode 100644 qapi/qapi-clone-visitor.c create mode 100644 tests/test-clone-visitor.c
include/qapi/visitor.h +21 -12
@@ -24,15 +24,16 @@* for doing work at each node of a QAPI graph; it can also be used * for a virtual walk, where there is no actual QAPI C struct. * -* There are three kinds of visitor classes: input visitors (QMP, +* There are four kinds of visitor classes: input visitors (QMP, * string, and QemuOpts) parse an external representation and build * the corresponding QAPI graph, output visitors (QMP and string) take -* a completed QAPI graph and generate an external representation, and -* the dealloc visitor can take a QAPI graph (possibly partially -* constructed) and recursively free its resources. While the dealloc -* and QMP input/output visitors are general, the string and QemuOpts -* visitors have some implementation limitations; see the -* documentation for each visitor for more details on what it +* a completed QAPI graph and generate an external representation, the +* dealloc visitor can take a QAPI graph (possibly partially +* constructed) and recursively free its resources, and the clone +* visitor performs a deep clone of one QAPI object to another. While +* the dealloc and QMP input/output visitors are general, the string, +* QemuOpts, and clone visitors have some implementation limitations; +* see the documentation for each visitor for more details on what it * supports. Also, see visitor-impl.h for the callback contracts * implemented by each visitor, and docs/qapi-code-gen.txt for more * about the QAPI code generator.
@@ -102,11 +103,19 @@* * void qapi_free_FOO(FOO *obj); * -* which behaves like free() in that @obj may be NULL. Because of -* these functions, the dealloc visitor is seldom used directly -* outside of generated code. QAPI types can also inherit from a base -* class; when this happens, a function is generated for easily going -* from the derived type to the base type: +* where behaves like free() in that @obj may be NULL. Such objects +* may also be used with the following macro, provided alongside the +* clone visitor: +* +* Type *QAPI_CLONE(Type, src); +* +* in order to perform a deep clone of @src. Because of the generated +* qapi_free functions and the QAPI_CLONE() macro, the clone and +* dealloc visitor should not be used directly outside of QAPI code. +* +* QAPI types can also inherit from a base class; when this happens, a +* function is generated for easily going from the derived type to the +* base type: * * BASE *qapi_CHILD_base(CHILD *obj); *
include/qapi/visitor-impl.h +7 -5
@@ -27,14 +27,16 @@*/ /* -* There are three classes of visitors; setting the class determines +* There are four classes of visitors; setting the class determines * how QAPI enums are visited, as well as what additional restrictions -* can be asserted. +* can be asserted. The values are intentionally chosen so as to +* permit some assertions based on whether a given bit is set. */ typedef enum VisitorType { -VISITOR_INPUT , -VISITOR_OUTPUT , -VISITOR_DEALLOC , +VISITOR_INPUT =1 , +VISITOR_OUTPUT =2 , +VISITOR_CLONE =3 , +VISITOR_DEALLOC =4 , }VisitorType ;struct Visitor
include/qapi/clone-visitor.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* Clone Visitor +* +* Copyright (C) 2016 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef QAPI_CLONE_VISITOR_H +#define QAPI_CLONE_VISITOR_H + +#include "qapi/visitor.h" + +/* +* The clone visitor is for direct use only by the QAPI_CLONE() macro; +* it requires that the root visit occur on an object, list, or +* alternate, and is not usable directly on built-in QAPI types. +*/ +typedef struct QapiCloneVisitor QapiCloneVisitor ; + +void *qapi_clone (const void *src,void (*visit_type)(Visitor *,const char *, +void **,Error **)); + +/* +* Deep-clone QAPI object @src of the given @type, and return the result. +* +* Not usable on QAPI scalars (integers, strings, enums), nor on a +* QAPI object that references the 'any' type. Safe when @src is NULL. +*/ +#define QAPI_CLONE (type, src) \ + ((type *)qapi_clone(src, \ + (void (*)(Visitor *, const char *, void**, \ + Error **))visit_type_ ## type)) + +#endif
qapi/qapi-visit-core.c +10 -8
@@ -44,10 +44,10 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,if (obj) {assert (size); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj); } v->start_struct (v, name, obj, size, &err); -if (obj && v->type ==VISITOR_INPUT ) { +if (obj && (v->type &VISITOR_INPUT )) {assert (!err != !*obj); }error_propagate (errp, err);
@@ -72,7 +72,7 @@ void visit_start_list(Visitor *v, const char *name, GenericList **list,assert (!list || size >=sizeof (GenericList)); v->start_list (v, name, list, size, &err); -if (list && v->type ==VISITOR_INPUT ) { +if (list && (v->type &VISITOR_INPUT )) {assert (!(err && *list)); }error_propagate (errp, err);
@@ -96,11 +96,11 @@ void visit_start_alternate(Visitor *v, const char *name,Error *err =NULL ;assert (obj && size >=sizeof (GenericAlternate)); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj);if (v->start_alternate) { v->start_alternate (v, name, obj, size, promote_int, &err); } -if (v->type ==VISITOR_INPUT ) { +if (v->type &VISITOR_INPUT ) {assert (v->start_alternate && !err != !*obj); }error_propagate (errp, err);
@@ -252,9 +252,10 @@ void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)assert (obj);/* TODO: Fix callers to not pass NULL when they mean "", so that we * can enable: -assert(v->type != VISITOR_OUTPUT || *obj); +assert(!(v->type & VISITOR_OUTPUT) || *obj); */ v->type_str (v, name, obj, &err); +/* Likewise, use of NULL means we can't do (v->type & VISITOR_INPUT) */ if (v->type ==VISITOR_INPUT ) {assert (!err != !*obj); }
@@ -273,9 +274,9 @@ void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)Error *err =NULL ;assert (obj); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj); v->type_any (v, name, obj, &err); -if (v->type ==VISITOR_INPUT ) { +if (v->type &VISITOR_INPUT ) {assert (!err != !*obj); }error_propagate (errp, err);
@@ -342,4 +343,5 @@ void visit_type_enum(Visitor *v, const char *name, int *obj, }else if (v->type ==VISITOR_OUTPUT ) {output_type_enum (v, name, obj, strings, errp); } +/* dealloc and clone visitors have nothing to do */ }
qapi/qapi-clone-visitor.c new file +178 -0
178 lines not on this page · raw message
tests/test-clone-visitor.c new file +207 -0
207 lines not on this page · raw message
qapi/Makefile.objs +1 -1
@@ -1,6 +1,6 @@ util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o util-obj-y += string-input-visitor.o string-output-visitor.o -util-obj-y += opts-visitor.o +util-obj-y += opts-visitor.o qapi-clone-visitor.o util-obj-y += qmp-event.o util-obj-y += qapi-util.o
tests/.gitignore +1 -0
@@ -12,6 +12,7 @@ test-aio test-base64 test-bitops test-blockjob-txn +test-clone-visitor test-coroutine test-crypto-afsplit test-crypto-block
tests/Makefile +4 -0
@@ -22,6 +22,8 @@ check-unit-y += tests/check-qobject-json$(EXESUF) gcov-files-check-qobject-json-y = qobject/qobject-json.c check-unit-y += tests/test-qmp-output-visitor$(EXESUF) gcov-files-test-qmp-output-visitor-y = qapi/qmp-output-visitor.c +check-unit-y += tests/test-clone-visitor$(EXESUF) +gcov-files-test-clone-visitor-y = qapi/qapi-clone-visitor.c check-unit-y += tests/test-qmp-input-visitor$(EXESUF) gcov-files-test-qmp-input-visitor-y = qapi/qmp-input-visitor.c check-unit-y += tests/test-qmp-input-strict$(EXESUF)
@@ -388,6 +390,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/check-qobject-json.o \ tests/test-coroutine.o tests/test-string-output-visitor.o \ tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \ + tests/test-clone-visitor.o \ tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \ tests/test-qmp-commands.o tests/test-visitor-serialization.o \ tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \
@@ -478,6 +481,7 @@ tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $( tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y) tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y) tests/test-qmp-output-visitor$(EXESUF): tests/test-qmp-output-visitor.o $(test-qapi-obj-y) +tests/test-clone-visitor$(EXESUF): tests/test-clone-visitor.o $(test-qapi-obj-y) tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(test-qapi-obj-y) tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y) tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marshal.o $(test-qapi-obj-y)
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 13/28] qapi: Add new clone visitor
Quoted 90 lines · Eric Blake
We have a couple places in the code base that want to deep-clone one QAPI object into another, and they were resorting to serializing the struct out to QObject then reparsing it. A much more efficient version can be done by adding a new clone visitor.
Since cloning is still relatively uncommon, expose the use of the new visitor via a
QAPI_CLONE()macro that takes care of type-punning the underlying function pointer, rather than generating lots of unused functions for types that won't be cloned. And yes, we're relying on the compiler treating all pointers equally, even though a strict C program cannot portably do so - but we're not the first one in the qemu code base to expect it to work (hello, glib!).Note that we can only clone objects (including alternates) and lists, not built-ins or enums. The visitor core hides integer width from the actual visitor (since commit 04e070d), and as long as that's the case, we can't clone top-level integers. Then again, those can always be cloned by direct copy, since they are not objects with deep pointers, so it's no real loss. And restricting cloning to just objects and lists is cleaner than restricting it to non-integers. As such, I documented that the clone visitor is for direct use only by code internal to QAPI.
Scalars not at the root of the clone copy just fine, by virtue of a
g_memdup()each time we push another struct onto the stack.Cloning an 'any' type could be possible by incrementing the QObject refcnt, but it's not obvious whether that is better than implementing a QObject deep clone. So for now, we document it as unsupported, and intentionally omit the .type_any() callback to let a developer know their usage needs implementation.
The choice of adding a fourth visitor type deserves some explanation. On the surface, the clone visitor is mostly an input visitor (it takes arbitrary input - in this case, another QAPI object - and creates a new QAPI object during the course of the visit). But ever since commit da72ab0 consolidated enum visits based on the visitor type, using
VISITOR_INPUTwould cause us to runvisit_type_str(), even though for cloning there is nothing to do (we just copy the enum value across, without regards to its mapping
to strings). Also, since our input happens to be a QAPI object,
we can also satisfy the internal checks forVISITOR_OUTPUT. So in the end, I settled with a newVISITOR_CLONE, and chose its value such that many internal checks can use 'v->type& mask', sticking to 'v->type== value' where the difference matters.Add testsuite coverage for several different clone situations, to ensure that the code is working. I also tested that valgrind was happy with the test.
Signed-off-by Eric Blake <[email protected]>---
v4: hoist earlier in series, drop wasted generated functions and replace it withQAPI_CLONE()macro, drop 'any' support, tweak core assertion checking, internal improve commit message
v3: new patch--- include/qapi/visitor.h | 33 ++++--- include/qapi/visitor-impl.h | 12 +-- include/qapi/clone-visitor.h | 37 ++++++++ qapi/qapi-visit-core.c | 18 ++-- qapi/qapi-clone-visitor.c | 178 +++++++++++++++++++++++++++++++++++++ tests/test-clone-visitor.c | 207 +++++++++++++++++++++++++++++++++++++++++++ qapi/Makefile.objs | 2 +- tests/.gitignore | 1 + tests/Makefile | 4 + 9 files changed, 466 insertions(+), 26 deletions(-) create mode 100644 include/qapi/clone-visitor.h create mode 100644 qapi/qapi-clone-visitor.c create mode 100644 tests/test-clone-visitor.cinclude/qapi/visitor.h +1
-7@@ -24,15 +24,16 @@* for doing work at each node of a QAPI graph; it can also be used * for a virtual walk, where there is no actual QAPI C struct. * -* There are three kinds of visitor classes: input visitors (QMP, +* There are four kinds of visitor classes: input visitors (QMP, * string, and QemuOpts) parse an external representation and build * the corresponding QAPI graph, output visitors (QMP and string) take -* a completed QAPI graph and generate an external representation, and -* the dealloc visitor can take a QAPI graph (possibly partially -* constructed) and recursively free its resources. While the dealloc -* and QMP input/output visitors are general, the string and QemuOpts -* visitors have some implementation limitations; see the -* documentation for each visitor for more details on what it
include/qapi/visitor.h line 39
Quoted 10 lines
include/qapi/visitor.h +7
-0+* a completed QAPI graph and generate an external representation, the +* dealloc visitor can take a QAPI graph (possibly partially +* constructed) and recursively free its resources, and the clone +* visitor performs a deep clone of one QAPI object to another. While +* the dealloc and QMP input/output visitors are general, the string, +* QemuOpts, and clone visitors have some implementation limitations; +* see the documentation for each visitor for more details on what it * supports. Also, see visitor-impl.h for the callback contracts * implemented by each visitor, and docs/qapi-code-gen.txt for more * about the QAPI code generator.
[...]
* If an error is detected during visit_type_FOO() with an input
* visitor, then *@obj will be NULL for pointer types, and left
* unchanged for scalar types. Using an output visitor with an
* incomplete object has undefined behavior (other than a special case
* for visit_type_str() treating NULL like ""), while the dealloc
* visitor safely handles incomplete objects. Since input visitors
* never produce an incomplete object, such an object is possible only
* by manual construction.
What about the clone visitor?
Quoted 33 lines
include/qapi/visitor.h +13
-5@@ -102,11 +103,19 @@* * void qapi_free_FOO(FOO *obj); * -* which behaves like free() in that @obj may be NULL. Because of -* these functions, the dealloc visitor is seldom used directly -* outside of generated code. QAPI types can also inherit from a base -* class; when this happens, a function is generated for easily going -* from the derived type to the base type: +* where behaves like free() in that @obj may be NULL. Such objects +* may also be used with the following macro, provided alongside the +* clone visitor: +* +* Type *QAPI_CLONE(Type, src); +* +* in order to perform a deep clone of @src. Because of the generated +* qapi_free functions and the QAPI_CLONE() macro, the clone and +* dealloc visitor should not be used directly outside of QAPI code. +* +* QAPI types can also inherit from a base class; when this happens, a +* function is generated for easily going from the derived type to the +* base type: * * BASE *qapi_CHILD_base(CHILD *obj); * include/qapi/visitor-impl.h +0
-0
include/qapi/visitor-impl.h line 33
Quoted 9 lines
include/qapi/visitor-impl.h +3
-2@@ -27,14 +27,16 @@*/ /* -* There are three classes of visitors; setting the class determines +* There are four classes of visitors; setting the class determines * how QAPI enums are visited, as well as what additional restrictions -* can be asserted. +* can be asserted. The values are intentionally chosen so as to +* permit some assertions based on whether a given bit is set.
We'll see below that the code now wants to accept a VISITOR_CLONE in addition to VISITOR_INPUT in most places, and similarly in addition to VISITOR_OUTPUT. Subtle.
include/qapi/visitor-impl.h line 42
Quoted 12 lines
include/qapi/visitor-impl.h +4
-3*/ typedef enum VisitorType { -VISITOR_INPUT , -VISITOR_OUTPUT , -VISITOR_DEALLOC , +VISITOR_INPUT =1 , +VISITOR_OUTPUT =2 , +VISITOR_CLONE =3 , +VISITOR_DEALLOC =4 , }VisitorType ;struct Visitor
[...]
/*** Visiting structures ***/
/*
* Start visiting an object @obj (struct or union).
*
* @name expresses the relationship of this object to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL for a real walk, in which case @size
* determines how much memory an input visitor will allocate into
* *@obj. @obj may also be NULL for a virtual walk, in which case
* @size is ignored.
What about the clone visitor?
*
* @errp obeys typical error usage, and reports failures such as a
* member @name is not present, or present but not an object. On
* error, input visitors set *@obj to NULL.
What about the clone visitor?
*
* 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
* 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?
*/
void visit_start_struct(Visitor *v, const char *name, void **obj,
size_t size, Error **errp);
[...]
/*** Visiting lists ***/
/*
* Start visiting a list.
*
* @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.
What about the clone visitor?
*
* @errp obeys typical error usage, and reports failures such as a
* member @name is not present, or present but not a list. On error,
* input visitors set *@list to NULL.
What about the clone visitor?
*
* After visit_start_list() succeeds, the caller may visit its members
* 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 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);
[...]
/*** Visiting alternates ***/
/*
* Start the visit of an alternate @obj.
*
* @name expresses the relationship of this alternate to its parent
* container; see the general description of @name above. *
* @obj must not be NULL. Input visitors use @size to determine how
* much memory to allocate into *@obj, then determine the qtype of the
* next thing to be visited, stored in (*@obj)->type. Other visitors
* will leave @obj unchanged.
What about the clone visitor?
*
* If @promote_int, treat integers as QTYPE_FLOAT.
*
* 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.
*/
[...]
/*** Other helpers ***/
/*
* Does optional struct member @name need visiting?
*
* @name must not be NULL. This function is only useful between
* visit_start_struct() and visit_end_struct(), since only objects
* have optional keys.
*
* @present points to the address of the optional member's has_ flag. *
* Input visitors set *@present according to input; other visitors
* leave it unchanged. In either case, return *@present for
* convenience.
I guess this is correct for the clone visitor.
*/
bool visit_optional(Visitor *v, const char *name, bool *present);
/*
* Visit an enum value.
*
* @name expresses the relationship of this enum to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors parse input and set *@obj to
* the enumeration value, leaving @obj unchanged on error; other
* visitors use *@obj but leave it unchanged.
I guess this is correct for the clone visitor.
*
* Currently, all input visitors parse text input, and all output
* visitors produce text output. The mapping between enumeration
* values and strings is done by the visitor core, using @strings; it
* should be the ENUM_lookup array from visit-types.h.
*
* May call visit_type_str() under the hood, and the enum visit may
* fail even if the corresponding string visit succeeded; this implies
* that visit_type_str() must have no unwelcome side effects. */
void visit_type_enum(Visitor *v, const char *name, int *obj,
const char *const strings[], Error **errp);
/*
* Check if visitor is an input visitor.
Does the clone visitor count as input visitor here? Should it?
*/
bool visit_is_input(Visitor *v);
/*** Visiting built-in types ***/
/*
* Visit an integer value.
*
* @name expresses the relationship of this integer to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors set *@obj to the value;
* other visitors will leave *@obj unchanged.
I guess this is correct for the clone visitor.
*/
void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp);
[...]
/*
* Visit a boolean value.
*
* @name expresses the relationship of this boolean to its parent
* container; see the general description of @name above.
*
* @obj must be non-NULL. Input visitors set *@obj to the value;
* other visitors will leave *@obj unchanged.
I guess this is correct for the clone visitor.
*/
void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp);
/*
* Visit a string value.
*
* @name expresses the relationship of this string to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors set *@obj to the value
* (never NULL). Other visitors leave *@obj unchanged, and commonly
* treat NULL like "".
I guess this is correct for the clone visitor.
*
* It is safe to cast away const when preparing a (const char *) value
* into @obj for use by an output visitor.
*
* FIXME: Callers that try to output NULL *obj should not be allowed. */
void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp);
/*
* Visit a number (i.e. double) value.
*
* @name expresses the relationship of this number to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors set *@obj to the value;
* other visitors will leave *@obj unchanged. Visitors should
* document if infinity or NaN are not permitted.
I guess this is correct for the clone visitor.
*/
void visit_type_number(Visitor *v, const char *name, double *obj,
Error **errp); /*
* Visit an arbitrary value.
*
* @name expresses the relationship of this value to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors set *@obj to the value;
* other visitors will leave *@obj unchanged. *@obj must be non-NULL
* for output visitors.
Fine, as the clone visitor doesn't support any.
*/
void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp);
Quoted 31 lines
include/qapi/clone-visitor.h new file +25
-0@@ -0,0 +1,37 @@ +/* +* Clone Visitor +* +* Copyright (C) 2016 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef QAPI_CLONE_VISITOR_H +#define QAPI_CLONE_VISITOR_H + +#include "qapi/visitor.h" + +/* +* The clone visitor is for direct use only by the QAPI_CLONE() macro; +* it requires that the root visit occur on an object, list, or +* alternate, and is not usable directly on built-in QAPI types. +*/ +typedef struct QapiCloneVisitor QapiCloneVisitor ; + +void *qapi_clone (const void *src,void (*visit_type)(Visitor *,const char *, +void **,Error **)); +
include/qapi/clone-visitor.h line 35
Quoted 10 lines
include/qapi/clone-visitor.h new file +10
-0+/* +* Deep-clone QAPI object @src of the given @type, and return the result. +* +* Not usable on QAPI scalars (integers, strings, enums), nor on a +* QAPI object that references the 'any' type. Safe when @src is NULL. +*/ +#define QAPI_CLONE (type, src) \ + ((type *)qapi_clone(src, \ + (void (*)(Visitor *, const char *, void**, \ + Error **))visit_type_ ## type))
This casts visit_type_FOO(), where FOO is a QAPI-generated complex or alternate type, from
void (*)(Visitor *, const char *, FOO **, Error **)
to
void (*)(Visitor *, const char *, void **, Error **)
Okay. Can't see how to avoid the type-punning without generating lots of wrapper functions.
Note that scalar QAPI types have one * less: FOO * instead of FOO **. But they're explicitly not permitted here.
qapi/qapi-visit-core.c
Quoted 6 lines
include/qapi/clone-visitor.h new file +2
-0+ +#endif qapi/qapi-visit-core.c +0
-0
As we'll see further down, @obj points into the clone, except at the root, where it points to qapi_clone()'s local variable @dst. A pointer-valued *@obj still points into the source.
Now let's go through the v->type checks real slow.
First one:
void visit_complete(Visitor *v, void *opaque)
{
if (v->type == VISITOR_OUTPUT) {
assert(v->complete);
}An output visitor needs a complete(), because without it, there's no way to get the output (unless you resort to side effects). qapi-visit-core.c chooses to enforce this. Not really necessary, because qapi-visit-core.c works just fine without it.
The clone visitor has no use for complete, because it returns its output
differently: qapi_clone() returns it. Therefore, we don't want to treat clone as output here. Okay.
qapi/qapi-visit-core.c void visit_start_struct(Visitor *v, const char *name, void **obj, · line 48
Quoted 7 lines
qapi/qapi-visit-core.c +1
-1@@ -44,10 +44,10 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,if (obj) {assert (size); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj); }
For real walks (obj != NULL):
* Input visitors write *obj, and don't care for the old value.
* Output visitors read *obj, and a struct can't be null.
* The dealloc visitor reads *obj, but null is fine (partially constructed object).
* The clone visitor reads like an output visitor (except at the root) and writes like an input visitor.
Before the patch, we assert "if output visitor, then *obj isn't null".
After the patch, we do the same for the clone visitor. Correct, except at the root. There, @obj points to qapi_clone()'s @dst, which is uninitialized. I'm afraid this assertion fails if @dst happens to be null.
Can we fix this by removing the "except at the root" special case? Change qapi_clone to initialize dst = src, drop QapiCloneVisitor member @root and qapi_clone_visitor_new() parameter @src.
qapi/qapi-visit-core.c void visit_start_struct(Visitor *v, const char *name, void **obj, · line 52
Quoted 5 lines
qapi/qapi-visit-core.c +1
-1v->start_struct (v, name, obj, size, &err); -if (obj && v->type ==VISITOR_INPUT ) { +if (obj && (v->type &VISITOR_INPUT )) {assert (!err != !*obj); }
Before the patch, we assert "input visitor must either fail or create *obj for a real walk."
After the patch, we do the same for the clone visitor. Okay.
qapi/qapi-visit-core.c void visit_start_list(Visitor *v, const char *name, GenericList **list, · line 77
Quoted 9 lines
qapi/qapi-visit-core.c +1
-1error_propagate (errp, err);@@ -72,7 +72,7 @@ void visit_start_list(Visitor *v, const char *name, GenericList **list,assert (!list || size >=sizeof (GenericList)); v->start_list (v, name, list, size, &err); -if (list && v->type ==VISITOR_INPUT ) { +if (list && (v->type &VISITOR_INPUT )) {assert (!(err && *list)); }
Likewise.
Quoted 5 lines
qapi/qapi-visit-core.c +0
-0error_propagate (errp, err);@@ -96,11 +96,11 @@ void visit_start_alternate(Visitor *v, const char *name,Error *err =NULL ;assert (obj && size >=sizeof (GenericAlternate));
qapi/qapi-visit-core.c void visit_start_alternate(Visitor *v, const char *name, · line 106
Quoted 10 lines
qapi/qapi-visit-core.c +2
-2-assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj);if (v->start_alternate) { v->start_alternate (v, name, obj, size, promote_int, &err); } -if (v->type ==VISITOR_INPUT ) { +if (v->type &VISITOR_INPUT ) {assert (v->start_alternate && !err != !*obj); }error_propagate (errp, err);
Same analysis as for visit_start_struct().
[...]
bool visit_is_input(Visitor *v)
{
return v->type == VISITOR_INPUT;}
This answers my question "Does the clone visitor count as input visitor here?" Remaining question: "Should it?"
qapi/qapi-visit-core.c void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp) · line 261
Quoted 12 lines
qapi/qapi-visit-core.c +2
-1@@ -252,9 +252,10 @@ void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)assert (obj);/* TODO: Fix callers to not pass NULL when they mean "", so that we * can enable: -assert(v->type != VISITOR_OUTPUT || *obj); +assert(!(v->type & VISITOR_OUTPUT) || *obj); */ v->type_str (v, name, obj, &err); +/* Likewise, use of NULL means we can't do (v->type & VISITOR_INPUT) */ if (v->type ==VISITOR_INPUT ) {assert (!err != !*obj); }
If your head doesn't hurt by know, you either wrote this, or you're not reading closely :)
If the TODOs were already addressed, we'd again get the same analysis as for visit_start_struct(), except for the arguments about the root, which don't apply here, because the clone visitor doesn't accept scalar roots.
In the current state, the analysis needs to be modified as follows.
First assertion:
Before the patch, we'd like to assert "if output or clone visitor, then *obj isn't null". We can't as long as we need to treat null as the empty string.
After the patch, the situation is the same for the clone visitor. Okay.
Second assertion:
Before the patch, we assert "input visitor must either fail or create *obj for a real walk." The TODO doesn't apply; we create "", not null.
After the patch, we'd like to assert the same for the clone visitor, but we can't: the clone of null is null. Okay.
qapi/qapi-visit-core.c void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp) · line 282
Quoted 12 lines
qapi/qapi-visit-core.c +2
-2@@ -273,9 +274,9 @@ void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)Error *err =NULL ;assert (obj); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj); v->type_any (v, name, obj, &err); -if (v->type ==VISITOR_INPUT ) { +if (v->type &VISITOR_INPUT ) {assert (!err != !*obj); }error_propagate (errp, err);
v->type_any() will crash for the clone visitor, so these changes aren't. necessary. If you want them to future-proof the code, I need to convince myself the changes make sense, similar to what I did for the other ones in this file.
qapi/qapi-visit-core.c void visit_type_enum(Visitor *v, const char *name, int *obj, · line 347
Quoted 6 lines
qapi/qapi-visit-core.c +1
-0@@ -342,4 +343,5 @@ void visit_type_enum(Visitor *v, const char *name, int *obj, }else if (v->type ==VISITOR_OUTPUT ) {output_type_enum (v, name, obj, strings, errp); } +/* dealloc and clone visitors have nothing to do */ }
I'm upgrade my verdict from "subtle" to "scarily subtle" %-}
Quoted 31 lines
qapi/qapi-clone-visitor.c new file +25
-0@@ -0,0 +1,178 @@ +/* +* Copy one QAPI object to another +* +* Copyright (C) 2016 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include "qemu/osdep.h" +#include "qapi/clone-visitor.h" +#include "qapi/visitor-impl.h" +#include "qapi/error.h" + +struct QapiCloneVisitor { +Visitor visitor; +const void *root;/* Must be object, alternate, or list */ +size_t depth; +}; + +static QapiCloneVisitor *to_qcv (Visitor *v) +{ +return container_of (v, QapiCloneVisitor, visitor); +}
qapi/qapi-clone-visitor.c line 35
Quoted 10 lines
qapi/qapi-clone-visitor.c new file +10
-0+ +static void qapi_clone_start_struct (Visitor *v,const char *name,void **obj, +size_t size,Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +if (!obj) { +/* Only possible when visiting an alternate's object +* branch. Nothing to do here, since the earlier +* visit_start_alternate() already copied memory. */
Should visitor-impl.h explain how method start_struct() is used with alternates? I once again forgot how this works... Hmm, you explained it to me during review of v3.
Despite there's "nothing to do here", you found something to do:
Quoted 22 lines
qapi/qapi-clone-visitor.c new file +22
-0+assert (qcv->depth); +return ; + } + + *obj =g_memdup (qcv->depth ? *obj : qcv->root, size); + qcv->depth++; +} + +static void qapi_clone_end (Visitor *v,void **obj) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +if (obj) { + qcv->depth--; + } +} + +static void qapi_clone_start_list (Visitor *v,const char *name, +GenericList **listp,size_t size, +Error **errp) +{
qapi/qapi-clone-visitor.c line 67
Quoted 10 lines
qapi/qapi-clone-visitor.c new file +10
-0+qapi_clone_start_struct (v, name, (void **)listp, size, errp); +} + +static GenericList *qapi_clone_next_list (Visitor *v,GenericList *tail, +size_t size) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +/* Unshare the tail of the list cloned by g_memdup */
Humor me: g_memdup()
Quoted 101 lines
qapi/qapi-clone-visitor.c new file +101
-0+ tail->next =g_memdup (tail->next, size); +return tail->next; +} + +static void qapi_clone_start_alternate (Visitor *v,const char *name, +GenericAlternate **obj,size_t size, +bool promote_int,Error **errp) +{ +qapi_clone_start_struct (v, name, (void **)obj, size, errp); +} + +static void qapi_clone_type_int64 (Visitor *v,const char *name,int64_t *obj, +Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +/* Value was already cloned by g_memdup */ +} + +static void qapi_clone_type_uint64 (Visitor *v,const char *name, +uint64_t *obj,Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +/* Value was already cloned by g_memdup */ +} + +static void qapi_clone_type_bool (Visitor *v,const char *name,bool *obj, +Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +/* Value was already cloned by g_memdup */ +} + +static void qapi_clone_type_str (Visitor *v,const char *name,char **obj, +Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +/* Pointer was already cloned by g_memdup; create fresh copy */ + *obj =g_strdup (*obj); +} + +static void qapi_clone_type_number (Visitor *v,const char *name,double *obj, +Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +/* Value was already cloned by g_memdup */ +} + +static void qapi_clone_type_null (Visitor *v,const char *name,Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +assert (qcv->depth); +/* Nothing to do */ +} + +static void qapi_clone_free (Visitor *v) +{ +g_free (v); +} + +static Visitor *qapi_clone_visitor_new (const void *src) +{ +QapiCloneVisitor *v; + + v =g_malloc0 (sizeof (*v)); + v->root = src; + + v->visitor.type =VISITOR_CLONE ; + v->visitor.start_struct = qapi_clone_start_struct; + v->visitor.end_struct = qapi_clone_end; + v->visitor.start_list = qapi_clone_start_list; + v->visitor.next_list = qapi_clone_next_list; + v->visitor.end_list = qapi_clone_end; + v->visitor.start_alternate = qapi_clone_start_alternate; + v->visitor.end_alternate = qapi_clone_end; + v->visitor.type_int64 = qapi_clone_type_int64; + v->visitor.type_uint64 = qapi_clone_type_uint64; + v->visitor.type_bool = qapi_clone_type_bool; + v->visitor.type_str = qapi_clone_type_str; + v->visitor.type_number = qapi_clone_type_number; + v->visitor.type_null = qapi_clone_type_null; + v->visitor.free = qapi_clone_free; + +return &v->visitor; +} + +void *qapi_clone (const void *src,void (*visit_type)(Visitor *,const char *, +void **,Error **)) +{ +Visitor *v; +void *dst;
qapi/qapi-clone-visitor.c line 178
Quoted 10 lines
qapi/qapi-clone-visitor.c new file +10
-0+ +if (!src) { +return NULL ; + } + + v =qapi_clone_visitor_new (src); +visit_type (v,NULL , &dst, &error_abort); +visit_free (v); +return dst; +}
[Skipping the tests for now to get this review out today...]
Re: [Qemu-devel] [PATCH v4 13/28] qapi: Add new clone visitor
Quoted 1 line · Markus Armbruster
Eric Blake <[email protected]> writes:
[...]
Quoted 4 lines
Quoted 4 lines
qapi/qapi-visit-core.c +0
-0
[...]
Quoted 20 lines
qapi/qapi-visit-core.c void visit_start_struct(Visitor *v, const char *name, void **obj, · line 48
Quoted 7 lines
qapi/qapi-visit-core.c +1
-1@@ -44,10 +44,10 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,if (obj) {assert (size); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj); }For real walks (obj != NULL):
* Input visitors write *obj, and don't care for the old value.
* Output visitors read *obj, and a struct can't be null.
* The dealloc visitor reads *obj, but null is fine (partially constructed object).
* The clone visitor reads like an output visitor (except at the root) and writes like an input visitor.
Quoted 6 lines
Before the patch, we assert "if output visitor, then *obj isn't null".
After the patch, we do the same for the clone visitor. Correct, except at the root. There, @obj points to qapi_clone()'s @dst, which is uninitialized. I'm afraid this assertion fails if @dst happens to be null.
I can observe this failure in make check when I compile with optimization.
[...]
Re: [Qemu-devel] [PATCH v4 13/28] qapi: Add new clone visitor
In reply to Markus ArmbrusterQuoted 18 lines · Markus Armbruster
Quoted 5 lines · Eric Blake
We have a couple places in the code base that want to deep-clone one QAPI object into another, and they were resorting to serializing the struct out to QObject then reparsing it. A much more efficient version can be done by adding a new clone visitor.
[...]
* If an error is detected duringvisit_type_FOO()with an input
* visitor, then *@obj will be NULL for pointer types, and left
* unchanged for scalar types. Using an output visitor with an
* incomplete object has undefined behavior (other than a special case
* forvisit_type_str()treating NULL like ""), while the dealloc
* visitor safely handles incomplete objects. Since input visitors
* never produce an incomplete object, such an object is possible only
* by manual construction.
Quoted 1 line
What about the clone visitor?
Probably safest to document it as undefined on incomplete objects.
Quoted 11 lines
/*
* Start visiting an object @obj (struct or union).
*
* @name expresses the relationship of this object to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL for a real walk, in which case @size
* determines how much memory an input visitor will allocate into
* *@obj. @obj may also be NULL for a virtual walk, in which case
* @size is ignored.
Quoted 1 line
What about the clone visitor?
Yes, clone visitors also use size.
Quoted 7 lines
*
* @errp obeys typical error usage, and reports failures such as a
* member @name is not present, or present but not an object. On
* error, input visitors set *@obj to NULL.What about the clone visitor?
Never sets an error (ie. it can't fail on a complete source object, if you don't include abort-due-to-OOM scenarios), so I'm not sure I need to word anything differently here.
Quoted 11 lines
* Start visiting a list.
*
* @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.
Quoted 8 lines
What about the clone visitor?
*
* @errp obeys typical error usage, and reports failures such as a
* member @name is not present, or present but not a list. On error,
* input visitors set *@list to NULL.What about the clone visitor?
Same as for start_struct.
Quoted 13 lines
/*
* Does optional struct member @name need visiting?
*
* @name must not be NULL. This function is only useful between
*visit_start_struct()andvisit_end_struct(), since only objects
* have optional keys.
*
* @present points to the address of the optional member'shas_flag. *
* Input visitors set *@present according to input; other visitors
* leave it unchanged. In either case, return *@present for
* convenience.
Quoted 1 line
I guess this is correct for the clone visitor.
Clone visitor leaves it alone (it is reading *@present on the dest, which was already set earlier during the g_memdup() of visit_start_*).
Quoted 11 lines
/*
* Visit an enum value.
*
* @name expresses the relationship of this enum to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors parse input and set *@obj to
* the enumeration value, leaving @obj unchanged on error; other
* visitors use *@obj but leave it unchanged.I guess this is correct for the clone visitor.
It's a bit of a stretch, but "use *@obj" can certainly mean "do nothing with it, because it is a scalar that was already set earlier during the g_memdup() of visit_start_*". So yes, the clone visitor wants visit_type_enum() to be a no-op.
Quoted 5 lines
/*
* Check if visitor is an input visitor.Does the clone visitor count as input visitor here? Should it?
No, and probably no. A clone visitor never sets errp, and therefore there is no reason to clean up after a failed clone; and our current use of visit_is_input() is only for cleaning up after failures in an input visitor.
Quoted 15 lines
*/bool
visit_is_input(Visitor *v);/*** Visiting built-in types ***/
/*
* Visit an integer value.
*
* @name expresses the relationship of this integer to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors set *@obj to the value;
* other visitors will leave *@obj unchanged.
Quoted 1 line
I guess this is correct for the clone visitor.
Again correct - the clone visitor doesn't set anything at this point, because the integer was already copied earlier during the g_memdup() of visit_start_*().
Quoted 11 lines
/*
* Visit a string value.
*
* @name expresses the relationship of this string to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors set *@obj to the value
* (never NULL). Other visitors leave *@obj unchanged, and commonly
* treat NULL like "".I guess this is correct for the clone visitor.
The clone visitor could morph NULL into "" (I didn't code it that way, though). Here, the clone visitor DOES set *@obj, in order to dedupe the pointer from the source object, so maybe a third sentence is needed?
Quoted 11 lines
/*
* Visit an arbitrary value.
*
* @name expresses the relationship of this value to its parent
* container; see the general description of @name above. *
* @obj must be non-NULL. Input visitors set *@obj to the value;
* other visitors will leave *@obj unchanged. *@obj must be non-NULL
* for output visitors.Fine, as the clone visitor doesn't support any.
It could, if we use the JSON output visitor code later in the series to create a QObject deep-cloner, but I'd rather not do it unless we find an actual need (keeping 'any' out of clone does simplify the number of corner cases to think about).
Quoted 31 lines
Quoted 1 line
+++
b/qapi/qapi-visit-core.cAs we'll see further down, @obj points into the clone, except at the root, where it points to qapi_clone()'s local variable @dst. A pointer-valued *@obj still points into the source.
Now let's go through the
v->typechecks real slow.qapi/qapi-visit-core.c void visit_start_struct(Visitor *v, const char *name, void **obj, · line 48
Quoted 7 lines
qapi/qapi-visit-core.c +1
-1@@ -44,10 +44,10 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,if (obj) {assert (size); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj); }For real walks (obj != NULL):
* Input visitors write *obj, and don't care for the old value.
* Output visitors read *obj, and a struct can't be null.
* The dealloc visitor reads *obj, but null is fine (partially constructed object).
* The clone visitor reads like an output visitor (except at the root) and writes like an input visitor.
Before the patch, we assert "if output visitor, then *obj isn't null".
Quoted 8 lines
After the patch, we do the same for the clone visitor. Correct, except at the root. There, @obj points to qapi_clone()'s @dst, which is uninitialized. I'm afraid this assertion fails if @dst happens to be null.
Can we fix this by removing the "except at the root" special case? Change
qapi_cloneto initialize dst = src, drop QapiCloneVisitor member @root andqapi_clone_visitor_new()parameter @src.
Cool idea! And it avoids the crash (I was indeed compiling without optimization, and getting lucky that the uninit value wasn't crashing my tests; wonder why valgrind wasn't flagging it).
Quoted 8 lines
[...]
boolvisit_is_input(Visitor *v){ return v->type == VISITOR_INPUT;}
This answers my question "Does the clone visitor count as input visitor here?" Remaining question: "Should it?"
I'm still not convinced, again on the grounds that this is used for cleanup after a failed visit, but clone visits don't fail.
Quoted 16 lines
qapi/qapi-visit-core.c void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp) · line 261
Quoted 13 lines
qapi/qapi-visit-core.c +2
-1@@ -252,9 +252,10 @@ void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)assert (obj);/* TODO: Fix callers to not pass NULL when they mean "", so that we * can enable: -assert(v->type != VISITOR_OUTPUT || *obj); +assert(!(v->type & VISITOR_OUTPUT) || *obj); */ v->type_str (v, name, obj, &err); +/* Likewise, use of NULL means we can't do (v->type & VISITOR_INPUT) */ if (v->type ==VISITOR_INPUT ) {assert (!err != !*obj); }If your head doesn't hurt by know, you either wrote this, or you're not reading closely :)
And there's my idea of making the clone visitor auto-magically clone NULL into "", at which point the conditions in the assertions would change.
Quoted 26 lines
If the TODOs were already addressed, we'd again get the same analysis as for
visit_start_struct(), except for the arguments about the root, which don't apply here, because the clone visitor doesn't accept scalar roots.In the current state, the analysis needs to be modified as follows.
First assertion:
Before the patch, we'd like to assert "if output or clone visitor, then *obj isn't null". We can't as long as we need to treat null as the empty string.
After the patch, the situation is the same for the clone visitor. Okay.
Second assertion:
Before the patch, we assert "input visitor must either fail or create *obj for a real walk." The TODO doesn't apply; we create "", not null.
After the patch, we'd like to assert the same for the clone visitor, but we can't: the clone of null is null. Okay.
Quoted 3 lines
(quoted diff) +0
-0@@ -273,9 +274,9 @@ void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp) Error *err = NULL;
Quoted 14 lines
qapi/qapi-visit-core.c void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp) · line 282
Quoted 9 lines
qapi/qapi-visit-core.c +2
-2assert (obj); -assert (v->type !=VISITOR_OUTPUT || *obj); +assert (!(v->type &VISITOR_OUTPUT ) || *obj); v->type_any (v, name, obj, &err); -if (v->type ==VISITOR_INPUT ) { +if (v->type &VISITOR_INPUT ) {assert (!err != !*obj); }error_propagate (errp, err);
v->type_any()will crash for the clone visitor, so these changes aren't. necessary. If you want them to future-proof the code, I need to convince myself the changes make sense, similar to what I did for the other ones in this file.
Okay, I can leave this hunk out for now.
Quoted 10 lines
qapi/qapi-visit-core.c void visit_type_enum(Visitor *v, const char *name, int *obj, · line 347
Quoted 7 lines
qapi/qapi-visit-core.c +1
-0@@ -342,4 +343,5 @@ void visit_type_enum(Visitor *v, const char *name, int *obj, }else if (v->type ==VISITOR_OUTPUT ) {output_type_enum (v, name, obj, strings, errp); } +/* dealloc and clone visitors have nothing to do */ }I'm upgrade my verdict from "subtle" to "scarily subtle" %-}
Any comments I can add to make it more obvious to the next reader?
Quoted 15 lines
qapi/qapi-clone-visitor.c line 35
Quoted 10 lines
qapi/qapi-clone-visitor.c new file +10
-0+ +static void qapi_clone_start_struct (Visitor *v,const char *name,void **obj, +size_t size,Error **errp) +{ +QapiCloneVisitor *qcv =to_qcv (v); + +if (!obj) { +/* Only possible when visiting an alternate's object +* branch. Nothing to do here, since the earlier +* visit_start_alternate() already copied memory. */ Should
visitor-impl.hexplain how methodstart_struct()is used with alternates? I once again forgot how this works... Hmm, you explained it to me during review of v3.
Quoted 3 lines
Despite there's "nothing to do here", you found something to do:
Quoted 1 line
+
assert(qcv->depth);
That's really only asserting that the clone itself is a real visit; we don't allow cloning on a virtual visit, even though the real visit of an alternate also involves the virtual visit of an object, if the alternate's object branch is selected.
Quoted 1 line
[Skipping the tests for now to get this review out today...]
Did you want to review the tests in any further detail?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v4 14/28 sockets: Use new QAPI cloning
In reply to Eric BlakeRather than rolling our own clone via an expensive conversion in and back out of QObject, use the new clone visitor.
---
v4: completely drop qapi_copy_SocketAddress(), rebase to earlier changes
v3: new patch
--- include/io/task.h | 2 +- include/qapi/clone-visitor.h | 2 ++ include/qemu/sockets.h | 4 ---- io/channel-socket.c | 9 +++++---- qemu-char.c | 5 ++--- util/qemu-sockets.c | 23 ----------------------- 6 files changed, 10 insertions(+), 35 deletions(-)
include/io/task.h +1 -1
@@ -159,7 +159,7 @@ typedef int (*QIOTaskWorker)(QIOTask *task,* QIOTask *task; * SocketAddress *addrCopy; * -* qapi_copy_SocketAddress(&addrCopy, addr); +* addrCopy = QAPI_CLONE(SocketAddress, addr); * task = qio_task_new(OBJECT(obj), func, opaque, notify); * * qio_task_run_in_thread(task, myobject_listen_worker,
include/qapi/clone-visitor.h +2 -0
@@ -11,7 +11,9 @@#ifndef QAPI_CLONE_VISITOR_H #define QAPI_CLONE_VISITOR_H +#include "qemu/typedefs.h" #include "qapi/visitor.h" +#include "qapi-visit.h" /* * The clone visitor is for direct use only by the QAPI_CLONE() macro;
include/qemu/sockets.h +0 -4
@@ -106,8 +106,4 @@ SocketAddress *socket_local_address(int fd, Error **errp);*/ SocketAddress *socket_remote_address (int fd,Error **errp); - -void qapi_copy_SocketAddress (SocketAddress **p_dest, -SocketAddress *src); -#endif /* QEMU_SOCKET_H */
io/channel-socket.c +5 -4
@@ -23,6 +23,7 @@#include "io/channel-socket.h" #include "io/channel-watch.h" #include "trace.h" +#include "qapi/clone-visitor.h" #define SOCKET_MAX_FDS 16
@@ -182,7 +183,7 @@ void qio_channel_socket_connect_async(QIOChannelSocket *ioc, OBJECT(ioc), callback, opaque, destroy);SocketAddress *addrCopy; -qapi_copy_SocketAddress (&addrCopy, addr); + addrCopy =QAPI_CLONE (SocketAddress, addr);/* socket_connect() does a non-blocking connect(), but it * still blocks in DNS lookups, so we must use a thread */
@@ -244,7 +245,7 @@ void qio_channel_socket_listen_async(QIOChannelSocket *ioc, OBJECT(ioc), callback, opaque, destroy);SocketAddress *addrCopy; -qapi_copy_SocketAddress (&addrCopy, addr); + addrCopy =QAPI_CLONE (SocketAddress, addr);/* socket_listen() blocks in DNS lookups, so we must use a thread */ trace_qio_channel_socket_listen_async (ioc, addr);
@@ -324,8 +325,8 @@ void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,struct QIOChannelSocketDGramWorkerData *data =g_new0 ( struct QIOChannelSocketDGramWorkerData, 1); -qapi_copy_SocketAddress (&data->localAddr, localAddr); -qapi_copy_SocketAddress (&data->remoteAddr, remoteAddr); + data->localAddr =QAPI_CLONE (SocketAddress, localAddr); + data->remoteAddr =QAPI_CLONE (SocketAddress, remoteAddr);trace_qio_channel_socket_dgram_async (ioc, localAddr, remoteAddr); qio_task_run_in_thread(task,
qemu-char.c +2 -3
@@ -32,8 +32,7 @@#include "sysemu/char.h" #include "hw/usb.h" #include "qmp-commands.h" -#include "qapi/qmp-input-visitor.h" -#include "qapi/qmp-output-visitor.h" +#include "qapi/clone-visitor.h" #include "qapi-visit.h" #include "qemu/base64.h" #include "io/channel-socket.h"
@@ -4399,7 +4398,7 @@ static CharDriverState *qmp_chardev_open_socket(const char *id, } } -qapi_copy_SocketAddress (&s->addr, sock->addr); + s->addr =QAPI_CLONE (SocketAddress, sock->addr); chr->opaque = s; chr->chr_write = tcp_chr_write;
util/qemu-sockets.c +0 -23
@@ -1124,26 +1124,3 @@ SocketAddress *socket_remote_address(int fd, Error **errp)return socket_sockaddr_to_address (&ss, sslen, errp); } - - -void qapi_copy_SocketAddress (SocketAddress **p_dest, -SocketAddress *src) -{ -Visitor *ov, *iv; -QObject *obj; - - *p_dest =NULL ; - - ov =qmp_output_visitor_new (&obj); -visit_type_SocketAddress (ov,NULL , &src, &error_abort); -visit_complete (ov, &obj); -visit_free (ov); -if (!obj) { -return ; - } - - iv =qmp_input_visitor_new (obj, true); -visit_type_SocketAddress (iv,NULL , p_dest, &error_abort); -visit_free (iv); -qobject_decref (obj); -}
Signature · 1 line
2.5.5
PATCH v4 15/28 replay: Use new QAPI cloning
In reply to Eric BlakeRather than rolling our own clone via an expensive conversion in and back out of QObject, use the new clone visitor.
---
v4: rebase to earlier changes
v3: new patch
--- replay/replay-input.c | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-)
replay/replay-input.c +3 -27
@@ -16,31 +16,7 @@#include "replay-internal.h" #include "qemu/notify.h" #include "ui/input.h" -#include "qapi/qmp-output-visitor.h" -#include "qapi/qmp-input-visitor.h" -#include "qapi-visit.h" - -static InputEvent *qapi_clone_InputEvent (InputEvent *src) -{ -Visitor *ov, *iv; -QObject *obj; -InputEvent *dst =NULL ; - - ov =qmp_output_visitor_new (&obj); -visit_type_InputEvent (ov,NULL , &src, &error_abort); -visit_complete (ov, &obj); -visit_free (ov); -if (!obj) { -return NULL ; - } - - iv =qmp_input_visitor_new (obj, true); -visit_type_InputEvent (iv,NULL , &dst, &error_abort); -visit_free (iv); -qobject_decref (obj); - -return dst; -} +#include "qapi/clone-visitor.h" void replay_save_input_event (InputEvent *evt) {
@@ -139,7 +115,7 @@ InputEvent *replay_read_input_event(void)break ; } -return qapi_clone_InputEvent (&evt); +return QAPI_CLONE (InputEvent, &evt); }void replay_input_event (QemuConsole *src,InputEvent *evt)
@@ -147,7 +123,7 @@ void replay_input_event(QemuConsole *src, InputEvent *evt)if (replay_mode ==REPLAY_MODE_PLAY ) {/* Nothing */ }else if (replay_mode ==REPLAY_MODE_RECORD ) { -replay_add_input_event (qapi_clone_InputEvent (evt)); +replay_add_input_event (QAPI_CLONE (InputEvent, evt)); }else {qemu_input_event_send_impl (src, evt); }
Signature · 1 line
2.5.5
PATCH v4 16/28 qapi: Factor out JSON string escaping
In reply to Eric BlakePull out a new qstring_append_json_string() helper, so that all JSON output producers can use the same output escaping rules.
While it appears that vmstate's use of the simpler qjson.c formatter is not currently encountering any string that needs escapes to be valid JSON, it is better to be safe than sorry, and this substitution is not adding any potential asserts during migration.
Adding a return value will let callers that care diagnose the situation where an attempt was made to output invalid JSON (we use substitute characters, and in fact guarantee that our output is ASCII via escapes even if the input was UTF-8). None of the current callers care, but a future patch wants to make it possible to turn this situation into an error.
---
v4: keep helper in qobject-json.[ch], tweak variable name and for loop iterator, add return value, drop R-b
v3: rebase to include cleanups in master
v2: no change
--- include/qapi/qmp/qobject-json.h | 2 + migration/qjson.c | 10 ++-- qobject/qobject-json.c | 125 ++++++++++++++++++++++------------------ 3 files changed, 75 insertions(+), 62 deletions(-)
include/qapi/qmp/qobject-json.h +2 -0
@@ -24,4 +24,6 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap) GCC_FMT_ATTR(1, 0);QString *qobject_to_json (const QObject *obj);QString *qobject_to_json_pretty (const QObject *obj); +int qstring_append_json_string (QString *qstring,const char *str); +#endif /* QJSON_H */
migration/qjson.c +4 -6
@@ -26,6 +26,7 @@#include "qemu/osdep.h" #include "qapi/qmp/qstring.h" #include "migration/qjson.h" +#include "qapi/qmp/qobject-json.h" struct QJSON {QString *str;
@@ -42,9 +43,8 @@ static void json_emit_element(QJSON *json, const char *name) }if (name) { -qstring_append (json->str,"\"" ); -qstring_append (json->str, name); -qstring_append (json->str,"\" : " ); +qstring_append_json_string (json->str, name); +qstring_append (json->str," : " ); } }
@@ -83,9 +83,7 @@ void json_prop_int(QJSON *json, const char *name, int64_t val)void json_prop_str (QJSON *json,const char *name,const char *str) {json_emit_element (json, name); -qstring_append_chr (json->str,'"' ); -qstring_append (json->str, str); -qstring_append_chr (json->str,'"' ); +qstring_append_json_string (json->str, str); }const char *qjson_get_str (QJSON *json)
qobject/qobject-json.c +69 -56
146 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 16/28] qapi: Factor out JSON string escaping
Quoted 17 lines · Eric Blake
Pull out a new
qstring_append_json_string()helper, so that all JSON output producers can use the same output escaping rules.While it appears that vmstate's use of the simpler
qjson.cformatter is not currently encountering any string that needs escapes to be valid JSON, it is better to be safe than sorry, and this substitution is not adding any potential asserts during migration.Adding a return value will let callers that care diagnose the situation where an attempt was made to output invalid JSON (we use substitute characters, and in fact guarantee that our output is ASCII via escapes even if the input was UTF-8). None of the current callers care, but a future patch wants to make it possible to turn this situation into an error.
Signed-off-by Eric Blake <[email protected]>
[...]
qobject/qobject-json.c QString *qobject_to_json_pretty(const QObject *obj) · line 248
Quoted 12 lines
qobject/qobject-json.c +8
-0@@ -290,3 +238,68 @@ QString *qobject_to_json_pretty(const QObject *obj)return str; } + +/** +* Append a JSON string to @qstring that encodes the C string @str. +* +* The JSON string is enclosed in double quotes and has funny +* characters escaped. Returns -1 if encoding errors were replaced, +* or 0 if the input was already valid UTF-8. +*/
Suggest:
* @str is in UTF-8. The JSON string is enclosed in double quotes
* and has funny characters escaped. Invalid UTF-8 sequences are
* replaced by (properly escaped) U+0xFFFD replacement characters.
* Return -1 if invalid sequences were replaced, else 0.
Quoted 57 lines
qobject/qobject-json.c +57
-0+int qstring_append_json_string (QString *qstring,const char *str) +{ +const char *ptr; +int cp; +char buf[16 ]; +char *end; +int result =0 ; + +qstring_append (qstring,"\"" ); + +for (ptr = str; *ptr; ptr = end) { + cp =mod_utf8_codepoint (ptr,6 , &end); +switch (cp) { +case '\"' : +qstring_append (qstring,"\\\"" ); +break ; +case '\\' : +qstring_append (qstring,"\\\\" ); +break ; +case '\b' : +qstring_append (qstring,"\\b" ); +break ; +case '\f' : +qstring_append (qstring,"\\f" ); +break ; +case '\n' : +qstring_append (qstring,"\\n" ); +break ; +case '\r' : +qstring_append (qstring,"\\r" ); +break ; +case '\t' : +qstring_append (qstring,"\\t" ); +break ; +default : +if (cp <0 ) { + cp =0xFFFD ;/* replacement character */ + result =-1 ; + } +if (cp >0xFFFF ) { +/* beyond BMP; need a surrogate pair */ +snprintf (buf,sizeof (buf),"\\u%04X\\u%04X" , +0xD800 + ((cp -0x10000 ) >>10 ), +0xDC00 + ((cp -0x10000 ) &0x3FF )); + }else if (cp <0x20 || cp >=0x7F ) { +snprintf (buf,sizeof (buf),"\\u%04X" , cp); + }else { + buf[0 ] = cp; + buf[1 ] =0 ; + } +qstring_append (qstring, buf); + } + }; + +qstring_append (qstring,"\"" ); +return result; +}
PATCH v4 17/28 qapi: Factor out JSON number formatting
In reply to Eric BlakePull out a new qstring_append_json_number() helper, so that all JSON output producers can use a consistent style for printing floating point without duplicating code (since we are doing more data massaging than a simple printf format can handle). (For now, there is only one client, but later patches will use it.)
Adding a return value will let callers that care diagnose the situation where an attempt was made to output invalid JSON (which does not specify Infinity or NaN). None of the current callers care, but a future patch wants to make it possible to turn this situation into an error.
---
v4: keep helper in qobject-json.[ch], don't use Error **errp for inf/NaN handling, drop R-b
v3: rebase to latest; even though the patch differs quite a bit from v2, the changes are due to prior comment changes that are just moving between files, so R-b kept
v2: minor wording tweaks
--- include/qapi/qmp/qobject-json.h | 1 + qobject/qobject-json.c | 63 +++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 24 deletions(-)
include/qapi/qmp/qobject-json.h +1 -0
@@ -25,5 +25,6 @@ QString *qobject_to_json(const QObject *obj);QString *qobject_to_json_pretty (const QObject *obj);int qstring_append_json_string (QString *qstring,const char *str); +int qstring_append_json_number (QString *qstring,double number);#endif /* QJSON_H */
qobject/qobject-json.c +39 -24
78 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting
Quoted 13 lines · Eric Blake
Pull out a new
qstring_append_json_number()helper, so that all JSON output producers can use a consistent style for printing floating point without duplicating code (since we are doing more data massaging than a simple printf format can handle). (For now, there is only one client, but later patches will use it.)Adding a return value will let callers that care diagnose the situation where an attempt was made to output invalid JSON (which does not specify Infinity or NaN). None of the current callers care, but a future patch wants to make it possible to turn this situation into an error.
Signed-off-by Eric Blake <[email protected]>
[...]
qobject/qobject-json.c int qstring_append_json_string(QString *qstring, const char *str) · line 291
Quoted 11 lines
qobject/qobject-json.c +7
-0@@ -303,3 +282,39 @@ int qstring_append_json_string(QString *qstring, const char *str)qstring_append (qstring,"\"" );return result; } + +/** +* Append a JSON representation of @number to @qstring. +* +* Returns -1 if the added text is not strict JSON, or 0 if the number +* was finite. +*/
Suggest:
* Return 0 if the number is finite, as required by RFC 7159, else -1.
The return value makes some sense only for symmetry with qstring_append_json_string(). Without that, I'd ask you to keep this function simple. Callers could just as easily test isfinite() themselves.
Quoted 29 lines
qobject/qobject-json.c +29
-0+int qstring_append_json_number (QString *qstring,double number) +{ +char buffer[1024 ]; +int len; + +/* FIXME: snprintf() is locale dependent; but JSON requires +* numbers to be formatted as if in the C locale. Dependence +* on C locale is a pervasive issue in QEMU. */ +/* FIXME: This risks printing Inf or NaN, which are not valid +* JSON values. */ +/* FIXME: the default precision of 6 for %f often causes +* rounding errors; we should be using DBL_DECIMAL_DIG (17), +* and only rounding to a shorter number if the result would +* still produce the same floating point value. */ + len =snprintf (buffer,sizeof (buffer),"%f" , number); +assert (len >0 && len <sizeof (buffer)); +while (len >0 && buffer[len -1 ] =='0' ) { + len--; + } + +if (len && buffer[len -1 ] =='.' ) { + buffer[len -1 ] =0 ; + }else { + buffer[len] =0 ; + } + +qstring_append (qstring, buffer); +return isfinite (number) ?0 :-1 ; +}
Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting
Quoted 15 lines · Markus Armbruster
Quoted 11 lines · Eric Blake
Pull out a new
qstring_append_json_number()helper, so that all JSON output producers can use a consistent style for printing floating point without duplicating code (since we are doing more data massaging than a simple printf format can handle). (For now, there is only one client, but later patches will use it.)Adding a return value will let callers that care diagnose the situation where an attempt was made to output invalid JSON (which does not specify Infinity or NaN). None of the current callers care, but a future patch wants to make it possible to turn this situation into an error.
Quoted 8 lines
Suggest:
* Return 0 if the number is finite, as required by RFC 7159, else -1.
The return value makes some sense only for symmetry with
qstring_append_json_string(). Without that, I'd ask you to keep this function simple. Callers could just as easily testisfinite()themselves.
I'm actually thinking of modifying this, given the recent thread pointing out that libvirt chokes hard on JSON extensions:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg04398.html
That is, for symmetry with qstring_append_json_string(), I'm thinking of changing NaN to 0 and Inf to DBL_MAX, and always outputting a finite value, in addition to returning -1 to inform the caller that a substitution was made, so that the output is always strict JSON.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting
Quoted 28 lines · Eric Blake
Quoted 15 lines · Markus Armbruster
Quoted 11 lines · Eric Blake
Pull out a new
qstring_append_json_number()helper, so that all JSON output producers can use a consistent style for printing floating point without duplicating code (since we are doing more data massaging than a simple printf format can handle). (For now, there is only one client, but later patches will use it.)Adding a return value will let callers that care diagnose the situation where an attempt was made to output invalid JSON (which does not specify Infinity or NaN). None of the current callers care, but a future patch wants to make it possible to turn this situation into an error.
Quoted 8 lines
Suggest:
* Return 0 if the number is finite, as required by RFC 7159, else -1.
The return value makes some sense only for symmetry with
qstring_append_json_string(). Without that, I'd ask you to keep this function simple. Callers could just as easily testisfinite()themselves.I'm actually thinking of modifying this, given the recent thread pointing out that libvirt chokes hard on JSON extensions:
Quoted 6 lines
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg04398.html
That is, for symmetry with
qstring_append_json_string(), I'm thinking of changing NaN to 0 and Inf toDBL_MAX, and always outputting a finite value, in addition to returning -1 to inform the caller that a substitution was made, so that the output is always strict JSON.
Mapping infinities to DBL_MIN and DBL_MAX is debatable, but mapping NaN to zero is outright wrong.
If we decide QMP should stick to JSON here and avoid non-finite numbers, we need to treat an attempt to emit a non-finite number as a bug:
assert(isfinite(...)). Making sure nothing ever attempts to emit such numbers will be tedious.
If we decide QMP should remain as it is, we need to document non-finite numbers among its JSON extensions. We should also fix our QMP parsers to accept non-finite numbers then. Including the one in libvirt. Attempts to emit non-finite numbers then *may* be bugs. Really no different than finite numbers outside their intended range, such as a negative size. Catching these bugs is of course also tedious. The difference is that they manifest in QMP as semantic instead of lexical errors. Lexical errors are the worst to handle gracefully.
Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting
Quoted 14 lines · Markus Armbruster
Quoted 14 lines
Quoted 8 lines
Suggest:
* Return 0 if the number is finite, as required by RFC 7159, else -1.
The return value makes some sense only for symmetry with
qstring_append_json_string(). Without that, I'd ask you to keep this function simple. Callers could just as easily testisfinite()themselves.I'm actually thinking of modifying this, given the recent thread pointing out that libvirt chokes hard on JSON extensions:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg04398.html
Quoted 7 lines
Quoted 4 lines
That is, for symmetry with
qstring_append_json_string(), I'm thinking of changing NaN to 0 and Inf toDBL_MAX, and always outputting a finite value, in addition to returning -1 to inform the caller that a substitution was made, so that the output is always strict JSON.Mapping infinities to
DBL_MINandDBL_MAXis debatable, but mapping NaN to zero is outright wrong.
How about this alternative:
Finite values remain numbers:
"number":1
But non-finite values are output as strings, so that our output is always valid JSON - the recipient may not be expecting a string in place of a number, but at least should be able to parse the output rather than choking hard.
"number":"nan"
The return value -1 then indicates that a stringized replacement was used, so that any later patch can use a strict flag on whether to allow the replacement output or assert.
Quoted 6 lines
If we decide QMP should stick to JSON here and avoid non-finite numbers, we need to treat an attempt to emit a non-finite number as a bug:
assert(isfinite(...)). Making sure nothing ever attempts to emit such numbers will be tedious.
Quoted 8 lines
If we decide QMP should remain as it is, we need to document non-finite numbers among its JSON extensions. We should also fix our QMP parsers to accept non-finite numbers then. Including the one in libvirt. Attempts to emit non-finite numbers then *may* be bugs. Really no different than finite numbers outside their intended range, such as a negative size. Catching these bugs is of course also tedious. The difference is that they manifest in QMP as semantic instead of lexical errors. Lexical errors are the worst to handle gracefully.
I may still try to tackle fixing the QMP parser to accept NaN and infinity on input (since it's hand-written, we at least have control over that) - it will certainly be easier than getting libvirt to parse non-finite numbers (libvirt uses libyajl, and my emails to the yajl mailing list have gone unanswered, making me think the project is not very vibrant and thus not very patchable). But with my proposal of producing a stringized non-finite value, we at least convert lexical into semantic errors, which I agree with your assessment is a nicer way of dealing with it.
Of course, a policy change of outputting stringized non-finite numbers should be separate from refactoring patches that just move functions around.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting
Quoted 35 lines · Eric Blake
Quoted 14 lines · Markus Armbruster
Quoted 14 lines
Quoted 8 lines
Suggest:
* Return 0 if the number is finite, as required by RFC 7159, else -1.
The return value makes some sense only for symmetry with
qstring_append_json_string(). Without that, I'd ask you to keep this function simple. Callers could just as easily testisfinite()themselves.I'm actually thinking of modifying this, given the recent thread pointing out that libvirt chokes hard on JSON extensions:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg04398.html
Quoted 7 lines
Quoted 4 lines
That is, for symmetry with
qstring_append_json_string(), I'm thinking of changing NaN to 0 and Inf toDBL_MAX, and always outputting a finite value, in addition to returning -1 to inform the caller that a substitution was made, so that the output is always strict JSON.Mapping infinities to
DBL_MINandDBL_MAXis debatable, but mapping NaN to zero is outright wrong.How about this alternative:
Finite values remain numbers:
"number":1But non-finite values are output as strings, so that our output is always valid JSON - the recipient may not be expecting a string in place of a number, but at least should be able to parse the output rather than choking hard.
"number":"nan"
Quoted 3 lines
The return value -1 then indicates that a stringized replacement was used, so that any later patch can use a strict flag on whether to allow the replacement output or assert.
I dislike this a lot less than mapping non-finite numbers to finite ones.
I still dislike it, because it defeats fitting a schema to QMP: instead of the true JSON type "number", we'd need the sum type of "number" and "string", which this really isn't: only a few special strings are valid, and they're not actually strings. If the schema language can do sum types, we'd even be stuck with their common super-type.
The most practical solution isn't always a likable one, though.
Quoted 14 lines
Quoted 5 lines
If we decide QMP should stick to JSON here and avoid non-finite numbers, we need to treat an attempt to emit a non-finite number as a bug:
assert(isfinite(...)). Making sure nothing ever attempts to emit such numbers will be tedious.Quoted 8 lines
If we decide QMP should remain as it is, we need to document non-finite numbers among its JSON extensions. We should also fix our QMP parsers to accept non-finite numbers then. Including the one in libvirt. Attempts to emit non-finite numbers then *may* be bugs. Really no different than finite numbers outside their intended range, such as a negative size. Catching these bugs is of course also tedious. The difference is that they manifest in QMP as semantic instead of lexical errors. Lexical errors are the worst to handle gracefully.
Quoted 3 lines
I may still try to tackle fixing the QMP parser to accept NaN and infinity on input (since it's hand-written, we at least have control over that)
Making json-lexer.c recognize infinities and NaNs in strtod() syntax shouldn't be hard. I'd omit nan(n-char-sequence-opt), because its semantics are implementation defined. I'd also omit all spellings other than inf and nan. That leaves inf, +inf, -inf, nan, +nan, -nan.
Quoted 4 lines
- it will certainly be easier than getting libvirt to parse
non-finite numbers (libvirt uses libyajl, and my emails to the yajl mailing list have gone unanswered, making me think the project is not very vibrant and thus not very patchable).
Nobody likes to carry downstream patches, but an unresponsive upstream may leave you no choice.
Quoted 7 lines
But with my proposal of
producing a stringized non-finite value, we at least convert lexical into semantic errors, which I agree with your assessment is a nicer way of dealing with it.Of course, a policy change of outputting stringized non-finite numbers should be separate from refactoring patches that just move functions around.
Yes.
Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting
Quoted 8 lines · Markus Armbruster
Quoted 3 lines
I may still try to tackle fixing the QMP parser to accept NaN and infinity on input (since it's hand-written, we at least have control over that)
Making
json-lexer.crecognize infinities and NaNs instrtod()syntax shouldn't be hard. I'd omit nan(n-char-sequence-opt), because its semantics are implementation defined. I'd also omit all spellings other than inf and nan. That leaves inf, +inf, -inf, nan, +nan, -nan.
+inf and +nan weren't worth it (JSON doesn't accept +0 either), but '-inf' and '-nan' were easy. In fact, '-infinity' was easy too - see the posted patches:
https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg02652.html
and you are right that it was not worth 'nan(n-char-seq)'.
Quoted 8 lines
Quoted 5 lines
- it will certainly be easier than getting libvirt to parse
non-finite numbers (libvirt uses libyajl, and my emails to the yajl mailing list have gone unanswered, making me think the project is not very vibrant and thus not very patchable).Nobody likes to carry downstream patches, but an unresponsive upstream may leave you no choice.
Another (possibly-ugly) option that I thought of over the weekend: We have 'qmp_capabilities' for handshakes between client and server. The server could advertise a new capability 'nonfinite' in its initial greeting, and if the client replies with the same capability, then the server knows that the client is prepared to accept bareword non-finite numbers. A client that doesn't see the server advertise anything has no guarantees, and a server that knows the capability but doesn't see the client request the capability should avoid sending bareword non-finite.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting
Quoted 26 lines · Eric Blake
Quoted 8 lines · Markus Armbruster
Quoted 3 lines
I may still try to tackle fixing the QMP parser to accept NaN and infinity on input (since it's hand-written, we at least have control over that)
Making
json-lexer.crecognize infinities and NaNs instrtod()syntax shouldn't be hard. I'd omit nan(n-char-sequence-opt), because its semantics are implementation defined. I'd also omit all spellings other than inf and nan. That leaves inf, +inf, -inf, nan, +nan, -nan.+inf and +nan weren't worth it (JSON doesn't accept +0 either), but '-inf' and '-nan' were easy. In fact, '-infinity' was easy too - see the posted patches:
https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg02652.html
and you are right that it was not worth 'nan(n-char-seq)'.Quoted 8 lines
Quoted 5 lines
- it will certainly be easier than getting libvirt to parse
non-finite numbers (libvirt uses libyajl, and my emails to the yajl mailing list have gone unanswered, making me think the project is not very vibrant and thus not very patchable).Nobody likes to carry downstream patches, but an unresponsive upstream may leave you no choice.
Quoted 8 lines
Another (possibly-ugly) option that I thought of over the weekend: We have '
qmp_capabilities' for handshakes between client and server. The server could advertise a new capability 'nonfinite' in its initial greeting, and if the client replies with the same capability, then the server knows that the client is prepared to accept bareword non-finite numbers. A client that doesn't see the server advertise anything has no guarantees, and a server that knows the capability but doesn't see the client request the capability should avoid sending bareword non-finite.
The capability negotiation mechanism exists to support things like that. However, the problem what to do when the command reply contains non-finite numbers remains. A capability can at best hide whatever ugly solution we have for that, and perhaps help us get rid of it in an orderly fashion eventually, by refusing to talk to clients that don't accept the capability.
Moreover, we're already sending non-finite numbers! Changing QMP to send something else unless the client opts into non-finite numbers via capability "nonfinite" is an incompatible change. We could weasel out of that by declaring our sending of them a bug, then turn around and declare it a feature when capability "nonfinite" is accepted. Meh.
PATCH v4 18/28 qapi: Add qstring_append_printf()
In reply to Eric BlakeBack in commit 764c1ca (Nov 2009), we added qstring_append_int(). However, it did not see any use until commit 190c882 (Jan 2015). Furthermore, it has a rather limited use case - to print anything else, callers still have to format into a temporary buffer, unless we want to introduce an explosion of new qstring_append_* methods for each useful type to print.
A much better approach is to add a wrapper that merges printf behavior onto qstring_append, via the new qstring_append_printf() (and its vararg counterpart), with a name based on glib's g_string_append_printf(). In fact, with our wrapper in place, we no longer need qstring_append_int().
Other immediate uses for the new function include simplifying two existing clients of qstring_append() on a just-formatted buffer, and the fact that we can take advantage of printf width manipulations for more efficient indentation.
---
v4: retitle, s/_format/_printf/, drop R-b, move up in series
v3: rebase to master
v2: also simplify qstring_append_json_string(), add assertion that format is well-formed
--- include/qapi/qmp/qstring.h | 7 +++++-- migration/qjson.c | 2 +- qobject/qobject-json.c | 38 ++++++++++---------------------------- qobject/qstring.c | 26 +++++++++++++++++++++----- 4 files changed, 37 insertions(+), 36 deletions(-)
include/qapi/qmp/qstring.h +5 -2
@@ -1,7 +1,7 @@/* * QString Module * -* Copyright (C) 2009 Red Hat Inc. +* Copyright (C) 2009-2016 Red Hat Inc. * * Authors: * Luiz Capitulino <[email protected]>
@@ -27,9 +27,12 @@ QString *qstring_from_str(const char *str);QString *qstring_from_substr (const char *str,int start,int end);size_t qstring_get_length (const QString *qstring);const char *qstring_get_str (const QString *qstring); -void qstring_append_int (QString *qstring,int64_t value);void qstring_append (QString *qstring,const char *str);void qstring_append_chr (QString *qstring,int c); +void qstring_append_printf(QString *qstring, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +void qstring_append_vprintf(QString *qstring, const char *fmt, va_list ap) + GCC_FMT_ATTR(2, 0);QString *qobject_to_qstring (const QObject *obj);void qstring_destroy_obj (QObject *obj);
migration/qjson.c +1 -1
@@ -77,7 +77,7 @@ void json_end_array(QJSON *json)void json_prop_int (QJSON *json,const char *name,int64_t val) {json_emit_element (json, name); -qstring_append_int (json->str, val); +qstring_append_printf (json->str,"%" PRId64, val); }void json_prop_str (QJSON *json,const char *name,const char *str)
qobject/qobject-json.c +10 -28
@@ -82,16 +82,13 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent);static void to_json_dict_iter (const char *key,QObject *obj,void *opaque) {ToJsonIterState *s = opaque; -int j;if (s->count) {qstring_append (s->str, s->pretty ?"," :", " ); }if (s->pretty) { -qstring_append (s->str,"\n" ); -for (j =0 ; j < s->indent ; j++) -qstring_append (s->str," " ); +qstring_append_printf (s->str,"\n%*s" ,4 * s->indent,"" ); }qstring_append_json_string (s->str, key);
@@ -104,16 +101,13 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)static void to_json_list_iter (QObject *obj,void *opaque) {ToJsonIterState *s = opaque; -int j;if (s->count) {qstring_append (s->str, s->pretty ?"," :", " ); }if (s->pretty) { -qstring_append (s->str,"\n" ); -for (j =0 ; j < s->indent ; j++) -qstring_append (s->str," " ); +qstring_append_printf (s->str,"\n%*s" ,4 * s->indent,"" ); }to_json (obj, s->str, s->pretty, s->indent);
@@ -128,10 +122,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)break ;case QTYPE_QINT : {QInt *val =qobject_to_qint (obj); -char buffer[1024 ]; - -snprintf (buffer,sizeof (buffer),"%" PRId64,qint_get_int (val)); -qstring_append (str, buffer); +qstring_append_printf (str,"%" PRId64,qint_get_int (val));break ; }case QTYPE_QSTRING : {
@@ -152,10 +143,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)qstring_append (str,"{" );qdict_iter (val, to_json_dict_iter, &s);if (pretty) { -int j; -qstring_append (str,"\n" ); -for (j =0 ; j < indent ; j++) -qstring_append (str," " ); +qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); }qstring_append (str,"}" );break ;
@@ -171,10 +159,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)qstring_append (str,"[" );qlist_iter (val, (void *)to_json_list_iter, &s);if (pretty) { -int j; -qstring_append (str,"\n" ); -for (j =0 ; j < indent ; j++) -qstring_append (str," " ); +qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); }qstring_append (str,"]" );break ;
@@ -229,7 +214,6 @@ int qstring_append_json_string(QString *qstring, const char *str) {const char *ptr;int cp; -char buf[16 ];char *end;int result =0 ;
@@ -266,16 +250,14 @@ int qstring_append_json_string(QString *qstring, const char *str) }if (cp >0xFFFF ) {/* beyond BMP; need a surrogate pair */ -snprintf (buf,sizeof (buf),"\\u%04X\\u%04X" , -0xD800 + ((cp -0x10000 ) >>10 ), -0xDC00 + ((cp -0x10000 ) &0x3FF )); +qstring_append_printf (qstring,"\\u%04X\\u%04X" , +0xD800 + ((cp -0x10000 ) >>10 ), +0xDc00 + ((cp -0x10000 ) &0x3FF )); }else if (cp <0x20 || cp >=0x7F ) { -snprintf (buf,sizeof (buf),"\\u%04X" , cp); +qstring_append_printf (qstring,"\\u%04X" , cp); }else { - buf[0 ] = cp; - buf[1 ] =0 ; +qstring_append_chr (qstring, cp); } -qstring_append (qstring, buf); } };
qobject/qstring.c +21 -5
@@ -1,7 +1,7 @@/* * QString Module * -* Copyright (C) 2009 Red Hat Inc. +* Copyright (C) 2009-2016 Red Hat Inc. * * Authors: * Luiz Capitulino <[email protected]>
@@ -88,12 +88,28 @@ void qstring_append(QString *qstring, const char *str) qstring->string[qstring->length] =0 ; } -void qstring_append_int (QString *qstring,int64_t value) +void qstring_append_printf (QString *qstring,const char *fmt, ...) { -char num[32 ]; +va_list ap; -snprintf (num,sizeof (num),"%" PRId64, value); -qstring_append (qstring, num); +va_start (ap, fmt); +qstring_append_vprintf (qstring, fmt, ap); +va_end (ap); +} + +void qstring_append_vprintf (QString *qstring,const char *fmt,va_list ap) +{ +va_list ap2; +int len; + +va_copy (ap2, ap); + len =vsnprintf (NULL ,0 , fmt, ap2); +assert (len >=0 ); +va_end (ap2); + +capacity_increase (qstring, len); +vsnprintf (qstring->string + qstring->length, len +1 , fmt, ap); + qstring->length += len; }/**
Signature · 1 line
2.5.5
PATCH v4 19/28 qapi: Use qstring_append_chr() where appropriate
In reply to Eric Blakeqstring_append_chr() is more efficient than qstring_append() when dealing with a one-byte string (including the case of a temporary 2-byte buffer just for creating a dynamic one-byte string).
---
v4: also convert qstring_append() of one-byte strings
v3: no change
v2: no change
--- qobject/json-parser.c | 25 ++++++++++--------------- qobject/qobject-json.c | 12 ++++++------ 2 files changed, 16 insertions(+), 21 deletions(-)
qobject/json-parser.c +10 -15
@@ -143,39 +143,39 @@ static QString *qstring_from_escaped_str(JSONParserContext *ctxt,switch (*ptr) {case '"' : -qstring_append (str,"\"" ); +qstring_append_chr (str,'"' ); ptr++;break ;case '\'' : -qstring_append (str,"'" ); +qstring_append_chr (str,'\'' ); ptr++;break ;case '\\' : -qstring_append (str,"\\" ); +qstring_append_chr (str,'\\' ); ptr++;break ;case '/' : -qstring_append (str,"/" ); +qstring_append_chr (str,'/' ); ptr++;break ;case 'b' : -qstring_append (str,"\b" ); +qstring_append_chr (str,'\b' ); ptr++;break ;case 'f' : -qstring_append (str,"\f" ); +qstring_append_chr (str,'\f' ); ptr++;break ;case 'n' : -qstring_append (str,"\n" ); +qstring_append_chr (str,'\n' ); ptr++;break ;case 'r' : -qstring_append (str,"\r" ); +qstring_append_chr (str,'\r' ); ptr++;break ;case 't' : -qstring_append (str,"\t" ); +qstring_append_chr (str,'\t' ); ptr++;break ;case 'u' : {
@@ -204,12 +204,7 @@ static QString *qstring_from_escaped_str(JSONParserContext *ctxt, goto out; } }else { -char dummy[2 ]; - - dummy[0 ] = *ptr++; - dummy[1 ] =0 ; - -qstring_append (str, dummy); +qstring_append_chr (str, *ptr++); } }
qobject/qobject-json.c +6 -6
@@ -140,12 +140,12 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) s.str = str; s.indent = indent +1 ; s.pretty = pretty; -qstring_append (str,"{" ); +qstring_append_chr (str,'{' );qdict_iter (val, to_json_dict_iter, &s);if (pretty) {qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); } -qstring_append (str,"}" ); +qstring_append_chr (str,'}' );break ; }case QTYPE_QLIST : {
@@ -156,12 +156,12 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) s.str = str; s.indent = indent +1 ; s.pretty = pretty; -qstring_append (str,"[" ); +qstring_append_chr (str,'[' );qlist_iter (val, (void *)to_json_list_iter, &s);if (pretty) {qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); } -qstring_append (str,"]" ); +qstring_append_chr (str,']' );break ; }case QTYPE_QFLOAT : {
@@ -217,7 +217,7 @@ int qstring_append_json_string(QString *qstring, const char *str)char *end;int result =0 ; -qstring_append (qstring,"\"" ); +qstring_append_chr (qstring,'"' );for (ptr = str; *ptr; ptr = end) { cp =mod_utf8_codepoint (ptr,6 , &end);
@@ -261,7 +261,7 @@ int qstring_append_json_string(QString *qstring, const char *str) } }; -qstring_append (qstring,"\"" ); +qstring_append_chr (qstring,'"' );return result; }
Signature · 1 line
2.5.5
PATCH v4 20/28 qstring: Add qstring_consume_str()
In reply to Eric BlakeSimilar to g_string_free(), there are cases where we want to destroy our reference to a QString while grabbing its contents, where we want to avoid use-after-free but also avoid a needless strdup(). But unlike g_string_free(), we are at least sensible enough to add this feature via a different function name, instead of trying to overload two completely separate concepts into a single g_string_free(). Do this by introducing qstring_consume_str(), then use it where it makes sense.
In the case of monitor.c, note that QString _always_ has a non-NULL embedded string with at least one byte allocated for a terminating NUL, so special-casing on length 0 was wasted code.
---
v4: new patch, suggested by Markus
--- include/qapi/qmp/qstring.h | 1 + monitor.c | 6 +----- qobject/qstring.c | 23 +++++++++++++++++++++++ qom/object.c | 3 +-- tests/check-qstring.c | 31 +++++++++++++++++++++++++++---- 5 files changed, 53 insertions(+), 11 deletions(-)
include/qapi/qmp/qstring.h +1 -0
@@ -27,6 +27,7 @@ QString *qstring_from_str(const char *str);QString *qstring_from_substr (const char *str,int start,int end);size_t qstring_get_length (const QString *qstring);const char *qstring_get_str (const QString *qstring); +char *qstring_consume_str (QString *qstring);void qstring_append (QString *qstring,const char *str);void qstring_append_chr (QString *qstring,int c);void qstring_append_printf (QString *qstring,const char *fmt, ...)
monitor.c +1 -5
@@ -661,11 +661,7 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, cur_mon = old_mon;qemu_mutex_lock (&hmp.out_lock); -if (qstring_get_length (hmp.outbuf) >0 ) { - output =g_strdup (qstring_get_str (hmp.outbuf)); - }else { - output =g_strdup ("" ); - } + output =qstring_consume_str (hmp.outbuf);qemu_mutex_unlock (&hmp.out_lock); out:
qobject/qstring.c +23 -0
@@ -145,6 +145,29 @@ const char *qstring_get_str(const QString *qstring) }/** +* qstring_consume_str(): Destructively convert a QString to string +* +* The reference count of @qstring is decremented by one, and the +* caller is responsible for calling g_free() on the result. In the +* common case where @qstring was not shared, this is faster than +* using strdup() on the result of qstring_get_str(); otherwise, the +* result is a copy and remaining users of @qstring are unaffected. +*/ +char *qstring_consume_str (QString *qstring) +{ +char *result; + +if (qstring->base.refcnt ==1 ) { + result = qstring->string; + qstring->string =NULL ; + }else { + result =g_strdup (qstring->string); + } +qobject_decref (&qstring->base); +return result; +} + +/** * qstring_destroy_obj(): Free all memory allocated by a QString * object */
qom/object.c +1 -2
@@ -1106,10 +1106,9 @@ char *object_property_get_str(Object *obj, const char *name,error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name,"string" ); retval =NULL ; }else { - retval =g_strdup (qstring_get_str (qstring)); + retval =qstring_consume_str (qstring); } -QDECREF (qstring);return retval; }
tests/check-qstring.c +27 -4
@@ -1,7 +1,7 @@/* * QString unit-tests. * -* Copyright (C) 2009 Red Hat Inc. +* Copyright (C) 2009-2016 Red Hat Inc. * * Authors: * Luiz Capitulino <[email protected]>
@@ -32,9 +32,7 @@ static void qstring_from_str_test(void)g_assert (strcmp (str, qstring->string) ==0 );g_assert (qobject_type (QOBJECT (qstring)) ==QTYPE_QSTRING ); -// destroy doesn't exit yet -g_free (qstring->string); -g_free (qstring); +QDECREF (qstring); }static void qstring_destroy_test (void )
@@ -56,6 +54,30 @@ static void qstring_get_str_test(void)QDECREF (qstring); } +static void qstring_consume_str_test (void ) +{ +QString *qstring; +char *ret_str; +const char *str ="QEMU/KVM" ; +char *ptr; + + qstring =qstring_from_str (str); +QINCREF (qstring); + g_assert_cmpint(qstring->base.refcnt, ==, 2); + ptr = qstring->string; + + ret_str =qstring_consume_str (qstring); + g_assert_cmpint(qstring->base.refcnt, ==, 1); +g_assert (ret_str != ptr); + g_assert_cmpstr(ret_str, ==, str); +g_free (ret_str); + + ret_str =qstring_consume_str (qstring); +g_assert (ret_str == ptr); + g_assert_cmpstr(ret_str, ==, str); +g_free (ret_str); +} +static void qstring_append_chr_test (void ) {int i;
@@ -103,6 +125,7 @@ int main(int argc, char **argv)g_test_add_func ("/public/append_chr" , qstring_append_chr_test);g_test_add_func ("/public/from_substr" , qstring_from_substr_test);g_test_add_func ("/public/to_qstring" , qobject_to_qstring_test); +g_test_add_func ("/public/consume_str" , qstring_consume_str_test);return g_test_run (); }
Signature · 1 line
2.5.5
PATCH v4 21/28 qstring: Add qstring_wrap_str()
In reply to Eric BlakeSeveral spots in the code malloc a string, then wrap it in a QString, which has to duplicate the input. Adding a new constructor with transfer semantics makes this pattern more efficient, comparable to the just-added transfer semantics to go from QString back to raw string. Use the new qstring_wrap_str() where it makes sense.
The new test still passes under valgrind.
---
v4: new patch
--- include/qapi/qmp/qstring.h | 1 + block.c | 3 ++- block/archipelago.c | 6 ++---- blockdev.c | 3 +-- qobject/qstring.c | 25 +++++++++++++++++++++++++ tests/check-qstring.c | 24 ++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 7 deletions(-)
include/qapi/qmp/qstring.h +1 -0
@@ -25,6 +25,7 @@ typedef struct QString {QString *qstring_new (void );QString *qstring_from_str (const char *str);QString *qstring_from_substr (const char *str,int start,int end); +QString *qstring_wrap_str (char *str);size_t qstring_get_length (const QString *qstring);const char *qstring_get_str (const QString *qstring);char *qstring_consume_str (QString *qstring);
block.c +2 -1
@@ -1448,7 +1448,8 @@ static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags,qdict_put (snapshot_options,"file.driver" ,qstring_from_str ("file" ));qdict_put (snapshot_options,"file.filename" , -qstring_from_str (tmp_filename)); +qstring_wrap_str (tmp_filename)); + tmp_filename =NULL ;qdict_put (snapshot_options,"driver" ,qstring_from_str ("qcow2" ));
block/archipelago.c +2 -4
@@ -426,13 +426,11 @@ static void archipelago_parse_filename(const char *filename, QDict *options,parse_filename_opts (filename, errp, &volume, &segment_name, &mport, &vport);if (volume) { -qdict_put (options,ARCHIPELAGO_OPT_VOLUME ,qstring_from_str (volume)); -g_free (volume); +qdict_put (options,ARCHIPELAGO_OPT_VOLUME ,qstring_wrap_str (volume)); }if (segment_name) {qdict_put (options,ARCHIPELAGO_OPT_SEGMENT , -qstring_from_str (segment_name)); -g_free (segment_name); +qstring_wrap_str (segment_name)); }if (mport != NoPort) {qdict_put (options,ARCHIPELAGO_OPT_MPORT ,qint_from_int (mport));
blockdev.c +1 -2
@@ -1089,8 +1089,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) new_id =g_strdup_printf ("%s%s%i" , if_name[type], mediastr, unit_id); } -qdict_put (bs_opts,"id" ,qstring_from_str (new_id)); -g_free (new_id); +qdict_put (bs_opts,"id" ,qstring_wrap_str (new_id)); }/* Add virtio block device */
qobject/qstring.c +25 -0
@@ -66,6 +66,31 @@ QString *qstring_from_str(const char *str)return qstring_from_substr (str,0 ,strlen (str) -1 ); } +/** +* qstring_wrap_str(): Create a new QString around a malloc'd C string +* +* Returns a strong reference, and caller must not use @str any more. +* @str may be NULL, in which case the QString will be "". +*/ +QString *qstring_wrap_str (char *str) +{ +QString *qstring; + + qstring =g_malloc (sizeof (*qstring)); +qobject_init (QOBJECT (qstring),QTYPE_QSTRING ); + +if (str) { + qstring->string = str; + qstring->length =strlen (str); + }else { + qstring->string =g_strdup ("" ); + qstring->length =0 ; + } + qstring->capacity = qstring->length; + +return qstring; +} +static void capacity_increase (QString *qstring,size_t len) {if (qstring->capacity < (qstring->length + len)) {
tests/check-qstring.c +24 -0
@@ -35,6 +35,29 @@ static void qstring_from_str_test(void)QDECREF (qstring); } +static void qstring_wrap_str_test (void ) +{ +QString *qstring; +char *str =g_strdup ("QEMU" ); + + qstring =qstring_wrap_str (str); +g_assert (qstring !=NULL ); +g_assert (qstring->base.refcnt ==1 ); +g_assert (strcmp ("QEMU" , qstring->string) ==0 ); +g_assert (qobject_type (QOBJECT (qstring)) ==QTYPE_QSTRING ); + +QDECREF (qstring); + + str =NULL ; + qstring =qstring_wrap_str (str); +g_assert (qstring !=NULL ); +g_assert (qstring->base.refcnt ==1 ); +g_assert (strcmp ("" , qstring->string) ==0 ); +g_assert (qobject_type (QOBJECT (qstring)) ==QTYPE_QSTRING ); + +QDECREF (qstring); +} +static void qstring_destroy_test (void ) {QString *qstring =qstring_from_str ("destroy test" );
@@ -120,6 +143,7 @@ int main(int argc, char **argv)g_test_init (&argc, &argv,NULL );g_test_add_func ("/public/from_str" , qstring_from_str_test); +g_test_add_func ("/public/wrap_str" , qstring_wrap_str_test);g_test_add_func ("/public/destroy" , qstring_destroy_test);g_test_add_func ("/public/get_str" , qstring_get_str_test);g_test_add_func ("/public/append_chr" , qstring_append_chr_test);
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 21/28] qstring: Add qstring_wrap_str()
Quoted 39 lines · Eric Blake
Several spots in the code malloc a string, then wrap it in a QString, which has to duplicate the input. Adding a new constructor with transfer semantics makes this pattern more efficient, comparable to the just-added transfer semantics to go from QString back to raw string. Use the new
qstring_wrap_str()where it makes sense.The new test still passes under valgrind.
Signed-off-by Eric Blake <[email protected]>---
v4: new patch--- include/qapi/qmp/qstring.h | 1 + block.c | 3 ++- block/archipelago.c | 6 ++---- blockdev.c | 3 +-- qobject/qstring.c | 25 +++++++++++++++++++++++++ tests/check-qstring.c | 24 ++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 7 deletions(-)include/qapi/qmp/qstring.h +1
-0@@ -25,6 +25,7 @@ typedef struct QString {QString *qstring_new (void );QString *qstring_from_str (const char *str);QString *qstring_from_substr (const char *str,int start,int end); +QString *qstring_wrap_str (char *str);size_t qstring_get_length (const QString *qstring);const char *qstring_get_str (const QString *qstring);char *qstring_consume_str (QString *qstring);block.c +0
-0
block.c static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, · line 1455
Quoted 9 lines
block.c +2
-1@@ -1448,7 +1448,8 @@ static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags,qdict_put (snapshot_options,"file.driver" ,qstring_from_str ("file" ));qdict_put (snapshot_options,"file.filename" , -qstring_from_str (tmp_filename)); +qstring_wrap_str (tmp_filename)); + tmp_filename =NULL ;qdict_put (snapshot_options,"driver" ,qstring_from_str ("qcow2" ));
The g_free(tmp_filename) stays, because there are error paths bypassing this spot. Good.
Quoted 39 lines
block/archipelago.c +2
-4@@ -426,13 +426,11 @@ static void archipelago_parse_filename(const char *filename, QDict *options,parse_filename_opts (filename, errp, &volume, &segment_name, &mport, &vport);if (volume) { -qdict_put (options,ARCHIPELAGO_OPT_VOLUME ,qstring_from_str (volume)); -g_free (volume); +qdict_put (options,ARCHIPELAGO_OPT_VOLUME ,qstring_wrap_str (volume)); }if (segment_name) {qdict_put (options,ARCHIPELAGO_OPT_SEGMENT , -qstring_from_str (segment_name)); -g_free (segment_name); +qstring_wrap_str (segment_name)); }if (mport != NoPort) {qdict_put (options,ARCHIPELAGO_OPT_MPORT ,qint_from_int (mport));blockdev.c +1
-2@@ -1089,8 +1089,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) new_id =g_strdup_printf ("%s%s%i" , if_name[type], mediastr, unit_id); } -qdict_put (bs_opts,"id" ,qstring_from_str (new_id)); -g_free (new_id); +qdict_put (bs_opts,"id" ,qstring_wrap_str (new_id)); }/* Add virtio block device */ qobject/qstring.c +0
-0
qobject/qstring.c QString *qstring_from_str(const char *str) · line 73
Quoted 8 lines
qobject/qstring.c +5
-0@@ -66,6 +66,31 @@ QString *qstring_from_str(const char *str)return qstring_from_substr (str,0 ,strlen (str) -1 ); } +/** +* qstring_wrap_str(): Create a new QString around a malloc'd C string +* +* Returns a strong reference, and caller must not use @str any more. +* @str may be NULL, in which case the QString will be "".
I'm not fond of conflating null pointers and "" (see also the trouble with visit_type_str()). For what it's worth, qstring_from_str(NULL) crashes. Can we reject null?
Quoted 65 lines
qobject/qstring.c +20
-0+*/ +QString *qstring_wrap_str (char *str) +{ +QString *qstring; + + qstring =g_malloc (sizeof (*qstring)); +qobject_init (QOBJECT (qstring),QTYPE_QSTRING ); + +if (str) { + qstring->string = str; + qstring->length =strlen (str); + }else { + qstring->string =g_strdup ("" ); + qstring->length =0 ; + } + qstring->capacity = qstring->length; + +return qstring; +} +static void capacity_increase (QString *qstring,size_t len) {if (qstring->capacity < (qstring->length + len)) {tests/check-qstring.c +24
-0@@ -35,6 +35,29 @@ static void qstring_from_str_test(void)QDECREF (qstring); } +static void qstring_wrap_str_test (void ) +{ +QString *qstring; +char *str =g_strdup ("QEMU" ); + + qstring =qstring_wrap_str (str); +g_assert (qstring !=NULL ); +g_assert (qstring->base.refcnt ==1 ); +g_assert (strcmp ("QEMU" , qstring->string) ==0 ); +g_assert (qobject_type (QOBJECT (qstring)) ==QTYPE_QSTRING ); + +QDECREF (qstring); + + str =NULL ; + qstring =qstring_wrap_str (str); +g_assert (qstring !=NULL ); +g_assert (qstring->base.refcnt ==1 ); +g_assert (strcmp ("" , qstring->string) ==0 ); +g_assert (qobject_type (QOBJECT (qstring)) ==QTYPE_QSTRING ); + +QDECREF (qstring); +} +static void qstring_destroy_test (void ) {QString *qstring =qstring_from_str ("destroy test" );@@ -120,6 +143,7 @@ int main(int argc, char **argv)g_test_init (&argc, &argv,NULL );g_test_add_func ("/public/from_str" , qstring_from_str_test); +g_test_add_func ("/public/wrap_str" , qstring_wrap_str_test);g_test_add_func ("/public/destroy" , qstring_destroy_test);g_test_add_func ("/public/get_str" , qstring_get_str_test);g_test_add_func ("/public/append_chr" , qstring_append_chr_test);
Re: [Qemu-devel] [PATCH v4 21/28] qstring: Add qstring_wrap_str()
Quoted 19 lines · Markus Armbruster
Quoted 17 lines · Eric Blake
Several spots in the code malloc a string, then wrap it in a QString, which has to duplicate the input. Adding a new constructor with transfer semantics makes this pattern more efficient, comparable to the just-added transfer semantics to go from QString back to raw string. Use the new
qstring_wrap_str()where it makes sense.The new test still passes under valgrind.
Signed-off-by Eric Blake <[email protected]>+++
b/qobject/qstring.cqobject/qstring.c +0
-0@@ -66,6 +66,31 @@ QString *qstring_from_str(const char *str)return qstring_from_substr (str,0 ,strlen (str) -1 ); }
Quoted 9 lines
qobject/qstring.c QString *qstring_from_str(const char *str) · line 73
Quoted 5 lines
qobject/qstring.c +5
-0+/** +* qstring_wrap_str(): Create a new QString around a malloc'd C string +* +* Returns a strong reference, and caller must not use @str any more. +* @str may be NULL, in which case the QString will be "". I'm not fond of conflating null pointers and "" (see also the trouble with
visit_type_str()). For what it's worth,qstring_from_str(NULL)crashes. Can we reject null?
Probably. I'll add an assert, and update the interface to match if things still pass with my usage.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v4 22/28 qobject: Consolidate qobject_to_json() calls
In reply to Eric BlakeIt's simpler to have a single conversion function that takes a bool parameter, rather than two functions where the choice of function determines an internal bool. Similar to commit fc471c18.
While at it, the conversion currently cannot fail (maybe it SHOULD be possible to choose to fail, when encountering invalid UTF-8 encoding or an Infinity or NaN valued double, but that's a story for another day), so clean up callers to avoid a needless assert. And use bool rather than int for 'pretty'.
---
v4: new patch
--- include/qapi/qmp/qobject-json.h | 4 ++-- block.c | 2 +- monitor.c | 4 +--- qemu-img.c | 9 +++------ qga/main.c | 5 +---- qobject/qobject-json.c | 19 +++++-------------- tests/check-qobject-json.c | 27 +++++++++++---------------- tests/libqtest.c | 2 +- tests/test-visitor-serialization.c | 2 +- 9 files changed, 26 insertions(+), 48 deletions(-)
include/qapi/qmp/qobject-json.h +2 -2
@@ -21,8 +21,8 @@ QObject *qobject_from_json(const char *string);QObject *qobject_from_jsonf (const char *string, ...)GCC_FMT_ATTR (1 ,2 );QObject *qobject_from_jsonv (const char *string,va_list *ap)GCC_FMT_ATTR (1 ,0 ); -QString *qobject_to_json (const QObject *obj); -QString *qobject_to_json_pretty (const QObject *obj); +/* Convert the object to QString; does not fail. */ +QString *qobject_to_json (const QObject *obj,bool pretty);int qstring_append_json_string (QString *qstring,const char *str);int qstring_append_json_number (QString *qstring,double number);
block.c +1 -1
@@ -4006,7 +4006,7 @@ void bdrv_refresh_filename(BlockDriverState *bs)if (bs->exact_filename[0 ]) {pstrcpy (bs->filename,sizeof (bs->filename), bs->exact_filename); }else if (bs->full_open_options) { -QString *json =qobject_to_json (QOBJECT (bs->full_open_options)); +QString *json =qobject_to_json (QOBJECT (bs->full_open_options), false);snprintf (bs->filename,sizeof (bs->filename),"json:%s" ,qstring_get_str (json));QDECREF (json);
monitor.c +1 -3
@@ -389,9 +389,7 @@ static void monitor_json_emitter(Monitor *mon, const QObject *data) {QString *json; - json = mon->flags &MONITOR_USE_PRETTY ?qobject_to_json_pretty (data) : -qobject_to_json (data); -assert (json !=NULL ); + json =qobject_to_json (data, mon->flags &MONITOR_USE_PRETTY );qstring_append_chr (json,'\n' );monitor_puts (mon,qstring_get_str (json));
qemu-img.c +3 -6
@@ -485,8 +485,7 @@ static void dump_json_image_check(ImageCheck *check, bool quiet)visit_type_ImageCheck (v,NULL , &check, &error_abort);visit_complete (v, &obj); - str =qobject_to_json_pretty (obj); -assert (str !=NULL ); + str =qobject_to_json (obj, true);qprintf (quiet,"%s\n" ,qstring_get_str (str));qobject_decref (obj);visit_free (v);
@@ -2175,8 +2174,7 @@ static void dump_json_image_info_list(ImageInfoList *list)visit_type_ImageInfoList (v,NULL , &list, &error_abort);visit_complete (v, &obj); - str =qobject_to_json_pretty (obj); -assert (str !=NULL ); + str =qobject_to_json (obj, true);printf ("%s\n" ,qstring_get_str (str));qobject_decref (obj);visit_free (v);
@@ -2191,8 +2189,7 @@ static void dump_json_image_info(ImageInfo *info)visit_type_ImageInfo (v,NULL , &info, &error_abort);visit_complete (v, &obj); - str =qobject_to_json_pretty (obj); -assert (str !=NULL ); + str =qobject_to_json (obj, true);printf ("%s\n" ,qstring_get_str (str));qobject_decref (obj);visit_free (v);
qga/main.c +1 -4
@@ -523,10 +523,7 @@ static int send_response(GAState *s, QObject *payload)g_assert (payload && s->channel); - payload_qstr =qobject_to_json (payload); -if (!payload_qstr) { -return -EINVAL ; - } + payload_qstr =qobject_to_json (payload, false);if (s->delimit_response) { s->delimit_response = false;
qobject/qobject-json.c +5 -14
@@ -72,12 +72,12 @@ QObject *qobject_from_jsonf(const char *string, ...)typedef struct ToJsonIterState {int indent; -int pretty; +bool pretty;int count;QString *str; }ToJsonIterState ; -static void to_json (const QObject *obj,QString *str,int pretty,int indent); +static void to_json (const QObject *obj,QString *str,bool pretty,int indent);static void to_json_dict_iter (const char *key,QObject *obj,void *opaque) {
@@ -114,7 +114,7 @@ static void to_json_list_iter(QObject *obj, void *opaque) s->count++; } -static void to_json (const QObject *obj,QString *str,int pretty,int indent) +static void to_json (const QObject *obj,QString *str,bool pretty,int indent) {switch (qobject_type (obj)) {case QTYPE_QNULL :
@@ -185,20 +185,11 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) } } -QString *qobject_to_json (const QObject *obj) +QString *qobject_to_json (const QObject *obj,bool pretty) {QString *str =qstring_new (); -to_json (obj, str,0 ,0 ); - -return str; -} - -QString *qobject_to_json_pretty (const QObject *obj) -{ -QString *str =qstring_new (); - -to_json (obj, str,1 ,0 ); +to_json (obj, str, pretty,0 );return str; }
tests/check-qobject-json.c +11 -16
@@ -64,7 +64,7 @@ static void escaped_string(void) g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);if (test_cases[i].skip ==0 ) { - str =qobject_to_json (obj); + str =qobject_to_json (obj, false); g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);qobject_decref (obj); }
@@ -98,7 +98,7 @@ static void simple_string(void) str =qobject_to_qstring (obj);g_assert (strcmp (qstring_get_str (str), test_cases[i].decoded) ==0 ); - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);g_assert (strcmp (qstring_get_str (str), test_cases[i].encoded) ==0 );qobject_decref (obj);
@@ -832,13 +832,8 @@ static void utf8_string(void)qobject_decref (obj); obj =QOBJECT (qstring_from_str (utf8_in)); - str =qobject_to_json (obj); -if (json_out) { -g_assert (str); - g_assert_cmpstr(qstring_get_str(str), ==, json_out); - }else { -g_assert (!str); - } + str =qobject_to_json (obj, false); + g_assert_cmpstr(qstring_get_str(str), ==, json_out);QDECREF (str);qobject_decref (obj);
@@ -913,7 +908,7 @@ static void simple_number(void)if (test_cases[i].skip ==0 ) {QString *str; - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);g_assert (strcmp (qstring_get_str (str), test_cases[i].encoded) ==0 );QDECREF (str); }
@@ -951,7 +946,7 @@ static void float_number(void)if (test_cases[i].skip ==0 ) {QString *str; - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);g_assert (strcmp (qstring_get_str (str), test_cases[i].encoded) ==0 );QDECREF (str); }
@@ -1011,7 +1006,7 @@ static void keyword_literal(void) qbool =qobject_to_qbool (obj);g_assert (qbool_get_bool (qbool) == true); - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);g_assert (strcmp (qstring_get_str (str),"true" ) ==0 );QDECREF (str);
@@ -1024,7 +1019,7 @@ static void keyword_literal(void) qbool =qobject_to_qbool (obj);g_assert (qbool_get_bool (qbool) == false); - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);g_assert (strcmp (qstring_get_str (str),"false" ) ==0 );QDECREF (str);
@@ -1191,7 +1186,7 @@ static void simple_dict(void)g_assert (compare_litqobj_to_qobj (&test_cases[i].decoded, obj) ==1 ); - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);qobject_decref (obj); obj =qobject_from_json (qstring_get_str (str));
@@ -1306,7 +1301,7 @@ static void simple_list(void)g_assert (compare_litqobj_to_qobj (&test_cases[i].decoded, obj) ==1 ); - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);qobject_decref (obj); obj =qobject_from_json (qstring_get_str (str));
@@ -1374,7 +1369,7 @@ static void simple_whitespace(void)g_assert (compare_litqobj_to_qobj (&test_cases[i].decoded, obj) ==1 ); - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);qobject_decref (obj); obj =qobject_from_json (qstring_get_str (str));
tests/libqtest.c +1 -1
@@ -421,7 +421,7 @@ void qmp_fd_sendv(int fd, const char *fmt, va_list ap)/* No need to send anything for an empty QObject. */ if (qobj) {int log =getenv ("QTEST_LOG" ) !=NULL ; -QString *qstr =qobject_to_json (qobj); +QString *qstr =qobject_to_json (qobj, false);const char *str =qstring_get_str (qstr);size_t size =qstring_get_length (qstr);
tests/test-visitor-serialization.c +1 -1
@@ -1037,7 +1037,7 @@ static void qmp_deserialize(void **native_out, void *datap,visit_complete (d->qov, &d->obj); obj_orig = d->obj; - output_json =qobject_to_json (obj_orig); + output_json =qobject_to_json (obj_orig, false); obj =qobject_from_json (qstring_get_str (output_json));QDECREF (output_json);
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 22/28] qobject: Consolidate qobject_to_json() calls
Quoted 11 lines · Eric Blake
It's simpler to have a single conversion function that takes a bool parameter, rather than two functions where the choice of function determines an internal bool. Similar to commit
fc471c18.While at it, the conversion currently cannot fail (maybe it SHOULD be possible to choose to fail, when encountering invalid UTF-8 encoding or an Infinity or NaN valued double, but that's a story for another day), so clean up callers to avoid a needless assert. And use bool rather than int for 'pretty'.
Signed-off-by Eric Blake <[email protected]>
[...]
Quoted 23 lines
tests/check-qobject-json.c +2
-2@@ -64,7 +64,7 @@ static void escaped_string(void) g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);if (test_cases[i].skip ==0 ) { - str =qobject_to_json (obj); + str =qobject_to_json (obj, false); g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);qobject_decref (obj); }@@ -98,7 +98,7 @@ static void simple_string(void) str =qobject_to_qstring (obj);g_assert (strcmp (qstring_get_str (str), test_cases[i].decoded) ==0 ); - str =qobject_to_json (obj); + str =qobject_to_json (obj, false);g_assert (strcmp (qstring_get_str (str), test_cases[i].encoded) ==0 );qobject_decref (obj);
tests/check-qobject-json.c static void utf8_string(void) · line 839
Quoted 8 lines
tests/check-qobject-json.c +0
-5@@ -832,13 +832,8 @@ static void utf8_string(void)qobject_decref (obj); obj =QOBJECT (qstring_from_str (utf8_in)); - str =qobject_to_json (obj); -if (json_out) { -g_assert (str); - g_assert_cmpstr(qstring_get_str(str), ==, json_out); - }else {
Unreachable, because json_out can be null only when test_cases[o].json_in is, and null json_in would've crashed in qobject_from_json().
tests/check-qobject-json.c static void utf8_string(void) · line 839
Quoted 7 lines
tests/check-qobject-json.c +2
-2-g_assert (!str); - } + str =qobject_to_json (obj, false); + g_assert_cmpstr(qstring_get_str(str), ==, json_out);QDECREF (str);qobject_decref (obj);
Thanks for cleaning up my mess.
[...]
PATCH v4 23/28 tests: Test qobject_to_json() pretty formatting
In reply to Eric BlakeIt's risky to refactor qobject_to_json() without at least testing that pretty output remains unchanged :)
Note that the new simple_pretty() test is a bit sensitive to our current notion of prettiness, as well as to the hash ordering in QDict (most of the tests in check-qobject-json intentionally do not compare the original string to the round-trip string, because we liberally accept more input forms than the canonical form that we output).
---
v4: new patch, split from v3 12/18
--- tests/check-qobject-json.c | 66 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-)
tests/check-qobject-json.c +65 -1
@@ -1,6 +1,6 @@/* * Copyright IBM, Corp. 2009 -* Copyright (c) 2013, 2015 Red Hat Inc. +* Copyright (c) 2013-2016 Red Hat Inc. * * Authors: * Anthony Liguori <[email protected]>
@@ -1383,6 +1383,69 @@ static void simple_whitespace(void) } } +static void simple_pretty (void ) +{ +int i; +struct { +const char *encoded; +LiteralQObject decoded; + } test_cases[] = { + { + .encoded = +"[\n" +" 43,\n" +" 42\n" +"]" , + .decoded =QLIT_QLIST (((LiteralQObject []){ +QLIT_QINT (43 ), +QLIT_QINT (42 ), + { } + })), + }, + { + .encoded = +"[\n" +" 43,\n" +" {\n" +" \"a\": 32,\n" +" \"h\": \"b\"\n" +" },\n" +" [\n" +" ],\n" +" 42\n" +"]" , + .decoded =QLIT_QLIST (((LiteralQObject []){ +QLIT_QINT (43 ), +QLIT_QDICT (((LiteralQDictEntry []){ + {"a" ,QLIT_QINT (32 ) }, + {"h" ,QLIT_QSTR ("b" ) }, + { } })), +QLIT_QLIST (((LiteralQObject []){ + { } })), +QLIT_QINT (42 ), + { } + })), + }, + { } + }; + +for (i =0 ; test_cases[i].encoded; i++) { +QObject *obj; +QString *str; + + obj =qobject_from_json (test_cases[i].encoded); +g_assert (obj !=NULL ); +g_assert (qobject_type (obj) ==QTYPE_QLIST ); + +g_assert (compare_litqobj_to_qobj (&test_cases[i].decoded, obj) ==1 ); + + str =qobject_to_json (obj, true); +qobject_decref (obj); + g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded); +QDECREF (str); + } +} +static void simple_varargs (void ) {QObject *embedded_obj;
@@ -1520,6 +1583,7 @@ int main(int argc, char **argv)g_test_add_func ("/lists/simple_list" , simple_list);g_test_add_func ("/whitespace/simple_whitespace" , simple_whitespace); +g_test_add_func ("/whitespace/simple_pretty" , simple_pretty);g_test_add_func ("/varargs/simple_varargs" , simple_varargs);
Signature · 1 line
2.5.5
PATCH v4 24/28 qapi: Add JSON output visitor
In reply to Eric BlakeWe have several places that want to go from qapi to JSON; right now, they have to create an intermediate QObject to do the work. That also has the drawback that the JSON formatting of a QDict will rearrange keys (according to a deterministic, but unpredictable, hash), when humans have an easier time if dicts are produced in the same order as the qapi type.
For these reasons, it is time to add a new JSON output visitor. This patch just adds the basic visitor and tests that it works; later patches will add pretty-printing, support for visit_type_any(), and conversion of clients to use the visitor.
Design choices: Unlike the QMP output visitor, the JSON visitor refuses to visit a required string with a NULL value, via abort(). Reusing QString to grow the contents means that we easily share code with both qobject-json.c and qjson.c; although it might be nice to enhance things to take an optional output callback function so that the output can truly be streamed instead of collected in memory.
---
v4: retitle, split off inf/NaN handling, rebase to visit_complete and visit_free changes, defer type_any, address other findings from Markus
v3: retitle, rebase to master, minor cleanups
v2: rebase to qapi subset E v8; add test of error outputting infinity; use unsigned depth
--- include/qapi/visitor.h | 33 ++-- include/qapi/json-output-visitor.h | 28 +++ qapi/json-output-visitor.c | 193 +++++++++++++++++++ tests/test-json-output-visitor.c | 383 +++++++++++++++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 5 + qapi/Makefile.objs | 2 +- tests/.gitignore | 1 + tests/Makefile | 5 +- 8 files changed, 632 insertions(+), 18 deletions(-) create mode 100644 include/qapi/json-output-visitor.h create mode 100644 qapi/json-output-visitor.c create mode 100644 tests/test-json-output-visitor.c
include/qapi/visitor.h +17 -16
@@ -26,17 +26,17 @@* * There are four kinds of visitor classes: input visitors (QMP, * string, and QemuOpts) parse an external representation and build -* the corresponding QAPI graph, output visitors (QMP and string) take -* a completed QAPI graph and generate an external representation, the -* dealloc visitor can take a QAPI graph (possibly partially -* constructed) and recursively free its resources, and the clone -* visitor performs a deep clone of one QAPI object to another. While -* the dealloc and QMP input/output visitors are general, the string, -* QemuOpts, and clone visitors have some implementation limitations; -* see the documentation for each visitor for more details on what it -* supports. Also, see visitor-impl.h for the callback contracts -* implemented by each visitor, and docs/qapi-code-gen.txt for more -* about the QAPI code generator. +* the corresponding QAPI graph, output visitors (QMP, string, and +* JSON) take a completed QAPI graph and generate an external +* representation, the dealloc visitor can take a QAPI graph (possibly +* partially constructed) and recursively free its resources, and the +* clone visitor performs a deep clone of one QAPI object to another. +* While the dealloc, JSON output, and QMP input/output visitors are +* general, the string, QemuOpts, and clone visitors have some +* implementation limitations; see the documentation for each visitor +* for more details on what it supports. Also, see visitor-impl.h for +* the callback contracts implemented by each visitor, and +* docs/qapi-code-gen.txt for more about the QAPI code generator. * * All of the visitors are created via: *
@@ -59,11 +59,12 @@* visitors are declared here; the remaining visitors are generated in * qapi-visit.h. * -* The @name parameter of visit_type_FOO() describes the relation -* between this QAPI value and its parent container. When visiting -* the root of a tree, @name is ignored; when visiting a member of an -* object, @name is the key associated with the value; and when -* visiting a member of a list, @name is NULL. +* The @name parameter of visit_type_FOO() and visit_start_OBJECT() +* describes the relation between this QAPI value and its parent +* container. When visiting the root of a tree, @name is ignored; +* when visiting a member of an object, @name is the key associated +* with the value; and when visiting a member of a list, @name is +* NULL. * * FIXME: Clients must pass NULL for @name when visiting a member of a * list, but this leads to poor error messages; it might be nicer to
include/qapi/json-output-visitor.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* JSON Output Visitor +* +* Copyright (C) 2015-2016 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef JSON_OUTPUT_VISITOR_H +#define JSON_OUTPUT_VISITOR_H + +#include "qapi/visitor.h" + +typedef struct JsonOutputVisitor JsonOutputVisitor ; + +/* +* Create a new JSON output visitor. +* +* If everything else succeeds, pass @result to visit_complete() to +* collect the result of the visit. +* +* For now, this cannot be used to visit the 'any' type. +*/ +Visitor *json_output_visitor_new (char **result); + +#endif
qapi/json-output-visitor.c new file +193 -0
193 lines not on this page · raw message
tests/test-json-output-visitor.c new file +383 -0
383 lines not on this page · raw message
tests/test-qmp-output-visitor.c +5 -0
@@ -10,6 +10,11 @@* See the COPYING file in the top-level directory. */ +/* +* Similar in design to test-json-output-visitor; if you add tests +* here, consider adding tests there as well. +*/ +#include "qemu/osdep.h" #include <glib.h>
qapi/Makefile.objs +1 -1
@@ -1,6 +1,6 @@ util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o util-obj-y += string-input-visitor.o string-output-visitor.o -util-obj-y += opts-visitor.o qapi-clone-visitor.o +util-obj-y += opts-visitor.o qapi-clone-visitor.o json-output-visitor.o util-obj-y += qmp-event.o util-obj-y += qapi-util.o
tests/.gitignore +1 -0
@@ -41,6 +41,7 @@ test-io-channel-file.txt test-io-channel-socket test-io-channel-tls test-io-task +test-json-output-visitor test-logging test-mul64 test-opts-visitor
tests/Makefile +4 -1
@@ -24,6 +24,8 @@ check-unit-y += tests/test-qmp-output-visitor$(EXESUF) gcov-files-test-qmp-output-visitor-y = qapi/qmp-output-visitor.c check-unit-y += tests/test-clone-visitor$(EXESUF) gcov-files-test-clone-visitor-y = qapi/qapi-clone-visitor.c +check-unit-y += tests/test-json-output-visitor$(EXESUF) +gcov-files-test-json-output-visitor-y = qapi/json-output-visitor.c check-unit-y += tests/test-qmp-input-visitor$(EXESUF) gcov-files-test-qmp-input-visitor-y = qapi/qmp-input-visitor.c check-unit-y += tests/test-qmp-input-strict$(EXESUF)
@@ -390,7 +392,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/check-qobject-json.o \ tests/test-coroutine.o tests/test-string-output-visitor.o \ tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \ - tests/test-clone-visitor.o \ + tests/test-clone-visitor.o tests/test-json-output-visitor.o \ tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \ tests/test-qmp-commands.o tests/test-visitor-serialization.o \ tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \
@@ -482,6 +484,7 @@ tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(te tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y) tests/test-qmp-output-visitor$(EXESUF): tests/test-qmp-output-visitor.o $(test-qapi-obj-y) tests/test-clone-visitor$(EXESUF): tests/test-clone-visitor.o $(test-qapi-obj-y) +tests/test-json-output-visitor$(EXESUF): tests/test-json-output-visitor.o $(test-qapi-obj-y) tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(test-qapi-obj-y) tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y) tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marshal.o $(test-qapi-obj-y)
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 24/28] qapi: Add JSON output visitor
Quoted 81 lines · Eric Blake
We have several places that want to go from qapi to JSON; right now, they have to create an intermediate QObject to do the work. That also has the drawback that the JSON formatting of a QDict will rearrange keys (according to a deterministic, but unpredictable, hash), when humans have an easier time if dicts are produced in the same order as the qapi type.
For these reasons, it is time to add a new JSON output visitor. This patch just adds the basic visitor and tests that it works; later patches will add pretty-printing, support for
visit_type_any(), and conversion of clients to use the visitor.Design choices: Unlike the QMP output visitor, the JSON visitor refuses to visit a required string with a NULL value, via
abort(). Reusing QString to grow the contents means that we easily share code with bothqobject-json.candqjson.c; although it might be nice to enhance things to take an optional output callback function so that the output can truly be streamed instead of collected in memory.Signed-off-by Eric Blake <[email protected]>---
v4: retitle, split off inf/NaN handling, rebase tovisit_completeandvisit_freechanges, defertype_any, address other findings from Markus
v3: retitle, rebase to master, minor cleanups
v2: rebase to qapi subset E v8; add test of error outputting infinity; use unsigned depth--- include/qapi/visitor.h | 33 ++-- include/qapi/json-output-visitor.h | 28 +++ qapi/json-output-visitor.c | 193 +++++++++++++++++++ tests/test-json-output-visitor.c | 383 +++++++++++++++++++++++++++++++++++++ tests/test-qmp-output-visitor.c | 5 + qapi/Makefile.objs | 2 +- tests/.gitignore | 1 + tests/Makefile | 5 +- 8 files changed, 632 insertions(+), 18 deletions(-) create mode 100644 include/qapi/json-output-visitor.h create mode 100644 qapi/json-output-visitor.c create mode 100644 tests/test-json-output-visitor.cinclude/qapi/visitor.h +11
-12@@ -26,17 +26,17 @@* * There are four kinds of visitor classes: input visitors (QMP, * string, and QemuOpts) parse an external representation and build -* the corresponding QAPI graph, output visitors (QMP and string) take -* a completed QAPI graph and generate an external representation, the -* dealloc visitor can take a QAPI graph (possibly partially -* constructed) and recursively free its resources, and the clone -* visitor performs a deep clone of one QAPI object to another. While -* the dealloc and QMP input/output visitors are general, the string, -* QemuOpts, and clone visitors have some implementation limitations; -* see the documentation for each visitor for more details on what it -* supports. Also, see visitor-impl.h for the callback contracts -* implemented by each visitor, and docs/qapi-code-gen.txt for more -* about the QAPI code generator. +* the corresponding QAPI graph, output visitors (QMP, string, and +* JSON) take a completed QAPI graph and generate an external +* representation, the dealloc visitor can take a QAPI graph (possibly +* partially constructed) and recursively free its resources, and the +* clone visitor performs a deep clone of one QAPI object to another. +* While the dealloc, JSON output, and QMP input/output visitors are +* general, the string, QemuOpts, and clone visitors have some +* implementation limitations; see the documentation for each visitor +* for more details on what it supports. Also, see visitor-impl.h for +* the callback contracts implemented by each visitor, and +* docs/qapi-code-gen.txt for more about the QAPI code generator. * * All of the visitors are created via: * @@ -59,11 +59,12 @@* visitors are declared here; the remaining visitors are generated in * qapi-visit.h. * -* The @name parameter of visit_type_FOO() describes the relation
include/qapi/visitor.h line 67
Quoted 10 lines
include/qapi/visitor.h +6
-4-* between this QAPI value and its parent container. When visiting -* the root of a tree, @name is ignored; when visiting a member of an -* object, @name is the key associated with the value; and when -* visiting a member of a list, @name is NULL. +* The @name parameter of visit_type_FOO() and visit_start_OBJECT() +* describes the relation between this QAPI value and its parent +* container. When visiting the root of a tree, @name is ignored; +* when visiting a member of an object, @name is the key associated +* with the value; and when visiting a member of a list, @name is +* NULL.
Not related to this patch, just came up in review of v3. Splitting it out into its own patch doesn't seem worthwhile, so let's keep it here.
Quoted 191 lines
include/qapi/visitor.h +0
-0* * FIXME: Clients must pass NULL for @name when visiting a member of a * list, but this leads to poor error messages; it might be nicer to include/qapi/json-output-visitor.h new file +28
-0@@ -0,0 +1,28 @@ +/* +* JSON Output Visitor +* +* Copyright (C) 2015-2016 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef JSON_OUTPUT_VISITOR_H +#define JSON_OUTPUT_VISITOR_H + +#include "qapi/visitor.h" + +typedef struct JsonOutputVisitor JsonOutputVisitor ; + +/* +* Create a new JSON output visitor. +* +* If everything else succeeds, pass @result to visit_complete() to +* collect the result of the visit. +* +* For now, this cannot be used to visit the 'any' type. +*/ +Visitor *json_output_visitor_new (char **result); + +#endif qapi/json-output-visitor.c new file +148
-0@@ -0,0 +1,193 @@ +/* +* Convert QAPI to JSON +* +* Copyright (C) 2015-2016 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include "qemu/osdep.h" +#include "qapi/json-output-visitor.h" +#include "qapi/visitor-impl.h" +#include "qapi/qmp/qstring.h" +#include "qapi/qmp/qobject-json.h" + +struct JsonOutputVisitor { +Visitor visitor; +QString *str; +bool comma; +unsigned int depth; +char **result; +}; + +static JsonOutputVisitor *to_jov (Visitor *v) +{ +return container_of (v, JsonOutputVisitor, visitor); +} + +static void json_output_name (JsonOutputVisitor *jov,const char *name) +{ +if (jov->comma) { +qstring_append (jov->str,", " ); + }else { + jov->comma = true; + } +if (name && jov->depth) { +qstring_append_json_string (jov->str, name); +qstring_append (jov->str,": " ); + } +} + +static void json_output_start_struct (Visitor *v,const char *name,void **obj, +size_t unused,Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +json_output_name (jov, name); +qstring_append (jov->str,"{" ); + jov->comma = false; + jov->depth++; +} + +static void json_output_end_struct (Visitor *v,void **obj) +{ +JsonOutputVisitor *jov =to_jov (v); + +assert (jov->depth); + jov->depth--; +qstring_append (jov->str,"}" ); + jov->comma = true; +} + +static void json_output_start_list (Visitor *v,const char *name, +GenericList **listp,size_t size, +Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +json_output_name (jov, name); +qstring_append (jov->str,"[" ); + jov->comma = false; + jov->depth++; +} + +static GenericList *json_output_next_list (Visitor *v,GenericList *tail, +size_t size) +{ +return tail->next; +} + +static void json_output_end_list (Visitor *v,void **obj) +{ +JsonOutputVisitor *jov =to_jov (v); + +assert (jov->depth); + jov->depth--; +qstring_append (jov->str,"]" ); + jov->comma = true; +} + +static void json_output_type_int64 (Visitor *v,const char *name,int64_t *obj, +Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +json_output_name (jov, name); +qstring_append_printf (jov->str,"%" PRId64, *obj); +} + +static void json_output_type_uint64 (Visitor *v,const char *name, +uint64_t *obj,Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +json_output_name (jov, name); +qstring_append_printf (jov->str,"%" PRIu64, *obj); +} + +static void json_output_type_bool (Visitor *v,const char *name,bool *obj, +Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +json_output_name (jov, name); +qstring_append (jov->str, *obj ?"true" :"false" ); +} + +static void json_output_type_str (Visitor *v,const char *name,char **obj, +Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +assert (*obj); +json_output_name (jov, name); +/* FIXME: report invalid UTF-8 encoding */ +qstring_append_json_string (jov->str, *obj); +} + +static void json_output_type_number (Visitor *v,const char *name,double *obj, +Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +json_output_name (jov, name); +/* FIXME: report Inf/NaN problems */ +qstring_append_json_number (jov->str, *obj); +} + +static void json_output_type_null (Visitor *v,const char *name,Error **errp) +{ +JsonOutputVisitor *jov =to_jov (v); + +json_output_name (jov, name); +qstring_append (jov->str,"null" ); +} + +static void json_output_complete (Visitor *v,void *result)
qapi/json-output-visitor.c line 158
Quoted 10 lines
qapi/json-output-visitor.c new file +10
-0+{ +JsonOutputVisitor *jov =to_jov (v); + +assert (!jov->depth); +assert (qstring_get_length (jov->str)); +assert (jov->result == result); + *jov->result =qstring_consume_str (jov->str); + jov->str =NULL ; + jov->result =NULL ; +}
Related: discussion of complete() semantics in review of PATCH 12. Non-idempotent semantics save us a copy, like in string_output_complete().
qapi/json-output-visitor.c line 165
Quoted 7 lines
qapi/json-output-visitor.c new file +7
-0+ +static void json_output_free (Visitor *v) +{ +JsonOutputVisitor *jov =to_jov (v); + +QDECREF (jov->str); +g_free (jov);
I'm afraid this leaks jov->result when the visitor is destroyed without calling its complete() method.
string_output_free() appears to have the same leak.
Quoted 28 lines
qapi/json-output-visitor.c new file +28
-0+} + +Visitor *json_output_visitor_new (char **result) +{ +JsonOutputVisitor *v; + + v =g_malloc0 (sizeof (*v)); + v->result = result; + *result =NULL ; + v->str =qstring_new (); + + v->visitor.type =VISITOR_OUTPUT ; + v->visitor.start_struct = json_output_start_struct; + v->visitor.end_struct = json_output_end_struct; + v->visitor.start_list = json_output_start_list; + v->visitor.next_list = json_output_next_list; + v->visitor.end_list = json_output_end_list; + v->visitor.type_int64 = json_output_type_int64; + v->visitor.type_uint64 = json_output_type_uint64; + v->visitor.type_bool = json_output_type_bool; + v->visitor.type_str = json_output_type_str; + v->visitor.type_number = json_output_type_number; + v->visitor.type_null = json_output_type_null; + v->visitor.complete = json_output_complete; + v->visitor.free = json_output_free; + +return &v->visitor; +}
[...]
Re: [Qemu-devel] [PATCH v4 24/28] qapi: Add JSON output visitor
Quoted 35 lines · Markus Armbruster
Quoted 18 lines · Eric Blake
We have several places that want to go from qapi to JSON; right now, they have to create an intermediate QObject to do the work. That also has the drawback that the JSON formatting of a QDict will rearrange keys (according to a deterministic, but unpredictable, hash), when humans have an easier time if dicts are produced in the same order as the qapi type.
qapi/json-output-visitor.c new file +9
-0+struct JsonOutputVisitor { +Visitor visitor; +QString *str; +bool comma; +unsigned int depth; +char **result; +}; + +static void json_output_complete (Visitor *v,void *result)qapi/json-output-visitor.c line 52
Quoted 10 lines
qapi/json-output-visitor.c new file +10
-0+{ +JsonOutputVisitor *jov =to_jov (v); + +assert (!jov->depth); +assert (qstring_get_length (jov->str)); +assert (jov->result == result); + *jov->result =qstring_consume_str (jov->str); + jov->str =NULL ; + jov->result =NULL ; +}Related: discussion of
complete()semantics in review of PATCH 12. Non-idempotent semantics save us a copy, like instring_output_complete().
Quoted 10 lines
qapi/json-output-visitor.c line 165
Quoted 7 lines
qapi/json-output-visitor.c new file +7
-0+ +static void json_output_free (Visitor *v) +{ +JsonOutputVisitor *jov =to_jov (v); + +QDECREF (jov->str); +g_free (jov);I'm afraid this leaks
jov->resultwhen the visitor is destroyed without calling itscomplete()method.
Not true. jov->result is owned by the caller, and not something we allocate locally. We set jov->result to NULL to make sure complete() is not called twice, but we are not responsible for freeing it, since we didn't allocate it.
Quoted 2 lines
string_output_free()appears to have the same leak.
Same non-bug.
Quoted 12 lines
Quoted 12 lines
qapi/json-output-visitor.c new file +11
-0+} + +Visitor *json_output_visitor_new (char **result) +{ +JsonOutputVisitor *v; + + v =g_malloc0 (sizeof (*v)); + v->result = result; + *result =NULL ; + v->str =qstring_new (); +
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v4 24/28] qapi: Add JSON output visitor
Quoted 53 lines · Eric Blake
Quoted 35 lines · Markus Armbruster
Quoted 18 lines · Eric Blake
We have several places that want to go from qapi to JSON; right now, they have to create an intermediate QObject to do the work. That also has the drawback that the JSON formatting of a QDict will rearrange keys (according to a deterministic, but unpredictable, hash), when humans have an easier time if dicts are produced in the same order as the qapi type.
qapi/json-output-visitor.c new file +9
-0+struct JsonOutputVisitor { +Visitor visitor; +QString *str; +bool comma; +unsigned int depth; +char **result; +}; + +static void json_output_complete (Visitor *v,void *result)qapi/json-output-visitor.c line 52
Quoted 10 lines
qapi/json-output-visitor.c new file +10
-0+{ +JsonOutputVisitor *jov =to_jov (v); + +assert (!jov->depth); +assert (qstring_get_length (jov->str)); +assert (jov->result == result); + *jov->result =qstring_consume_str (jov->str); + jov->str =NULL ; + jov->result =NULL ; +}Related: discussion of
complete()semantics in review of PATCH 12. Non-idempotent semantics save us a copy, like instring_output_complete().Quoted 10 lines
qapi/json-output-visitor.c line 165
Quoted 7 lines
qapi/json-output-visitor.c new file +7
-0+ +static void json_output_free (Visitor *v) +{ +JsonOutputVisitor *jov =to_jov (v); + +QDECREF (jov->str); +g_free (jov);I'm afraid this leaks
jov->resultwhen the visitor is destroyed without calling itscomplete()method.Not true.
jov->resultis owned by the caller, and not something we allocate locally. We setjov->resultto NULL to make surecomplete()is not called twice, but we are not responsible for freeing it, since we didn't allocate it.Quoted 1 line
Quoted 3 lines
Quoted 1 line
string_output_free()appears to have the same leak.Same non-bug.
You're right.
PATCH v4 25/28 qapi: Support pretty printing in JSON output visitor
In reply to Eric BlakeSimilar to pretty printing in the QObject visitor. The trickiest part is probably that the testsuite now has to honor parameterization on whether pretty printing is enabled.
---
v4: rebase to earlier changes, defer type_any() to later
v3: rebase to earlier changes
v2: rebase to earlier changes
--- include/qapi/json-output-visitor.h | 5 +- qapi/json-output-visitor.c | 15 ++++- tests/test-json-output-visitor.c | 122 +++++++++++++++++++++++++------------ 3 files changed, 99 insertions(+), 43 deletions(-)
include/qapi/json-output-visitor.h +4 -1
@@ -21,8 +21,11 @@ typedef struct JsonOutputVisitor JsonOutputVisitor;* If everything else succeeds, pass @result to visit_complete() to * collect the result of the visit. * +* If @pretty, make the output legible with newlines and indentation; +* otherwise the output uses a single line. +* * For now, this cannot be used to visit the 'any' type. */ -Visitor *json_output_visitor_new (char **result); +Visitor *json_output_visitor_new (bool pretty,char **result);#endif
qapi/json-output-visitor.c +13 -2
@@ -17,6 +17,7 @@struct JsonOutputVisitor {Visitor visitor;QString *str; +bool pretty;bool comma;unsigned int depth;char **result;
@@ -30,10 +31,13 @@ static JsonOutputVisitor *to_jov(Visitor *v)static void json_output_name (JsonOutputVisitor *jov,const char *name) {if (jov->comma) { -qstring_append (jov->str,", " ); +qstring_append (jov->str, jov->pretty ?"," :", " ); }else { jov->comma = true; } +if (jov->pretty && jov->depth) { +qstring_append_printf (jov->str,"\n%*s" ,4 * jov->depth,"" ); + }if (name && jov->depth) {qstring_append_json_string (jov->str, name);qstring_append (jov->str,": " );
@@ -57,6 +61,9 @@ static void json_output_end_struct(Visitor *v, void **obj)assert (jov->depth); jov->depth--; +if (jov->pretty) { +qstring_append_printf (jov->str,"\n%*s" ,4 * jov->depth,"" ); + }qstring_append (jov->str,"}" ); jov->comma = true; }
@@ -85,6 +92,9 @@ static void json_output_end_list(Visitor *v, void **obj)assert (jov->depth); jov->depth--; +if (jov->pretty) { +qstring_append_printf (jov->str,"\n%*s" ,4 * jov->depth,"" ); + }qstring_append (jov->str,"]" ); jov->comma = true; }
@@ -165,11 +175,12 @@ static void json_output_free(Visitor *v)g_free (jov); } -Visitor *json_output_visitor_new (char **result) +Visitor *json_output_visitor_new (bool pretty,char **result) {JsonOutputVisitor *v; v =g_malloc0 (sizeof (*v)); + v->pretty = pretty; v->result = result; *result =NULL ; v->str =qstring_new ();
tests/test-json-output-visitor.c +82 -40
175 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 25/28] qapi: Support pretty printing in JSON output visitor
Quoted 3 lines · Eric Blake
Similar to pretty printing in the QObject visitor. The trickiest part is probably that the testsuite now has to honor parameterization on whether pretty printing is enabled.
Worth mentioning that the pretty-printing matches the one in qobject-json.c?
Quoted 99 lines
Signed-off-by Eric Blake <[email protected]>---
v4: rebase to earlier changes, defertype_any()to later
v3: rebase to earlier changes
v2: rebase to earlier changes--- include/qapi/json-output-visitor.h | 5 +- qapi/json-output-visitor.c | 15 ++++- tests/test-json-output-visitor.c | 122 +++++++++++++++++++++++++------------ 3 files changed, 99 insertions(+), 43 deletions(-)include/qapi/json-output-visitor.h +4
-1@@ -21,8 +21,11 @@ typedef struct JsonOutputVisitor JsonOutputVisitor;* If everything else succeeds, pass @result to visit_complete() to * collect the result of the visit. * +* If @pretty, make the output legible with newlines and indentation; +* otherwise the output uses a single line. +* * For now, this cannot be used to visit the 'any' type. */ -Visitor *json_output_visitor_new (char **result); +Visitor *json_output_visitor_new (bool pretty,char **result);#endif qapi/json-output-visitor.c +13
-2@@ -17,6 +17,7 @@struct JsonOutputVisitor {Visitor visitor;QString *str; +bool pretty;bool comma;unsigned int depth;char **result;@@ -30,10 +31,13 @@ static JsonOutputVisitor *to_jov(Visitor *v)static void json_output_name (JsonOutputVisitor *jov,const char *name) {if (jov->comma) { -qstring_append (jov->str,", " ); +qstring_append (jov->str, jov->pretty ?"," :", " ); }else { jov->comma = true; } +if (jov->pretty && jov->depth) { +qstring_append_printf (jov->str,"\n%*s" ,4 * jov->depth,"" ); + }if (name && jov->depth) {qstring_append_json_string (jov->str, name);qstring_append (jov->str,": " );@@ -57,6 +61,9 @@ static void json_output_end_struct(Visitor *v, void **obj)assert (jov->depth); jov->depth--; +if (jov->pretty) { +qstring_append_printf (jov->str,"\n%*s" ,4 * jov->depth,"" ); + }qstring_append (jov->str,"}" ); jov->comma = true; }@@ -85,6 +92,9 @@ static void json_output_end_list(Visitor *v, void **obj)assert (jov->depth); jov->depth--; +if (jov->pretty) { +qstring_append_printf (jov->str,"\n%*s" ,4 * jov->depth,"" ); + }qstring_append (jov->str,"]" ); jov->comma = true; }@@ -165,11 +175,12 @@ static void json_output_free(Visitor *v)g_free (jov); } -Visitor *json_output_visitor_new (char **result) +Visitor *json_output_visitor_new (bool pretty,char **result) {JsonOutputVisitor *v; v =g_malloc0 (sizeof (*v)); + v->pretty = pretty; v->result = result; *result =NULL ; v->str =qstring_new ();tests/test-json-output-visitor.c +0
-0@@ -24,14 +24,17 @@ typedef struct TestOutputVisitorData { Visitor *ov;
tests/test-json-output-visitor.c line 34
Quoted 10 lines
tests/test-json-output-visitor.c +3
-2+bool pretty;char *str; } TestOutputVisitorData;static void visitor_output_setup (TestOutputVisitorData *data, -const void *unused) +const void *arg) { - data->ov =json_output_visitor_new (&data->str); +const bool *pretty = arg;
Could do bool pretty = *(bool *)arg. Matter of taste. Same elsewhere.
Blank line between declarations and statements, please.
tests/test-json-output-visitor.c line 37
Quoted 3 lines
tests/test-json-output-visitor.c +2
-0+ data->ov =json_output_visitor_new (*pretty, &data->str);g_assert (data->ov); + data->pretty = *pretty;
I'd put this line before data->ov = ...
tests/test-json-output-visitor.c static const char *visitor_get(TestOutputVisitorData *data) · line 58
Quoted 8 lines
tests/test-json-output-visitor.c +1
-0} static void visitor_output_teardown(TestOutputVisitorData *data,@@ -52,8 +55,9 @@ static const char *visitor_get(TestOutputVisitorData *data)static void visitor_reset (TestOutputVisitorData *data) { +bool pretty = data->pretty;
Blank line between declarations and statements, please.
tests/test-json-output-visitor.c static const char *visitor_get(TestOutputVisitorData *data) · line 60
Quoted 3 lines
tests/test-json-output-visitor.c +1
-1visitor_output_teardown (data,NULL ); -visitor_output_setup (data,NULL ); +visitor_output_setup (data, &pretty);
We could pass &data->pretty (teardown leaves it alone), but I guess not relying on that is slightly cleaner.
Quoted 74 lines
tests/test-json-output-visitor.c +37
-22} static void test_visitor_out_int(TestOutputVisitorData *data,@@ -161,8 +165,9 @@ static void test_visitor_out_struct(TestOutputVisitorData *data, }static void test_visitor_out_struct_nested (TestOutputVisitorData *data, -const void *unused) +const void *arg) { +const bool *pretty = arg;int64_t value =42 ;UserDefTwo *ud2;const char *string ="user def string" ;@@ -192,27 +197,51 @@ static void test_visitor_out_struct_nested(TestOutputVisitorData *data,visit_type_UserDefTwo (data->ov,"unused" , &ud2, &error_abort); out =visitor_get (data); - g_assert_cmpstr(out, ==, -"{" -"\"string0\": \"forty two\", " -"\"dict1\": {" -"\"string1\": \"forty three\", " -"\"dict2\": {" -"\"userdef\": {" -"\"integer\": 42, " -"\"string\": \"user def string\"" -"}, " -"\"string\": \"forty four\"" -"}, " -"\"dict3\": {" -"\"userdef\": {" -"\"integer\": 42, " -"\"string\": \"user def string\"" -"}, " -"\"string\": \"forty five\"" -"}" -"}" -"}" ); +if (*pretty) { + g_assert_cmpstr(out, ==, +"{\n" +" \"string0\": \"forty two\",\n" +" \"dict1\": {\n" +" \"string1\": \"forty three\",\n" +" \"dict2\": {\n" +" \"userdef\": {\n" +" \"integer\": 42,\n" +" \"string\": \"user def string\"\n" +" },\n" +" \"string\": \"forty four\"\n" +" },\n" +" \"dict3\": {\n" +" \"userdef\": {\n" +" \"integer\": 42,\n" +" \"string\": \"user def string\"\n" +" },\n" +" \"string\": \"forty five\"\n" +" }\n" +" }\n" +"}" ); + }else { + g_assert_cmpstr(out, ==, +"{" +"\"string0\": \"forty two\", " +"\"dict1\": {" +"\"string1\": \"forty three\", " +"\"dict2\": {" +"\"userdef\": {" +"\"integer\": 42, " +"\"string\": \"user def string\"" +"}, " +"\"string\": \"forty four\"" + "}, "
tests/test-json-output-visitor.c static void test_visitor_out_struct_nested(TestOutputVisitorData *data, · line 244
Quoted 10 lines
tests/test-json-output-visitor.c +10
-0+"\"dict3\": {" +"\"userdef\": {" +"\"integer\": 42, " +"\"string\": \"user def string\"" +"}, " +"\"string\": \"forty five\"" +"}" +"}" + "}"); + }
The not-pretty case is unchanged. Good.
Quoted 67 lines
tests/test-json-output-visitor.c +28
-15qapi_free_UserDefTwo (ud2); }@@ -346,36 +375,49 @@ static void test_visitor_out_null(TestOutputVisitorData *data, g_assert_cmpstr(out, ==, "null"); } -static void output_visitor_test_add (const char *testpath, +static void output_visitor_test_add (const char *testpath,bool *data,void (*test_func)(TestOutputVisitorData *,const void *)) { -g_test_add (testpath, TestOutputVisitorData,NULL , visitor_output_setup, +g_test_add (testpath, TestOutputVisitorData, data, visitor_output_setup, test_func, visitor_output_teardown); }int main (int argc,char **argv) { +bool plain = false; +bool pretty = true; +g_test_init (&argc, &argv,NULL ); -output_visitor_test_add ("/visitor/json/int" , test_visitor_out_int); -output_visitor_test_add ("/visitor/json/bool" , test_visitor_out_bool); -output_visitor_test_add ("/visitor/json/number" , test_visitor_out_number); -output_visitor_test_add ("/visitor/json/string" , test_visitor_out_string); -output_visitor_test_add ("/visitor/json/enum" , test_visitor_out_enum); -output_visitor_test_add ("/visitor/json/enum-errors" , +output_visitor_test_add ("/visitor/json/int" , &plain, + test_visitor_out_int); +output_visitor_test_add ("/visitor/json/bool" , &plain, + test_visitor_out_bool); +output_visitor_test_add ("/visitor/json/number" , &plain, + test_visitor_out_number); +output_visitor_test_add ("/visitor/json/string" , &plain, + test_visitor_out_string); +output_visitor_test_add ("/visitor/json/enum" , &plain, + test_visitor_out_enum); +output_visitor_test_add ("/visitor/json/enum-errors" , &plain, test_visitor_out_enum_errors); -output_visitor_test_add ("/visitor/json/struct" , test_visitor_out_struct); -output_visitor_test_add ("/visitor/json/struct-nested" , +output_visitor_test_add ("/visitor/json/struct" , &plain, + test_visitor_out_struct); +output_visitor_test_add ("/visitor/json/struct-nested" , &plain, test_visitor_out_struct_nested); -output_visitor_test_add ("/visitor/json/struct-errors" , +output_visitor_test_add ("/visitor/json-pretty/struct-nested" , &pretty, + test_visitor_out_struct_nested); +output_visitor_test_add ("/visitor/json/struct-errors" , &plain, test_visitor_out_struct_errors); -output_visitor_test_add ("/visitor/json/list" , test_visitor_out_list); -output_visitor_test_add ("/visitor/json/union-flat" , +output_visitor_test_add ("/visitor/json/list" , &plain, + test_visitor_out_list); +output_visitor_test_add ("/visitor/json/union-flat" , &plain, test_visitor_out_union_flat); -output_visitor_test_add ("/visitor/json/alternate" , +output_visitor_test_add ("/visitor/json/alternate" , &plain, test_visitor_out_alternate); -output_visitor_test_add ("/visitor/json/null" , test_visitor_out_null); +output_visitor_test_add ("/visitor/json/null" , &plain, + test_visitor_out_null);g_test_run ();
Re: [Qemu-devel] [PATCH v4 25/28] qapi: Support pretty printing in JSON output visitor
Quoted 8 lines · Markus Armbruster
Quoted 3 lines · Eric Blake
Similar to pretty printing in the QObject visitor. The trickiest part is probably that the testsuite now has to honor parameterization on whether pretty printing is enabled.
Worth mentioning that the pretty-printing matches the one in
qobject-json.c?
Yes, particularly since we later rely on that fact to rewrite qobject-json.c on top of the JSON visitor :)
Quoted 8 lines
tests/test-json-output-visitor.c line 34
Quoted 6 lines
static void visitor_output_setup(TestOutputVisitorData *data,
tests/test-json-output-visitor.c +2
-2- const void *unused) + const void *arg) { - data->ov =json_output_visitor_new (&data->str); +const bool *pretty = arg;Could do bool pretty = *(bool *)arg. Matter of taste. Same elsewhere.
Requires a cast. I like avoiding casts where C lets us do so. But I also agree that *pretty looks ugly, so maybe:
...(const void *arg)
{
const bool *data;
bool pretty = *data;Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v4 25/28] qapi: Support pretty printing in JSON output visitor
Quoted 23 lines · Eric Blake
Quoted 8 lines · Markus Armbruster
Quoted 3 lines · Eric Blake
Similar to pretty printing in the QObject visitor. The trickiest part is probably that the testsuite now has to honor parameterization on whether pretty printing is enabled.
Worth mentioning that the pretty-printing matches the one in
qobject-json.c?Yes, particularly since we later rely on that fact to rewrite
qobject-json.con top of the JSON visitor :)Quoted 8 lines
tests/test-json-output-visitor.c line 34
Quoted 6 lines
static void visitor_output_setup(TestOutputVisitorData *data,
tests/test-json-output-visitor.c +2
-2- const void *unused) + const void *arg) { - data->ov =json_output_visitor_new (&data->str); +const bool *pretty = arg;Could do bool pretty = *(bool *)arg. Matter of taste. Same elsewhere.
Quoted 7 lines
Requires a cast. I like avoiding casts where C lets us do so. But I also agree that *pretty looks ugly, so maybe:
...(const void *arg)
{ const bool *data; bool pretty = *data;
Fewer casts are generally better, but so are fewer variables. Pick a solution you like. The one you chose for this patch is acceptable.
PATCH v4 26/28 qobject: Implement qobject_to_json() atop JSON visitor
In reply to Eric BlakeRather than open-code two different JSON visitors, it's nicer to make qobject_to_json() reuse the JSON output visitor. This also lets us pass QObject to any output visitor (passing it to the string output visitor will fail if structs are involved, but passing it to the QMP output visitor would provide a way to deep-clone a QObject).
---
v4: new patch, inspired by discussion on v3 12/18
--- include/qapi/qmp/qobject-json.h | 9 ++++ qobject/qobject-json.c | 108 +++++++++++----------------------------- 2 files changed, 39 insertions(+), 78 deletions(-)
include/qapi/qmp/qobject-json.h +9 -0
@@ -24,6 +24,15 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap) GCC_FMT_ATTR(1, 0);/* Convert the object to QString; does not fail. */ QString *qobject_to_json (const QObject *obj,bool pretty); +/* +* Visit the object with an output visitor @v. +* +* @name represents the relationship of this object to any parent +* (ignored for a top-level visit, must be set if part of a +* dictionary, should be NULL if part of a list). +*/ +void qobject_visit_output (Visitor *v,const char *name,const QObject *obj); +int qstring_append_json_string (QString *qstring,const char *str);int qstring_append_json_number (QString *qstring,double number);
qobject/qobject-json.c +30 -78
162 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 26/28] qobject: Implement qobject_to_json() atop JSON visitor
Quoted 6 lines · Eric Blake
Rather than open-code two different JSON visitors, it's nicer to make
qobject_to_json()reuse the JSON output visitor. This also lets us pass QObject to any output visitor (passing it to the string output visitor will fail if structs are involved, but passing it to the QMP output visitor would provide a way to deep-clone a QObject).
If I understand this correctly, the basic idea is as follows.
The JSON ouput visitor is normally used via QAPI-generated visit functions to convert a QAPI object (in general a tree) to JSON. This is a "real" visit in QAPI visitor parlance.
Note that the tree walk is in the QAPI-generated visit functions, and the actual conversion is in the JSON output visitor. This lets us reuse the latter with a different tree walk, namely one of a QObject (also a tree). This is a "virtual" visit in QAPI visitor parlance. The result is a replacement for qobject_to_json() that is shorter and avoids duplicating the conversion.
We can also use this QObject tree walk with other output visitors. The string output visitor isn't terribly interesting. With the (misnamed) QMP output visitor, we get a QObject deep clone.
Makes me wonder whether we have any QObject deep-clones in the tree we could replace now.
Quoted 67 lines
Signed-off-by Eric Blake <[email protected]>---
v4: new patch, inspired by discussion on v3 12/18--- include/qapi/qmp/qobject-json.h | 9 ++++ qobject/qobject-json.c | 108 +++++++++++----------------------------- 2 files changed, 39 insertions(+), 78 deletions(-)include/qapi/qmp/qobject-json.h +9
-0@@ -24,6 +24,15 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap) GCC_FMT_ATTR(1, 0);/* Convert the object to QString; does not fail. */ QString *qobject_to_json (const QObject *obj,bool pretty); +/* +* Visit the object with an output visitor @v. +* +* @name represents the relationship of this object to any parent +* (ignored for a top-level visit, must be set if part of a +* dictionary, should be NULL if part of a list). +*/ +void qobject_visit_output (Visitor *v,const char *name,const QObject *obj); +int qstring_append_json_string (QString *qstring,const char *str);int qstring_append_json_number (QString *qstring,double number);qobject/qobject-json.c +4
-16@@ -18,6 +18,9 @@#include "qapi/qmp/qobject-json.h" #include "qemu/unicode.h" #include "qapi/qmp/types.h" +#include "qapi/error.h" +#include "qapi/visitor.h" +#include "qapi/json-output-visitor.h" #include <math.h> typedef struct JSONParsingState@@ -69,115 +72,62 @@ QObject *qobject_from_jsonf(const char *string, ...)return obj; } -typedef struct ToJsonIterState -{ -int indent; -bool pretty; -int count; -QString *str; -}ToJsonIterState ; - -static void to_json (const QObject *obj,QString *str,bool pretty,int indent); -static void to_json_dict_iter (const char *key,QObject *obj,void *opaque) { -ToJsonIterState *s = opaque; +Visitor *v = opaque; -if (s->count) { -qstring_append (s->str, s->pretty ?"," :", " ); - } - -if (s->pretty) {
qobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 80
Quoted 10 lines
qobject/qobject-json.c +1
-8-qstring_append_printf (s->str,"\n%*s" ,4 * s->indent,"" ); - } - -qstring_append_json_string (s->str, key); - -qstring_append (s->str,": " ); -to_json (obj, s->str, s->pretty, s->indent); - s->count++; +qobject_visit_output (v, key, obj); }
This function is now a trivial wrapper. It's used like this:
qdict_iter(val, to_json_dict_iter, v);
You might want to replace it by something like
for (ent = qdict_first(val); ent; ent = qdict_next(val, ent)) {
qobject_visit_output(v, ent->key, ent->value);
}I'd find that easier to understand.
Quoted 8 lines
qobject/qobject-json.c +1
-3static void to_json_list_iter (QObject *obj,void *opaque) { -ToJsonIterState *s = opaque; +Visitor *v = opaque; -if (s->count) { -qstring_append (s->str, s->pretty ?"," :", " );
qobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 87
Quoted 10 lines
qobject/qobject-json.c +1
-8- } - -if (s->pretty) { -qstring_append_printf (s->str,"\n%*s" ,4 * s->indent,"" ); - } - -to_json (obj, s->str, s->pretty, s->indent); - s->count++; +qobject_visit_output (v,NULL , obj); }
Likewise.
Quoted 11 lines
qobject/qobject-json.c +2
-3-static void to_json (const QObject *obj,QString *str,bool pretty,int indent) +void qobject_visit_output (Visitor *v,const char *name,const QObject *obj) {switch (qobject_type (obj)) {case QTYPE_QNULL : -qstring_append (str,"null" ); +visit_type_null (v, name, &error_abort);break ;case QTYPE_QINT : { -QInt *val =qobject_to_qint (obj);
qobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 103
Quoted 10 lines
qobject/qobject-json.c +3
-2-qstring_append_printf (str,"%" PRId64,qint_get_int (val)); +int64_t val =qint_get_int (qobject_to_qint (obj)); +visit_type_int64 (v, name, &val, &error_abort);break ; }case QTYPE_QSTRING : { -QString *val =qobject_to_qstring (obj); +const char *str =qstring_get_str (qobject_to_qstring (obj));/* FIXME: no way inform user if we modified the string to * avoid encoding errors */
More precise: no way to inform the user if we replaced invalid UTF-8 sequences in the QString.
Quoted 82 lines
qobject/qobject-json.c +18
-38-qstring_append_json_string (str,qstring_get_str (val)); +visit_type_str (v, name, (char **)&str, &error_abort);break ; }case QTYPE_QDICT : { -ToJsonIterState s;QDict *val =qobject_to_qdict (obj); - - s.count =0 ; - s.str = str; - s.indent = indent +1 ; - s.pretty = pretty; -qstring_append_chr (str,'{' ); -qdict_iter (val, to_json_dict_iter, &s); -if (pretty) { -qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); - } -qstring_append_chr (str,'}' ); +visit_start_struct (v, name,NULL ,0 , &error_abort); +qdict_iter (val, to_json_dict_iter, v); +visit_check_struct (v, &error_abort); +visit_end_struct (v,NULL );break ; }case QTYPE_QLIST : { -ToJsonIterState s;QList *val =qobject_to_qlist (obj); - - s.count =0 ; - s.str = str; - s.indent = indent +1 ; - s.pretty = pretty; -qstring_append_chr (str,'[' ); -qlist_iter (val, (void *)to_json_list_iter, &s); -if (pretty) { -qstring_append_printf (str,"\n%*s" ,4 * indent,"" ); - } -qstring_append_chr (str,']' ); +visit_start_list (v, name,NULL ,0 , &error_abort); +qlist_iter (val, to_json_list_iter, v); +visit_end_list (v,NULL );break ; }case QTYPE_QFLOAT : { -QFloat *val =qobject_to_qfloat (obj); +double val =qfloat_get_double (qobject_to_qfloat (obj));/* FIXME: no way inform user if we generated invalid JSON */ -qstring_append_json_number (str,qfloat_get_double (val)); +visit_type_number (v, name, &val,NULL );break ; }case QTYPE_QBOOL : { -QBool *val =qobject_to_qbool (obj); - -if (qbool_get_bool (val)) { -qstring_append (str,"true" ); - }else { -qstring_append (str,"false" ); - } +bool val =qbool_get_bool (qobject_to_qbool (obj)); +visit_type_bool (v, name, &val, &error_abort);break ; }default :@@ -187,11 +137,13 @@ static void to_json(const QObject *obj, QString *str, bool pretty, int indent)QString *qobject_to_json (const QObject *obj,bool pretty) { -QString *str =qstring_new (); +char *str; +Visitor *v =json_output_visitor_new (pretty, &str); -to_json (obj, str, pretty,0 ); - -return str; +qobject_visit_output (v,NULL , obj); +visit_complete (v, &str); +visit_free (v); +return qstring_wrap_str (str); }/**
PATCH v4 27/28 qapi: Add 'any' support to JSON output
In reply to Eric BlakeNow that we can visit any QObject, it's easy to add support for visit_type_any() in the JSON output visitor.
---
v4: new patch, split out of v3 7/18, but made simpler by not requiring v3 12/18
--- include/qapi/json-output-visitor.h | 2 -- qapi/json-output-visitor.c | 7 ++++++ tests/test-json-output-visitor.c | 50 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-)
include/qapi/json-output-visitor.h +0 -2
@@ -23,8 +23,6 @@ typedef struct JsonOutputVisitor JsonOutputVisitor;* * If @pretty, make the output legible with newlines and indentation; * otherwise the output uses a single line. -* -* For now, this cannot be used to visit the 'any' type. */ Visitor *json_output_visitor_new (bool pretty,char **result);
qapi/json-output-visitor.c +7 -0
@@ -147,6 +147,12 @@ static void json_output_type_number(Visitor *v, const char *name, double *obj,qstring_append_json_number (jov->str, *obj); } +static void json_output_type_any (Visitor *v,const char *name,QObject **obj, +Error **errp) +{ +qobject_visit_output (v, name, *obj); +} +static void json_output_type_null (Visitor *v,const char *name,Error **errp) {JsonOutputVisitor *jov =to_jov (v);
@@ -196,6 +202,7 @@ Visitor *json_output_visitor_new(bool pretty, char **result) v->visitor.type_bool = json_output_type_bool; v->visitor.type_str = json_output_type_str; v->visitor.type_number = json_output_type_number; + v->visitor.type_any = json_output_type_any; v->visitor.type_null = json_output_type_null; v->visitor.complete = json_output_complete; v->visitor.free = json_output_free;
tests/test-json-output-visitor.c +50 -0
62 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 27/28] qapi: Add 'any' support to JSON output
Quoted 4 lines · Eric Blake
Now that we can visit any QObject, it's easy to add support for
visit_type_any()in the JSON output visitor.Signed-off-by Eric Blake <[email protected]>
Unlike in v3, this actually clones the QObject behind the any type. Much cleaner, and very little code due to your elegant use of visitors.
PATCH v4 28/28 qemu-img: Use new JSON output formatter
In reply to Eric BlakeNow that we can pretty-print straight to JSON from a visitor, we can eliminate the temporary conversion into QObject inside qemu-img.
The changes to qemu-iotests 043 expected output demonstrates the fact that output is now done in qapi declaration order, rather than QDict hash order.
---
v4: rebase to earlier changes
v3: rebase to master
v2: Drop RFC, adjust expected output of 043; rebase to 'name' motion
Overlaps with Fam's qemu-img edits, although he has expressed interest in getting this one in first.
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01756.html
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01806.html
--- qemu-img.c | 43 +++++++++++++++++-------------------------- tests/qemu-iotests/043.out | 22 +++++++++++----------- 2 files changed, 28 insertions(+), 37 deletions(-)
qemu-img.c +17 -26
@@ -24,9 +24,9 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "qapi-visit.h" -#include "qapi/qmp-output-visitor.h" +#include "qapi/json-output-visitor.h" #include "qapi/qmp/qerror.h" -#include "qapi/qmp/qobject-json.h" +#include "qapi/qmp/types.h" #include "qemu/cutils.h" #include "qemu/config-file.h" #include "qemu/option.h"
@@ -479,17 +479,14 @@ fail:static void dump_json_image_check (ImageCheck *check,bool quiet) { -QString *str; -QObject *obj; -Visitor *v =qmp_output_visitor_new (&obj); +char *str; +Visitor *v =json_output_visitor_new (true, &str);visit_type_ImageCheck (v,NULL , &check, &error_abort); -visit_complete (v, &obj); - str =qobject_to_json (obj, true); -qprintf (quiet,"%s\n" ,qstring_get_str (str)); -qobject_decref (obj); +visit_complete (v, &str); +qprintf (quiet,"%s\n" , str); +g_free (str);visit_free (v); -QDECREF (str); }static void dump_human_image_check (ImageCheck *check,bool quiet)
@@ -2168,32 +2165,26 @@ static void dump_snapshots(BlockDriverState *bs)static void dump_json_image_info_list (ImageInfoList *list) { -QString *str; -QObject *obj; -Visitor *v =qmp_output_visitor_new (&obj); +char *str; +Visitor *v =json_output_visitor_new (true, &str);visit_type_ImageInfoList (v,NULL , &list, &error_abort); -visit_complete (v, &obj); - str =qobject_to_json (obj, true); -printf ("%s\n" ,qstring_get_str (str)); -qobject_decref (obj); +visit_complete (v, &str); +printf ("%s\n" , str);visit_free (v); -QDECREF (str); +g_free (str); }static void dump_json_image_info (ImageInfo *info) { -QString *str; -QObject *obj; -Visitor *v =qmp_output_visitor_new (&obj); +char *str; +Visitor *v =json_output_visitor_new (true, &str);visit_type_ImageInfo (v,NULL , &info, &error_abort); -visit_complete (v, &obj); - str =qobject_to_json (obj, true); -printf ("%s\n" ,qstring_get_str (str)); -qobject_decref (obj); +visit_complete (v, &str); +printf ("%s\n" , str);visit_free (v); -QDECREF (str); +g_free (str); }static void dump_human_image_info_list (ImageInfoList *list)
tests/qemu-iotests/043.out +11 -11
@@ -40,29 +40,29 @@ cluster_size: 65536 == finite chain of length 3 (json) == [ { - "virtual-size": 134217728, "filename": "TEST_DIR/t.IMGFMT", - "cluster-size": 65536, "format": "IMGFMT", - "full-backing-filename": "TEST_DIR/t.IMGFMT.2.base", + "dirty-flag": false, + "virtual-size": 134217728, + "cluster-size": 65536, "backing-filename": "TEST_DIR/t.IMGFMT.2.base", - "dirty-flag": false + "full-backing-filename": "TEST_DIR/t.IMGFMT.2.base", }, { - "virtual-size": 134217728, "filename": "TEST_DIR/t.IMGFMT.2.base", - "cluster-size": 65536, "format": "IMGFMT", - "full-backing-filename": "TEST_DIR/t.IMGFMT.1.base", + "dirty-flag": false, + "virtual-size": 134217728, + "cluster-size": 65536, "backing-filename": "TEST_DIR/t.IMGFMT.1.base", - "dirty-flag": false + "full-backing-filename": "TEST_DIR/t.IMGFMT.1.base", }, { - "virtual-size": 134217728, "filename": "TEST_DIR/t.IMGFMT.1.base", - "cluster-size": 65536, "format": "IMGFMT", - "dirty-flag": false + "dirty-flag": false, + "virtual-size": 134217728, + "cluster-size": 65536, } ] *** done
Signature · 1 line
2.5.5
Quoted 10 lines · Eric Blake
Hard prerequisites (for all patches to apply):
Markus' qjson movement:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg00471.htmlSoft prerequisites (for valgrind to be happy with all touched tests):
My fix for memleak inrange.h:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg02666.html
My fix for memleak injson-streamer.c:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg03186.html
Forgot to mention: available as a tag at:
git fetch git://repo.or.cz/qemu/ericb.git qapi-jsonv4
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
[Qemu-devel] [PATCH v4 29/28] qapi: Add strict mode to JSON output visitor
In reply to Eric BlakeLet the caller decide whether output must be strict JSON (and raise an error on an attempt to output an encoding error or non-finite number), vs. the status quo of relaxed (encoding errors are rewritten to use substitute U+fffd characters, and non-finite numbers are output).
Adjust the testsuite to cover this: check-qobject-json checks relaxed mode (since qobject_to_json() is unchanged in behavior), test-qmp-output-visitor checks that QObject doesn't care about JSON restrictions, and test-json-output-visitor is now in strict mode and flags the errors.
---
v4: split out as new patch
[was parts of 04/18 and 07/18 in v3]
--- include/qapi/json-output-visitor.h | 7 ++++- include/qapi/qmp/qobject-json.h | 3 +- qapi/json-output-visitor.c | 19 ++++++++---- qemu-img.c | 6 ++-- qobject/qobject-json.c | 61 ++++++++++++++++++++++++-------------- tests/check-qobject-json.c | 15 ++++++++-- tests/test-json-output-visitor.c | 17 +++++++++-- tests/test-qmp-output-visitor.c | 17 +++++++++++ 8 files changed, 107 insertions(+), 38 deletions(-)
include/qapi/json-output-visitor.h +6 -1
@@ -23,7 +23,12 @@ typedef struct JsonOutputVisitor JsonOutputVisitor;* * If @pretty, make the output legible with newlines and indentation; * otherwise the output uses a single line. +* +* If @strict, attempts to output invalid JSON (strings not properly +* encoded in UTF-8, or Infinity or NaN as a number) will be treated +* as an error; otherwise, output is best-effort even if not pure +* JSON. */ -Visitor *json_output_visitor_new (bool pretty,char **result); +Visitor *json_output_visitor_new (bool pretty,bool strict,char **result);#endif
include/qapi/qmp/qobject-json.h +2 -1
@@ -31,7 +31,8 @@ QString *qobject_to_json(const QObject *obj, bool pretty);* (ignored for a top-level visit, must be set if part of a * dictionary, should be NULL if part of a list). */ -void qobject_visit_output (Visitor *v,const char *name,const QObject *obj); +void qobject_visit_output (Visitor *v,const char *name,const QObject *obj, +Error **errp);int qstring_append_json_string (QString *qstring,const char *str);int qstring_append_json_number (QString *qstring,double number);
qapi/json-output-visitor.c +13 -6
@@ -13,11 +13,13 @@#include "qapi/visitor-impl.h" #include "qapi/qmp/qstring.h" #include "qapi/qmp/qobject-json.h" +#include "qapi/error.h" struct JsonOutputVisitor {Visitor visitor;QString *str;bool pretty; +bool strict;bool comma;unsigned int depth;char **result;
@@ -133,8 +135,10 @@ static void json_output_type_str(Visitor *v, const char *name, char **obj,assert (*obj);json_output_name (jov, name); -/* FIXME: report invalid UTF-8 encoding */ -qstring_append_json_string (jov->str, *obj); +if (qstring_append_json_string (jov->str, *obj) <0 && jov->strict) { +error_setg (errp,"string value of '%s' is not valid UTF-8" , + name ? name :"null" ); + } } static void json_output_type_number(Visitor *v, const char *name, double *obj,
@@ -143,14 +147,16 @@ static void json_output_type_number(Visitor *v, const char *name, double *obj,JsonOutputVisitor *jov =to_jov (v);json_output_name (jov, name); -/* FIXME: report Inf/NaN problems */ -qstring_append_json_number (jov->str, *obj); +if (qstring_append_json_number (jov->str, *obj) <0 && jov->strict) { +error_setg (errp,"number value of '%s' is not finite" , + name ? name :"null" ); + } }static void json_output_type_any (Visitor *v,const char *name,QObject **obj,Error **errp) { -qobject_visit_output (v, name, *obj); +qobject_visit_output (v, name, *obj, errp); }static void json_output_type_null (Visitor *v,const char *name,Error **errp)
@@ -181,12 +187,13 @@ static void json_output_free(Visitor *v)g_free (jov); } -Visitor *json_output_visitor_new (bool pretty,char **result) +Visitor *json_output_visitor_new (bool pretty,bool strict,char **result) {JsonOutputVisitor *v; v =g_malloc0 (sizeof (*v)); v->pretty = pretty; + v->strict = strict; v->result = result; *result =NULL ; v->str =qstring_new ();
qemu-img.c +3 -3
@@ -480,7 +480,7 @@ fail:static void dump_json_image_check (ImageCheck *check,bool quiet) {char *str; -Visitor *v =json_output_visitor_new (true, &str); +Visitor *v =json_output_visitor_new (true, true, &str);visit_type_ImageCheck (v,NULL , &check, &error_abort);visit_complete (v, &str);
@@ -2166,7 +2166,7 @@ static void dump_snapshots(BlockDriverState *bs)static void dump_json_image_info_list (ImageInfoList *list) {char *str; -Visitor *v =json_output_visitor_new (true, &str); +Visitor *v =json_output_visitor_new (true, true, &str);visit_type_ImageInfoList (v,NULL , &list, &error_abort);visit_complete (v, &str);
@@ -2178,7 +2178,7 @@ static void dump_json_image_info_list(ImageInfoList *list)static void dump_json_image_info (ImageInfo *info) {char *str; -Visitor *v =json_output_visitor_new (true, &str); +Visitor *v =json_output_visitor_new (true, true, &str);visit_type_ImageInfo (v,NULL , &info, &error_abort);visit_complete (v, &str);
qobject/qobject-json.c +39 -22
118 lines not on this page · raw message
tests/check-qobject-json.c +12 -3
@@ -12,6 +12,7 @@*/ #include "qemu/osdep.h" #include <glib.h> +#include <math.h> #include "qapi/qmp/types.h" #include "qapi/qmp/qobject-json.h"
@@ -920,6 +921,8 @@ static void simple_number(void)static void float_number (void ) {int i; +QFloat *qfloat; +QString *str;struct {const char *encoded;double decoded;
@@ -934,7 +937,6 @@ static void float_number(void)for (i =0 ; test_cases[i].encoded; i++) {QObject *obj; -QFloat *qfloat; obj =qobject_from_json (test_cases[i].encoded);g_assert (obj !=NULL );
@@ -944,8 +946,6 @@ static void float_number(void)g_assert (qfloat_get_double (qfloat) == test_cases[i].decoded);if (test_cases[i].skip ==0 ) { -QString *str; - str =qobject_to_json (obj, false);g_assert (strcmp (qstring_get_str (str), test_cases[i].encoded) ==0 );QDECREF (str);
@@ -953,6 +953,15 @@ static void float_number(void)QDECREF (qfloat); } + +/* Although Infinity/NaN are not valid JSON, we can generate them +* on output as an extension. +* FIXME: The parser doesn't handle infinity/NaN on input. */ + qfloat =qfloat_from_double (INFINITY ); + str =qobject_to_json (QOBJECT (qfloat), false); + g_assert_cmpstr(qstring_get_str(str), ==, "inf"); +QDECREF (str); +QDECREF (qfloat); }static void vararg_number (void )
tests/test-json-output-visitor.c +15 -2
@@ -14,6 +14,7 @@#include "qemu/osdep.h" #include <glib.h> +#include <math.h> #include "qemu-common.h" #include "qapi/json-output-visitor.h"
@@ -32,7 +33,7 @@ static void visitor_output_setup(TestOutputVisitorData *data, const void *arg) {const bool *pretty = arg; - data->ov =json_output_visitor_new (*pretty, &data->str); + data->ov =json_output_visitor_new (*pretty, true, &data->str);g_assert (data->ov); data->pretty = *pretty; }
@@ -89,13 +90,18 @@ static void test_visitor_out_number(TestOutputVisitorData *data, {double value =3.14 ;const char *out; +Error *err =NULL ;visit_type_number (data->ov,NULL , &value, &error_abort); out =visitor_get (data); g_assert_cmpstr(out, ==, "3.14"); -/* FIXME: JSON requires finite numbers */ +/* JSON requires finite numbers */ +visitor_reset (data); + value =INFINITY ; +visit_type_number (data->ov,NULL , &value, &err); +error_free_or_abort (&err); } static void test_visitor_out_string(TestOutputVisitorData *data,
@@ -103,11 +109,18 @@ static void test_visitor_out_string(TestOutputVisitorData *data, {char *string = (char *)"Q E M U" ;const char *out; +Error *err =NULL ;visit_type_str (data->ov,NULL , &string, &error_abort); out =visitor_get (data); g_assert_cmpstr(out, ==, "\"Q E M U\""); + +/* JSON requires valid UTF-8 encodings */ +visitor_reset (data); + string = (char *)"\xfe" ; +visit_type_str (data->ov,NULL , &string, &err); +error_free_or_abort (&err); } static void test_visitor_out_enum(TestOutputVisitorData *data,
tests/test-qmp-output-visitor.c +17 -0
@@ -17,6 +17,7 @@#include "qemu/osdep.h" #include <glib.h> +#include <math.h> #include "qemu-common.h" #include "qapi/error.h"
@@ -97,6 +98,14 @@ static void test_visitor_out_number(TestOutputVisitorData *data, obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QFLOAT );g_assert (qfloat_get_double (qobject_to_qfloat (obj)) == value); + +/* While JSON requires finite values, QObject does not. */ +visitor_reset (data); + value =INFINITY ; +visit_type_number (data->ov,NULL , &value, &error_abort); + obj =visitor_get (data); +g_assert (qobject_type (obj) ==QTYPE_QFLOAT ); +g_assert (qfloat_get_double (qobject_to_qfloat (obj)) ==INFINITY ); } static void test_visitor_out_string(TestOutputVisitorData *data,
@@ -110,6 +119,14 @@ static void test_visitor_out_string(TestOutputVisitorData *data, obj =visitor_get (data);g_assert (qobject_type (obj) ==QTYPE_QSTRING ); g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); + +/* While JSON requires valid UTF-8 encoding, QObject does not. */ +visitor_reset (data); + string = (char *)"\xfe" ; +visit_type_str (data->ov,NULL , &string, &error_abort); + obj =visitor_get (data); +g_assert (qobject_type (obj) ==QTYPE_QSTRING ); + g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); } static void test_visitor_out_no_string(TestOutputVisitorData *data,
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH v4 29/28] qapi: Add strict mode to JSON output visitor
Quoted 17 lines · Eric Blake
Let the caller decide whether output must be strict JSON (and raise an error on an attempt to output an encoding error or non-finite number), vs. the status quo of relaxed (encoding errors are rewritten to use substitute U+fffd characters, and non-finite numbers are output).
Adjust the testsuite to cover this: check-qobject-json checks relaxed mode (since
qobject_to_json()is unchanged in behavior), test-qmp-output-visitor checks that QObject doesn't care about JSON restrictions, and test-json-output-visitor is now in strict mode and flags the errors.Signed-off-by Eric Blake <[email protected]>+++
b/qobject/qobject-json.c
qobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 77
Quoted 6 lines
qobject/qobject-json.c +3
-0@@ -72,62 +72,81 @@ QObject *qobject_from_jsonf(const char *string, ...)return obj; } +typedef struct ToJson { +{ + Visitor *v;
Uggh, posted the wrong version. checkpatch complained the { was on the wrong line, so I added it in the right one and then pushed before re-saving after deleting the duplicate one.
Actually, if this patch is worthwhile, I'm thinking that in v5, I'll minimize some of the churn and keep the ToJsonIterState struct that I removed in 26/28, even if it only has a single 'Visitor *v' member at that time, rather than completely removing it there to reinstate it here.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v4 29/28] qapi: Add strict mode to JSON output visitor
Quoted 29 lines · Eric Blake
Quoted 17 lines · Eric Blake
Let the caller decide whether output must be strict JSON (and raise an error on an attempt to output an encoding error or non-finite number), vs. the status quo of relaxed (encoding errors are rewritten to use substitute U+fffd characters, and non-finite numbers are output).
Adjust the testsuite to cover this: check-qobject-json checks relaxed mode (since
qobject_to_json()is unchanged in behavior), test-qmp-output-visitor checks that QObject doesn't care about JSON restrictions, and test-json-output-visitor is now in strict mode and flags the errors.Signed-off-by Eric Blake <[email protected]>+++
b/qobject/qobject-json.cqobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 77
Quoted 6 lines
qobject/qobject-json.c +3
-0@@ -72,62 +72,81 @@ QObject *qobject_from_jsonf(const char *string, ...)return obj; } +typedef struct ToJson { +{ + Visitor *v;Uggh, posted the wrong version. checkpatch complained the { was on the wrong line, so I added it in the right one and then pushed before re-saving after deleting the duplicate one.
Quoted 4 lines
Actually, if this patch is worthwhile, I'm thinking that in v5, I'll minimize some of the churn and keep the ToJsonIterState struct that I removed in 26/28, even if it only has a single 'Visitor *v' member at that time, rather than completely removing it there to reinstate it here.
I suspect you won't need it if you replace qdict_iter() by qdict_first(), qdict_next(), and qlist_iter() by QLIST_FOREACH_ENTRY.
Re: [Qemu-devel] [PATCH v4 29/28] qapi: Add strict mode to JSON output visitor
In reply to Eric BlakeQuoted 6 lines · Eric Blake
Let the caller decide whether output must be strict JSON (and raise an error on an attempt to output an encoding error or non-finite number), vs. the status quo of relaxed (encoding errors are rewritten to use substitute U+fffd characters, and non-finite numbers are output).
Quoted 7 lines
Adjust the testsuite to cover this: check-qobject-json checks relaxed mode (since
qobject_to_json()is unchanged in behavior), test-qmp-output-visitor checks that QObject doesn't care about JSON restrictions, and test-json-output-visitor is now in strict mode and flags the errors.Signed-off-by Eric Blake <[email protected]>
We need to decide whether QMP should keep extending JSON for numbers (see my review of PATCH 17), and how to treat invalid UTF-8. If the decision leads to two JSON dialects in QEMU, one strict and one QMP, we need this patch.
Quoted 117 lines
---
v4: split out as new patch
[was parts of 04/18 and 07/18 in v3]--- include/qapi/json-output-visitor.h | 7 ++++- include/qapi/qmp/qobject-json.h | 3 +- qapi/json-output-visitor.c | 19 ++++++++---- qemu-img.c | 6 ++-- qobject/qobject-json.c | 61 ++++++++++++++++++++++++-------------- tests/check-qobject-json.c | 15 ++++++++-- tests/test-json-output-visitor.c | 17 +++++++++-- tests/test-qmp-output-visitor.c | 17 +++++++++++ 8 files changed, 107 insertions(+), 38 deletions(-)include/qapi/json-output-visitor.h +6
-1@@ -23,7 +23,12 @@ typedef struct JsonOutputVisitor JsonOutputVisitor;* * If @pretty, make the output legible with newlines and indentation; * otherwise the output uses a single line. +* +* If @strict, attempts to output invalid JSON (strings not properly +* encoded in UTF-8, or Infinity or NaN as a number) will be treated +* as an error; otherwise, output is best-effort even if not pure +* JSON. */ -Visitor *json_output_visitor_new (bool pretty,char **result); +Visitor *json_output_visitor_new (bool pretty,bool strict,char **result);#endif include/qapi/qmp/qobject-json.h +2
-1@@ -31,7 +31,8 @@ QString *qobject_to_json(const QObject *obj, bool pretty);* (ignored for a top-level visit, must be set if part of a * dictionary, should be NULL if part of a list). */ -void qobject_visit_output (Visitor *v,const char *name,const QObject *obj); +void qobject_visit_output (Visitor *v,const char *name,const QObject *obj, +Error **errp);int qstring_append_json_string (QString *qstring,const char *str);int qstring_append_json_number (QString *qstring,double number);qapi/json-output-visitor.c +13
-6@@ -13,11 +13,13 @@#include "qapi/visitor-impl.h" #include "qapi/qmp/qstring.h" #include "qapi/qmp/qobject-json.h" +#include "qapi/error.h" struct JsonOutputVisitor {Visitor visitor;QString *str;bool pretty; +bool strict;bool comma;unsigned int depth;char **result;@@ -133,8 +135,10 @@ static void json_output_type_str(Visitor *v, const char *name, char **obj,assert (*obj);json_output_name (jov, name); -/* FIXME: report invalid UTF-8 encoding */ -qstring_append_json_string (jov->str, *obj); +if (qstring_append_json_string (jov->str, *obj) <0 && jov->strict) { +error_setg (errp,"string value of '%s' is not valid UTF-8" , + name ? name :"null" ); + } } static void json_output_type_number(Visitor *v, const char *name, double *obj,@@ -143,14 +147,16 @@ static void json_output_type_number(Visitor *v, const char *name, double *obj,JsonOutputVisitor *jov =to_jov (v);json_output_name (jov, name); -/* FIXME: report Inf/NaN problems */ -qstring_append_json_number (jov->str, *obj); +if (qstring_append_json_number (jov->str, *obj) <0 && jov->strict) { +error_setg (errp,"number value of '%s' is not finite" , + name ? name :"null" ); + } }static void json_output_type_any (Visitor *v,const char *name,QObject **obj,Error **errp) { -qobject_visit_output (v, name, *obj); +qobject_visit_output (v, name, *obj, errp); }static void json_output_type_null (Visitor *v,const char *name,Error **errp)@@ -181,12 +187,13 @@ static void json_output_free(Visitor *v)g_free (jov); } -Visitor *json_output_visitor_new (bool pretty,char **result) +Visitor *json_output_visitor_new (bool pretty,bool strict,char **result) {JsonOutputVisitor *v; v =g_malloc0 (sizeof (*v)); v->pretty = pretty; + v->strict = strict; v->result = result; *result =NULL ; v->str =qstring_new ();qemu-img.c +0
-0
qemu-img.c fail: · line 483
Quoted 5 lines
qemu-img.c +1
-1@@ -480,7 +480,7 @@ fail:static void dump_json_image_check (ImageCheck *check,bool quiet) {char *str; -Visitor *v =json_output_visitor_new (true, &str); +Visitor *v =json_output_visitor_new (true, true, &str);
Switch to strict not documented in the commit message. Is it a no-op?
More of the same below.
Quoted 82 lines
qemu-img.c +2
-2visit_type_ImageCheck (v,NULL , &check, &error_abort);visit_complete (v, &str);@@ -2166,7 +2166,7 @@ static void dump_snapshots(BlockDriverState *bs)static void dump_json_image_info_list (ImageInfoList *list) {char *str; -Visitor *v =json_output_visitor_new (true, &str); +Visitor *v =json_output_visitor_new (true, true, &str);visit_type_ImageInfoList (v,NULL , &list, &error_abort);visit_complete (v, &str);@@ -2178,7 +2178,7 @@ static void dump_json_image_info_list(ImageInfoList *list)static void dump_json_image_info (ImageInfo *info) {char *str; -Visitor *v =json_output_visitor_new (true, &str); +Visitor *v =json_output_visitor_new (true, true, &str);visit_type_ImageInfo (v,NULL , &info, &error_abort);visit_complete (v, &str);qobject/qobject-json.c +22
-10@@ -72,62 +72,81 @@ QObject *qobject_from_jsonf(const char *string, ...)return obj; } +typedef struct ToJson { +{ +Visitor *v; +Error **errp; +}ToJson ; +static void to_json_dict_iter (const char *key,QObject *obj,void *opaque) { -Visitor *v = opaque; +ToJson *s = opaque; -qobject_visit_output (v, key, obj); +if (!*s->errp) { +qobject_visit_output (s->v, key, obj, s->errp); + } }static void to_json_list_iter (QObject *obj,void *opaque) { -Visitor *v = opaque; +ToJson *s = opaque; -qobject_visit_output (v,NULL , obj); +if (!*s->errp) { +qobject_visit_output (s->v,NULL , obj, s->errp); + } } -void qobject_visit_output (Visitor *v,const char *name,const QObject *obj) +void qobject_visit_output (Visitor *v,const char *name,const QObject *obj, +Error **errp) { +Error *err =NULL ; +ToJson s = { .v = v, .errp = &err, }; +switch (qobject_type (obj)) {case QTYPE_QNULL : -visit_type_null (v, name, &error_abort); +visit_type_null (v, name, errp);break ;case QTYPE_QINT : {int64_t val =qint_get_int (qobject_to_qint (obj)); -visit_type_int64 (v, name, &val, &error_abort); +visit_type_int64 (v, name, &val, errp);break ; }case QTYPE_QSTRING : {const char *str =qstring_get_str (qobject_to_qstring (obj)); -/* FIXME: no way inform user if we modified the string to -* avoid encoding errors */ -visit_type_str (v, name, (char **)&str, &error_abort); +visit_type_str (v, name, (char **)&str, errp);break ;
qobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 124
Quoted 10 lines
qobject/qobject-json.c +4
-3}case QTYPE_QDICT : {QDict *val =qobject_to_qdict (obj); -visit_start_struct (v, name,NULL ,0 , &error_abort); -qdict_iter (val, to_json_dict_iter, v); -visit_check_struct (v, &error_abort); +visit_start_struct (v, name,NULL ,0 , &err); +if (!err) { +qdict_iter (val, to_json_dict_iter, &s); + }
for (ent = qdict_first(val); ent; ent = qdict_next(val, ent)) {
qobject_visit_output(v, ent->key, ent->value, &err);
}Quoted 4 lines
qobject/qobject-json.c +4
-0+if (!err) { +visit_check_struct (v, &err); + } +error_propagate (errp, err);
qobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 136
Quoted 10 lines
qobject/qobject-json.c +3
-2visit_end_struct (v,NULL );break ; }case QTYPE_QLIST : {QList *val =qobject_to_qlist (obj); -visit_start_list (v, name,NULL ,0 , &error_abort); -qlist_iter (val, to_json_list_iter, v); +visit_start_list (v, name,NULL ,0 , &err); +if (!err) { +qlist_iter (val, to_json_list_iter, &s);
QLIST_FOREACH_ENTRY(val, ent) {
qobject_visit_output(s->v, NULL, obj, errp);
}qobject/qobject-json.c QObject *qobject_from_jsonf(const char *string, ...) · line 144
Quoted 10 lines
qobject/qobject-json.c +3
-2+ } +error_propagate (errp, err);visit_end_list (v,NULL );break ; }case QTYPE_QFLOAT : {double val =qfloat_get_double (qobject_to_qfloat (obj)); -/* FIXME: no way inform user if we generated invalid JSON */ -visit_type_number (v, name, &val,NULL ); +visit_type_number (v, name, &val, errp);
Just noticed the pre-patch difference NULL here and &error_abort elsewhere. I'd use &error_abort here, too [PATCH 26].
Quoted 22 lines
qobject/qobject-json.c +3
-3break ; }case QTYPE_QBOOL : {bool val =qbool_get_bool (qobject_to_qbool (obj)); -visit_type_bool (v, name, &val, &error_abort); +visit_type_bool (v, name, &val, errp);break ; }default :@@ -138,9 +157,9 @@ void qobject_visit_output(Visitor *v, const char *name, const QObject *obj)QString *qobject_to_json (const QObject *obj,bool pretty) {char *str; -Visitor *v =json_output_visitor_new (pretty, &str); +Visitor *v =json_output_visitor_new (pretty, false, &str); -qobject_visit_output (v,NULL , obj); +qobject_visit_output (v,NULL , obj, &error_abort);visit_complete (v, &str);visit_free (v);return qstring_wrap_str (str);
qobject/qobject-json.c int qstring_append_json_number(QString *qstring, double number) · line 226
Quoted 5 lines
qobject/qobject-json.c +0
-2@@ -222,8 +241,6 @@ int qstring_append_json_number(QString *qstring, double number)/* FIXME: snprintf() is locale dependent; but JSON requires * numbers to be formatted as if in the C locale. Dependence * on C locale is a pervasive issue in QEMU. */ -/* FIXME: This risks printing Inf or NaN, which are not valid -* JSON values. */
I think this one should be dropped in PATCH 17, where the invalid JSON becomes a documented interface option.
Quoted 3 lines
qobject/qobject-json.c +0
-0/* FIXME: the default precision of 6 for %f often causes * rounding errors; we should be using DBL_DECIMAL_DIG (17), * and only rounding to a shorter number if the result would
[...]
Meant to inquire about the next batch, but of course you beat me to the punch :)
I'll be on vacation next week, so review will be delayed.
Most patches are ready as is or with minor touch-ups.
I'd probably drop PATCH 01. The file name collision no longer exists, and moving files just to improve their names a bit doesn't seem worth the bother.
There are memory leaks in PATCH 12 and 24 (see review of PATCH 24).
PATCH 13 needs more work than I'm comfortable to do on commit. Mostly missing comment updates, but also a crash bug.
On PATCH 21, I have minor interface design doubts.
The only difficult question is what to do about invalid UTF-8 strings and non-finite numbers. Patches before PATCH 29 show opinions, but a decision isn't made until PATCH 29. Good, because it leaves most of this series not blocked by the debate on what we should do.
I guess I could take PATCH 02-12 now, and let you respin the rest. But perhaps its easier if you respin all of them.
Quoted 5 lines · Markus Armbruster
Most patches are ready as is or with minor touch-ups.
I'd probably drop PATCH 01. The file name collision no longer exists, and moving files just to improve their names a bit doesn't seem worth the bother.
It's a bit more churn to the rest of the series, but I can live with it. It's also more related to the JSON visitor than the clone visitor, so even if I do want to keep it, it makes more sense to keep it with the second half of the series.
Quoted 5 lines
There are memory leaks in PATCH 12 and 24 (see review of PATCH 24).
PATCH 13 needs more work than I'm comfortable to do on commit. Mostly missing comment updates, but also a crash bug.
I've got that fix ready to post.
Quoted 10 lines
On PATCH 21, I have minor interface design doubts.
The only difficult question is what to do about invalid UTF-8 strings and non-finite numbers. Patches before PATCH 29 show opinions, but a decision isn't made until PATCH 29. Good, because it leaves most of this series not blocked by the debate on what we should do.
I guess I could take PATCH 02-12 now, and let you respin the rest. But perhaps its easier if you respin all of them.
I'm posting v5 of 2-15 now, then focusing on your comments on the JSON visitor for a later posting.
I also think my subset F series (adding a 'box' parameter) is relatively orthogonal, if you want to start reviewing that:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg03569.html
I'd still LOVE to get blockdev-add working for ALL block devices before 2.7 soft freeze (there's patches for NBD that I need to review, and I know the gluster folks were waiting on qapi patches to be able to use a SocketAddress union as the basis for the gluster branch of the blockdev-add union - and my qapi patches are almost all the way there to permitting it).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 11 lines · Eric Blake
Quoted 5 lines · Markus Armbruster
Most patches are ready as is or with minor touch-ups.
I'd probably drop PATCH 01. The file name collision no longer exists, and moving files just to improve their names a bit doesn't seem worth the bother.
It's a bit more churn to the rest of the series, but I can live with it. It's also more related to the JSON visitor than the clone visitor, so even if I do want to keep it, it makes more sense to keep it with the second half of the series.
Thanks.
Quoted 15 lines
Quoted 4 lines
There are memory leaks in PATCH 12 and 24 (see review of PATCH 24).
PATCH 13 needs more work than I'm comfortable to do on commit. Mostly missing comment updates, but also a crash bug.
I've got that fix ready to post.
Quoted 8 lines
On PATCH 21, I have minor interface design doubts.
The only difficult question is what to do about invalid UTF-8 strings and non-finite numbers. Patches before PATCH 29 show opinions, but a decision isn't made until PATCH 29. Good, because it leaves most of this series not blocked by the debate on what we should do.
Quoted 5 lines
Quoted 2 lines
I guess I could take PATCH 02-12 now, and let you respin the rest. But perhaps its easier if you respin all of them.
I'm posting v5 of 2-15 now, then focusing on your comments on the JSON visitor for a later posting.
Posted as "[PATCH v5 00/15] Add clone visitor". Will review as soon as I can.
Quoted 10 lines
I also think my subset F series (adding a 'box' parameter) is relatively orthogonal, if you want to start reviewing that:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg03569.htmlI'd still LOVE to get blockdev-add working for ALL block devices before 2.7 soft freeze (there's patches for NBD that I need to review, and I know the gluster folks were waiting on qapi patches to be able to use a SocketAddress union as the basis for the gluster branch of the blockdev-add union - and my qapi patches are almost all the way there to permitting it).
I'd very much like to finish this huge task, too. Let's try.