qapi-clone-visitor.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "qemu/osdep.h"
  11. #include "qapi/clone-visitor.h"
  12. #include "qapi/visitor-impl.h"
  13. #include "qapi/error.h"
  14. #include "qapi/qmp/qnull.h"
  15. struct QapiCloneVisitor {
  16. Visitor visitor;
  17. size_t depth;
  18. };
  19. static QapiCloneVisitor *to_qcv(Visitor *v)
  20. {
  21. return container_of(v, QapiCloneVisitor, visitor);
  22. }
  23. static void qapi_clone_start_struct(Visitor *v, const char *name, void **obj,
  24. size_t size, Error **errp)
  25. {
  26. QapiCloneVisitor *qcv = to_qcv(v);
  27. if (!obj) {
  28. assert(qcv->depth);
  29. /* Only possible when visiting an alternate's object
  30. * branch. Nothing further to do here, since the earlier
  31. * visit_start_alternate() already copied memory. */
  32. return;
  33. }
  34. *obj = g_memdup(*obj, size);
  35. qcv->depth++;
  36. }
  37. static void qapi_clone_end(Visitor *v, void **obj)
  38. {
  39. QapiCloneVisitor *qcv = to_qcv(v);
  40. assert(qcv->depth);
  41. if (obj) {
  42. qcv->depth--;
  43. }
  44. }
  45. static void qapi_clone_start_list(Visitor *v, const char *name,
  46. GenericList **listp, size_t size,
  47. Error **errp)
  48. {
  49. qapi_clone_start_struct(v, name, (void **)listp, size, errp);
  50. }
  51. static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail,
  52. size_t size)
  53. {
  54. QapiCloneVisitor *qcv = to_qcv(v);
  55. assert(qcv->depth);
  56. /* Unshare the tail of the list cloned by g_memdup() */
  57. tail->next = g_memdup(tail->next, size);
  58. return tail->next;
  59. }
  60. static void qapi_clone_start_alternate(Visitor *v, const char *name,
  61. GenericAlternate **obj, size_t size,
  62. Error **errp)
  63. {
  64. qapi_clone_start_struct(v, name, (void **)obj, size, errp);
  65. }
  66. static void qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj,
  67. Error **errp)
  68. {
  69. QapiCloneVisitor *qcv = to_qcv(v);
  70. assert(qcv->depth);
  71. /* Value was already cloned by g_memdup() */
  72. }
  73. static void qapi_clone_type_uint64(Visitor *v, const char *name,
  74. uint64_t *obj, Error **errp)
  75. {
  76. QapiCloneVisitor *qcv = to_qcv(v);
  77. assert(qcv->depth);
  78. /* Value was already cloned by g_memdup() */
  79. }
  80. static void qapi_clone_type_bool(Visitor *v, const char *name, bool *obj,
  81. Error **errp)
  82. {
  83. QapiCloneVisitor *qcv = to_qcv(v);
  84. assert(qcv->depth);
  85. /* Value was already cloned by g_memdup() */
  86. }
  87. static void qapi_clone_type_str(Visitor *v, const char *name, char **obj,
  88. Error **errp)
  89. {
  90. QapiCloneVisitor *qcv = to_qcv(v);
  91. assert(qcv->depth);
  92. /*
  93. * Pointer was already cloned by g_memdup; create fresh copy.
  94. * Note that as long as qobject-output-visitor accepts NULL instead of
  95. * "", then we must do likewise. However, we want to obey the
  96. * input visitor semantics of never producing NULL when the empty
  97. * string is intended.
  98. */
  99. *obj = g_strdup(*obj ?: "");
  100. }
  101. static void qapi_clone_type_number(Visitor *v, const char *name, double *obj,
  102. Error **errp)
  103. {
  104. QapiCloneVisitor *qcv = to_qcv(v);
  105. assert(qcv->depth);
  106. /* Value was already cloned by g_memdup() */
  107. }
  108. static void qapi_clone_type_null(Visitor *v, const char *name, QNull **obj,
  109. Error **errp)
  110. {
  111. QapiCloneVisitor *qcv = to_qcv(v);
  112. assert(qcv->depth);
  113. *obj = qnull();
  114. }
  115. static void qapi_clone_free(Visitor *v)
  116. {
  117. g_free(v);
  118. }
  119. static Visitor *qapi_clone_visitor_new(void)
  120. {
  121. QapiCloneVisitor *v;
  122. v = g_malloc0(sizeof(*v));
  123. v->visitor.type = VISITOR_CLONE;
  124. v->visitor.start_struct = qapi_clone_start_struct;
  125. v->visitor.end_struct = qapi_clone_end;
  126. v->visitor.start_list = qapi_clone_start_list;
  127. v->visitor.next_list = qapi_clone_next_list;
  128. v->visitor.end_list = qapi_clone_end;
  129. v->visitor.start_alternate = qapi_clone_start_alternate;
  130. v->visitor.end_alternate = qapi_clone_end;
  131. v->visitor.type_int64 = qapi_clone_type_int64;
  132. v->visitor.type_uint64 = qapi_clone_type_uint64;
  133. v->visitor.type_bool = qapi_clone_type_bool;
  134. v->visitor.type_str = qapi_clone_type_str;
  135. v->visitor.type_number = qapi_clone_type_number;
  136. v->visitor.type_null = qapi_clone_type_null;
  137. v->visitor.free = qapi_clone_free;
  138. return &v->visitor;
  139. }
  140. void *qapi_clone(const void *src, void (*visit_type)(Visitor *, const char *,
  141. void **, Error **))
  142. {
  143. Visitor *v;
  144. void *dst = (void *) src; /* Cast away const */
  145. if (!src) {
  146. return NULL;
  147. }
  148. v = qapi_clone_visitor_new();
  149. visit_type(v, NULL, &dst, &error_abort);
  150. visit_free(v);
  151. return dst;
  152. }
  153. void qapi_clone_members(void *dst, const void *src, size_t sz,
  154. void (*visit_type_members)(Visitor *, void *,
  155. Error **))
  156. {
  157. Visitor *v;
  158. v = qapi_clone_visitor_new();
  159. memcpy(dst, src, sz);
  160. to_qcv(v)->depth++;
  161. visit_type_members(v, dst, &error_abort);
  162. visit_free(v);
  163. }