bus: simplify name handling

Simplify a bit the code by using g_strdup_printf() and store it in a
non-const value so casting is no longer needed, and ownership is
clearer.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Marc-André Lureau 2016-07-15 12:04:49 +02:00
parent e9529768d4
commit f73480c36f
2 changed files with 7 additions and 16 deletions

View File

@ -78,8 +78,7 @@ static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
{ {
const char *typename = object_get_typename(OBJECT(bus)); const char *typename = object_get_typename(OBJECT(bus));
BusClass *bc; BusClass *bc;
char *buf; int i, bus_id;
int i, len, bus_id;
bus->parent = parent; bus->parent = parent;
@ -88,23 +87,15 @@ static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
} else if (bus->parent && bus->parent->id) { } else if (bus->parent && bus->parent->id) {
/* parent device has id -> use it plus parent-bus-id for bus name */ /* parent device has id -> use it plus parent-bus-id for bus name */
bus_id = bus->parent->num_child_bus; bus_id = bus->parent->num_child_bus;
bus->name = g_strdup_printf("%s.%d", bus->parent->id, bus_id);
len = strlen(bus->parent->id) + 16;
buf = g_malloc(len);
snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
bus->name = buf;
} else { } else {
/* no id -> use lowercase bus type plus global bus-id for bus name */ /* no id -> use lowercase bus type plus global bus-id for bus name */
bc = BUS_GET_CLASS(bus); bc = BUS_GET_CLASS(bus);
bus_id = bc->automatic_ids++; bus_id = bc->automatic_ids++;
bus->name = g_strdup_printf("%s.%d", typename, bus_id);
len = strlen(typename) + 16; for (i = 0; bus->name[i]; i++) {
buf = g_malloc(len); bus->name[i] = qemu_tolower(bus->name[i]);
len = snprintf(buf, len, "%s.%d", typename, bus_id);
for (i = 0; i < len; i++) {
buf[i] = qemu_tolower(buf[i]);
} }
bus->name = buf;
} }
if (bus->parent) { if (bus->parent) {
@ -229,7 +220,7 @@ static void qbus_finalize(Object *obj)
{ {
BusState *bus = BUS(obj); BusState *bus = BUS(obj);
g_free((char *)bus->name); g_free(bus->name);
} }
static const TypeInfo bus_info = { static const TypeInfo bus_info = {

View File

@ -224,7 +224,7 @@ typedef struct BusChild {
struct BusState { struct BusState {
Object obj; Object obj;
DeviceState *parent; DeviceState *parent;
const char *name; char *name;
HotplugHandler *hotplug_handler; HotplugHandler *hotplug_handler;
int max_index; int max_index;
bool realized; bool realized;