From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36393) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bVaTE-0003YO-G9 for qemu-devel@nongnu.org; Fri, 05 Aug 2016 04:27:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bVaTA-0002ev-54 for qemu-devel@nongnu.org; Fri, 05 Aug 2016 04:27:07 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35282) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bVaT9-0002eq-Vc for qemu-devel@nongnu.org; Fri, 05 Aug 2016 04:27:04 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9E56F85376 for ; Fri, 5 Aug 2016 08:27:03 +0000 (UTC) From: marcandre.lureau@redhat.com Date: Fri, 5 Aug 2016 12:24:16 +0400 Message-Id: <20160805082421.21994-32-marcandre.lureau@redhat.com> In-Reply-To: <20160805082421.21994-1-marcandre.lureau@redhat.com> References: <20160805082421.21994-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH for-2.7 v4 31/36] bus: simplify name handling List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, eblake@redhat.com, armbru@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= From: Marc-Andr=C3=A9 Lureau 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.9.0