root / qapi / qapi-dealloc-visitor.c @ a22f123c
History | View | Annotate | Download (3.5 kB)
1 | d5f3c29c | Michael Roth | /*
|
---|---|---|---|
2 | d5f3c29c | Michael Roth | * Dealloc Visitor
|
3 | d5f3c29c | Michael Roth | *
|
4 | d5f3c29c | Michael Roth | * Copyright IBM, Corp. 2011
|
5 | d5f3c29c | Michael Roth | *
|
6 | d5f3c29c | Michael Roth | * Authors:
|
7 | d5f3c29c | Michael Roth | * Michael Roth <mdroth@linux.vnet.ibm.com>
|
8 | d5f3c29c | Michael Roth | *
|
9 | d5f3c29c | Michael Roth | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
10 | d5f3c29c | Michael Roth | * See the COPYING.LIB file in the top-level directory.
|
11 | d5f3c29c | Michael Roth | *
|
12 | d5f3c29c | Michael Roth | */
|
13 | d5f3c29c | Michael Roth | |
14 | d5f3c29c | Michael Roth | #include "qapi-dealloc-visitor.h" |
15 | d5f3c29c | Michael Roth | #include "qemu-queue.h" |
16 | d5f3c29c | Michael Roth | #include "qemu-common.h" |
17 | d5f3c29c | Michael Roth | #include "qemu-objects.h" |
18 | d5f3c29c | Michael Roth | |
19 | d5f3c29c | Michael Roth | typedef struct StackEntry |
20 | d5f3c29c | Michael Roth | { |
21 | d5f3c29c | Michael Roth | void *value;
|
22 | d5f3c29c | Michael Roth | QTAILQ_ENTRY(StackEntry) node; |
23 | d5f3c29c | Michael Roth | } StackEntry; |
24 | d5f3c29c | Michael Roth | |
25 | d5f3c29c | Michael Roth | struct QapiDeallocVisitor
|
26 | d5f3c29c | Michael Roth | { |
27 | d5f3c29c | Michael Roth | Visitor visitor; |
28 | d5f3c29c | Michael Roth | QTAILQ_HEAD(, StackEntry) stack; |
29 | d5f3c29c | Michael Roth | }; |
30 | d5f3c29c | Michael Roth | |
31 | d5f3c29c | Michael Roth | static QapiDeallocVisitor *to_qov(Visitor *v)
|
32 | d5f3c29c | Michael Roth | { |
33 | d5f3c29c | Michael Roth | return container_of(v, QapiDeallocVisitor, visitor);
|
34 | d5f3c29c | Michael Roth | } |
35 | d5f3c29c | Michael Roth | |
36 | d5f3c29c | Michael Roth | static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value) |
37 | d5f3c29c | Michael Roth | { |
38 | 7267c094 | Anthony Liguori | StackEntry *e = g_malloc0(sizeof(*e));
|
39 | d5f3c29c | Michael Roth | |
40 | d5f3c29c | Michael Roth | e->value = value; |
41 | d5f3c29c | Michael Roth | QTAILQ_INSERT_HEAD(&qov->stack, e, node); |
42 | d5f3c29c | Michael Roth | } |
43 | d5f3c29c | Michael Roth | |
44 | d5f3c29c | Michael Roth | static void *qapi_dealloc_pop(QapiDeallocVisitor *qov) |
45 | d5f3c29c | Michael Roth | { |
46 | d5f3c29c | Michael Roth | StackEntry *e = QTAILQ_FIRST(&qov->stack); |
47 | d5f3c29c | Michael Roth | QObject *value; |
48 | d5f3c29c | Michael Roth | QTAILQ_REMOVE(&qov->stack, e, node); |
49 | d5f3c29c | Michael Roth | value = e->value; |
50 | 7267c094 | Anthony Liguori | g_free(e); |
51 | d5f3c29c | Michael Roth | return value;
|
52 | d5f3c29c | Michael Roth | } |
53 | d5f3c29c | Michael Roth | |
54 | d5f3c29c | Michael Roth | static void qapi_dealloc_start_struct(Visitor *v, void **obj, const char *kind, |
55 | d5f3c29c | Michael Roth | const char *name, size_t unused, |
56 | d5f3c29c | Michael Roth | Error **errp) |
57 | d5f3c29c | Michael Roth | { |
58 | d5f3c29c | Michael Roth | QapiDeallocVisitor *qov = to_qov(v); |
59 | d5f3c29c | Michael Roth | qapi_dealloc_push(qov, obj); |
60 | d5f3c29c | Michael Roth | } |
61 | d5f3c29c | Michael Roth | |
62 | d5f3c29c | Michael Roth | static void qapi_dealloc_end_struct(Visitor *v, Error **errp) |
63 | d5f3c29c | Michael Roth | { |
64 | d5f3c29c | Michael Roth | QapiDeallocVisitor *qov = to_qov(v); |
65 | d5f3c29c | Michael Roth | void **obj = qapi_dealloc_pop(qov);
|
66 | d5f3c29c | Michael Roth | if (obj) {
|
67 | 7267c094 | Anthony Liguori | g_free(*obj); |
68 | d5f3c29c | Michael Roth | } |
69 | d5f3c29c | Michael Roth | } |
70 | d5f3c29c | Michael Roth | |
71 | d5f3c29c | Michael Roth | static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp) |
72 | d5f3c29c | Michael Roth | { |
73 | d5f3c29c | Michael Roth | } |
74 | d5f3c29c | Michael Roth | |
75 | d5f3c29c | Michael Roth | static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **list,
|
76 | d5f3c29c | Michael Roth | Error **errp) |
77 | d5f3c29c | Michael Roth | { |
78 | d5f3c29c | Michael Roth | GenericList *retval = *list; |
79 | 7267c094 | Anthony Liguori | g_free(retval->value); |
80 | d5f3c29c | Michael Roth | *list = retval->next; |
81 | d5f3c29c | Michael Roth | return retval;
|
82 | d5f3c29c | Michael Roth | } |
83 | d5f3c29c | Michael Roth | |
84 | d5f3c29c | Michael Roth | static void qapi_dealloc_end_list(Visitor *v, Error **errp) |
85 | d5f3c29c | Michael Roth | { |
86 | d5f3c29c | Michael Roth | } |
87 | d5f3c29c | Michael Roth | |
88 | d5f3c29c | Michael Roth | static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name, |
89 | d5f3c29c | Michael Roth | Error **errp) |
90 | d5f3c29c | Michael Roth | { |
91 | d5f3c29c | Michael Roth | if (obj) {
|
92 | 7267c094 | Anthony Liguori | g_free(*obj); |
93 | d5f3c29c | Michael Roth | } |
94 | d5f3c29c | Michael Roth | } |
95 | d5f3c29c | Michael Roth | |
96 | d5f3c29c | Michael Roth | static void qapi_dealloc_type_int(Visitor *v, int64_t *obj, const char *name, |
97 | d5f3c29c | Michael Roth | Error **errp) |
98 | d5f3c29c | Michael Roth | { |
99 | d5f3c29c | Michael Roth | } |
100 | d5f3c29c | Michael Roth | |
101 | d5f3c29c | Michael Roth | static void qapi_dealloc_type_bool(Visitor *v, bool *obj, const char *name, |
102 | d5f3c29c | Michael Roth | Error **errp) |
103 | d5f3c29c | Michael Roth | { |
104 | d5f3c29c | Michael Roth | } |
105 | d5f3c29c | Michael Roth | |
106 | d5f3c29c | Michael Roth | static void qapi_dealloc_type_number(Visitor *v, double *obj, const char *name, |
107 | d5f3c29c | Michael Roth | Error **errp) |
108 | d5f3c29c | Michael Roth | { |
109 | d5f3c29c | Michael Roth | } |
110 | d5f3c29c | Michael Roth | |
111 | d5f3c29c | Michael Roth | static void qapi_dealloc_type_enum(Visitor *v, int *obj, const char *strings[], |
112 | d5f3c29c | Michael Roth | const char *kind, const char *name, |
113 | d5f3c29c | Michael Roth | Error **errp) |
114 | d5f3c29c | Michael Roth | { |
115 | d5f3c29c | Michael Roth | } |
116 | d5f3c29c | Michael Roth | |
117 | d5f3c29c | Michael Roth | Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v) |
118 | d5f3c29c | Michael Roth | { |
119 | d5f3c29c | Michael Roth | return &v->visitor;
|
120 | d5f3c29c | Michael Roth | } |
121 | d5f3c29c | Michael Roth | |
122 | d5f3c29c | Michael Roth | void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
|
123 | d5f3c29c | Michael Roth | { |
124 | 7267c094 | Anthony Liguori | g_free(v); |
125 | d5f3c29c | Michael Roth | } |
126 | d5f3c29c | Michael Roth | |
127 | d5f3c29c | Michael Roth | QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
|
128 | d5f3c29c | Michael Roth | { |
129 | d5f3c29c | Michael Roth | QapiDeallocVisitor *v; |
130 | d5f3c29c | Michael Roth | |
131 | 7267c094 | Anthony Liguori | v = g_malloc0(sizeof(*v));
|
132 | d5f3c29c | Michael Roth | |
133 | d5f3c29c | Michael Roth | v->visitor.start_struct = qapi_dealloc_start_struct; |
134 | d5f3c29c | Michael Roth | v->visitor.end_struct = qapi_dealloc_end_struct; |
135 | d5f3c29c | Michael Roth | v->visitor.start_list = qapi_dealloc_start_list; |
136 | d5f3c29c | Michael Roth | v->visitor.next_list = qapi_dealloc_next_list; |
137 | d5f3c29c | Michael Roth | v->visitor.end_list = qapi_dealloc_end_list; |
138 | d5f3c29c | Michael Roth | v->visitor.type_enum = qapi_dealloc_type_enum; |
139 | d5f3c29c | Michael Roth | v->visitor.type_int = qapi_dealloc_type_int; |
140 | d5f3c29c | Michael Roth | v->visitor.type_bool = qapi_dealloc_type_bool; |
141 | d5f3c29c | Michael Roth | v->visitor.type_str = qapi_dealloc_type_str; |
142 | d5f3c29c | Michael Roth | v->visitor.type_number = qapi_dealloc_type_number; |
143 | d5f3c29c | Michael Roth | |
144 | d5f3c29c | Michael Roth | QTAILQ_INIT(&v->stack); |
145 | d5f3c29c | Michael Roth | |
146 | d5f3c29c | Michael Roth | return v;
|
147 | d5f3c29c | Michael Roth | } |