qapi-dealloc-visitor.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "qemu-objects.h"
  17. typedef struct StackEntry
  18. {
  19. void *value;
  20. QTAILQ_ENTRY(StackEntry) node;
  21. } StackEntry;
  22. struct QapiDeallocVisitor
  23. {
  24. Visitor visitor;
  25. QTAILQ_HEAD(, StackEntry) stack;
  26. };
  27. static QapiDeallocVisitor *to_qov(Visitor *v)
  28. {
  29. return container_of(v, QapiDeallocVisitor, visitor);
  30. }
  31. static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
  32. {
  33. StackEntry *e = qemu_mallocz(sizeof(*e));
  34. e->value = value;
  35. QTAILQ_INSERT_HEAD(&qov->stack, e, node);
  36. }
  37. static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
  38. {
  39. StackEntry *e = QTAILQ_FIRST(&qov->stack);
  40. QObject *value;
  41. QTAILQ_REMOVE(&qov->stack, e, node);
  42. value = e->value;
  43. qemu_free(e);
  44. return value;
  45. }
  46. static void qapi_dealloc_start_struct(Visitor *v, void **obj, const char *kind,
  47. const char *name, size_t unused,
  48. Error **errp)
  49. {
  50. QapiDeallocVisitor *qov = to_qov(v);
  51. qapi_dealloc_push(qov, obj);
  52. }
  53. static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
  54. {
  55. QapiDeallocVisitor *qov = to_qov(v);
  56. void **obj = qapi_dealloc_pop(qov);
  57. if (obj) {
  58. qemu_free(*obj);
  59. }
  60. }
  61. static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp)
  62. {
  63. }
  64. static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **list,
  65. Error **errp)
  66. {
  67. GenericList *retval = *list;
  68. qemu_free(retval->value);
  69. *list = retval->next;
  70. return retval;
  71. }
  72. static void qapi_dealloc_end_list(Visitor *v, Error **errp)
  73. {
  74. }
  75. static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
  76. Error **errp)
  77. {
  78. if (obj) {
  79. qemu_free(*obj);
  80. }
  81. }
  82. static void qapi_dealloc_type_int(Visitor *v, int64_t *obj, const char *name,
  83. Error **errp)
  84. {
  85. }
  86. static void qapi_dealloc_type_bool(Visitor *v, bool *obj, const char *name,
  87. Error **errp)
  88. {
  89. }
  90. static void qapi_dealloc_type_number(Visitor *v, double *obj, const char *name,
  91. Error **errp)
  92. {
  93. }
  94. static void qapi_dealloc_type_enum(Visitor *v, int *obj, const char *strings[],
  95. const char *kind, const char *name,
  96. Error **errp)
  97. {
  98. }
  99. Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
  100. {
  101. return &v->visitor;
  102. }
  103. void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
  104. {
  105. qemu_free(v);
  106. }
  107. QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
  108. {
  109. QapiDeallocVisitor *v;
  110. v = qemu_mallocz(sizeof(*v));
  111. v->visitor.start_struct = qapi_dealloc_start_struct;
  112. v->visitor.end_struct = qapi_dealloc_end_struct;
  113. v->visitor.start_list = qapi_dealloc_start_list;
  114. v->visitor.next_list = qapi_dealloc_next_list;
  115. v->visitor.end_list = qapi_dealloc_end_list;
  116. v->visitor.type_enum = qapi_dealloc_type_enum;
  117. v->visitor.type_int = qapi_dealloc_type_int;
  118. v->visitor.type_bool = qapi_dealloc_type_bool;
  119. v->visitor.type_str = qapi_dealloc_type_str;
  120. v->visitor.type_number = qapi_dealloc_type_number;
  121. QTAILQ_INIT(&v->stack);
  122. return v;
  123. }