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.
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
{
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
);
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. */
39 *obj
= g_memdup(*obj
, size
);
43 static void qapi_clone_end(Visitor
*v
, void **obj
)
45 QapiCloneVisitor
*qcv
= to_qcv(v
);
53 static void qapi_clone_start_list(Visitor
*v
, const char *name
,
54 GenericList
**listp
, size_t size
,
57 qapi_clone_start_struct(v
, name
, (void **)listp
, size
, errp
);
60 static GenericList
*qapi_clone_next_list(Visitor
*v
, GenericList
*tail
,
63 QapiCloneVisitor
*qcv
= to_qcv(v
);
66 /* Unshare the tail of the list cloned by g_memdup() */
67 tail
->next
= g_memdup(tail
->next
, size
);
71 static void qapi_clone_start_alternate(Visitor
*v
, const char *name
,
72 GenericAlternate
**obj
, size_t size
,
73 bool promote_int
, 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
,
81 QapiCloneVisitor
*qcv
= to_qcv(v
);
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
);
93 /* Value was already cloned by g_memdup() */
96 static void qapi_clone_type_bool(Visitor
*v
, const char *name
, bool *obj
,
99 QapiCloneVisitor
*qcv
= to_qcv(v
);
102 /* Value was already cloned by g_memdup() */
105 static void qapi_clone_type_str(Visitor
*v
, const char *name
, char **obj
,
108 QapiCloneVisitor
*qcv
= to_qcv(v
);
112 * Pointer was already cloned by g_memdup; create fresh copy.
113 * Note that as long as qmp-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
,
124 QapiCloneVisitor
*qcv
= to_qcv(v
);
127 /* Value was already cloned by g_memdup() */
130 static void qapi_clone_type_null(Visitor
*v
, const char *name
, Error
**errp
)
132 QapiCloneVisitor
*qcv
= to_qcv(v
);
138 static void qapi_clone_free(Visitor
*v
)
143 static Visitor
*qapi_clone_visitor_new(void)
147 v
= g_malloc0(sizeof(*v
));
149 v
->visitor
.type
= VISITOR_CLONE
;
150 v
->visitor
.start_struct
= qapi_clone_start_struct
;
151 v
->visitor
.end_struct
= qapi_clone_end
;
152 v
->visitor
.start_list
= qapi_clone_start_list
;
153 v
->visitor
.next_list
= qapi_clone_next_list
;
154 v
->visitor
.end_list
= qapi_clone_end
;
155 v
->visitor
.start_alternate
= qapi_clone_start_alternate
;
156 v
->visitor
.end_alternate
= qapi_clone_end
;
157 v
->visitor
.type_int64
= qapi_clone_type_int64
;
158 v
->visitor
.type_uint64
= qapi_clone_type_uint64
;
159 v
->visitor
.type_bool
= qapi_clone_type_bool
;
160 v
->visitor
.type_str
= qapi_clone_type_str
;
161 v
->visitor
.type_number
= qapi_clone_type_number
;
162 v
->visitor
.type_null
= qapi_clone_type_null
;
163 v
->visitor
.free
= qapi_clone_free
;
168 void *qapi_clone(const void *src
, void (*visit_type
)(Visitor
*, const char *,
172 void *dst
= (void *) src
; /* Cast away const */
178 v
= qapi_clone_visitor_new();
179 visit_type(v
, NULL
, &dst
, &error_abort
);