Skip to content

Commit b92bea5

Browse files
committed
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.
1 parent 8c62ecf commit b92bea5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+392
-554
lines changed

src/core/dbus-execute.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ int bus_execute_append_cpu_sched_priority(DBusMessageIter *i, const char *proper
160160
if (c->cpu_sched_set)
161161
n = c->cpu_sched_priority;
162162
else {
163-
struct sched_param p;
163+
struct sched_param p = {};
164164

165-
zero(p);
166165
if (sched_getparam(0, &p) >= 0)
167166
n = p.sched_priority;
168167
else
@@ -280,9 +279,8 @@ int bus_execute_append_rlimits(DBusMessageIter *i, const char *property, void *d
280279
if (c->rlimit[r])
281280
u = (uint64_t) c->rlimit[r]->rlim_max;
282281
else {
283-
struct rlimit rl;
282+
struct rlimit rl = {};
284283

285-
zero(rl);
286284
getrlimit(r, &rl);
287285

288286
u = (uint64_t) rl.rlim_max;

src/core/dbus.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,11 @@ static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
203203
}
204204

205205
static int bus_timeout_arm(Manager *m, Watch *w) {
206-
struct itimerspec its;
206+
struct itimerspec its = {};
207207

208208
assert(m);
209209
assert(w);
210210

211-
zero(its);
212-
213211
if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
214212
timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
215213
its.it_interval = its.it_value;

src/core/device.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,15 @@ static void device_shutdown(Manager *m) {
477477
}
478478

479479
static int device_enumerate(Manager *m) {
480-
struct epoll_event ev;
481480
int r;
482481
struct udev_enumerate *e = NULL;
483482
struct udev_list_entry *item = NULL, *first = NULL;
484483

485484
assert(m);
486485

487486
if (!m->udev) {
487+
struct epoll_event ev;
488+
488489
if (!(m->udev = udev_new()))
489490
return -ENOMEM;
490491

src/core/execute.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ static int open_null_as(int flags, int nfd) {
205205

206206
static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd) {
207207
int fd, r;
208-
union sockaddr_union sa;
208+
union sockaddr_union sa = {
209+
.un.sun_family = AF_UNIX,
210+
.un.sun_path = "/run/systemd/journal/stdout",
211+
};
209212

210213
assert(context);
211214
assert(output < _EXEC_OUTPUT_MAX);
@@ -216,10 +219,6 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
216219
if (fd < 0)
217220
return -errno;
218221

219-
zero(sa);
220-
sa.un.sun_family = AF_UNIX;
221-
strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
222-
223222
r = connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
224223
if (r < 0) {
225224
close_nointr_nofail(fd);
@@ -938,7 +937,7 @@ static int apply_seccomp(uint32_t *syscall_filter) {
938937
int i;
939938
unsigned n;
940939
struct sock_filter *f;
941-
struct sock_fprog prog;
940+
struct sock_fprog prog = {};
942941

943942
assert(syscall_filter);
944943

@@ -970,7 +969,6 @@ static int apply_seccomp(uint32_t *syscall_filter) {
970969
memcpy(f + (ELEMENTSOF(header) + 2*n), footer, sizeof(footer));
971970

972971
/* Third: install the filter */
973-
zero(prog);
974972
prog.len = ELEMENTSOF(header) + ELEMENTSOF(footer) + 2*n;
975973
prog.filter = f;
976974
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0)
@@ -1210,10 +1208,9 @@ int exec_spawn(ExecCommand *command,
12101208
}
12111209

12121210
if (context->cpu_sched_set) {
1213-
struct sched_param param;
1214-
1215-
zero(param);
1216-
param.sched_priority = context->cpu_sched_priority;
1211+
struct sched_param param = {
1212+
.sched_priority = context->cpu_sched_priority,
1213+
};
12171214

12181215
r = sched_setscheduler(0,
12191216
context->cpu_sched_policy |
@@ -1701,7 +1698,7 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
17011698
int k;
17021699
bool ignore = false;
17031700
char **p;
1704-
glob_t pglob;
1701+
glob_t pglob = {};
17051702
int count, n;
17061703

17071704
fn = *i;
@@ -1721,7 +1718,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
17211718
}
17221719

17231720
/* Filename supports globbing, take all matching files */
1724-
zero(pglob);
17251721
errno = 0;
17261722
if (glob(fn, 0, NULL, &pglob) != 0) {
17271723
globfree(&pglob);

src/core/job.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,12 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
858858
}
859859

860860
int job_start_timer(Job *j) {
861-
struct itimerspec its;
862-
struct epoll_event ev;
861+
struct itimerspec its = {};
862+
struct epoll_event ev = {
863+
.data.ptr = &j->timer_watch,
864+
.events = EPOLLIN,
865+
};
863866
int fd, r;
864-
assert(j);
865867

866868
if (j->unit->job_timeout <= 0 ||
867869
j->timer_watch.type == WATCH_JOB_TIMER)
@@ -874,18 +876,13 @@ int job_start_timer(Job *j) {
874876
goto fail;
875877
}
876878

877-
zero(its);
878879
timespec_store(&its.it_value, j->unit->job_timeout);
879880

880881
if (timerfd_settime(fd, 0, &its, NULL) < 0) {
881882
r = -errno;
882883
goto fail;
883884
}
884885

885-
zero(ev);
886-
ev.data.ptr = &j->timer_watch;
887-
ev.events = EPOLLIN;
888-
889886
if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
890887
r = -errno;
891888
goto fail;
@@ -1064,15 +1061,14 @@ int job_deserialize(Job *j, FILE *f, FDSet *fds) {
10641061
}
10651062

10661063
int job_coldplug(Job *j) {
1067-
struct epoll_event ev;
1064+
struct epoll_event ev = {
1065+
.data.ptr = &j->timer_watch,
1066+
.events = EPOLLIN,
1067+
};
10681068

10691069
if (j->timer_watch.type != WATCH_JOB_TIMER)
10701070
return 0;
10711071

1072-
zero(ev);
1073-
ev.data.ptr = &j->timer_watch;
1074-
ev.events = EPOLLIN;
1075-
10761072
if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, j->timer_watch.fd, &ev) < 0)
10771073
return -errno;
10781074

src/core/locale-setup.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ static const char * const variable_names[_VARIABLE_MAX] = {
6868
};
6969

7070
int locale_setup(void) {
71-
char *variables[_VARIABLE_MAX];
71+
char *variables[_VARIABLE_MAX] = {};
7272
int r = 0, i;
7373

74-
zero(variables);
75-
7674
if (detect_container(NULL) <= 0) {
7775
r = parse_env_file("/proc/cmdline", WHITESPACE,
7876
"locale.LANG", &variables[VARIABLE_LANG],

src/core/loopback-setup.c

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -88,39 +88,38 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) {
8888
union {
8989
struct sockaddr sa;
9090
struct sockaddr_nl nl;
91-
} sa;
91+
} sa = {
92+
.nl.nl_family = AF_NETLINK,
93+
};
94+
9295
union {
9396
struct nlmsghdr header;
9497
uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
9598
NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
9699
RTA_LENGTH(sizeof(struct in6_addr))];
97-
} request;
100+
} request = {
101+
.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
102+
.header.nlmsg_type = RTM_NEWADDR,
103+
.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK,
104+
.header.nlmsg_seq = *requests + 1,
105+
};
98106

99107
struct ifaddrmsg *ifaddrmsg;
100108
uint32_t ipv4_address = htonl(INADDR_LOOPBACK);
101109
int r;
102110

103-
zero(request);
104-
105-
request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
106-
request.header.nlmsg_type = RTM_NEWADDR;
107-
request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK;
108-
request.header.nlmsg_seq = *requests + 1;
109-
110111
ifaddrmsg = NLMSG_DATA(&request.header);
111112
ifaddrmsg->ifa_family = AF_INET;
112113
ifaddrmsg->ifa_prefixlen = 8;
113114
ifaddrmsg->ifa_flags = IFA_F_PERMANENT;
114115
ifaddrmsg->ifa_scope = RT_SCOPE_HOST;
115116
ifaddrmsg->ifa_index = if_loopback;
116117

117-
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &ipv4_address, sizeof(ipv4_address));
118+
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL,
119+
&ipv4_address, sizeof(ipv4_address));
118120
if (r < 0)
119121
return r;
120122

121-
zero(sa);
122-
sa.nl.nl_family = AF_NETLINK;
123-
124123
if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0)
125124
return -errno;
126125
(*requests)++;
@@ -134,7 +133,8 @@ static int add_adresses(int fd, int if_loopback, unsigned *requests) {
134133
ifaddrmsg->ifa_family = AF_INET6;
135134
ifaddrmsg->ifa_prefixlen = 128;
136135

137-
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &in6addr_loopback, sizeof(in6addr_loopback));
136+
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL,
137+
&in6addr_loopback, sizeof(in6addr_loopback));
138138
if (r < 0)
139139
return r;
140140

@@ -149,31 +149,29 @@ static int start_interface(int fd, int if_loopback, unsigned *requests) {
149149
union {
150150
struct sockaddr sa;
151151
struct sockaddr_nl nl;
152-
} sa;
152+
} sa = {
153+
.nl.nl_family = AF_NETLINK,
154+
};
155+
153156
union {
154157
struct nlmsghdr header;
155158
uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
156159
NLMSG_ALIGN(sizeof(struct ifinfomsg))];
157-
} request;
160+
} request = {
161+
.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
162+
.header.nlmsg_type = RTM_NEWLINK,
163+
.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK,
164+
.header.nlmsg_seq = *requests + 1,
165+
};
158166

159167
struct ifinfomsg *ifinfomsg;
160168

161-
zero(request);
162-
163-
request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
164-
request.header.nlmsg_type = RTM_NEWLINK;
165-
request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
166-
request.header.nlmsg_seq = *requests + 1;
167-
168169
ifinfomsg = NLMSG_DATA(&request.header);
169170
ifinfomsg->ifi_family = AF_UNSPEC;
170171
ifinfomsg->ifi_index = if_loopback;
171172
ifinfomsg->ifi_flags = IFF_UP;
172173
ifinfomsg->ifi_change = IFF_UP;
173174

174-
zero(sa);
175-
sa.nl.nl_family = AF_NETLINK;
176-
177175
if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0)
178176
return -errno;
179177

@@ -234,7 +232,10 @@ static int check_loopback(void) {
234232
union {
235233
struct sockaddr sa;
236234
struct sockaddr_in in;
237-
} sa;
235+
} sa = {
236+
.in.sin_family = AF_INET,
237+
.in.sin_addr.s_addr = INADDR_LOOPBACK,
238+
};
238239

239240
/* If we failed to set up the loop back device, check whether
240241
* it might already be set up */
@@ -243,10 +244,6 @@ static int check_loopback(void) {
243244
if (fd < 0)
244245
return -errno;
245246

246-
zero(sa);
247-
sa.in.sin_family = AF_INET;
248-
sa.in.sin_addr.s_addr = INADDR_LOOPBACK;
249-
250247
if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
251248
r = 1;
252249
else
@@ -260,7 +257,9 @@ int loopback_setup(void) {
260257
union {
261258
struct sockaddr sa;
262259
struct sockaddr_nl nl;
263-
} sa;
260+
} sa = {
261+
.nl.nl_family = AF_NETLINK,
262+
};
264263
unsigned requests = 0, i;
265264
int _cleanup_close_ fd = -1;
266265
bool eperm = false;
@@ -274,8 +273,6 @@ int loopback_setup(void) {
274273
if (fd < 0)
275274
return -errno;
276275

277-
zero(sa);
278-
sa.nl.nl_family = AF_NETLINK;
279276
if (bind(fd, &sa.sa, sizeof(sa)) < 0) {
280277
r = -errno;
281278
goto error;

0 commit comments

Comments
 (0)