Statistics
| Branch: | Revision:

root / qapi / qapi-dealloc-visitor.c @ dc1c13d9

History | View | Annotate | Download (4.1 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 0b9d8542 Michael Roth
    bool is_list_head;
23 d5f3c29c Michael Roth
    QTAILQ_ENTRY(StackEntry) node;
24 d5f3c29c Michael Roth
} StackEntry;
25 d5f3c29c Michael Roth
26 d5f3c29c Michael Roth
struct QapiDeallocVisitor
27 d5f3c29c Michael Roth
{
28 d5f3c29c Michael Roth
    Visitor visitor;
29 d5f3c29c Michael Roth
    QTAILQ_HEAD(, StackEntry) stack;
30 5666dd19 Michael Roth
    bool is_list_head;
31 d5f3c29c Michael Roth
};
32 d5f3c29c Michael Roth
33 d5f3c29c Michael Roth
static QapiDeallocVisitor *to_qov(Visitor *v)
34 d5f3c29c Michael Roth
{
35 d5f3c29c Michael Roth
    return container_of(v, QapiDeallocVisitor, visitor);
36 d5f3c29c Michael Roth
}
37 d5f3c29c Michael Roth
38 d5f3c29c Michael Roth
static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
39 d5f3c29c Michael Roth
{
40 7267c094 Anthony Liguori
    StackEntry *e = g_malloc0(sizeof(*e));
41 d5f3c29c Michael Roth
42 d5f3c29c Michael Roth
    e->value = value;
43 0b9d8542 Michael Roth
44 0b9d8542 Michael Roth
    /* see if we're just pushing a list head tracker */
45 0b9d8542 Michael Roth
    if (value == NULL) {
46 0b9d8542 Michael Roth
        e->is_list_head = true;
47 0b9d8542 Michael Roth
    }
48 d5f3c29c Michael Roth
    QTAILQ_INSERT_HEAD(&qov->stack, e, node);
49 d5f3c29c Michael Roth
}
50 d5f3c29c Michael Roth
51 d5f3c29c Michael Roth
static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
52 d5f3c29c Michael Roth
{
53 d5f3c29c Michael Roth
    StackEntry *e = QTAILQ_FIRST(&qov->stack);
54 d5f3c29c Michael Roth
    QObject *value;
55 d5f3c29c Michael Roth
    QTAILQ_REMOVE(&qov->stack, e, node);
56 d5f3c29c Michael Roth
    value = e->value;
57 7267c094 Anthony Liguori
    g_free(e);
58 d5f3c29c Michael Roth
    return value;
59 d5f3c29c Michael Roth
}
60 d5f3c29c Michael Roth
61 d5f3c29c Michael Roth
static void qapi_dealloc_start_struct(Visitor *v, void **obj, const char *kind,
62 d5f3c29c Michael Roth
                                      const char *name, size_t unused,
63 d5f3c29c Michael Roth
                                      Error **errp)
64 d5f3c29c Michael Roth
{
65 d5f3c29c Michael Roth
    QapiDeallocVisitor *qov = to_qov(v);
66 d5f3c29c Michael Roth
    qapi_dealloc_push(qov, obj);
67 d5f3c29c Michael Roth
}
68 d5f3c29c Michael Roth
69 d5f3c29c Michael Roth
static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
70 d5f3c29c Michael Roth
{
71 d5f3c29c Michael Roth
    QapiDeallocVisitor *qov = to_qov(v);
72 d5f3c29c Michael Roth
    void **obj = qapi_dealloc_pop(qov);
73 d5f3c29c Michael Roth
    if (obj) {
74 7267c094 Anthony Liguori
        g_free(*obj);
75 d5f3c29c Michael Roth
    }
76 d5f3c29c Michael Roth
}
77 d5f3c29c Michael Roth
78 d5f3c29c Michael Roth
static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp)
79 d5f3c29c Michael Roth
{
80 5666dd19 Michael Roth
    QapiDeallocVisitor *qov = to_qov(v);
81 0b9d8542 Michael Roth
    qapi_dealloc_push(qov, NULL);
82 d5f3c29c Michael Roth
}
83 d5f3c29c Michael Roth
84 5666dd19 Michael Roth
static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **listp,
85 d5f3c29c Michael Roth
                                           Error **errp)
