blob: c45c5caa3b89aa7fc0c93ecb2ab8d70251dd7e06 [file] [log] [blame]
Eric Blakea15fcc32016-06-09 10:48:44 -06001/*
2 * Copy one QAPI object to another
3 *
4 * Copyright (C) 2016 Red Hat, Inc.
5 *
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.
8 *
9 */
10
11#include "qemu/osdep.h"
12#include "qapi/clone-visitor.h"
13#include "qapi/visitor-impl.h"
14#include "qapi/error.h"
Max Reitz84be6292017-11-14 19:01:23 +010015#include "qapi/qmp/qnull.h"
Eric Blakea15fcc32016-06-09 10:48:44 -060016
17struct QapiCloneVisitor {
18 Visitor visitor;
19 size_t depth;
20};
21
22static QapiCloneVisitor *to_qcv(Visitor *v)
23{
24 return container_of(v, QapiCloneVisitor, visitor);
25}
26
Markus Armbruster012d4c92020-07-07 18:05:45 +020027static bool qapi_clone_start_struct(Visitor *v, const char *name, void **obj,
Eric Blakea15fcc32016-06-09 10:48:44 -060028 size_t size, Error **errp)
29{
30 QapiCloneVisitor *qcv = to_qcv(v);
31
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. */
Markus Armbruster012d4c92020-07-07 18:05:45 +020037 return true;
Eric Blakea15fcc32016-06-09 10:48:44 -060038 }
39
40 *obj = g_memdup(*obj, size);
41 qcv->depth++;
Markus Armbruster012d4c92020-07-07 18:05:45 +020042 return true;
Eric Blakea15fcc32016-06-09 10:48:44 -060043}
44
45static void qapi_clone_end(Visitor *v, void **obj)
46{
47 QapiCloneVisitor *qcv = to_qcv(v);
48
49 assert(qcv->depth);
50 if (obj) {
51 qcv->depth--;
52 }
53}
54
Markus Armbruster012d4c92020-07-07 18:05:45 +020055static bool qapi_clone_start_list(Visitor *v, const char *name,
Eric Blakea15fcc32016-06-09 10:48:44 -060056 GenericList **listp, size_t size,
57 Error **errp)
58{
Markus Armbruster012d4c92020-07-07 18:05:45 +020059 return qapi_clone_start_struct(v, name, (void **)listp, size, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -060060}
61
62static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail,
63 size_t size)
64{
65 QapiCloneVisitor *qcv = to_qcv(v);
66
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;
71}
72
Markus Armbruster012d4c92020-07-07 18:05:45 +020073static bool qapi_clone_start_alternate(Visitor *v, const char *name,
Eric Blakea15fcc32016-06-09 10:48:44 -060074 GenericAlternate **obj, size_t size,
Marc-André Lureau60390d22017-06-07 20:35:59 +040075 Error **errp)
Eric Blakea15fcc32016-06-09 10:48:44 -060076{
Markus Armbruster012d4c92020-07-07 18:05:45 +020077 return qapi_clone_start_struct(v, name, (void **)obj, size, errp);
Eric Blakea15fcc32016-06-09 10:48:44 -060078}
79
Markus Armbruster012d4c92020-07-07 18:05:45 +020080static bool qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blakea15fcc32016-06-09 10:48:44 -060081 Error **errp)
82{
83 QapiCloneVisitor *qcv = to_qcv(v);
84
85 assert(qcv->depth);
86 /* Value was already cloned by g_memdup() */
Markus Armbruster012d4c92020-07-07 18:05:45 +020087 return true;
Eric Blakea15fcc32016-06-09 10:48:44 -060088}
89
Markus Armbruster012d4c92020-07-07 18:05:45 +020090static bool qapi_clone_type_uint64(Visitor *v, const char *name,
91 uint64_t *obj, Error **errp)
92{
93 QapiCloneVisitor *qcv = to_qcv(v);
94
95 assert(qcv->depth);
96 /* Value was already cloned by g_memdup() */
97 return true;
98}
99
100static bool qapi_clone_type_bool(Visitor *v, const char *name, bool *obj,
Eric Blakea15fcc32016-06-09 10:48:44 -0600101 Error **errp)
102{
103 QapiCloneVisitor *qcv = to_qcv(v);
104
105 assert(qcv->depth);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200106 /* Value was already cloned by g_memdup() */
107 return true;
108}
109
110static bool qapi_clone_type_str(Visitor *v, const char *name, char **obj,
111 Error **errp)
112{
113 QapiCloneVisitor *qcv = to_qcv(v);
114
115 assert(qcv->depth);
Eric Blakea15fcc32016-06-09 10:48:44 -0600116 /*
117 * Pointer was already cloned by g_memdup; create fresh copy.
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100118 * Note that as long as qobject-output-visitor accepts NULL instead of
Eric Blakea15fcc32016-06-09 10:48:44 -0600119 * "", 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.
122 */
123 *obj = g_strdup(*obj ?: "");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200124 return true;
Eric Blakea15fcc32016-06-09 10:48:44 -0600125}
126
Markus Armbruster012d4c92020-07-07 18:05:45 +0200127static bool qapi_clone_type_number(Visitor *v, const char *name, double *obj,
128 Error **errp)
Eric Blakea15fcc32016-06-09 10:48:44 -0600129{
130 QapiCloneVisitor *qcv = to_qcv(v);
131
132 assert(qcv->depth);
133 /* Value was already cloned by g_memdup() */
Markus Armbruster012d4c92020-07-07 18:05:45 +0200134 return true;
Eric Blakea15fcc32016-06-09 10:48:44 -0600135}
136
Markus Armbruster012d4c92020-07-07 18:05:45 +0200137static bool qapi_clone_type_null(Visitor *v, const char *name, QNull **obj,
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200138 Error **errp)
Eric Blakea15fcc32016-06-09 10:48:44 -0600139{
140 QapiCloneVisitor *qcv = to_qcv(v);
141
142 assert(qcv->depth);
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200143 *obj = qnull();
Markus Armbruster012d4c92020-07-07 18:05:45 +0200144 return true;
Eric Blakea15fcc32016-06-09 10:48:44 -0600145}
146
147static void qapi_clone_free(Visitor *v)
148{
149 g_free(v);
150}
151
152static Visitor *qapi_clone_visitor_new(void)
153{
154 QapiCloneVisitor *v;
155
156 v = g_malloc0(sizeof(*v));
157
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;
173
174 return &v->visitor;
175}
176
Markus Armbruster012d4c92020-07-07 18:05:45 +0200177void *qapi_clone(const void *src, bool (*visit_type)(Visitor *, const char *,
Eric Blakea15fcc32016-06-09 10:48:44 -0600178 void **, Error **))
179{
180 Visitor *v;
181 void *dst = (void *) src; /* Cast away const */
182
183 if (!src) {
184 return NULL;
185 }
186
187 v = qapi_clone_visitor_new();
188 visit_type(v, NULL, &dst, &error_abort);
189 visit_free(v);
190 return dst;
191}
Markus Armbruster4626a192017-04-26 09:36:38 +0200192
193void qapi_clone_members(void *dst, const void *src, size_t sz,
Markus Armbruster012d4c92020-07-07 18:05:45 +0200194 bool (*visit_type_members)(Visitor *, void *,
Markus Armbruster4626a192017-04-26 09:36:38 +0200195 Error **))
196{
197 Visitor *v;
198
199 v = qapi_clone_visitor_new();
200 memcpy(dst, src, sz);
201 to_qcv(v)->depth++;
202 visit_type_members(v, dst, &error_abort);
203 visit_free(v);
204}