xref: /openbmc/qemu/qapi/qapi-clone-visitor.c (revision dc5bd18f)
1 /*
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"
15 #include "qapi/qmp/qnull.h"
16 
17 struct QapiCloneVisitor {
18     Visitor visitor;
19     size_t depth;
20 };
21 
22 static QapiCloneVisitor *to_qcv(Visitor *v)
23 {
24     return container_of(v, QapiCloneVisitor, visitor);
25 }
26 
27 static void qapi_clone_start_struct(Visitor *v, const char *name, void **obj,
28                                     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. */
37         return;
38     }
39 
40     *obj = g_memdup(*obj, size);
41     qcv->depth++;
42 }
43 
44 static void qapi_clone_end(Visitor *v, void **obj)
45 {
46     QapiCloneVisitor *qcv = to_qcv(v);
47 
48     assert(qcv->depth);
49     if (obj) {
50         qcv->depth--;
51     }
52 }
53 
54 static void qapi_clone_start_list(Visitor *v, const char *name,
55                                   GenericList **listp, size_t size,
56                                   Error **errp)
57 {
58     qapi_clone_start_struct(v, name, (void **)listp, size, errp);
59 }
60 
61 static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail,
62                                          size_t size)
63 {
64     QapiCloneVisitor *qcv = to_qcv(v);
65 
66     assert(qcv->depth);
67     /* Unshare the tail of the list cloned by g_memdup() */
68     tail->next = g_memdup(tail->next, size);
69     return tail->next;
70 }
71 
72 static void qapi_clone_start_alternate(Visitor *v, const char *name,
73                                        GenericAlternate **obj, size_t size,
74                                        Error **errp)
75 {
76     qapi_clone_start_struct(v, name, (void **)obj, size, errp);
77 }
78 
79 static void qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj,
80                                    Error **errp)
81 {
82     QapiCloneVisitor *qcv = to_qcv(v);
83 
84     assert(qcv->depth);
85     /* Value was already cloned by g_memdup() */
86 }
87 
88 static void qapi_clone_type_uint64(Visitor *v, const char *name,
89                                     uint64_t *obj, Error **errp)
90 {
91     QapiCloneVisitor *qcv = to_qcv(v);
92 
93     assert(qcv->depth);
94     /* Value was already cloned by g_memdup() */
95 }
96 
97 static void qapi_clone_type_bool(Visitor *v, const char *name, bool *obj,
98                                   Error **errp)
99 {
100     QapiCloneVisitor *qcv = to_qcv(v);
101 
102     assert(qcv->depth);
103     /* Value was already cloned by g_memdup() */
104 }
105 
106 static void qapi_clone_type_str(Visitor *v, const char *name, char **obj,
107                                  Error **errp)
108 {
109     QapiCloneVisitor *qcv = to_qcv(v);
110 
111     assert(qcv->depth);
112     /*
113      * Pointer was already cloned by g_memdup; create fresh copy.
114      * Note that as long as qobject-output-visitor accepts NULL instead of
115      * "", then we must do likewise. However, we want to obey the
116      * input visitor semantics of never producing NULL when the empty
117      * string is intended.
118      */
119     *obj = g_strdup(*obj ?: "");
120 }
121 
122 static void qapi_clone_type_number(Visitor *v, const char *name, double *obj,
123                                     Error **errp)
124 {
125     QapiCloneVisitor *qcv = to_qcv(v);
126 
127     assert(qcv->depth);
128     /* Value was already cloned by g_memdup() */
129 }
130 
131 static void qapi_clone_type_null(Visitor *v, const char *name, QNull **obj,
132                                  Error **errp)
133 {
134     QapiCloneVisitor *qcv = to_qcv(v);
135 
136     assert(qcv->depth);
137     *obj = qnull();
138 }
139 
140 static void qapi_clone_free(Visitor *v)
141 {
142     g_free(v);
143 }
144 
145 static Visitor *qapi_clone_visitor_new(void)
146 {
147     QapiCloneVisitor *v;
148 
149     v = g_malloc0(sizeof(*v));
150 
151     v->visitor.type = VISITOR_CLONE;
152     v->visitor.start_struct = qapi_clone_start_struct;
153     v->visitor.end_struct = qapi_clone_end;
154     v->visitor.start_list = qapi_clone_start_list;
155     v->visitor.next_list = qapi_clone_next_list;
156     v->visitor.end_list = qapi_clone_end;
157     v->visitor.start_alternate = qapi_clone_start_alternate;
158     v->visitor.end_alternate = qapi_clone_end;
159     v->visitor.type_int64 = qapi_clone_type_int64;
160     v->visitor.type_uint64 = qapi_clone_type_uint64;
161     v->visitor.type_bool = qapi_clone_type_bool;
162     v->visitor.type_str = qapi_clone_type_str;
163     v->visitor.type_number = qapi_clone_type_number;
164     v->visitor.type_null = qapi_clone_type_null;
165     v->visitor.free = qapi_clone_free;
166 
167     return &v->visitor;
168 }
169 
170 void *qapi_clone(const void *src, void (*visit_type)(Visitor *, const char *,
171                                                      void **, Error **))
172 {
173     Visitor *v;
174     void *dst = (void *) src; /* Cast away const */
175 
176     if (!src) {
177         return NULL;
178     }
179 
180     v = qapi_clone_visitor_new();
181     visit_type(v, NULL, &dst, &error_abort);
182     visit_free(v);
183     return dst;
184 }
185 
186 void qapi_clone_members(void *dst, const void *src, size_t sz,
187                         void (*visit_type_members)(Visitor *, void *,
188                                                    Error **))
189 {
190     Visitor *v;
191 
192     v = qapi_clone_visitor_new();
193     memcpy(dst, src, sz);
194     to_qcv(v)->depth++;
195     visit_type_members(v, dst, &error_abort);
196     visit_free(v);
197 }
198