qemu/qapi/qapi-dealloc-visitor.c
<<
>>
Prefs
   1/*
   2 * Dealloc Visitor
   3 *
   4 * Copyright (C) 2012-2016 Red Hat, Inc.
   5 * Copyright IBM, Corp. 2011
   6 *
   7 * Authors:
   8 *  Michael Roth   <mdroth@linux.vnet.ibm.com>
   9 *
  10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  11 * See the COPYING.LIB file in the top-level directory.
  12 *
  13 */
  14
  15#include "qemu/osdep.h"
  16#include "qapi/dealloc-visitor.h"
  17#include "qapi/qmp/qnull.h"
  18#include "qapi/visitor-impl.h"
  19
  20struct QapiDeallocVisitor
  21{
  22    Visitor visitor;
  23};
  24
  25static bool qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
  26                                      size_t unused, Error **errp)
  27{
  28    return true;
  29}
  30
  31static void qapi_dealloc_end_struct(Visitor *v, void **obj)
  32{
  33    if (obj) {
  34        g_free(*obj);
  35    }
  36}
  37
  38static void qapi_dealloc_end_alternate(Visitor *v, void **obj)
  39{
  40    if (obj) {
  41        g_free(*obj);
  42    }
  43}
  44
  45static bool qapi_dealloc_start_list(Visitor *v, const char *name,
  46                                    GenericList **list, size_t size,
  47                                    Error **errp)
  48{
  49    return true;
  50}
  51
  52static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList *tail,
  53                                           size_t size)
  54{
  55    GenericList *next = tail->next;
  56    g_free(tail);
  57    return next;
  58}
  59
  60static void qapi_dealloc_end_list(Visitor *v, void **obj)
  61{
  62}
  63
  64static bool qapi_dealloc_type_str(Visitor *v, const char *name, char **obj,
  65                                  Error **errp)
  66{
  67    if (obj) {
  68        g_free(*obj);
  69    }
  70    return true;
  71}
  72
  73static bool qapi_dealloc_type_int64(Visitor *v, const char *name, int64_t *obj,
  74                                    Error **errp)
  75{
  76    return true;
  77}
  78
  79static bool qapi_dealloc_type_uint64(Visitor *v, const char *name,
  80                                     uint64_t *obj, Error **errp)
  81{
  82    return true;
  83}
  84
  85static bool qapi_dealloc_type_bool(Visitor *v, const char *name, bool *obj,
  86                                   Error **errp)
  87{
  88    return true;
  89}
  90
  91static bool qapi_dealloc_type_number(Visitor *v, const char *name, double *obj,
  92                                     Error **errp)
  93{
  94    return true;
  95}
  96
  97static bool qapi_dealloc_type_anything(Visitor *v, const char *name,
  98                                       QObject **obj, Error **errp)
  99{
 100    if (obj) {
 101        qobject_unref(*obj);
 102    }
 103    return true;
 104}
 105
 106static bool qapi_dealloc_type_null(Visitor *v, const char *name,
 107                                   QNull **obj, Error **errp)
 108{
 109    if (obj) {
 110        qobject_unref(*obj);
 111    }
 112    return true;
 113}
 114
 115static void qapi_dealloc_free(Visitor *v)
 116{
 117    g_free(container_of(v, QapiDeallocVisitor, visitor));
 118}
 119
 120Visitor *qapi_dealloc_visitor_new(void)
 121{
 122    QapiDeallocVisitor *v;
 123
 124    v = g_malloc0(sizeof(*v));
 125
 126    v->visitor.type = VISITOR_DEALLOC;
 127    v->visitor.start_struct = qapi_dealloc_start_struct;
 128    v->visitor.end_struct = qapi_dealloc_end_struct;
 129    v->visitor.end_alternate = qapi_dealloc_end_alternate;
 130    v->visitor.start_list = qapi_dealloc_start_list;
 131    v->visitor.next_list = qapi_dealloc_next_list;
 132    v->visitor.end_list = qapi_dealloc_end_list;
 133    v->visitor.type_int64 = qapi_dealloc_type_int64;
 134    v->visitor.type_uint64 = qapi_dealloc_type_uint64;
 135    v->visitor.type_bool = qapi_dealloc_type_bool;
 136    v->visitor.type_str = qapi_dealloc_type_str;
 137    v->visitor.type_number = qapi_dealloc_type_number;
 138    v->visitor.type_any = qapi_dealloc_type_anything;
 139    v->visitor.type_null = qapi_dealloc_type_null;
 140    v->visitor.free = qapi_dealloc_free;
 141
 142    return &v->visitor;
 143}
 144