86 d5f3c29c Michael Roth
{
87 5666dd19 Michael Roth
    GenericList *list = *listp;
88 5666dd19 Michael Roth
    QapiDeallocVisitor *qov = to_qov(v);
89 0b9d8542 Michael Roth
    StackEntry *e = QTAILQ_FIRST(&qov->stack);
90 5666dd19 Michael Roth
91 0b9d8542 Michael Roth
    if (e && e->is_list_head) {
92 0b9d8542 Michael Roth
        e->is_list_head = false;
93 0b9d8542 Michael Roth
        return list;
94 5666dd19 Michael Roth
    }
95 5666dd19 Michael Roth
96 0b9d8542 Michael Roth
    if (list) {
97 0b9d8542 Michael Roth
        list = list->next;
98 0b9d8542 Michael Roth
        g_free(*listp);
99 0b9d8542 Michael Roth
        return list;
100 0b9d8542 Michael Roth
    }
101 0b9d8542 Michael Roth
102 0b9d8542 Michael Roth
    return NULL;
103 d5f3c29c Michael Roth
}
104 d5f3c29c Michael Roth
105 d5f3c29c Michael Roth
static void qapi_dealloc_end_list(Visitor *v, Error **errp)
106 d5f3c29c Michael Roth
{
107 0b9d8542 Michael Roth
    QapiDeallocVisitor *qov = to_qov(v);
108 0b9d8542 Michael Roth
    void *obj = qapi_dealloc_pop(qov);
109 0b9d8542 Michael Roth
    assert(obj == NULL); /* should've been list head tracker with no payload */
110 d5f3c29c Michael Roth
}
111 d5f3c29c Michael Roth
112 d5f3c29c Michael Roth
static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
113 d5f3c29c Michael Roth
                                  Error **errp)
114 d5f3c29c Michael Roth
{
115 d5f3c29c Michael Roth
    if (obj) {
116 7267c094 Anthony Liguori
        g_free(*obj);
117 d5f3c29c Michael Roth
    }
118 d5f3c29c Michael Roth
}
119 d5f3c29c Michael Roth
120 d5f3c29c Michael Roth
static void qapi_dealloc_type_int(Visitor *v, int64_t *obj, const char *name,
121 d5f3c29c Michael Roth
                                  Error **errp)
122 d5f3c29c Michael Roth
{
123 d5f3c29c Michael Roth
}
124 d5f3c29c Michael Roth
125 d5f3c29c Michael Roth
static void qapi_dealloc_type_bool(Visitor *v, bool *obj, const char *name,
126 d5f3c29c Michael Roth
                                   Error **errp)
127 d5f3c29c Michael Roth
{
128 d5f3c29c Michael Roth
}
129 d5f3c29c Michael Roth
130 d5f3c29c Michael Roth
static void qapi_dealloc_type_number(Visitor *v, double *obj, const char *name,
131 d5f3c29c Michael Roth
                                     Error **errp)
132 d5f3c29c Michael Roth
{
133 d5f3c29c Michael Roth
}
134 d5f3c29c Michael Roth
135 d5f3c29c Michael Roth
static void qapi_dealloc_type_enum(Visitor *v, int *obj, const char *strings[],
136 d5f3c29c Michael Roth
                                   const char *kind, const char *name,
137 d5f3c29c Michael Roth
                                   Error **errp)
138 d5f3c29c Michael Roth
{
139 d5f3c29c Michael Roth
}
140 d5f3c29c Michael Roth
141 d5f3c29c Michael Roth
Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
142 d5f3c29c Michael Roth
{
143 d5f3c29c Michael Roth
    return &v->visitor;
144 d5f3c29c Michael Roth
}
145 d5f3c29c Michael Roth
146 d5f3c29c Michael Roth
void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
147 d5f3c29c Michael Roth
{
148 7267c094 Anthony Liguori
    g_free(v);
149 d5f3c29c Michael Roth
}
150 d5f3c29c Michael Roth
151 d5f3c29c Michael Roth
QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
152 d5f3c29c Michael Roth
{
153 d5f3c29c Michael Roth
    QapiDeallocVisitor *v;
154 d5f3c29c Michael Roth
155 7267c094 Anthony Liguori
    v = g_malloc0(sizeof(*v));
156 d5f3c29c Michael Roth
157 d5f3c29c Michael Roth
    v->visitor.start_struct = qapi_dealloc_start_struct;
158 d5f3c29c Michael Roth
    v->visitor.end_struct = qapi_dealloc_end_struct;
159 d5f3c29c Michael Roth
    v->visitor.start_list = qapi_dealloc_start_list;
160 d5f3c29c Michael Roth
    v->visitor.next_list = qapi_dealloc_next_list;
161 d5f3c29c Michael Roth
    v->visitor.end_list = qapi_dealloc_end_list;
162 d5f3c29c Michael Roth
    v->visitor.type_enum = qapi_dealloc_type_enum;
163 d5f3c29c Michael Roth
    v->visitor.type_int = qapi_dealloc_type_int;
164 d5f3c29c Michael Roth
    v->visitor.type_bool = qapi_dealloc_type_bool;
165 d5f3c29c Michael Roth
    v->visitor.type_str = qapi_dealloc_type_str;
166 d5f3c29c Michael Roth
    v->visitor.type_number = qapi_dealloc_type_number;
167 d5f3c29c Michael Roth
168 d5f3c29c Michael Roth
    QTAILQ_INIT(&v->stack);
169 d5f3c29c Michael Roth
170 d5f3c29c Michael Roth
    return v;
171 d5f3c29c Michael Roth
}