xref: /qemu/qapi/qapi-dealloc-visitor.c (revision de4905f4)
1 /*
2  * Dealloc Visitor
3  *
4  * Copyright (C) 2012-2016 Red Hat, Inc.
5  * Copyright IBM, Corp. 2011
6  *
7  * Authors:
8  *  Michael Roth   <mdroth@linux.vnet.ibm.com>
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 
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"
21 
22 typedef struct StackEntry
23 {
24     void *value;
25     QTAILQ_ENTRY(StackEntry) node;
26 } StackEntry;
27 
28 struct QapiDeallocVisitor
29 {
30     Visitor visitor;
31     QTAILQ_HEAD(, StackEntry) stack;
32 };
33 
34 static QapiDeallocVisitor *to_qov(Visitor *v)
35 {
36     return container_of(v, QapiDeallocVisitor, visitor);
37 }
38 
39 static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
40 {
41     StackEntry *e = g_malloc0(sizeof(*e));
42 
43     e->value = value;
44 
45     QTAILQ_INSERT_HEAD(&qov->stack, e, node);
46 }
47 
48 static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
49 {
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;
56 }
57 
58 static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
59                                       size_t unused, Error **errp)
60 {
61     QapiDeallocVisitor *qov = to_qov(v);
62     qapi_dealloc_push(qov, obj);
63 }
64 
65 static void qapi_dealloc_end_struct(Visitor *v)
66 {
67     QapiDeallocVisitor *qov = to_qov(v);
68     void **obj = qapi_dealloc_pop(qov);
69     if (obj) {
70         g_free(*obj);
71     }
72 }
73 
74 static void qapi_dealloc_start_alternate(Visitor *v, const char *name,
75                                          GenericAlternate **obj, size_t size,
76                                          bool promote_int, Error **errp)
77 {
78     QapiDeallocVisitor *qov = to_qov(v);
79     qapi_dealloc_push(qov, obj);
80 }
81 
82 static void qapi_dealloc_end_alternate(Visitor *v)
83 {
84     QapiDeallocVisitor *qov = to_qov(v);
85     void **obj = qapi_dealloc_pop(qov);
86     if (obj) {
87         g_free(*obj);
88     }
89 }
90 
91 static void qapi_dealloc_start_list(Visitor *v, const char *name,
92                                     GenericList **list, size_t size,
93                                     Error **errp)
94 {
95 }
96 
97 static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList *tail,
98                                            size_t size)
99 {
100     GenericList *next = tail->next;
101     g_free(tail);
102     return next;
103 }
104 
105 static void qapi_dealloc_end_list(Visitor *v)
106 {
107 }
108 
109 static void qapi_dealloc_type_str(Visitor *v, const char *name, char **obj,
110                                   Error **errp)
111 {
112     if (obj) {
113         g_free(*obj);
114     }
115 }
116 
117 static void qapi_dealloc_type_int64(Visitor *v, const char *name, int64_t *obj,
118                                     Error **errp)
119 {
120 }
121 
122 static void qapi_dealloc_type_uint64(Visitor *v, const char *name,
123                                      uint64_t *obj, Error **errp)
124 {
125 }
126 
127 static void qapi_dealloc_type_bool(Visitor *v, const char *name, bool *obj,
128                                    Error **errp)
129 {
130 }
131 
132 static void qapi_dealloc_type_number(Visitor *v, const char *name, double *obj,
133                                      Error **errp)
134 {
135 }
136 
137 static void qapi_dealloc_type_anything(Visitor *v, const char *name,
138                                        QObject **obj, Error **errp)
139 {
140     if (obj) {
141         qobject_decref(*obj);
142     }
143 }
144 
145 static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp)
146 {
147 }
148 
149 Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
150 {
151     return &v->visitor;
152 }
153 
154 void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
155 {
156     g_free(v);
157 }
158 
159 QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
160 {
161     QapiDeallocVisitor *v;
162 
163     v = g_malloc0(sizeof(*v));
164 
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;
180 
181     QTAILQ_INIT(&v->stack);
182 
183     return v;
184 }
185