qapi-clone-visitor.c 5.2 KB

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