]>
Commit | Line | Data |
---|---|---|
d5f3c29c MR |
1 | /* |
2 | * Dealloc Visitor | |
3 | * | |
08f9541d | 4 | * Copyright (C) 2012-2016 Red Hat, Inc. |
d5f3c29c MR |
5 | * Copyright IBM, Corp. 2011 |
6 | * | |
7 | * Authors: | |
8 | * Michael Roth <[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 | ||
cbf21151 | 15 | #include "qemu/osdep.h" |
7b1b5d19 | 16 | #include "qapi/dealloc-visitor.h" |
1de7afc9 | 17 | #include "qemu/queue.h" |
d5f3c29c | 18 | #include "qemu-common.h" |
7b1b5d19 PB |
19 | #include "qapi/qmp/types.h" |
20 | #include "qapi/visitor-impl.h" | |
d5f3c29c MR |
21 | |
22 | typedef struct StackEntry | |
23 | { | |
24 | void *value; | |
0b9d8542 | 25 | bool is_list_head; |
d5f3c29c MR |
26 | QTAILQ_ENTRY(StackEntry) node; |
27 | } StackEntry; | |
28 | ||
29 | struct QapiDeallocVisitor | |
30 | { | |
31 | Visitor visitor; | |
32 | QTAILQ_HEAD(, StackEntry) stack; | |
33 | }; | |
34 | ||
35 | static QapiDeallocVisitor *to_qov(Visitor *v) | |
36 | { | |
37 | return container_of(v, QapiDeallocVisitor, visitor); | |
38 | } | |
39 | ||
40 | static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value) | |
41 | { | |
7267c094 | 42 | StackEntry *e = g_malloc0(sizeof(*e)); |
d5f3c29c MR |
43 | |
44 | e->value = value; | |
0b9d8542 MR |
45 | |
46 | /* see if we're just pushing a list head tracker */ | |
47 | if (value == NULL) { | |
48 | e->is_list_head = true; | |
49 | } | |
d5f3c29c MR |
50 | QTAILQ_INSERT_HEAD(&qov->stack, e, node); |
51 | } | |
52 | ||
53 | static void *qapi_dealloc_pop(QapiDeallocVisitor *qov) | |
54 | { | |
55 | StackEntry *e = QTAILQ_FIRST(&qov->stack); | |
56 | QObject *value; | |
57 | QTAILQ_REMOVE(&qov->stack, e, node); | |
58 | value = e->value; | |
7267c094 | 59 | g_free(e); |
d5f3c29c MR |
60 | return value; |
61 | } | |
62 | ||
0b2a0d6b | 63 | static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj, |
337283df | 64 | size_t unused, Error **errp) |
d5f3c29c MR |
65 | { |
66 | QapiDeallocVisitor *qov = to_qov(v); | |
67 | qapi_dealloc_push(qov, obj); | |
68 | } | |
69 | ||
70 | static void qapi_dealloc_end_struct(Visitor *v, Error **errp) | |
71 | { | |
72 | QapiDeallocVisitor *qov = to_qov(v); | |
73 | void **obj = qapi_dealloc_pop(qov); | |
74 | if (obj) { | |
7267c094 | 75 | g_free(*obj); |
d5f3c29c MR |
76 | } |
77 | } | |
78 | ||
3dce9cad WX |
79 | static void qapi_dealloc_start_implicit_struct(Visitor *v, |
80 | void **obj, | |
81 | size_t size, | |
82 | Error **errp) | |
83 | { | |
84 | QapiDeallocVisitor *qov = to_qov(v); | |
85 | qapi_dealloc_push(qov, obj); | |
86 | } | |
87 | ||
08f9541d | 88 | static void qapi_dealloc_end_implicit_struct(Visitor *v) |
3dce9cad WX |
89 | { |
90 | QapiDeallocVisitor *qov = to_qov(v); | |
91 | void **obj = qapi_dealloc_pop(qov); | |
92 | if (obj) { | |
93 | g_free(*obj); | |
94 | } | |
95 | } | |
96 | ||
d5f3c29c MR |
97 | static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp) |
98 | { | |
5666dd19 | 99 | QapiDeallocVisitor *qov = to_qov(v); |
0b9d8542 | 100 | qapi_dealloc_push(qov, NULL); |
d5f3c29c MR |
101 | } |
102 | ||
e65d89bf EB |
103 | static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **listp, |
104 | size_t size) | |
d5f3c29c | 105 | { |
5666dd19 MR |
106 | GenericList *list = *listp; |
107 | QapiDeallocVisitor *qov = to_qov(v); | |
0b9d8542 | 108 | StackEntry *e = QTAILQ_FIRST(&qov->stack); |
5666dd19 | 109 | |
0b9d8542 MR |
110 | if (e && e->is_list_head) { |
111 | e->is_list_head = false; | |
112 | return list; | |
5666dd19 MR |
113 | } |
114 | ||
0b9d8542 MR |
115 | if (list) { |
116 | list = list->next; | |
117 | g_free(*listp); | |
118 | return list; | |
119 | } | |
120 | ||
121 | return NULL; | |
d5f3c29c MR |
122 | } |
123 | ||
08f9541d | 124 | static void qapi_dealloc_end_list(Visitor *v) |
d5f3c29c | 125 | { |
0b9d8542 MR |
126 | QapiDeallocVisitor *qov = to_qov(v); |
127 | void *obj = qapi_dealloc_pop(qov); | |
128 | assert(obj == NULL); /* should've been list head tracker with no payload */ | |
d5f3c29c MR |
129 | } |
130 | ||
0b2a0d6b | 131 | static void qapi_dealloc_type_str(Visitor *v, const char *name, char **obj, |
d5f3c29c MR |
132 | Error **errp) |
133 | { | |
b690d679 PL |
134 | if (obj) { |
135 | g_free(*obj); | |
136 | } | |
d5f3c29c MR |
137 | } |
138 | ||
0b2a0d6b | 139 | static void qapi_dealloc_type_int64(Visitor *v, const char *name, int64_t *obj, |
4c40314a | 140 | Error **errp) |
d5f3c29c MR |
141 | { |
142 | } | |
143 | ||
0b2a0d6b EB |
144 | static void qapi_dealloc_type_uint64(Visitor *v, const char *name, |
145 | uint64_t *obj, Error **errp) | |
f755dea7 EB |
146 | { |
147 | } | |
148 | ||
0b2a0d6b | 149 | static void qapi_dealloc_type_bool(Visitor *v, const char *name, bool *obj, |
d5f3c29c MR |
150 | Error **errp) |
151 | { | |
152 | } | |
153 | ||
0b2a0d6b | 154 | static void qapi_dealloc_type_number(Visitor *v, const char *name, double *obj, |
d5f3c29c MR |
155 | Error **errp) |
156 | { | |
157 | } | |
158 | ||
0b2a0d6b EB |
159 | static void qapi_dealloc_type_anything(Visitor *v, const char *name, |
160 | QObject **obj, Error **errp) | |
28770e05 MA |
161 | { |
162 | if (obj) { | |
163 | qobject_decref(*obj); | |
164 | } | |
165 | } | |
166 | ||
0b2a0d6b | 167 | static void qapi_dealloc_type_enum(Visitor *v, const char *name, int *obj, |
337283df | 168 | const char * const strings[], Error **errp) |
d5f3c29c MR |
169 | { |
170 | } | |
171 | ||
172 | Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v) | |
173 | { | |
174 | return &v->visitor; | |
175 | } | |
176 | ||
177 | void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v) | |
178 | { | |
7267c094 | 179 | g_free(v); |
d5f3c29c MR |
180 | } |
181 | ||
182 | QapiDeallocVisitor *qapi_dealloc_visitor_new(void) | |
183 | { | |
184 | QapiDeallocVisitor *v; | |
185 | ||
7267c094 | 186 | v = g_malloc0(sizeof(*v)); |
d5f3c29c MR |
187 | |
188 | v->visitor.start_struct = qapi_dealloc_start_struct; | |
189 | v->visitor.end_struct = qapi_dealloc_end_struct; | |
3dce9cad WX |
190 | v->visitor.start_implicit_struct = qapi_dealloc_start_implicit_struct; |
191 | v->visitor.end_implicit_struct = qapi_dealloc_end_implicit_struct; | |
d5f3c29c MR |
192 | v->visitor.start_list = qapi_dealloc_start_list; |
193 | v->visitor.next_list = qapi_dealloc_next_list; | |
194 | v->visitor.end_list = qapi_dealloc_end_list; | |
195 | v->visitor.type_enum = qapi_dealloc_type_enum; | |
4c40314a | 196 | v->visitor.type_int64 = qapi_dealloc_type_int64; |
f755dea7 | 197 | v->visitor.type_uint64 = qapi_dealloc_type_uint64; |
d5f3c29c MR |
198 | v->visitor.type_bool = qapi_dealloc_type_bool; |
199 | v->visitor.type_str = qapi_dealloc_type_str; | |
200 | v->visitor.type_number = qapi_dealloc_type_number; | |
28770e05 | 201 | v->visitor.type_any = qapi_dealloc_type_anything; |
d5f3c29c MR |
202 | |
203 | QTAILQ_INIT(&v->stack); | |
204 | ||
205 | return v; | |
206 | } |