qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 1.3 4/5] qdev: simplify (de)allocation of buses


From: Paolo Bonzini
Subject: Re: [Qemu-devel] [PATCH 1.3 4/5] qdev: simplify (de)allocation of buses
Date: Fri, 23 Nov 2012 18:16:32 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121016 Thunderbird/16.0.1

Il 23/11/2012 18:07, Andreas Färber ha scritto:
>> >      bus = g_malloc0(sizeof(*bus));
>> > -    bus->qbus.glib_allocated = true;
>> >      pci_bus_new_inplace(bus, parent, name, address_space_mem,
>> >                          address_space_io, devfn_min);
>> > +    OBJECT(bus)->free = g_free;
> Anthony asked not to use the macros like this, but in this case to have
> Object *obj; to assign it after g_malloc0() and use obj->free = g_free;
> here.

All of the alternatives look ugly:

    /* malloc mismatch, ouch */
    obj = g_malloc0(sizeof(PCIBus));
    bus = PCI_BUS(obj);

    /* Cast to superclass? */
    bus = g_malloc0(sizeof(*bus));
    obj = OBJECT(bus);

    /* Huge path to access the member */
    bus->qbus.obj->free = g_free;

The right solution would be to have qbus_init:

    void qbus_init(BusState *bus,
                   DeviceState *parent, const char *name)
    {
        object_initialize(bus, typename);

        bus->parent = parent;
        bus->name = name ? g_strdup(name) : NULL;
        qbus_realize(bus);
    }

    void qbus_create_inplace(BusState *bus, const char *typename,
                             DeviceState *parent, const char *name)
    {
        object_initialize(bus, typename);
        qbus_init(bus, parent, name);
    }

and just use object_new + qbus_init.  But it is more invasive, I'd
prefer the least pleasant but smaller alternative now.

Paolo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]