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 16 struct QapiCloneVisitor { 17 Visitor visitor; 18 size_t depth; 19 }; 20 21 static QapiCloneVisitor *to_qcv(Visitor *v) 22 { 23 return container_of(v, QapiCloneVisitor, visitor); 24 } 25 26 static void qapi_clone_start_struct(Visitor *v, const char *name, void **obj, 27 size_t size, Error **errp) 28 { 29 QapiCloneVisitor *qcv = to_qcv(v); 30 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; 37 } 38 39 *obj = g_memdup(*obj, size); 40 qcv->depth++; 41 } 42 43 static void qapi_clone_end(Visitor *v, void **obj) 44 { 45 QapiCloneVisitor *qcv = to_qcv(v); 46 47 assert(qcv->depth); 48 if (obj) { 49 qcv->depth--; 50 } 51 } 52 53 static void qapi_clone_start_list(Visitor *v, const char *name, 54 GenericList **listp, size_t size, 55 Error **errp) 56 { 57 qapi_clone_start_struct(v, name, (void **)listp, size, errp); 58 } 59 60 static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail, 61 size_t size) 62 { 63 QapiCloneVisitor *qcv = to_qcv(v); 64 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; 69 } 70 71 static void qapi_clone_start_alternate(Visitor *v, const char *name, 72 GenericAlternate **obj, size_t size, 73 bool promote_int, Error **errp) 74 { 75 qapi_clone_start_struct(v, name, (void **)obj, size, errp); 76 } 77 78 static void qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj, 79 Error **errp) 80 { 81 QapiCloneVisitor *qcv = to_qcv(v); 82 83 assert(qcv->depth); 84 /* Value was already cloned by g_memdup() */ 85 } 86 87 static void qapi_clone_type_uint64(Visitor *v, const char *name, 88 uint64_t *obj, Error **errp) 89 { 90 QapiCloneVisitor *qcv = to_qcv(v); 91 92 assert(qcv->depth); 93 /* Value was already cloned by g_memdup() */ 94 } 95 96 static void qapi_clone_type_bool(Visitor *v, const char *name, bool *obj, 97 Error **errp) 98 { 99 QapiCloneVisitor *qcv = to_qcv(v); 100 101 assert(qcv->depth); 102 /* Value was already cloned by g_memdup() */ 103 } 104 105 static void qapi_clone_type_str(Visitor *v, const char *name, char **obj, 106 Error **errp) 107 { 108 QapiCloneVisitor *qcv = to_qcv(v); 109 110 assert(qcv->depth); 111 /* 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. 117 */ 118 *obj = g_strdup(*obj ?: ""); 119 } 120 121 static void qapi_clone_type_number(Visitor *v, const char *name, double *obj, 122 Error **errp) 123 { 124 QapiCloneVisitor *qcv = to_qcv(v); 125 126 assert(qcv->depth); 127 /* Value was already cloned by g_memdup() */ 128 } 129 130 static void qapi_clone_type_null(Visitor *v, const char *name, Error **errp) 131 { 132 QapiCloneVisitor *qcv = to_qcv(v); 133 134 assert(qcv->depth); 135 /* Nothing to do */ 136 } 137 138 static void qapi_clone_free(Visitor *v) 139 { 140 g_free(v); 141 } 142 143 static Visitor *qapi_clone_visitor_new(void) 144 { 145 QapiCloneVisitor *v; 146 147 v = g_malloc0(sizeof(*v)); 148 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; 164 165 return &v->visitor; 166 } 167 168 void *qapi_clone(const void *src, void (*visit_type)(Visitor *, const char *, 169 void **, Error **)) 170 { 171 Visitor *v; 172 void *dst = (void *) src; /* Cast away const */ 173 174 if (!src) { 175 return NULL; 176 } 177 178 v = qapi_clone_visitor_new(); 179 visit_type(v, NULL, &dst, &error_abort); 180 visit_free(v); 181 return dst; 182 } 183 184 void qapi_clone_members(void *dst, const void *src, size_t sz, 185 void (*visit_type_members)(Visitor *, void *, 186 Error **)) 187 { 188 Visitor *v; 189 190 v = qapi_clone_visitor_new(); 191 memcpy(dst, src, sz); 192 to_qcv(v)->depth++; 193 visit_type_members(v, dst, &error_abort); 194 visit_free(v); 195 } 196