MAINTAINERS/s390x: add terminal3270.c
[qemu.git] / qapi / qapi-clone-visitor.c
blobd8b62792bc19f034c8fa977f3d3e1d245f591179
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"
16 struct QapiCloneVisitor {
17 Visitor visitor;
18 size_t depth;
21 static QapiCloneVisitor *to_qcv(Visitor *v)
23 return container_of(v, QapiCloneVisitor, visitor);
26 static void qapi_clone_start_struct(Visitor *v, const char *name, void **obj,
27 size_t size, Error **errp)
29 QapiCloneVisitor *qcv = to_qcv(v);
31 if (!obj) {
32 assert(qcv->depth);
33 /* Only possible when visiting an alternate's object
34 * branch. Nothing further to do here, since the earlier
35 * visit_start_alternate() already copied memory. */
36 return;
39 *obj = g_memdup(*obj, size);
40 qcv->depth++;
43 static void qapi_clone_end(Visitor *v, void **obj)
45 QapiCloneVisitor *qcv = to_qcv(v);
47 assert(qcv->depth);
48 if (obj) {
49 qcv->depth--;
53 static void qapi_clone_start_list(Visitor *v, const char *name,
54 GenericList **listp, size_t size,
55 Error **errp)
57 qapi_clone_start_struct(v, name, (void **)listp, size, errp);
60 static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail,
61 size_t size)
63 QapiCloneVisitor *qcv = to_qcv(v);
65 assert(qcv->depth);
66 /* Unshare the tail of the list cloned by g_memdup() */
67 tail->next = g_memdup(tail->next, size);
68 return tail->next;
71 static void qapi_clone_start_alternate(Visitor *v, const char *name,
72 GenericAlternate **obj, size_t size,
73 Error **errp)
75 qapi_clone_start_struct(v, name, (void **)obj, size, errp);
78 static void qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj,
79 Error **errp)
81 QapiCloneVisitor *qcv = to_qcv(v);
83 assert(qcv->depth);
84 /* Value was already cloned by g_memdup() */
87 static void qapi_clone_type_uint64(Visitor *v, const char *name,
88 uint64_t *obj, Error **errp)
90 QapiCloneVisitor *qcv = to_qcv(v);
92 assert(qcv->depth);
93 /* Value was already cloned by g_memdup() */
96 static void qapi_clone_type_bool(Visitor *v, const char *name, bool *obj,
97 Error **errp)
99 QapiCloneVisitor *qcv = to_qcv(v);
101 assert(qcv->depth);
102 /* Value was already cloned by g_memdup() */
105 static void qapi_clone_type_str(Visitor *v, const char *name, char **obj,
106 Error **errp)
108 QapiCloneVisitor *qcv = to_qcv(v);
110 assert(qcv->depth);
112 * Pointer was already cloned by g_memdup; create fresh copy.
113 * Note that as long as qobject-output-visitor accepts NULL instead of
114 * "", then we must do likewise. However, we want to obey the
115 * input visitor semantics of never producing NULL when the empty
116 * string is intended.
118 *obj = g_strdup(*obj ?: "");
121 static void qapi_clone_type_number(Visitor *v, const char *name, double *obj,
122 Error **errp)
124 QapiCloneVisitor *qcv = to_qcv(v);
126 assert(qcv->depth);
127 /* Value was already cloned by g_memdup() */
130 static void qapi_clone_type_null(Visitor *v, const char *name, QNull **obj,
131 Error **errp)
133 QapiCloneVisitor *qcv = to_qcv(v);
135 assert(qcv->depth);
136 *obj = qnull();
139 static void qapi_clone_free(Visitor *v)
141 g_free(v);
144 static Visitor *qapi_clone_visitor_new(void)
146 QapiCloneVisitor *v;
148 v = g_malloc0(sizeof(*v));
150 v->visitor.type = VISITOR_CLONE;
151 v->visitor.start_struct = qapi_clone_start_struct;
152 v->visitor.end_struct = qapi_clone_end;
153 v->visitor.start_list = qapi_clone_start_list;
154 v->visitor.next_list = qapi_clone_next_list;
155 v->visitor.end_list = qapi_clone_end;
156 v->visitor.start_alternate = qapi_clone_start_alternate;
157 v->visitor.end_alternate = qapi_clone_end;
158 v->visitor.type_int64 = qapi_clone_type_int64;
159 v->visitor.type_uint64 = qapi_clone_type_uint64;
160 v->visitor.type_bool = qapi_clone_type_bool;
161 v->visitor.type_str = qapi_clone_type_str;
162 v->visitor.type_number = qapi_clone_type_number;
163 v->visitor.type_null = qapi_clone_type_null;
164 v->visitor.free = qapi_clone_free;
166 return &v->visitor;
169 void *qapi_clone(const void *src, void (*visit_type)(Visitor *, const char *,
170 void **, Error **))
172 Visitor *v;
173 void *dst = (void *) src; /* Cast away const */
175 if (!src) {
176 return NULL;
179 v = qapi_clone_visitor_new();
180 visit_type(v, NULL, &dst, &error_abort);
181 visit_free(v);
182 return dst;
185 void qapi_clone_members(void *dst, const void *src, size_t sz,
186 void (*visit_type_members)(Visitor *, void *,
187 Error **))
189 Visitor *v;
191 v = qapi_clone_visitor_new();
192 memcpy(dst, src, sz);
193 to_qcv(v)->depth++;
194 visit_type_members(v, dst, &error_abort);
195 visit_free(v);