Use initalization instead of explicit zeroing

Before, we would initialize many fields twice: first
by filling the structure with zeros, and then a second
time with the real values. We can let the compiler do
the job for us, avoiding one copy.

A downside of this patch is that text gets slightly
bigger. This is because all zero() calls are effectively
inlined:

$ size build/.libs/systemd
         text    data     bss     dec     hex filename
before 897737  107300    2560 1007597   f5fed build/.libs/systemd
after  897873  107300    2560 1007733   f6075 build/.libs/systemd

… actually less than 1‰.

A few asserts that the parameter is not null had to be removed. I
don't think this changes much, because first, it is quite unlikely
for the assert to fail, and second, an immediate SEGV is almost as
good as an assert.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2013-04-05 19:50:57 -04:00
parent 8c62ecf1a9
commit b92bea5d2a
49 changed files with 392 additions and 554 deletions
+31 -34
View File
@@ -88,25 +88,26 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) {
union {
struct sockaddr sa;
struct sockaddr_nl nl;
} sa;
} sa = {
.nl.nl_family = AF_NETLINK,
};
union {
struct nlmsghdr header;
uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
RTA_LENGTH(sizeof(struct in6_addr))];
} request;
} request = {
.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
.header.nlmsg_type = RTM_NEWADDR,
.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK,
.header.nlmsg_seq = *requests + 1,
};
struct ifaddrmsg *ifaddrmsg;
uint32_t ipv4_address = htonl(INADDR_LOOPBACK);
int r;
zero(request);
request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
request.header.nlmsg_type = RTM_NEWADDR;
request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK;
request.header.nlmsg_seq = *requests + 1;
ifaddrmsg = NLMSG_DATA(&request.header);
ifaddrmsg->ifa_family = AF_INET;
ifaddrmsg->ifa_prefixlen = 8;
@@ -114,13 +115,11 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) {
ifaddrmsg->ifa_scope = RT_SCOPE_HOST;
ifaddrmsg->ifa_index = if_loopback;
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &ipv4_address, sizeof(ipv4_address));
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL,
&ipv4_address, sizeof(ipv4_address));
if (r < 0)
return r;
zero(sa);
sa.nl.nl_family = AF_NETLINK;
if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0)
return -errno;
(*requests)++;
@@ -134,7 +133,8 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) {
ifaddrmsg->ifa_family = AF_INET6;
ifaddrmsg->ifa_prefixlen = 128;
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &in6addr_loopback, sizeof(in6addr_loopback));
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL,
&in6addr_loopback, sizeof(in6addr_loopback));
if (r < 0)
return r;
@@ -149,31 +149,29 @@ static int start_interface(int fd, int if_loopback, unsigned *requests) {
union {
struct sockaddr sa;
struct sockaddr_nl nl;
} sa;
} sa = {
.nl.nl_family = AF_NETLINK,
};
union {
struct nlmsghdr header;
uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
NLMSG_ALIGN(sizeof(struct ifinfomsg))];
} request;
} request = {
.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.header.nlmsg_type = RTM_NEWLINK,
.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK,
.header.nlmsg_seq = *requests + 1,
};
struct ifinfomsg *ifinfomsg;
zero(request);
request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
request.header.nlmsg_type = RTM_NEWLINK;
request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
request.header.nlmsg_seq = *requests + 1;
ifinfomsg = NLMSG_DATA(&request.header);
ifinfomsg->ifi_family = AF_UNSPEC;
ifinfomsg->ifi_index = if_loopback;
ifinfomsg->ifi_flags = IFF_UP;
ifinfomsg->ifi_change = IFF_UP;
zero(sa);
sa.nl.nl_family = AF_NETLINK;
if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0)
return -errno;
@@ -234,7 +232,10 @@ static int check_loopback(void) {
union {
struct sockaddr sa;
struct sockaddr_in in;
} sa;
} sa = {
.in.sin_family = AF_INET,
.in.sin_addr.s_addr = INADDR_LOOPBACK,
};
/* If we failed to set up the loop back device, check whether
* it might already be set up */
@@ -243,10 +244,6 @@ static int check_loopback(void) {
if (fd < 0)
return -errno;
zero(sa);
sa.in.sin_family = AF_INET;
sa.in.sin_addr.s_addr = INADDR_LOOPBACK;
if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
r = 1;
else
@@ -260,7 +257,9 @@ int loopback_setup(void) {
union {
struct sockaddr sa;
struct sockaddr_nl nl;
} sa;
} sa = {
.nl.nl_family = AF_NETLINK,
};
unsigned requests = 0, i;
int _cleanup_close_ fd = -1;
bool eperm = false;
@@ -274,8 +273,6 @@ int loopback_setup(void) {
if (fd < 0)
return -errno;
zero(sa);
sa.nl.nl_family = AF_NETLINK;
if (bind(fd, &sa.sa, sizeof(sa)) < 0) {
r = -errno;
goto error;