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