hw/arm/armsse: Fix memory leak in error-exit path
[qemu/ar7.git] / hw / xen / xen-backend.c
blobda065f81b736e40ec65e8c9d3f2721fed786db75
1 /*
2 * Copyright (c) 2018 Citrix Systems Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
8 #include "qemu/osdep.h"
9 #include "qemu/error-report.h"
10 #include "qapi/error.h"
11 #include "hw/xen/xen-backend.h"
12 #include "hw/xen/xen-bus.h"
14 typedef struct XenBackendImpl {
15 const char *type;
16 XenBackendDeviceCreate create;
17 XenBackendDeviceDestroy destroy;
18 } XenBackendImpl;
20 struct XenBackendInstance {
21 QLIST_ENTRY(XenBackendInstance) entry;
22 const XenBackendImpl *impl;
23 XenBus *xenbus;
24 char *name;
25 XenDevice *xendev;
28 static GHashTable *xen_backend_table_get(void)
30 static GHashTable *table;
32 if (table == NULL) {
33 table = g_hash_table_new(g_str_hash, g_str_equal);
36 return table;
39 static void xen_backend_table_add(XenBackendImpl *impl)
41 g_hash_table_insert(xen_backend_table_get(), (void *)impl->type, impl);
44 static const XenBackendImpl *xen_backend_table_lookup(const char *type)
46 return g_hash_table_lookup(xen_backend_table_get(), type);
49 void xen_backend_register(const XenBackendInfo *info)
51 XenBackendImpl *impl = g_new0(XenBackendImpl, 1);
53 g_assert(info->type);
55 if (xen_backend_table_lookup(info->type)) {
56 error_report("attempt to register duplicate Xen backend type '%s'",
57 info->type);
58 abort();
61 if (!info->create) {
62 error_report("backend type '%s' has no creator", info->type);
63 abort();
66 impl->type = info->type;
67 impl->create = info->create;
68 impl->destroy = info->destroy;
70 xen_backend_table_add(impl);
73 static QLIST_HEAD(, XenBackendInstance) backend_list;
75 static void xen_backend_list_add(XenBackendInstance *backend)
77 QLIST_INSERT_HEAD(&backend_list, backend, entry);
80 static XenBackendInstance *xen_backend_list_find(XenDevice *xendev)
82 XenBackendInstance *backend;
84 QLIST_FOREACH(backend, &backend_list, entry) {
85 if (backend->xendev == xendev) {
86 return backend;
90 return NULL;
93 static void xen_backend_list_remove(XenBackendInstance *backend)
95 QLIST_REMOVE(backend, entry);
98 void xen_backend_device_create(XenBus *xenbus, const char *type,
99 const char *name, QDict *opts, Error **errp)
101 const XenBackendImpl *impl = xen_backend_table_lookup(type);
102 XenBackendInstance *backend;
103 Error *local_error = NULL;
105 if (!impl) {
106 return;
109 backend = g_new0(XenBackendInstance, 1);
110 backend->xenbus = xenbus;
111 backend->name = g_strdup(name);
113 impl->create(backend, opts, &local_error);
114 if (local_error) {
115 error_propagate(errp, local_error);
116 g_free(backend->name);
117 g_free(backend);
118 return;
121 backend->impl = impl;
122 xen_backend_list_add(backend);
125 XenBus *xen_backend_get_bus(XenBackendInstance *backend)
127 return backend->xenbus;
130 const char *xen_backend_get_name(XenBackendInstance *backend)
132 return backend->name;
135 void xen_backend_set_device(XenBackendInstance *backend,
136 XenDevice *xendev)
138 g_assert(!backend->xendev);
139 backend->xendev = xendev;
142 XenDevice *xen_backend_get_device(XenBackendInstance *backend)
144 return backend->xendev;
148 bool xen_backend_try_device_destroy(XenDevice *xendev, Error **errp)
150 XenBackendInstance *backend = xen_backend_list_find(xendev);
151 const XenBackendImpl *impl;
153 if (!backend) {
154 return false;
157 impl = backend->impl;
158 impl->destroy(backend, errp);
160 xen_backend_list_remove(backend);
161 g_free(backend->name);
162 g_free(backend);
164 return true;