Received: from eggs.gnu.org ([2001:4830:134:3::10]:57605) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bi0A7-0001gh-5X for qemu-devel@nongnu.org; Thu, 08 Sep 2016 10:18:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bi0A3-0000q5-1d for qemu-devel@nongnu.org; Thu, 08 Sep 2016 10:18:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57268) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bi0A2-0000pr-Ry for qemu-devel@nongnu.org; Thu, 08 Sep 2016 10:18:38 -0400 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Thu, 8 Sep 2016 18:17:16 +0400 Message-Id: <20160908141720.30641-22-marcandre.lureau@redhat.com> In-Reply-To: <20160908141720.30641-1-marcandre.lureau@redhat.com> References: <20160908141720.30641-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULLv2 21/25] bus: simplify name handling List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, eblake@redhat.com, armbru@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= 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=C3=A9 Lureau Reviewed-by: Eric Blake --- hw/core/bus.c | 21 ++++++--------------- include/hw/qdev-core.h | 2 +- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/hw/core/bus.c b/hw/core/bus.c index 3e3f8ac..cf383fc 100644 --- a/hw/core/bus.c +++ b/hw/core/bus.c @@ -78,8 +78,7 @@ static void qbus_realize(BusState *bus, DeviceState *pa= rent, const char *name) { const char *typename =3D object_get_typename(OBJECT(bus)); BusClass *bc; - char *buf; - int i, len, bus_id; + int i, bus_id; =20 bus->parent =3D parent; =20 @@ -88,23 +87,15 @@ static void qbus_realize(BusState *bus, DeviceState *= parent, const char *name) } else if (bus->parent && bus->parent->id) { /* parent device has id -> use it plus parent-bus-id for bus nam= e */ bus_id =3D bus->parent->num_child_bus; - - len =3D strlen(bus->parent->id) + 16; - buf =3D g_malloc(len); - snprintf(buf, len, "%s.%d", bus->parent->id, bus_id); - bus->name =3D buf; + bus->name =3D g_strdup_printf("%s.%d", bus->parent->id, bus_id); } else { /* no id -> use lowercase bus type plus global bus-id for bus na= me */ bc =3D BUS_GET_CLASS(bus); bus_id =3D bc->automatic_ids++; - - len =3D strlen(typename) + 16; - buf =3D g_malloc(len); - len =3D snprintf(buf, len, "%s.%d", typename, bus_id); - for (i =3D 0; i < len; i++) { - buf[i] =3D qemu_tolower(buf[i]); + bus->name =3D g_strdup_printf("%s.%d", typename, bus_id); + for (i =3D 0; bus->name[i]; i++) { + bus->name[i] =3D qemu_tolower(bus->name[i]); } - bus->name =3D buf; } =20 if (bus->parent) { @@ -229,7 +220,7 @@ static void qbus_finalize(Object *obj) { BusState *bus =3D BUS(obj); =20 - g_free((char *)bus->name); + g_free(bus->name); } =20 static const TypeInfo bus_info =3D { diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 4b4b33b..2c97347 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -224,7 +224,7 @@ typedef struct BusChild { struct BusState { Object obj; DeviceState *parent; - const char *name; + char *name; HotplugHandler *hotplug_handler; int max_index; bool realized; --=20 2.10.0