mptsas: fix memory leak
[qemu/rayw.git] / qapi / qapi-visit-core.c
blobf8562864613c9fc8a9b1e74229021deaca63c14e
1 /*
2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
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.
15 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/qmp/qobject.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qapi/visitor.h"
20 #include "qapi/visitor-impl.h"
22 void visit_start_struct(Visitor *v, const char *name, void **obj,
23 size_t size, Error **errp)
25 v->start_struct(v, name, obj, size, errp);
28 void visit_end_struct(Visitor *v, Error **errp)
30 v->end_struct(v, errp);
33 void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
34 Error **errp)
36 if (v->start_implicit_struct) {
37 v->start_implicit_struct(v, obj, size, errp);
41 void visit_end_implicit_struct(Visitor *v)
43 if (v->end_implicit_struct) {
44 v->end_implicit_struct(v);
48 void visit_start_list(Visitor *v, const char *name, Error **errp)
50 v->start_list(v, name, errp);
53 GenericList *visit_next_list(Visitor *v, GenericList **list)
55 return v->next_list(v, list);
58 void visit_end_list(Visitor *v)
60 v->end_list(v);
63 bool visit_start_union(Visitor *v, bool data_present, Error **errp)
65 if (v->start_union) {
66 return v->start_union(v, data_present, errp);
68 return true;
71 bool visit_optional(Visitor *v, const char *name, bool *present)
73 if (v->optional) {
74 v->optional(v, name, present);
76 return *present;
79 void visit_get_next_type(Visitor *v, const char *name, QType *type,
80 bool promote_int, Error **errp)
82 if (v->get_next_type) {
83 v->get_next_type(v, name, type, promote_int, errp);
87 void visit_type_enum(Visitor *v, const char *name, int *obj,
88 const char *const strings[], Error **errp)
90 v->type_enum(v, name, obj, strings, errp);
93 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
95 v->type_int64(v, name, obj, errp);
98 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
99 uint64_t max, const char *type, Error **errp)
101 Error *err = NULL;
102 uint64_t value = *obj;
104 v->type_uint64(v, name, &value, &err);
105 if (err) {
106 error_propagate(errp, err);
107 } else if (value > max) {
108 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
109 name ? name : "null", type);
110 } else {
111 *obj = value;
115 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
116 Error **errp)
118 uint64_t value = *obj;
119 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
120 *obj = value;
123 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
124 Error **errp)
126 uint64_t value = *obj;
127 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
128 *obj = value;
131 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
132 Error **errp)
134 uint64_t value = *obj;
135 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
136 *obj = value;
139 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
140 Error **errp)
142 v->type_uint64(v, name, obj, errp);
145 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
146 int64_t min, int64_t max, const char *type,
147 Error **errp)
149 Error *err = NULL;
150 int64_t value = *obj;
152 v->type_int64(v, name, &value, &err);
153 if (err) {
154 error_propagate(errp, err);
155 } else if (value < min || value > max) {
156 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
157 name ? name : "null", type);
158 } else {
159 *obj = value;
163 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
165 int64_t value = *obj;
166 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
167 *obj = value;
170 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
171 Error **errp)
173 int64_t value = *obj;
174 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
175 *obj = value;
178 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
179 Error **errp)
181 int64_t value = *obj;
182 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
183 *obj = value;
186 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
187 Error **errp)
189 v->type_int64(v, name, obj, errp);
192 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
193 Error **errp)
195 if (v->type_size) {
196 v->type_size(v, name, obj, errp);
197 } else {
198 v->type_uint64(v, name, obj, errp);
202 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
204 v->type_bool(v, name, obj, errp);
207 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
209 v->type_str(v, name, obj, errp);
212 void visit_type_number(Visitor *v, const char *name, double *obj,
213 Error **errp)
215 v->type_number(v, name, obj, errp);
218 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
220 v->type_any(v, name, obj, errp);
223 void output_type_enum(Visitor *v, const char *name, int *obj,
224 const char *const strings[], Error **errp)
226 int i = 0;
227 int value = *obj;
228 char *enum_str;
230 assert(strings);
231 while (strings[i++] != NULL);
232 if (value < 0 || value >= i - 1) {
233 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
234 return;
237 enum_str = (char *)strings[value];
238 visit_type_str(v, name, &enum_str, errp);
241 void input_type_enum(Visitor *v, const char *name, int *obj,
242 const char *const strings[], Error **errp)
244 Error *local_err = NULL;
245 int64_t value = 0;
246 char *enum_str;
248 assert(strings);
250 visit_type_str(v, name, &enum_str, &local_err);
251 if (local_err) {
252 error_propagate(errp, local_err);
253 return;
256 while (strings[value] != NULL) {
257 if (strcmp(strings[value], enum_str) == 0) {
258 break;
260 value++;
263 if (strings[value] == NULL) {
264 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
265 g_free(enum_str);
266 return;
269 g_free(enum_str);
270 *obj = value;