mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2024-12-24 00:25:42 +00:00
e096e62127
There's no reason to do two malloc's for a flat union; let's just inline the branch struct directly into the C union branch of the flat union. Surprisingly, fewer clients were actually using explicit references to the branch types in comparison to the number of flat unions thus modified. This lets us reduce the hack in qapi-types:gen_variants() added in the previous patch; we no longer need to distinguish between alternates and flat unions. The change to unboxed structs means that u.data (added in commit cee2dedb) is now coincident with random fields of each branch of the flat union, whereas beforehand it was only coincident with pointers (since all branches of a flat union have to be objects). Note that this was already the case for simple unions - but there we got lucky. Remember, visit_start_union() blindly returns true for all visitors except for the dealloc visitor, where it returns the value !!obj->u.data, and that this result then controls whether to proceed with the visit to the variant. Pre-patch, this meant that flat unions were testing whether the boxed pointer was still NULL, and thereby skipping visit_end_implicit_struct() and avoiding a NULL dereference if the pointer had not been allocated. The same was true for simple unions where the current branch had pointer type, except there we bypassed visit_type_FOO(). But for simple unions where the current branch had scalar type, the contents of that scalar meant that the decision to call visit_type_FOO() was data-dependent - the reason we got lucky there is that visit_type_FOO() for all scalar types in the dealloc visitor is a no-op (only the pointer variants had anything to free), so it did not matter whether the dealloc visit was skipped. But with this patch, we would risk leaking memory if we could skip a call to visit_type_FOO_fields() based solely on a data-dependent decision. But notice: in the dealloc visitor, visit_type_FOO() already handles a NULL obj - it was only the visit_type_implicit_FOO() that was failing to check for NULL. And now that we have refactored things to have the branch be part of the parent struct, we no longer have a separate pointer that can be NULL in the first place. So we can just delete the call to visit_start_union() altogether, and blindly visit the branch type; there is no change in behavior except to the dealloc visitor, where we now unconditionally visit the branch, but where that visit is now always safe (for a flat union, we can no longer dereference NULL, and for a simple union, visit_type_FOO() was already safely handling NULL on pointer types). Unfortunately, simple unions are not as easy to switch to unboxed layout; because we are special-casing the hidden implicit type with a single 'data' member, we really DO need to keep calling another layer of visit_start_struct(), with a second malloc; although there are some cleanups planned for simple unions in later patches. visit_start_union() and gen_visit_implicit_struct() are now unused. Drop them. Note that after this patch, the only remaining use of visit_start_implicit_struct() is for alternate types; the next patch will do further cleanup based on that fact. Backports commit 544a3731591f5d53e15f22de00ce5ac758d490b3 from qemu
212 lines
5.2 KiB
C
212 lines
5.2 KiB
C
/*
|
|
* Dealloc Visitor
|
|
*
|
|
* Copyright IBM, Corp. 2011
|
|
*
|
|
* Authors:
|
|
* Michael Roth <mdroth@linux.vnet.ibm.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
*
|
|
*/
|
|
|
|
#include "qemu/osdep.h"
|
|
#include "qapi/dealloc-visitor.h"
|
|
#include "qemu/queue.h"
|
|
#include "qemu-common.h"
|
|
#include "qapi/qmp/types.h"
|
|
#include "qapi/visitor-impl.h"
|
|
|
|
typedef struct StackEntry
|
|
{
|
|
void *value;
|
|
bool is_list_head;
|
|
QTAILQ_ENTRY(StackEntry) node;
|
|
} StackEntry;
|
|
|
|
struct QapiDeallocVisitor
|
|
{
|
|
Visitor visitor;
|
|
QTAILQ_HEAD(, StackEntry) stack;
|
|
bool is_list_head;
|
|
};
|
|
|
|
static QapiDeallocVisitor *to_qov(Visitor *v)
|
|
{
|
|
return container_of(v, QapiDeallocVisitor, visitor);
|
|
}
|
|
|
|
static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
|
|
{
|
|
StackEntry *e = g_malloc0(sizeof(*e));
|
|
|
|
e->value = value;
|
|
|
|
/* see if we're just pushing a list head tracker */
|
|
if (value == NULL) {
|
|
e->is_list_head = true;
|
|
}
|
|
QTAILQ_INSERT_HEAD(&qov->stack, e, node);
|
|
}
|
|
|
|
static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
|
|
{
|
|
StackEntry *e = QTAILQ_FIRST(&qov->stack);
|
|
QObject *value;
|
|
QTAILQ_REMOVE(&qov->stack, e, node);
|
|
value = e->value;
|
|
g_free(e);
|
|
return value;
|
|
}
|
|
|
|
static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
|
|
size_t unused, Error **errp)
|
|
{
|
|
QapiDeallocVisitor *qov = to_qov(v);
|
|
qapi_dealloc_push(qov, obj);
|
|
}
|
|
|
|
static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
|
|
{
|
|
QapiDeallocVisitor *qov = to_qov(v);
|
|
void **obj = qapi_dealloc_pop(qov);
|
|
if (obj) {
|
|
g_free(*obj);
|
|
}
|
|
}
|
|
|
|
static void qapi_dealloc_start_implicit_struct(Visitor *v,
|
|
void **obj,
|
|
size_t size,
|
|
Error **errp)
|
|
{
|
|
QapiDeallocVisitor *qov = to_qov(v);
|
|
qapi_dealloc_push(qov, obj);
|
|
}
|
|
|
|
static void qapi_dealloc_end_implicit_struct(Visitor *v)
|
|
{
|
|
QapiDeallocVisitor *qov = to_qov(v);
|
|
void **obj = qapi_dealloc_pop(qov);
|
|
if (obj) {
|
|
g_free(*obj);
|
|
}
|
|
}
|
|
|
|
static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp)
|
|
{
|
|
QapiDeallocVisitor *qov = to_qov(v);
|
|
qapi_dealloc_push(qov, NULL);
|
|
}
|
|
|
|
static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **listp)
|
|
{
|
|
GenericList *list = *listp;
|
|
QapiDeallocVisitor *qov = to_qov(v);
|
|
StackEntry *e = QTAILQ_FIRST(&qov->stack);
|
|
|
|
if (e && e->is_list_head) {
|
|
e->is_list_head = false;
|
|
return list;
|
|
}
|
|
|
|
if (list) {
|
|
list = list->next;
|
|
g_free(*listp);
|
|
return list;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void qapi_dealloc_end_list(Visitor *v)
|
|
{
|
|
QapiDeallocVisitor *qov = to_qov(v);
|
|
void *obj = qapi_dealloc_pop(qov);
|
|
assert(obj == NULL); /* should've been list head tracker with no payload */
|
|
}
|
|
|
|
static void qapi_dealloc_type_str(Visitor *v, const char *name, char **obj,
|
|
Error **errp)
|
|
{
|
|
if (obj) {
|
|
g_free(*obj);
|
|
}
|
|
}
|
|
|
|
static void qapi_dealloc_type_int64(Visitor *v, const char *name, int64_t *obj,
|
|
Error **errp)
|
|
{
|
|
}
|
|
|
|
static void qapi_dealloc_type_uint64(Visitor *v, const char *name,
|
|
uint64_t *obj, Error **errp)
|
|
{
|
|
}
|
|
|
|
static void qapi_dealloc_type_bool(Visitor *v, const char *name, bool *obj,
|
|
Error **errp)
|
|
{
|
|
}
|
|
|
|
static void qapi_dealloc_type_number(Visitor *v, const char *name, double *obj,
|
|
Error **errp)
|
|
{
|
|
}
|
|
|
|
static void qapi_dealloc_type_anything(Visitor *v, const char *name,
|
|
QObject **obj, Error **errp)
|
|
{
|
|
if (obj) {
|
|
qobject_decref(*obj);
|
|
}
|
|
}
|
|
|
|
static void qapi_dealloc_type_size(Visitor *v, const char *name, uint64_t *obj,
|
|
Error **errp)
|
|
{
|
|
}
|
|
|
|
static void qapi_dealloc_type_enum(Visitor *v, const char *name, int *obj,
|
|
const char * const strings[], Error **errp)
|
|
{
|
|
}
|
|
|
|
Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
|
|
{
|
|
return &v->visitor;
|
|
}
|
|
|
|
void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
|
|
{
|
|
g_free(v);
|
|
}
|
|
|
|
QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
|
|
{
|
|
QapiDeallocVisitor *v;
|
|
|
|
v = g_malloc0(sizeof(*v));
|
|
|
|
v->visitor.start_struct = qapi_dealloc_start_struct;
|
|
v->visitor.end_struct = qapi_dealloc_end_struct;
|
|
v->visitor.start_implicit_struct = qapi_dealloc_start_implicit_struct;
|
|
v->visitor.end_implicit_struct = qapi_dealloc_end_implicit_struct;
|
|
v->visitor.start_list = qapi_dealloc_start_list;
|
|
v->visitor.next_list = qapi_dealloc_next_list;
|
|
v->visitor.end_list = qapi_dealloc_end_list;
|
|
v->visitor.type_enum = qapi_dealloc_type_enum;
|
|
v->visitor.type_int64 = qapi_dealloc_type_int64;
|
|
v->visitor.type_uint64 = qapi_dealloc_type_uint64;
|
|
v->visitor.type_bool = qapi_dealloc_type_bool;
|
|
v->visitor.type_str = qapi_dealloc_type_str;
|
|
v->visitor.type_number = qapi_dealloc_type_number;
|
|
v->visitor.type_any = qapi_dealloc_type_anything;
|
|
v->visitor.type_size = qapi_dealloc_type_size;
|
|
|
|
QTAILQ_INIT(&v->stack);
|
|
|
|
return v;
|
|
}
|