2
0

qapi-dealloc-visitor.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Dealloc Visitor
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Michael Roth <mdroth@linux.vnet.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. *
  12. */
  13. #include "qapi/dealloc-visitor.h"
  14. #include "qemu/queue.h"
  15. #include "qemu-common.h"
  16. #include "qapi/qmp/types.h"
  17. #include "qapi/visitor-impl.h"
  18. typedef struct StackEntry
  19. {
  20. void *value;
  21. bool is_list_head;
  22. QTAILQ_ENTRY(StackEntry) node;
  23. } StackEntry;
  24. struct QapiDeallocVisitor
  25. {
  26. Visitor visitor;
  27. QTAILQ_HEAD(, StackEntry) stack;
  28. bool is_list_head;
  29. };
  30. static QapiDeallocVisitor *to_qov(Visitor *v)
  31. {
  32. return container_of(v, QapiDeallocVisitor, visitor);
  33. }
  34. static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
  35. {
  36. StackEntry *e = g_malloc0(sizeof(*e));
  37. e->value = value;
  38. /* see if we're just pushing a list head tracker */
  39. if (value == NULL) {
  40. e->is_list_head = true;
  41. }
  42. QTAILQ_INSERT_HEAD(&qov->stack, e, node);
  43. }
  44. static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
  45. {
  46. StackEntry *e = QTAILQ_FIRST(&qov->stack);
  47. QObject *value;
  48. QTAILQ_REMOVE(&qov->stack, e, node);
  49. value = e->value;
  50. g_free(e);
  51. return value;
  52. }
  53. static void qapi_dealloc_start_struct(Visitor *v, void **obj, const char *kind,
  54. const char *name, size_t unused,
  55. Error **errp)
  56. {
  57. QapiDeallocVisitor *qov = to_qov(v);
  58. qapi_dealloc_push(qov, obj);
  59. }
  60. static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
  61. {
  62. QapiDeallocVisitor *qov = to_qov(v);
  63. void **obj = qapi_dealloc_pop(qov);
  64. if (obj) {
  65. g_free(*obj);
  66. }
  67. }
  68. static void qapi_dealloc_start_implicit_struct(Visitor *v,
  69. void **obj,
  70. size_t size,
  71. Error **errp)
  72. {
  73. QapiDeallocVisitor *qov = to_qov(v);
  74. qapi_dealloc_push(qov, obj);
  75. }
  76. static void qapi_dealloc_end_implicit_struct(Visitor *v, Error **errp)
  77. {
  78. QapiDeallocVisitor *qov = to_qov(v);
  79. void **obj = qapi_dealloc_pop(qov);
  80. if (obj) {
  81. g_free(*obj);
  82. }
  83. }
  84. static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp)
  85. {
  86. QapiDeallocVisitor *qov = to_qov(v);
  87. qapi_dealloc_push(qov, NULL);
  88. }
  89. static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **listp,
  90. Error **errp)
  91. {
  92. GenericList *list = *listp;
  93. QapiDeallocVisitor *qov = to_qov(v);
  94. StackEntry *e = QTAILQ_FIRST(&qov->stack);
  95. if (e && e->is_list_head) {
  96. e->is_list_head = false;
  97. return list;
  98. }
  99. if (list) {
  100. list = list->next;
  101. g_free(*listp);
  102. return list;
  103. }
  104. return NULL;
  105. }
  106. static void qapi_dealloc_end_list(Visitor *v, Error **errp)
  107. {
  108. QapiDeallocVisitor *qov = to_qov(v);
  109. void *obj = qapi_dealloc_pop(qov);
  110. assert(obj == NULL); /* should've been list head tracker with no payload */
  111. }
  112. static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
  113. Error **errp)
  114. {
  115. if (obj) {
  116. g_free(*obj);
  117. }
  118. }
  119. static void qapi_dealloc_type_int(Visitor *v, int64_t *obj, const char *name,
  120. Error **errp)
  121. {
  122. }
  123. static void qapi_dealloc_type_bool(Visitor *v, bool *obj, const char *name,
  124. Error **errp)
  125. {
  126. }
  127. static void qapi_dealloc_type_number(Visitor *v, double *obj, const char *name,
  128. Error **errp)
  129. {
  130. }
  131. static void qapi_dealloc_type_size(Visitor *v, uint64_t *obj, const char *name,
  132. Error **errp)
  133. {
  134. }
  135. static void qapi_dealloc_type_enum(Visitor *v, int *obj, const char *strings[],
  136. const char *kind, const char *name,
  137. Error **errp)
  138. {
  139. }
  140. /* If there's no data present, the dealloc visitor has nothing to free.
  141. * Thus, indicate to visitor code that the subsequent union fields can
  142. * be skipped. This is not an error condition, since the cleanup of the
  143. * rest of an object can continue unhindered, so leave errp unset in
  144. * these cases.
  145. *
  146. * NOTE: In cases where we're attempting to deallocate an object that
  147. * may have missing fields, the field indicating the union type may
  148. * be missing. In such a case, it's possible we don't have enough
  149. * information to differentiate data_present == false from a case where
  150. * data *is* present but happens to be a scalar with a value of 0.
  151. * This is okay, since in the case of the dealloc visitor there's no
  152. * work that needs to done in either situation.
  153. *
  154. * The current inability in QAPI code to more thoroughly verify a union
  155. * type in such cases will likely need to be addressed if we wish to
  156. * implement this interface for other types of visitors in the future,
  157. * however.
  158. */
  159. static bool qapi_dealloc_start_union(Visitor *v, bool data_present,
  160. Error **errp)
  161. {
  162. return data_present;
  163. }
  164. Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
  165. {
  166. return &v->visitor;
  167. }
  168. void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
  169. {
  170. g_free(v);
  171. }
  172. QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
  173. {
  174. QapiDeallocVisitor *v;
  175. v = g_malloc0(sizeof(*v));
  176. v->visitor.start_struct = qapi_dealloc_start_struct;
  177. v->visitor.end_struct = qapi_dealloc_end_struct;
  178. v->visitor.start_implicit_struct = qapi_dealloc_start_implicit_struct;
  179. v->visitor.end_implicit_struct = qapi_dealloc_end_implicit_struct;
  180. v->visitor.start_list = qapi_dealloc_start_list;
  181. v->visitor.next_list = qapi_dealloc_next_list;
  182. v->visitor.end_list = qapi_dealloc_end_list;
  183. v->visitor.type_enum = qapi_dealloc_type_enum;
  184. v->visitor.type_int = qapi_dealloc_type_int;
  185. v->visitor.type_bool = qapi_dealloc_type_bool;
  186. v->visitor.type_str = qapi_dealloc_type_str;
  187. v->visitor.type_number = qapi_dealloc_type_number;
  188. v->visitor.type_size = qapi_dealloc_type_size;
  189. v->visitor.start_union = qapi_dealloc_start_union;
  190. QTAILQ_INIT(&v->stack);
  191. return v;
  192. }