virtio-net: Always set populate_hash
[qemu/armbru.git] / qapi / qapi-clone-visitor.c
blobbbf953698f380528b9fed20a2e2effd8e622cacb
1 /*
2 * Copy one QAPI object to another
4 * Copyright (C) 2016 Red Hat, Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "qapi/clone-visitor.h"
13 #include "qapi/visitor-impl.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qnull.h"
17 struct QapiCloneVisitor {
18 Visitor visitor;
19 size_t depth;
22 static QapiCloneVisitor *to_qcv(Visitor *v)
24 return container_of(v, QapiCloneVisitor, visitor);
27 static bool qapi_clone_start_struct(Visitor *v, const char *name, void **obj,
28 size_t size, Error **errp)
30 QapiCloneVisitor *qcv = to_qcv(v);
32 if (!obj) {
33 assert(qcv->depth);
34 /* Only possible when visiting an alternate's object
35 * branch. Nothing further to do here, since the earlier
36 * visit_start_alternate() already copied memory. */
37 return true;
40 *obj = g_memdup(*obj, size);
41 qcv->depth++;
42 return true;
45 static void qapi_clone_end(Visitor *v, void **obj)
47 QapiCloneVisitor *qcv = to_qcv(v);
49 assert(qcv->depth);
50 if (obj) {
51 qcv->depth--;
55 static bool qapi_clone_start_list(Visitor *v, const char *name,
56 GenericList **listp, size_t size,
57 Error **errp)
59 return qapi_clone_start_struct(v, name, (void **)listp, size, errp);
62 static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail,
63 size_t size)
65 QapiCloneVisitor *qcv = to_qcv(v);
67 assert(qcv->depth);
68 /* Unshare the tail of the list cloned by g_memdup() */
69 tail->next = g_memdup(tail->next, size);
70 return tail->next;
73 static bool qapi_clone_start_alternate(Visitor *v, const char *name,
74 GenericAlternate **obj, size_t size,
75 Error **errp)
77 return qapi_clone_start_struct(v, name, (void **)obj, size, errp);
80 static bool qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj,
81 Error **errp)
83 QapiCloneVisitor *qcv = to_qcv(v);
85 assert(qcv->depth);
86 /* Value was already cloned by g_memdup() */
87 return true;
90 static bool qapi_clone_type_uint64(Visitor *v, const char *name,
91 uint64_t *obj, Error **errp)
93 QapiCloneVisitor *qcv = to_qcv(v);
95 assert(qcv->depth);
96 /* Value was already cloned by g_memdup() */
97 return true;
100 static bool qapi_clone_type_bool(Visitor *v, const char *name, bool *obj,
101 Error **errp)
103 QapiCloneVisitor *qcv = to_qcv(v);
105 assert(qcv->depth);
106 /* Value was already cloned by g_memdup() */
107 return true;
110 static bool qapi_clone_type_str(Visitor *v, const char *name, char **obj,
111 Error **errp)
113 QapiCloneVisitor *qcv = to_qcv(v);
115 assert(qcv->depth);
117 * Pointer was already cloned by g_memdup; create fresh copy.
118 * Note that as long as qobject-output-visitor accepts NULL instead of
119 * "", then we must do likewise. However, we want to obey the
120 * input visitor semantics of never producing NULL when the empty
121 * string is intended.
123 *obj = g_strdup(*obj ?: "");
124 return true;
127 static bool qapi_clone_type_number(Visitor *v, const char *name, double *obj,
128 Error **errp)
130 QapiCloneVisitor *qcv = to_qcv(v);
132 assert(qcv->depth);
133 /* Value was already cloned by g_memdup() */
134 return true;
137 static bool qapi_clone_type_null(Visitor *v, const char *name, QNull **obj,
138 Error **errp)
140 QapiCloneVisitor *qcv = to_qcv(v);
142 assert(qcv->depth);
143 *obj = qnull();
144 return true;
147 static void qapi_clone_free(Visitor *v)
149 g_free(v);
152 Visitor *qapi_clone_visitor_new(void)
154 QapiCloneVisitor *v;
156 v = g_malloc0(sizeof(*v));
158 v->visitor.type = VISITOR_CLONE;
159 v->visitor.start_struct = qapi_clone_start_struct;
160 v->visitor.end_struct = qapi_clone_end;
161 v->visitor.start_list = qapi_clone_start_list;
162 v->visitor.next_list = qapi_clone_next_list;
163 v->visitor.end_list = qapi_clone_end;
164 v->visitor.start_alternate = qapi_clone_start_alternate;
165 v->visitor.end_alternate = qapi_clone_end;
166 v->visitor.type_int64 = qapi_clone_type_int64;
167 v->visitor.type_uint64 = qapi_clone_type_uint64;
168 v->visitor.type_bool = qapi_clone_type_bool;
169 v->visitor.type_str = qapi_clone_type_str;
170 v->visitor.type_number = qapi_clone_type_number;
171 v->visitor.type_null = qapi_clone_type_null;
172 v->visitor.free = qapi_clone_free;
174 return &v->visitor;
177 Visitor *qapi_clone_members_visitor_new(void)
179 Visitor *v = qapi_clone_visitor_new();
180 to_qcv(v)->depth++;
181 return v;