xref: /openbmc/qemu/qapi/qapi-dealloc-visitor.c (revision 3d9569b8)
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 "qapi/qmp/qnull.h"
18 #include "qemu/queue.h"
19 #include "qapi/visitor-impl.h"
20 
21 struct QapiDeallocVisitor
22 {
23     Visitor visitor;
24 };
25 
26 static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
27                                       size_t unused, Error **errp)
28 {
29 }
30 
31 static void qapi_dealloc_end_struct(Visitor *v, void **obj)
32 {
33     if (obj) {
34         g_free(*obj);
35     }
36 }
37 
38 static void qapi_dealloc_start_alternate(Visitor *v, const char *name,
39                                          GenericAlternate **obj, size_t size,
40                                          Error **errp)
41 {
42 }
43 
44 static void qapi_dealloc_end_alternate(Visitor *v, void **obj)
45 {
46     if (obj) {
47         g_free(*obj);
48     }
49 }
50 
51 static void qapi_dealloc_start_list(Visitor *v, const char *name,
52                                     GenericList **list, size_t size,
53                                     Error **errp)
54 {
55 }
56 
57 static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList *tail,
58                                            size_t size)
59 {
60     GenericList *next = tail->next;
61     g_free(tail);
62     return next;
63 }
64 
65 static void qapi_dealloc_end_list(Visitor *v, void **obj)
66 {
67 }
68 
69 static void qapi_dealloc_type_str(Visitor *v, const char *name, char **obj,
70                                   Error **errp)
71 {
72     if (obj) {
73         g_free(*obj);
74     }
75 }
76 
77 static void qapi_dealloc_type_int64(Visitor *v, const char *name, int64_t *obj,
78                                     Error **errp)
79 {
80 }
81 
82 static void qapi_dealloc_type_uint64(Visitor *v, const char *name,
83                                      uint64_t *obj, Error **errp)
84 {
85 }
86 
87 static void qapi_dealloc_type_bool(Visitor *v, const char *name, bool *obj,
88                                    Error **errp)
89 {
90 }
91 
92 static void qapi_dealloc_type_number(Visitor *v, const char *name, double *obj,
93                                      Error **errp)
94 {
95 }
96 
97 static void qapi_dealloc_type_anything(Visitor *v, const char *name,
98                                        QObject **obj, Error **errp)
99 {
100     if (obj) {
101         qobject_unref(*obj);
102     }
103 }
104 
105 static void qapi_dealloc_type_null(Visitor *v, const char *name,
106                                    QNull **obj, Error **errp)
107 {
108     if (obj) {
109         qobject_unref(*obj);
110     }
111 }
112 
113 static void qapi_dealloc_free(Visitor *v)
114 {
115     g_free(container_of(v, QapiDeallocVisitor, visitor));
116 }
117 
118 Visitor *qapi_dealloc_visitor_new(void)
119 {
120     QapiDeallocVisitor *v;
121 
122     v = g_malloc0(sizeof(*v));
123 
124     v->visitor.type = VISITOR_DEALLOC;
125     v->visitor.start_struct = qapi_dealloc_start_struct;
126     v->visitor.end_struct = qapi_dealloc_end_struct;
127     v->visitor.start_alternate = qapi_dealloc_start_alternate;
128     v->visitor.end_alternate = qapi_dealloc_end_alternate;
129     v->visitor.start_list = qapi_dealloc_start_list;
130     v->visitor.next_list = qapi_dealloc_next_list;
131     v->visitor.end_list = qapi_dealloc_end_list;
132     v->visitor.type_int64 = qapi_dealloc_type_int64;
133     v->visitor.type_uint64 = qapi_dealloc_type_uint64;
134     v->visitor.type_bool = qapi_dealloc_type_bool;
135     v->visitor.type_str = qapi_dealloc_type_str;
136     v->visitor.type_number = qapi_dealloc_type_number;
137     v->visitor.type_any = qapi_dealloc_type_anything;
138     v->visitor.type_null = qapi_dealloc_type_null;
139     v->visitor.free = qapi_dealloc_free;
140 
141     return &v->visitor;
142 }
143