]> Git Repo - qemu.git/blob - qapi/qmp-output-visitor.c
drive-backup: added support for data compression
[qemu.git] / qapi / qmp-output-visitor.c
1 /*
2  * Core Definitions for QAPI/QMP Command Registry
3  *
4  * Copyright (C) 2012-2016 Red Hat, Inc.
5  * Copyright IBM, Corp. 2011
6  *
7  * Authors:
8  *  Anthony Liguori   <[email protected]>
9  *
10  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11  * See the COPYING.LIB file in the top-level directory.
12  *
13  */
14
15 #include "qemu/osdep.h"
16 #include "qapi/qmp-output-visitor.h"
17 #include "qapi/visitor-impl.h"
18 #include "qemu/queue.h"
19 #include "qemu-common.h"
20 #include "qapi/qmp/types.h"
21
22 typedef struct QStackEntry
23 {
24     QObject *value;
25     void *qapi; /* sanity check that caller uses same pointer */
26     QSLIST_ENTRY(QStackEntry) node;
27 } QStackEntry;
28
29 struct QmpOutputVisitor
30 {
31     Visitor visitor;
32     QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
33     QObject *root; /* Root of the output visit */
34     QObject **result; /* User's storage location for result */
35 };
36
37 #define qmp_output_add(qov, name, value) \
38     qmp_output_add_obj(qov, name, QOBJECT(value))
39 #define qmp_output_push(qov, value, qapi) \
40     qmp_output_push_obj(qov, QOBJECT(value), qapi)
41
42 static QmpOutputVisitor *to_qov(Visitor *v)
43 {
44     return container_of(v, QmpOutputVisitor, visitor);
45 }
46
47 /* Push @value onto the stack of current QObjects being built */
48 static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
49                                 void *qapi)
50 {
51     QStackEntry *e = g_malloc0(sizeof(*e));
52
53     assert(qov->root);
54     assert(value);
55     e->value = value;
56     e->qapi = qapi;
57     QSLIST_INSERT_HEAD(&qov->stack, e, node);
58 }
59
60 /* Pop a value off the stack of QObjects being built, and return it. */
61 static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
62 {
63     QStackEntry *e = QSLIST_FIRST(&qov->stack);
64     QObject *value;
65
66     assert(e);
67     assert(e->qapi == qapi);
68     QSLIST_REMOVE_HEAD(&qov->stack, node);
69     value = e->value;
70     assert(value);
71     g_free(e);
72     return value;
73 }
74
75 /* Add @value to the current QObject being built.
76  * If the stack is visiting a dictionary or list, @value is now owned
77  * by that container. Otherwise, @value is now the root.  */
78 static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
79                                QObject *value)
80 {
81     QStackEntry *e = QSLIST_FIRST(&qov->stack);
82     QObject *cur = e ? e->value : NULL;
83
84     if (!cur) {
85         /* Don't allow reuse of visitor on more than one root */
86         assert(!qov->root);
87         qov->root = value;
88     } else {
89         switch (qobject_type(cur)) {
90         case QTYPE_QDICT:
91             assert(name);
92             qdict_put_obj(qobject_to_qdict(cur), name, value);
93             break;
94         case QTYPE_QLIST:
95             assert(!name);
96             qlist_append_obj(qobject_to_qlist(cur), value);
97             break;
98         default:
99             g_assert_not_reached();
100         }
101     }
102 }
103
104 static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
105                                     size_t unused, Error **errp)
106 {
107     QmpOutputVisitor *qov = to_qov(v);
108     QDict *dict = qdict_new();
109
110     qmp_output_add(qov, name, dict);
111     qmp_output_push(qov, dict, obj);
112 }
113
114 static void qmp_output_end_struct(Visitor *v, void **obj)
115 {
116     QmpOutputVisitor *qov = to_qov(v);
117     QObject *value = qmp_output_pop(qov, obj);
118     assert(qobject_type(value) == QTYPE_QDICT);
119 }
120
121 static void qmp_output_start_list(Visitor *v, const char *name,
122                                   GenericList **listp, size_t size,
123                                   Error **errp)
124 {
125     QmpOutputVisitor *qov = to_qov(v);
126     QList *list = qlist_new();
127
128     qmp_output_add(qov, name, list);
129     qmp_output_push(qov, list, listp);
130 }
131
132 static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
133                                          size_t size)
134 {
135     return tail->next;
136 }
137
138 static void qmp_output_end_list(Visitor *v, void **obj)
139 {
140     QmpOutputVisitor *qov = to_qov(v);
141     QObject *value = qmp_output_pop(qov, obj);
142     assert(qobject_type(value) == QTYPE_QLIST);
143 }
144
145 static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
146                                   Error **errp)
147 {
148     QmpOutputVisitor *qov = to_qov(v);
149     qmp_output_add(qov, name, qint_from_int(*obj));
150 }
151
152 static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
153                                    Error **errp)
154 {
155     /* FIXME: QMP outputs values larger than INT64_MAX as negative */
156     QmpOutputVisitor *qov = to_qov(v);
157     qmp_output_add(qov, name, qint_from_int(*obj));
158 }
159
160 static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
161                                  Error **errp)
162 {
163     QmpOutputVisitor *qov = to_qov(v);
164     qmp_output_add(qov, name, qbool_from_bool(*obj));
165 }
166
167 static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
168                                 Error **errp)
169 {
170     QmpOutputVisitor *qov = to_qov(v);
171     if (*obj) {
172         qmp_output_add(qov, name, qstring_from_str(*obj));
173     } else {
174         qmp_output_add(qov, name, qstring_from_str(""));
175     }
176 }
177
178 static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
179                                    Error **errp)
180 {
181     QmpOutputVisitor *qov = to_qov(v);
182     qmp_output_add(qov, name, qfloat_from_double(*obj));
183 }
184
185 static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
186                                 Error **errp)
187 {
188     QmpOutputVisitor *qov = to_qov(v);
189     qobject_incref(*obj);
190     qmp_output_add_obj(qov, name, *obj);
191 }
192
193 static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
194 {
195     QmpOutputVisitor *qov = to_qov(v);
196     qmp_output_add_obj(qov, name, qnull());
197 }
198
199 /* Finish building, and return the root object.
200  * The root object is never null. The caller becomes the object's
201  * owner, and should use qobject_decref() when done with it.  */
202 static void qmp_output_complete(Visitor *v, void *opaque)
203 {
204     QmpOutputVisitor *qov = to_qov(v);
205
206     /* A visit must have occurred, with each start paired with end.  */
207     assert(qov->root && QSLIST_EMPTY(&qov->stack));
208     assert(opaque == qov->result);
209
210     qobject_incref(qov->root);
211     *qov->result = qov->root;
212     qov->result = NULL;
213 }
214
215 static void qmp_output_free(Visitor *v)
216 {
217     QmpOutputVisitor *qov = to_qov(v);
218     QStackEntry *e;
219
220     while (!QSLIST_EMPTY(&qov->stack)) {
221         e = QSLIST_FIRST(&qov->stack);
222         QSLIST_REMOVE_HEAD(&qov->stack, node);
223         g_free(e);
224     }
225
226     qobject_decref(qov->root);
227     g_free(qov);
228 }
229
230 Visitor *qmp_output_visitor_new(QObject **result)
231 {
232     QmpOutputVisitor *v;
233
234     v = g_malloc0(sizeof(*v));
235
236     v->visitor.type = VISITOR_OUTPUT;
237     v->visitor.start_struct = qmp_output_start_struct;
238     v->visitor.end_struct = qmp_output_end_struct;
239     v->visitor.start_list = qmp_output_start_list;
240     v->visitor.next_list = qmp_output_next_list;
241     v->visitor.end_list = qmp_output_end_list;
242     v->visitor.type_int64 = qmp_output_type_int64;
243     v->visitor.type_uint64 = qmp_output_type_uint64;
244     v->visitor.type_bool = qmp_output_type_bool;
245     v->visitor.type_str = qmp_output_type_str;
246     v->visitor.type_number = qmp_output_type_number;
247     v->visitor.type_any = qmp_output_type_any;
248     v->visitor.type_null = qmp_output_type_null;
249     v->visitor.complete = qmp_output_complete;
250     v->visitor.free = qmp_output_free;
251
252     *result = NULL;
253     v->result = result;
254
255     return &v->visitor;
256 }
This page took 0.038531 seconds and 4 git commands to generate.