]> git.proxmox.com Git - mirror_qemu.git/blob - qapi/qmp-output-visitor.c
qmp-output-visitor: Favor new visit_free() function
[mirror_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 <aliguori@us.ibm.com>
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 QTAILQ_ENTRY(QStackEntry) node;
27 } QStackEntry;
28
29 typedef QTAILQ_HEAD(QStack, QStackEntry) QStack;
30
31 struct QmpOutputVisitor
32 {
33 Visitor visitor;
34 QStack stack; /* Stack of containers that haven't yet been finished */
35 QObject *root; /* Root of the output visit */
36 };
37
38 #define qmp_output_add(qov, name, value) \
39 qmp_output_add_obj(qov, name, QOBJECT(value))
40 #define qmp_output_push(qov, value, qapi) \
41 qmp_output_push_obj(qov, QOBJECT(value), qapi)
42
43 static QmpOutputVisitor *to_qov(Visitor *v)
44 {
45 return container_of(v, QmpOutputVisitor, visitor);
46 }
47
48 /* Push @value onto the stack of current QObjects being built */
49 static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
50 void *qapi)
51 {
52 QStackEntry *e = g_malloc0(sizeof(*e));
53
54 assert(qov->root);
55 assert(value);
56 e->value = value;
57 e->qapi = qapi;
58 QTAILQ_INSERT_HEAD(&qov->stack, e, node);
59 }
60
61 /* Pop a value off the stack of QObjects being built, and return it. */
62 static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
63 {
64 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
65 QObject *value;
66
67 assert(e);
68 assert(e->qapi == qapi);
69 QTAILQ_REMOVE(&qov->stack, e, node);
70 value = e->value;
71 assert(value);
72 g_free(e);
73 return value;
74 }
75
76 /* Add @value to the current QObject being built.
77 * If the stack is visiting a dictionary or list, @value is now owned
78 * by that container. Otherwise, @value is now the root. */
79 static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
80 QObject *value)
81 {
82 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
83 QObject *cur = e ? e->value : NULL;
84
85 if (!cur) {
86 /* Don't allow reuse of visitor on more than one root */
87 assert(!qov->root);
88 qov->root = value;
89 } else {
90 switch (qobject_type(cur)) {
91 case QTYPE_QDICT:
92 assert(name);
93 qdict_put_obj(qobject_to_qdict(cur), name, value);
94 break;
95 case QTYPE_QLIST:
96 assert(!name);
97 qlist_append_obj(qobject_to_qlist(cur), value);
98 break;
99 default:
100 g_assert_not_reached();
101 }
102 }
103 }
104
105 static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
106 size_t unused, Error **errp)
107 {
108 QmpOutputVisitor *qov = to_qov(v);
109 QDict *dict = qdict_new();
110
111 qmp_output_add(qov, name, dict);
112 qmp_output_push(qov, dict, obj);
113 }
114
115 static void qmp_output_end_struct(Visitor *v, void **obj)
116 {
117 QmpOutputVisitor *qov = to_qov(v);
118 QObject *value = qmp_output_pop(qov, obj);
119 assert(qobject_type(value) == QTYPE_QDICT);
120 }
121
122 static void qmp_output_start_list(Visitor *v, const char *name,
123 GenericList **listp, size_t size,
124 Error **errp)
125 {
126 QmpOutputVisitor *qov = to_qov(v);
127 QList *list = qlist_new();
128
129 qmp_output_add(qov, name, list);
130 qmp_output_push(qov, list, listp);
131 }
132
133 static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
134 size_t size)
135 {
136 return tail->next;
137 }
138
139 static void qmp_output_end_list(Visitor *v, void **obj)
140 {
141 QmpOutputVisitor *qov = to_qov(v);
142 QObject *value = qmp_output_pop(qov, obj);
143 assert(qobject_type(value) == QTYPE_QLIST);
144 }
145
146 static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
147 Error **errp)
148 {
149 QmpOutputVisitor *qov = to_qov(v);
150 qmp_output_add(qov, name, qint_from_int(*obj));
151 }
152
153 static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
154 Error **errp)
155 {
156 /* FIXME: QMP outputs values larger than INT64_MAX as negative */
157 QmpOutputVisitor *qov = to_qov(v);
158 qmp_output_add(qov, name, qint_from_int(*obj));
159 }
160
161 static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
162 Error **errp)
163 {
164 QmpOutputVisitor *qov = to_qov(v);
165 qmp_output_add(qov, name, qbool_from_bool(*obj));
166 }
167
168 static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
169 Error **errp)
170 {
171 QmpOutputVisitor *qov = to_qov(v);
172 if (*obj) {
173 qmp_output_add(qov, name, qstring_from_str(*obj));
174 } else {
175 qmp_output_add(qov, name, qstring_from_str(""));
176 }
177 }
178
179 static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
180 Error **errp)
181 {
182 QmpOutputVisitor *qov = to_qov(v);
183 qmp_output_add(qov, name, qfloat_from_double(*obj));
184 }
185
186 static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
187 Error **errp)
188 {
189 QmpOutputVisitor *qov = to_qov(v);
190 qobject_incref(*obj);
191 qmp_output_add_obj(qov, name, *obj);
192 }
193
194 static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
195 {
196 QmpOutputVisitor *qov = to_qov(v);
197 qmp_output_add_obj(qov, name, qnull());
198 }
199
200 /* Finish building, and return the root object.
201 * The root object is never null. The caller becomes the object's
202 * owner, and should use qobject_decref() when done with it. */
203 QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
204 {
205 /* A visit must have occurred, with each start paired with end. */
206 assert(qov->root && QTAILQ_EMPTY(&qov->stack));
207
208 qobject_incref(qov->root);
209 return qov->root;
210 }
211
212 Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)
213 {
214 return &v->visitor;
215 }
216
217 static void qmp_output_free(Visitor *v)
218 {
219 QmpOutputVisitor *qov = to_qov(v);
220 QStackEntry *e, *tmp;
221
222 QTAILQ_FOREACH_SAFE(e, &qov->stack, node, tmp) {
223 QTAILQ_REMOVE(&qov->stack, e, node);
224 g_free(e);
225 }
226
227 qobject_decref(qov->root);
228 g_free(qov);
229 }
230
231 QmpOutputVisitor *qmp_output_visitor_new(void)
232 {
233 QmpOutputVisitor *v;
234
235 v = g_malloc0(sizeof(*v));
236
237 v->visitor.type = VISITOR_OUTPUT;
238 v->visitor.start_struct = qmp_output_start_struct;
239 v->visitor.end_struct = qmp_output_end_struct;
240 v->visitor.start_list = qmp_output_start_list;
241 v->visitor.next_list = qmp_output_next_list;
242 v->visitor.end_list = qmp_output_end_list;
243 v->visitor.type_int64 = qmp_output_type_int64;
244 v->visitor.type_uint64 = qmp_output_type_uint64;
245 v->visitor.type_bool = qmp_output_type_bool;
246 v->visitor.type_str = qmp_output_type_str;
247 v->visitor.type_number = qmp_output_type_number;
248 v->visitor.type_any = qmp_output_type_any;
249 v->visitor.type_null = qmp_output_type_null;
250 v->visitor.free = qmp_output_free;
251
252 QTAILQ_INIT(&v->stack);
253
254 return v;
255 }