]> Git Repo - qemu.git/blob - qapi/qmp-output-visitor.c
Merge remote-tracking branch 'remotes/xtensa/tags/20160714-xtensa' into staging
[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     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     QObject **result; /* User's storage location for result */
37 };
38
39 #define qmp_output_add(qov, name, value) \
40     qmp_output_add_obj(qov, name, QOBJECT(value))
41 #define qmp_output_push(qov, value, qapi) \
42     qmp_output_push_obj(qov, QOBJECT(value), qapi)
43
44 static QmpOutputVisitor *to_qov(Visitor *v)
45 {
46     return container_of(v, QmpOutputVisitor, visitor);
47 }
48
49 /* Push @value onto the stack of current QObjects being built */
50 static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value,
51                                 void *qapi)
52 {
53     QStackEntry *e = g_malloc0(sizeof(*e));
54
55     assert(qov->root);
56     assert(value);
57     e->value = value;
58     e->qapi = qapi;
59     QTAILQ_INSERT_HEAD(&qov->stack, e, node);
60 }
61
62 /* Pop a value off the stack of QObjects being built, and return it. */
63 static QObject *qmp_output_pop(QmpOutputVisitor *qov, void *qapi)
64 {
65     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
66     QObject *value;
67
68     assert(e);
69     assert(e->qapi == qapi);
70     QTAILQ_REMOVE(&qov->stack, e, node);
71     value = e->value;
72     assert(value);
73     g_free(e);
74     return value;
75 }
76
77 /* Add @value to the current QObject being built.
78  * If the stack is visiting a dictionary or list, @value is now owned
79  * by that container. Otherwise, @value is now the root.  */
80 static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
81                                QObject *value)
82 {
83     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
84     QObject *cur = e ? e->value : NULL;
85
86     if (!cur) {
87         /* Don't allow reuse of visitor on more than one root */
88         assert(!qov->root);
89         qov->root = value;
90     } else {
91         switch (qobject_type(cur)) {
92         case QTYPE_QDICT:
93             assert(name);
94             qdict_put_obj(qobject_to_qdict(cur), name, value);
95             break;
96         case QTYPE_QLIST:
97             assert(!name);
98             qlist_append_obj(qobject_to_qlist(cur), value);
99             break;
100         default:
101             g_assert_not_reached();
102         }
103     }
104 }
105
106 static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
107                                     size_t unused, Error **errp)
108 {
109     QmpOutputVisitor *qov = to_qov(v);
110     QDict *dict = qdict_new();
111
112     qmp_output_add(qov, name, dict);
113     qmp_output_push(qov, dict, obj);
114 }
115
116 static void qmp_output_end_struct(Visitor *v, void **obj)
117 {
118     QmpOutputVisitor *qov = to_qov(v);
119     QObject *value = qmp_output_pop(qov, obj);
120     assert(qobject_type(value) == QTYPE_QDICT);
121 }
122
123 static void qmp_output_start_list(Visitor *v, const char *name,
124                                   GenericList **listp, size_t size,
125                                   Error **errp)
126 {
127     QmpOutputVisitor *qov = to_qov(v);
128     QList *list = qlist_new();
129
130     qmp_output_add(qov, name, list);
131     qmp_output_push(qov, list, listp);
132 }
133
134 static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,
135                                          size_t size)
136 {
137     return tail->next;
138 }
139
140 static void qmp_output_end_list(Visitor *v, void **obj)
141 {
142     QmpOutputVisitor *qov = to_qov(v);
143     QObject *value = qmp_output_pop(qov, obj);
144     assert(qobject_type(value) == QTYPE_QLIST);
145 }
146
147 static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
148                                   Error **errp)
149 {
150     QmpOutputVisitor *qov = to_qov(v);
151     qmp_output_add(qov, name, qint_from_int(*obj));
152 }
153
154 static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
155                                    Error **errp)
156 {
157     /* FIXME: QMP outputs values larger than INT64_MAX as negative */
158     QmpOutputVisitor *qov = to_qov(v);
159     qmp_output_add(qov, name, qint_from_int(*obj));
160 }
161
162 static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
163                                  Error **errp)
164 {
165     QmpOutputVisitor *qov = to_qov(v);
166     qmp_output_add(qov, name, qbool_from_bool(*obj));
167 }
168
169 static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
170                                 Error **errp)
171 {
172     QmpOutputVisitor *qov = to_qov(v);
173     if (*obj) {
174         qmp_output_add(qov, name, qstring_from_str(*obj));
175     } else {
176         qmp_output_add(qov, name, qstring_from_str(""));
177     }
178 }
179
180 static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
181                                    Error **errp)
182 {
183     QmpOutputVisitor *qov = to_qov(v);
184     qmp_output_add(qov, name, qfloat_from_double(*obj));
185 }
186
187 static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
188                                 Error **errp)
189 {
190     QmpOutputVisitor *qov = to_qov(v);
191     qobject_incref(*obj);
192     qmp_output_add_obj(qov, name, *obj);
193 }
194
195 static void qmp_output_type_null(Visitor *v, const char *name, Error **errp)
196 {
197     QmpOutputVisitor *qov = to_qov(v);
198     qmp_output_add_obj(qov, name, qnull());
199 }
200
201 /* Finish building, and return the root object.
202  * The root object is never null. The caller becomes the object's
203  * owner, and should use qobject_decref() when done with it.  */
204 static void qmp_output_complete(Visitor *v, void *opaque)
205 {
206     QmpOutputVisitor *qov = to_qov(v);
207
208     /* A visit must have occurred, with each start paired with end.  */
209     assert(qov->root && QTAILQ_EMPTY(&qov->stack));
210     assert(opaque == qov->result);
211
212     qobject_incref(qov->root);
213     *qov->result = qov->root;
214     qov->result = NULL;
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 Visitor *qmp_output_visitor_new(QObject **result)
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.complete = qmp_output_complete;
251     v->visitor.free = qmp_output_free;
252
253     QTAILQ_INIT(&v->stack);
254     *result = NULL;
255     v->result = result;
256
257     return &v->visitor;
258 }
This page took 0.040044 seconds and 4 git commands to generate.