From 0399293e5b9e5443b82103fa8e2c97deadef9825 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 3 Mar 2016 09:16:48 -0700 Subject: util: Shorten references into SocketAddress An upcoming patch will alter how simple unions, like SocketAddress, are laid out, which will impact all lines of the form 'addr->u.XXX' (expanding it to the longer 'addr->u.XXX.data'). For better legibility in that patch, and less need for line wrapping, it's better to use a temporary variable to reduce the effect of a layout change to just the variable initializations, rather than every reference within a SocketAddress. Also, take advantage of some C99 initialization where it makes sense (simplifying g_new0() to g_new()). Signed-off-by: Eric Blake Message-Id: <1457021813-10704-7-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- qemu-char.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'qemu-char.c') diff --git a/qemu-char.c b/qemu-char.c index 5ea1d34..af31102 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -3659,20 +3659,23 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, addr = g_new0(SocketAddress, 1); if (path) { + UnixSocketAddress *q_unix; addr->type = SOCKET_ADDRESS_KIND_UNIX; - addr->u.q_unix = g_new0(UnixSocketAddress, 1); - addr->u.q_unix->path = g_strdup(path); + q_unix = addr->u.q_unix = g_new0(UnixSocketAddress, 1); + q_unix->path = g_strdup(path); } else { addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet = g_new0(InetSocketAddress, 1); - addr->u.inet->host = g_strdup(host); - addr->u.inet->port = g_strdup(port); - addr->u.inet->has_to = qemu_opt_get(opts, "to"); - addr->u.inet->to = qemu_opt_get_number(opts, "to", 0); - addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); - addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); - addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); - addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); + addr->u.inet = g_new(InetSocketAddress, 1); + *addr->u.inet = (InetSocketAddress) { + .host = g_strdup(host), + .port = g_strdup(port), + .has_to = qemu_opt_get(opts, "to"), + .to = qemu_opt_get_number(opts, "to", 0), + .has_ipv4 = qemu_opt_get(opts, "ipv4"), + .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), + .has_ipv6 = qemu_opt_get(opts, "ipv6"), + .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), + }; } sock->addr = addr; } @@ -3711,22 +3714,26 @@ static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, addr = g_new0(SocketAddress, 1); addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet = g_new0(InetSocketAddress, 1); - addr->u.inet->host = g_strdup(host); - addr->u.inet->port = g_strdup(port); - addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); - addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); - addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); - addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); + addr->u.inet = g_new(InetSocketAddress, 1); + *addr->u.inet = (InetSocketAddress) { + .host = g_strdup(host), + .port = g_strdup(port), + .has_ipv4 = qemu_opt_get(opts, "ipv4"), + .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), + .has_ipv6 = qemu_opt_get(opts, "ipv6"), + .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), + }; udp->remote = addr; if (has_local) { udp->has_local = true; addr = g_new0(SocketAddress, 1); addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet = g_new0(InetSocketAddress, 1); - addr->u.inet->host = g_strdup(localaddr); - addr->u.inet->port = g_strdup(localport); + addr->u.inet = g_new(InetSocketAddress, 1); + *addr->u.inet = (InetSocketAddress) { + .host = g_strdup(localaddr), + .port = g_strdup(localport), + }; udp->local = addr; } } -- cgit v1.1