qed: fix use-after-free during l2 cache commit
[qemu/wangdongxu.git] / qapi / qmp-input-visitor.c
blobfcf8bf920b1ada0aa0e8111dc22ba1ff63aec28d
1 /*
2 * Input Visitor
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qmp-input-visitor.h"
15 #include "qemu-queue.h"
16 #include "qemu-common.h"
17 #include "qemu-objects.h"
18 #include "qerror.h"
20 #define QIV_STACK_SIZE 1024
22 typedef struct StackObject
24 const QObject *obj;
25 const QListEntry *entry;
26 } StackObject;
28 struct QmpInputVisitor
30 Visitor visitor;
31 QObject *obj;
32 StackObject stack[QIV_STACK_SIZE];
33 int nb_stack;
36 static QmpInputVisitor *to_qiv(Visitor *v)
38 return container_of(v, QmpInputVisitor, visitor);
41 static const QObject *qmp_input_get_object(QmpInputVisitor *qiv,
42 const char *name)
44 const QObject *qobj;
46 if (qiv->nb_stack == 0) {
47 qobj = qiv->obj;
48 } else {
49 qobj = qiv->stack[qiv->nb_stack - 1].obj;
52 if (name && qobject_type(qobj) == QTYPE_QDICT) {
53 return qdict_get(qobject_to_qdict(qobj), name);
54 } else if (qiv->nb_stack > 0 && qobject_type(qobj) == QTYPE_QLIST) {
55 return qlist_entry_obj(qiv->stack[qiv->nb_stack - 1].entry);
58 return qobj;
61 static void qmp_input_push(QmpInputVisitor *qiv, const QObject *obj, Error **errp)
63 qiv->stack[qiv->nb_stack].obj = obj;
64 if (qobject_type(obj) == QTYPE_QLIST) {
65 qiv->stack[qiv->nb_stack].entry = qlist_first(qobject_to_qlist(obj));
67 qiv->nb_stack++;
69 if (qiv->nb_stack >= QIV_STACK_SIZE) {
70 error_set(errp, QERR_BUFFER_OVERRUN);
71 return;
75 static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
77 qiv->nb_stack--;
78 if (qiv->nb_stack < 0) {
79 error_set(errp, QERR_BUFFER_OVERRUN);
80 return;
84 static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind,
85 const char *name, size_t size, Error **errp)
87 QmpInputVisitor *qiv = to_qiv(v);
88 const QObject *qobj = qmp_input_get_object(qiv, name);
90 if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
91 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
92 "QDict");
93 return;
96 qmp_input_push(qiv, qobj, errp);
97 if (error_is_set(errp)) {
98 return;
101 if (obj) {
102 *obj = g_malloc0(size);
106 static void qmp_input_end_struct(Visitor *v, Error **errp)
108 QmpInputVisitor *qiv = to_qiv(v);
110 qmp_input_pop(qiv, errp);
113 static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
115 QmpInputVisitor *qiv = to_qiv(v);
116 const QObject *qobj = qmp_input_get_object(qiv, name);
118 if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
119 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
120 "list");
121 return;
124 qmp_input_push(qiv, qobj, errp);
127 static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
128 Error **errp)
130 QmpInputVisitor *qiv = to_qiv(v);
131 GenericList *entry;
132 StackObject *so = &qiv->stack[qiv->nb_stack - 1];
134 if (so->entry == NULL) {
135 return NULL;
138 entry = g_malloc0(sizeof(*entry));
139 if (*list) {
140 so->entry = qlist_next(so->entry);
141 if (so->entry == NULL) {
142 g_free(entry);
143 return NULL;
145 (*list)->next = entry;
147 *list = entry;
150 return entry;
153 static void qmp_input_end_list(Visitor *v, Error **errp)
155 QmpInputVisitor *qiv = to_qiv(v);
157 qmp_input_pop(qiv, errp);
160 static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name,
161 Error **errp)
163 QmpInputVisitor *qiv = to_qiv(v);
164 const QObject *qobj = qmp_input_get_object(qiv, name);
166 if (!qobj || qobject_type(qobj) != QTYPE_QINT) {
167 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
168 "integer");
169 return;
172 *obj = qint_get_int(qobject_to_qint(qobj));
175 static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name,
176 Error **errp)
178 QmpInputVisitor *qiv = to_qiv(v);
179 const QObject *qobj = qmp_input_get_object(qiv, name);
181 if (!qobj || qobject_type(qobj) != QTYPE_QBOOL) {
182 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
183 "boolean");
184 return;
187 *obj = qbool_get_int(qobject_to_qbool(qobj));
190 static void qmp_input_type_str(Visitor *v, char **obj, const char *name,
191 Error **errp)
193 QmpInputVisitor *qiv = to_qiv(v);
194 const QObject *qobj = qmp_input_get_object(qiv, name);
196 if (!qobj || qobject_type(qobj) != QTYPE_QSTRING) {
197 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
198 "string");
199 return;
202 *obj = g_strdup(qstring_get_str(qobject_to_qstring(qobj)));
205 static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
206 Error **errp)
208 QmpInputVisitor *qiv = to_qiv(v);
209 const QObject *qobj = qmp_input_get_object(qiv, name);
211 if (!qobj || qobject_type(qobj) != QTYPE_QFLOAT) {
212 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
213 "double");
214 return;
217 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
220 static void qmp_input_type_enum(Visitor *v, int *obj, const char *strings[],
221 const char *kind, const char *name,
222 Error **errp)
224 int64_t value = 0;
225 char *enum_str;
227 assert(strings);
229 qmp_input_type_str(v, &enum_str, name, errp);
230 if (error_is_set(errp)) {
231 return;
234 while (strings[value] != NULL) {
235 if (strcmp(strings[value], enum_str) == 0) {
236 break;
238 value++;
241 if (strings[value] == NULL) {
242 error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
243 return;
246 *obj = value;
249 static void qmp_input_start_optional(Visitor *v, bool *present,
250 const char *name, Error **errp)
252 QmpInputVisitor *qiv = to_qiv(v);
253 const QObject *qobj = qmp_input_get_object(qiv, name);
255 if (!qobj) {
256 *present = false;
257 return;
260 *present = true;
263 static void qmp_input_end_optional(Visitor *v, Error **errp)
267 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
269 return &v->visitor;
272 void qmp_input_visitor_cleanup(QmpInputVisitor *v)
274 qobject_decref(v->obj);
275 g_free(v);
278 QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
280 QmpInputVisitor *v;
282 v = g_malloc0(sizeof(*v));
284 v->visitor.start_struct = qmp_input_start_struct;
285 v->visitor.end_struct = qmp_input_end_struct;
286 v->visitor.start_list = qmp_input_start_list;
287 v->visitor.next_list = qmp_input_next_list;
288 v->visitor.end_list = qmp_input_end_list;
289 v->visitor.type_enum = qmp_input_type_enum;
290 v->visitor.type_int = qmp_input_type_int;
291 v->visitor.type_bool = qmp_input_type_bool;
292 v->visitor.type_str = qmp_input_type_str;
293 v->visitor.type_number = qmp_input_type_number;
294 v->visitor.start_optional = qmp_input_start_optional;
295 v->visitor.end_optional = qmp_input_end_optional;
297 v->obj = obj;
298 qobject_incref(v->obj);
300 return v;