Statistics
| Branch: | Revision:

root / qapi / qapi-dealloc-visitor.c @ 3dce9cad

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