usb-bot: hotplug support
[qemu/kevin.git] / qapi / qapi-dealloc-visitor.c
blobcd68b55a1a797dcd7d689342ad4411e6ce589167
1 /*
2 * Dealloc Visitor
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
7 * Authors:
8 * Michael Roth <mdroth@linux.vnet.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/dealloc-visitor.h"
17 #include "qemu/queue.h"
18 #include "qemu-common.h"
19 #include "qapi/qmp/types.h"
20 #include "qapi/visitor-impl.h"
22 typedef struct StackEntry
24 void *value;
25 QTAILQ_ENTRY(StackEntry) node;
26 } StackEntry;
28 struct QapiDeallocVisitor
30 Visitor visitor;
31 QTAILQ_HEAD(, StackEntry) stack;
34 static QapiDeallocVisitor *to_qov(Visitor *v)
36 return container_of(v, QapiDeallocVisitor, visitor);
39 static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
41 StackEntry *e = g_malloc0(sizeof(*e));
43 e->value = value;
45 QTAILQ_INSERT_HEAD(&qov->stack, e, node);
48 static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
50 StackEntry *e = QTAILQ_FIRST(&qov->stack);
51 QObject *value;
52 QTAILQ_REMOVE(&qov->stack, e, node);
53 value = e->value;
54 g_free(e);
55 return value;
58 static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
59 size_t unused, Error **errp)
61 QapiDeallocVisitor *qov = to_qov(v);
62 qapi_dealloc_push(qov, obj);
65 static void qapi_dealloc_end_struct(Visitor *v)
67 QapiDeallocVisitor *qov = to_qov(v);
68 void **obj = qapi_dealloc_pop(qov);
69 if (obj) {
70 g_free(*obj);
74 static void qapi_dealloc_start_alternate(Visitor *v, const char *name,
75 GenericAlternate **obj, size_t size,
76 bool promote_int, Error **errp)
78 QapiDeallocVisitor *qov = to_qov(v);
79 qapi_dealloc_push(qov, obj);
82 static void qapi_dealloc_end_alternate(Visitor *v)
84 QapiDeallocVisitor *qov = to_qov(v);
85 void **obj = qapi_dealloc_pop(qov);
86 if (obj) {
87 g_free(*obj);
91 static void qapi_dealloc_start_list(Visitor *v, const char *name,
92 GenericList **list, size_t size,
93 Error **errp)
97 static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList *tail,
98 size_t size)
100 GenericList *next = tail->next;
101 g_free(tail);
102 return next;
105 static void qapi_dealloc_end_list(Visitor *v)
109 static void qapi_dealloc_type_str(Visitor *v, const char *name, char **obj,
110 Error **errp)
112 if (obj) {
113 g_free(*obj);
117 static void qapi_dealloc_type_int64(Visitor *v, const char *name, int64_t *obj,
118 Error **errp)
122 static void qapi_dealloc_type_uint64(Visitor *v, const char *name,
123 uint64_t *obj, Error **errp)
127 static void qapi_dealloc_type_bool(Visitor *v, const char *name, bool *obj,
128 Error **errp)
132 static void qapi_dealloc_type_number(Visitor *v, const char *name, double *obj,
133 Error **errp)
137 static void qapi_dealloc_type_anything(Visitor *v, const char *name,
138 QObject **obj, Error **errp)
140 if (obj) {
141 qobject_decref(*obj);
145 static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp)
149 Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
151 return &v->visitor;
154 void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
156 g_free(v);
159 QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
161 QapiDeallocVisitor *v;
163 v = g_malloc0(sizeof(*v));
165 v->visitor.type = VISITOR_DEALLOC;
166 v->visitor.start_struct = qapi_dealloc_start_struct;
167 v->visitor.end_struct = qapi_dealloc_end_struct;
168 v->visitor.start_alternate = qapi_dealloc_start_alternate;
169 v->visitor.end_alternate = qapi_dealloc_end_alternate;
170 v->visitor.start_list = qapi_dealloc_start_list;
171 v->visitor.next_list = qapi_dealloc_next_list;
172 v->visitor.end_list = qapi_dealloc_end_list;
173 v->visitor.type_int64 = qapi_dealloc_type_int64;
174 v->visitor.type_uint64 = qapi_dealloc_type_uint64;
175 v->visitor.type_bool = qapi_dealloc_type_bool;
176 v->visitor.type_str = qapi_dealloc_type_str;
177 v->visitor.type_number = qapi_dealloc_type_number;
178 v->visitor.type_any = qapi_dealloc_type_anything;
179 v->visitor.type_null = qapi_dealloc_type_null;
181 QTAILQ_INIT(&v->stack);
183 return v;