virtio-gpu: track and limit host memory allocations
[qemu.git] / qapi / qapi-visit-core.c
blob63bd97b341ea6517e3e3b4a5088d469ccc17e180
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 "qapi/error.h"
17 #include "qemu-common.h"
18 #include "qapi/qmp/qobject.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi/visitor.h"
21 #include "qapi/visitor-impl.h"
22 #include "trace.h"
24 void visit_complete(Visitor *v, void *opaque)
26 assert(v->type != VISITOR_OUTPUT || v->complete);
27 trace_visit_complete(v, opaque);
28 if (v->complete) {
29 v->complete(v, opaque);
33 void visit_free(Visitor *v)
35 trace_visit_free(v);
36 if (v) {
37 v->free(v);
41 void visit_start_struct(Visitor *v, const char *name, void **obj,
42 size_t size, Error **errp)
44 Error *err = NULL;
46 trace_visit_start_struct(v, name, obj, size);
47 if (obj) {
48 assert(size);
49 assert(!(v->type & VISITOR_OUTPUT) || *obj);
51 v->start_struct(v, name, obj, size, &err);
52 if (obj && (v->type & VISITOR_INPUT)) {
53 assert(!err != !*obj);
55 error_propagate(errp, err);
58 void visit_check_struct(Visitor *v, Error **errp)
60 trace_visit_check_struct(v);
61 if (v->check_struct) {
62 v->check_struct(v, errp);
66 void visit_end_struct(Visitor *v, void **obj)
68 trace_visit_end_struct(v, obj);
69 v->end_struct(v, obj);
72 void visit_start_list(Visitor *v, const char *name, GenericList **list,
73 size_t size, Error **errp)
75 Error *err = NULL;
77 assert(!list || size >= sizeof(GenericList));
78 trace_visit_start_list(v, name, list, size);
79 v->start_list(v, name, list, size, &err);
80 if (list && (v->type & VISITOR_INPUT)) {
81 assert(!(err && *list));
83 error_propagate(errp, err);
86 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
88 assert(tail && size >= sizeof(GenericList));
89 trace_visit_next_list(v, tail, size);
90 return v->next_list(v, tail, size);
93 void visit_end_list(Visitor *v, void **obj)
95 trace_visit_end_list(v, obj);
96 v->end_list(v, obj);
99 void visit_start_alternate(Visitor *v, const char *name,
100 GenericAlternate **obj, size_t size,
101 bool promote_int, Error **errp)
103 Error *err = NULL;
105 assert(obj && size >= sizeof(GenericAlternate));
106 assert(!(v->type & VISITOR_OUTPUT) || *obj);
107 trace_visit_start_alternate(v, name, obj, size, promote_int);
108 if (v->start_alternate) {
109 v->start_alternate(v, name, obj, size, promote_int, &err);
111 if (v->type & VISITOR_INPUT) {
112 assert(v->start_alternate && !err != !*obj);
114 error_propagate(errp, err);
117 void visit_end_alternate(Visitor *v, void **obj)
119 trace_visit_end_alternate(v, obj);
120 if (v->end_alternate) {
121 v->end_alternate(v, obj);
125 bool visit_optional(Visitor *v, const char *name, bool *present)
127 trace_visit_optional(v, name, present);
128 if (v->optional) {
129 v->optional(v, name, present);
131 return *present;
134 bool visit_is_input(Visitor *v)
136 return v->type == VISITOR_INPUT;
139 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
141 assert(obj);
142 trace_visit_type_int(v, name, obj);
143 v->type_int64(v, name, obj, errp);
146 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
147 uint64_t max, const char *type, Error **errp)
149 Error *err = NULL;
150 uint64_t value = *obj;
152 v->type_uint64(v, name, &value, &err);
153 if (err) {
154 error_propagate(errp, err);
155 } else if (value > max) {
156 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
157 name ? name : "null", type);
158 } else {
159 *obj = value;
163 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
164 Error **errp)
166 uint64_t value;
168 trace_visit_type_uint8(v, name, obj);
169 value = *obj;
170 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
171 *obj = value;
174 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
175 Error **errp)
177 uint64_t value;
179 trace_visit_type_uint16(v, name, obj);
180 value = *obj;
181 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
182 *obj = value;
185 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
186 Error **errp)
188 uint64_t value;
190 trace_visit_type_uint32(v, name, obj);
191 value = *obj;
192 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
193 *obj = value;
196 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
197 Error **errp)
199 assert(obj);
200 trace_visit_type_uint64(v, name, obj);
201 v->type_uint64(v, name, obj, errp);
204 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
205 int64_t min, int64_t max, const char *type,
206 Error **errp)
208 Error *err = NULL;
209 int64_t value = *obj;
211 v->type_int64(v, name, &value, &err);
212 if (err) {
213 error_propagate(errp, err);
214 } else if (value < min || value > max) {
215 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
216 name ? name : "null", type);
217 } else {
218 *obj = value;
222 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
224 int64_t value;
226 trace_visit_type_int8(v, name, obj);
227 value = *obj;
228 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
229 *obj = value;
232 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
233 Error **errp)
235 int64_t value;
237 trace_visit_type_int16(v, name, obj);
238 value = *obj;
239 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
240 *obj = value;
243 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
244 Error **errp)
246 int64_t value;
248 trace_visit_type_int32(v, name, obj);
249 value = *obj;
250 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
251 *obj = value;
254 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
255 Error **errp)
257 assert(obj);
258 trace_visit_type_int64(v, name, obj);
259 v->type_int64(v, name, obj, errp);
262 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
263 Error **errp)
265 assert(obj);
266 trace_visit_type_size(v, name, obj);
267 if (v->type_size) {
268 v->type_size(v, name, obj, errp);
269 } else {
270 v->type_uint64(v, name, obj, errp);
274 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
276 assert(obj);
277 trace_visit_type_bool(v, name, obj);
278 v->type_bool(v, name, obj, errp);
281 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
283 Error *err = NULL;
285 assert(obj);
286 /* TODO: Fix callers to not pass NULL when they mean "", so that we
287 * can enable:
288 assert(!(v->type & VISITOR_OUTPUT) || *obj);
290 trace_visit_type_str(v, name, obj);
291 v->type_str(v, name, obj, &err);
292 if (v->type & VISITOR_INPUT) {
293 assert(!err != !*obj);
295 error_propagate(errp, err);
298 void visit_type_number(Visitor *v, const char *name, double *obj,
299 Error **errp)
301 assert(obj);
302 trace_visit_type_number(v, name, obj);
303 v->type_number(v, name, obj, errp);
306 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
308 Error *err = NULL;
310 assert(obj);
311 assert(v->type != VISITOR_OUTPUT || *obj);
312 trace_visit_type_any(v, name, obj);
313 v->type_any(v, name, obj, &err);
314 if (v->type == VISITOR_INPUT) {
315 assert(!err != !*obj);
317 error_propagate(errp, err);
320 void visit_type_null(Visitor *v, const char *name, Error **errp)
322 trace_visit_type_null(v, name);
323 v->type_null(v, name, errp);
326 static void output_type_enum(Visitor *v, const char *name, int *obj,
327 const char *const strings[], Error **errp)
329 int i = 0;
330 int value = *obj;
331 char *enum_str;
333 while (strings[i++] != NULL);
334 if (value < 0 || value >= i - 1) {
335 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
336 return;
339 enum_str = (char *)strings[value];
340 visit_type_str(v, name, &enum_str, errp);
343 static void input_type_enum(Visitor *v, const char *name, int *obj,
344 const char *const strings[], Error **errp)
346 Error *local_err = NULL;
347 int64_t value = 0;
348 char *enum_str;
350 visit_type_str(v, name, &enum_str, &local_err);
351 if (local_err) {
352 error_propagate(errp, local_err);
353 return;
356 while (strings[value] != NULL) {
357 if (strcmp(strings[value], enum_str) == 0) {
358 break;
360 value++;
363 if (strings[value] == NULL) {
364 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
365 g_free(enum_str);
366 return;
369 g_free(enum_str);
370 *obj = value;
373 void visit_type_enum(Visitor *v, const char *name, int *obj,
374 const char *const strings[], Error **errp)
376 assert(obj && strings);
377 switch (v->type) {
378 case VISITOR_INPUT:
379 input_type_enum(v, name, obj, strings, errp);
380 break;
381 case VISITOR_OUTPUT:
382 output_type_enum(v, name, obj, strings, errp);
383 break;
384 case VISITOR_CLONE:
385 /* nothing further to do, scalar value was already copied by
386 * g_memdup() during visit_start_*() */
387 break;
388 case VISITOR_DEALLOC:
389 /* nothing to deallocate for a scalar */
390 break;