4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
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.
15 #include "qemu/osdep.h"
16 #include "qapi/dealloc-visitor.h"
17 #include "qemu/queue.h"
18 #include "qemu-common.h"
19 #include "qapi/qmp/types.h"
20 #include "qapi/visitor-impl.h"
22 typedef struct StackEntry
25 QTAILQ_ENTRY(StackEntry
) node
;
28 struct QapiDeallocVisitor
31 QTAILQ_HEAD(, StackEntry
) stack
;
34 static QapiDeallocVisitor
*to_qov(Visitor
*v
)
36 return container_of(v
, QapiDeallocVisitor
, visitor
);
39 static void qapi_dealloc_push(QapiDeallocVisitor
*qov
, void *value
)
41 StackEntry
*e
= g_malloc0(sizeof(*e
));
45 QTAILQ_INSERT_HEAD(&qov
->stack
, e
, node
);
48 static void *qapi_dealloc_pop(QapiDeallocVisitor
*qov
)
50 StackEntry
*e
= QTAILQ_FIRST(&qov
->stack
);
52 QTAILQ_REMOVE(&qov
->stack
, e
, node
);
58 static void qapi_dealloc_start_struct(Visitor
*v
, const char *name
, void **obj
,
59 size_t unused
, Error
**errp
)
61 QapiDeallocVisitor
*qov
= to_qov(v
);
62 qapi_dealloc_push(qov
, obj
);
65 static void qapi_dealloc_end_struct(Visitor
*v
)
67 QapiDeallocVisitor
*qov
= to_qov(v
);
68 void **obj
= qapi_dealloc_pop(qov
);
74 static void qapi_dealloc_start_alternate(Visitor
*v
, const char *name
,
75 GenericAlternate
**obj
, size_t size
,
76 bool promote_int
, Error
**errp
)
78 QapiDeallocVisitor
*qov
= to_qov(v
);
79 qapi_dealloc_push(qov
, obj
);
82 static void qapi_dealloc_end_alternate(Visitor
*v
)
84 QapiDeallocVisitor
*qov
= to_qov(v
);
85 void **obj
= qapi_dealloc_pop(qov
);
91 static void qapi_dealloc_start_list(Visitor
*v
, const char *name
,
92 GenericList
**list
, size_t size
,
97 static GenericList
*qapi_dealloc_next_list(Visitor
*v
, GenericList
*tail
,
100 GenericList
*next
= tail
->next
;
105 static void qapi_dealloc_end_list(Visitor
*v
)
109 static void qapi_dealloc_type_str(Visitor
*v
, const char *name
, char **obj
,
117 static void qapi_dealloc_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
122 static void qapi_dealloc_type_uint64(Visitor
*v
, const char *name
,
123 uint64_t *obj
, Error
**errp
)
127 static void qapi_dealloc_type_bool(Visitor
*v
, const char *name
, bool *obj
,
132 static void qapi_dealloc_type_number(Visitor
*v
, const char *name
, double *obj
,
137 static void qapi_dealloc_type_anything(Visitor
*v
, const char *name
,
138 QObject
**obj
, Error
**errp
)
141 qobject_decref(*obj
);
145 static void qapi_dealloc_type_null(Visitor
*v
, const char *name
, Error
**errp
)
149 Visitor
*qapi_dealloc_get_visitor(QapiDeallocVisitor
*v
)
154 void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor
*v
)
159 QapiDeallocVisitor
*qapi_dealloc_visitor_new(void)
161 QapiDeallocVisitor
*v
;
163 v
= g_malloc0(sizeof(*v
));
165 v
->visitor
.type
= VISITOR_DEALLOC
;
166 v
->visitor
.start_struct
= qapi_dealloc_start_struct
;
167 v
->visitor
.end_struct
= qapi_dealloc_end_struct
;
168 v
->visitor
.start_alternate
= qapi_dealloc_start_alternate
;
169 v
->visitor
.end_alternate
= qapi_dealloc_end_alternate
;
170 v
->visitor
.start_list
= qapi_dealloc_start_list
;
171 v
->visitor
.next_list
= qapi_dealloc_next_list
;
172 v
->visitor
.end_list
= qapi_dealloc_end_list
;
173 v
->visitor
.type_int64
= qapi_dealloc_type_int64
;
174 v
->visitor
.type_uint64
= qapi_dealloc_type_uint64
;
175 v
->visitor
.type_bool
= qapi_dealloc_type_bool
;
176 v
->visitor
.type_str
= qapi_dealloc_type_str
;
177 v
->visitor
.type_number
= qapi_dealloc_type_number
;
178 v
->visitor
.type_any
= qapi_dealloc_type_anything
;
179 v
->visitor
.type_null
= qapi_dealloc_type_null
;
181 QTAILQ_INIT(&v
->stack
);