summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-03-22 16:53:26 +0100
committerLennart Poettering <lennart@poettering.net>2018-03-22 20:21:42 +0100
commitae2a15bc14bc448e625ad93fd044bc077ede4b3f (patch)
treee503e6cf3b571d0a150dc2cea7d1838f55aaa6ab /src/core
parent1147eef0b6a5937526247ff81ca1e5e45205ed16 (diff)
downloadsystemd-ae2a15bc14bc448e625ad93fd044bc077ede4b3f.tar.gz
macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the pointer to NULL. This is useful as an explicit concept of passing ownership of a memory area between pointers. This takes inspiration from Rust: https://doc.rust-lang.org/std/option/enum.Option.html#method.take and was suggested by Alan Jenkins (@sourcejedi). It drops ~160 lines of code from our codebase, which makes me like it. Also, I think it clarifies passing of ownership, and thus helps readability a bit (at least for the initiated who know the new macro)
Diffstat (limited to 'src/core')
-rw-r--r--src/core/cgroup.c3
-rw-r--r--src/core/dbus-execute.c9
-rw-r--r--src/core/dbus-job.c3
-rw-r--r--src/core/dbus.c6
-rw-r--r--src/core/dynamic-user.c3
-rw-r--r--src/core/execute.c13
-rw-r--r--src/core/load-fragment.c42
-rw-r--r--src/core/mount-setup.c6
-rw-r--r--src/core/service.c3
-rw-r--r--src/core/unit.c22
10 files changed, 36 insertions, 74 deletions
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 65ed86580f..62a3d86ef1 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -1381,8 +1381,7 @@ int unit_set_cgroup_path(Unit *u, const char *path) {
unit_release_cgroup(u);
- u->cgroup_path = p;
- p = NULL;
+ u->cgroup_path = TAKE_PTR(p);
return 1;
}
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index 635213a866..7344623ebf 100644
--- a/src/core/dbus-execute.c
+++ b/src/core/dbus-execute.c
@@ -424,10 +424,8 @@ static int property_get_syscall_filter(
if (r < 0)
return -ENOMEM;
}
- } else {
- s = name;
- name = NULL;
- }
+ } else
+ s = TAKE_PTR(name);
r = strv_consume(&l, s);
if (r < 0)
@@ -1125,8 +1123,7 @@ int bus_set_transient_exec_command(
return -ENOMEM;
}
- c->argv = argv;
- argv = NULL;
+ c->argv = TAKE_PTR(argv);
c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;
diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c
index 0802fc9773..15d0e5dd8b 100644
--- a/src/core/dbus-job.c
+++ b/src/core/dbus-job.c
@@ -274,8 +274,7 @@ int bus_job_coldplug_bus_track(Job *j) {
assert(j);
- deserialized_clients = j->deserialized_clients;
- j->deserialized_clients = NULL;
+ deserialized_clients = TAKE_PTR(j->deserialized_clients);
if (strv_isempty(deserialized_clients))
return 0;
diff --git a/src/core/dbus.c b/src/core/dbus.c
index 56b43adcda..eb9ec44f5b 100644
--- a/src/core/dbus.c
+++ b/src/core/dbus.c
@@ -501,8 +501,7 @@ static int bus_job_enumerate(sd_bus *bus, const char *path, void *userdata, char
assert(hashmap_size(m->jobs) == k);
- *nodes = l;
- l = NULL;
+ *nodes = TAKE_PTR(l);
return k;
}
@@ -526,8 +525,7 @@ static int bus_unit_enumerate(sd_bus *bus, const char *path, void *userdata, cha
k++;
}
- *nodes = l;
- l = NULL;
+ *nodes = TAKE_PTR(l);
return k;
}
diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c
index de6aadd597..f87a5a20d4 100644
--- a/src/core/dynamic-user.c
+++ b/src/core/dynamic-user.c
@@ -770,8 +770,7 @@ int dynamic_user_lookup_uid(Manager *m, uid_t uid, char **ret) {
if (check_uid != uid) /* lock file doesn't match our own idea */
return -ESRCH;
- *ret = user;
- user = NULL;
+ *ret = TAKE_PTR(user);
return 0;
}
diff --git a/src/core/execute.c b/src/core/execute.c
index 7292b815db..bfd3dfdafc 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -1733,8 +1733,7 @@ static int build_environment(
our_env[n_env++] = NULL;
assert(n_env <= 12);
- *ret = our_env;
- our_env = NULL;
+ *ret = TAKE_PTR(our_env);
return 0;
}
@@ -1763,8 +1762,7 @@ static int build_pass_environment(const ExecContext *c, char ***ret) {
x = NULL;
}
- *ret = pass_env;
- pass_env = NULL;
+ *ret = TAKE_PTR(pass_env);
return 0;
}
@@ -2290,9 +2288,7 @@ static int compile_bind_mounts(
*ret_bind_mounts = bind_mounts;
*ret_n_bind_mounts = n;
- *ret_empty_directories = empty_directories;
-
- empty_directories = NULL;
+ *ret_empty_directories = TAKE_PTR(empty_directories);
return (int) n;
@@ -2696,8 +2692,7 @@ static int compile_suggested_paths(const ExecContext *c, const ExecParameters *p
}
}
- *ret = list;
- list = NULL;
+ *ret = TAKE_PTR(list);
return 0;
}
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 94605ab0d0..887eb1cf49 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -1248,8 +1248,7 @@ int config_parse_exec_cpu_affinity(const char *unit,
}
if (!c->cpuset) {
- c->cpuset = cpuset;
- cpuset = NULL;
+ c->cpuset = TAKE_PTR(cpuset);
c->cpuset_ncpus = (unsigned) ncpus;
return 0;
}
@@ -1257,8 +1256,7 @@ int config_parse_exec_cpu_affinity(const char *unit,
if (c->cpuset_ncpus < (unsigned) ncpus) {
CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, cpuset);
CPU_FREE(c->cpuset);
- c->cpuset = cpuset;
- cpuset = NULL;
+ c->cpuset = TAKE_PTR(cpuset);
c->cpuset_ncpus = (unsigned) ncpus;
return 0;
}
@@ -2089,8 +2087,7 @@ int config_parse_user_group(
return -ENOEXEC;
}
- n = k;
- k = NULL;
+ n = TAKE_PTR(k);
}
free(*user);
@@ -2320,10 +2317,8 @@ int config_parse_environ(
"Failed to resolve specifiers, ignoring: %s", word);
continue;
}
- } else {
- k = word;
- word = NULL;
- }
+ } else
+ k = TAKE_PTR(word);
if (!env_assignment_is_valid(k)) {
log_syntax(unit, LOG_ERR, filename, line, 0,
@@ -2390,10 +2385,8 @@ int config_parse_pass_environ(
"Failed to resolve specifiers, ignoring: %s", word);
continue;
}
- } else {
- k = word;
- word = NULL;
- }
+ } else
+ k = TAKE_PTR(word);
if (!env_name_is_valid(k)) {
log_syntax(unit, LOG_ERR, filename, line, 0,
@@ -2469,10 +2462,8 @@ int config_parse_unset_environ(
"Failed to resolve specifiers, ignoring: %s", word);
continue;
}
- } else {
- k = word;
- word = NULL;
- }
+ } else
+ k = TAKE_PTR(word);
if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
log_syntax(unit, LOG_ERR, filename, line, 0,
@@ -3515,8 +3506,7 @@ int config_parse_device_allow(
if (!a)
return log_oom();
- a->path = path;
- path = NULL;
+ a->path = TAKE_PTR(path);
a->r = !!strchr(m, 'r');
a->w = !!strchr(m, 'w');
a->m = !!strchr(m, 'm');
@@ -3615,8 +3605,7 @@ int config_parse_io_device_weight(
if (!w)
return log_oom();
- w->path = path;
- path = NULL;
+ w->path = TAKE_PTR(path);
w->weight = u;
@@ -3701,8 +3690,7 @@ int config_parse_io_limit(
if (!l)
return log_oom();
- l->path = path;
- path = NULL;
+ l->path = TAKE_PTR(path);
for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
l->limits[ttype] = cgroup_io_limit_defaults[ttype];
@@ -3804,8 +3792,7 @@ int config_parse_blockio_device_weight(
if (!w)
return log_oom();
- w->path = path;
- path = NULL;
+ w->path = TAKE_PTR(path);
w->weight = u;
@@ -3885,8 +3872,7 @@ int config_parse_blockio_bandwidth(
if (!b)
return log_oom();
- b->path = path;
- path = NULL;
+ b->path = TAKE_PTR(path);
b->rbps = CGROUP_LIMIT_MAX;
b->wbps = CGROUP_LIMIT_MAX;
diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c
index 9c27972aff..b7d654619c 100644
--- a/src/core/mount-setup.c
+++ b/src/core/mount-setup.c
@@ -315,10 +315,8 @@ int mount_cgroup_controllers(char ***join_controllers) {
options = strv_join(*k, ",");
if (!options)
return log_oom();
- } else {
- options = controller;
- controller = NULL;
- }
+ } else
+ options = TAKE_PTR(controller);
where = strappend("/sys/fs/cgroup/", options);
if (!where)
diff --git a/src/core/service.c b/src/core/service.c
index 23a5bcd1c4..588f08fef3 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -2554,8 +2554,7 @@ static int service_deserialize_exec_command(Unit *u, const char *key, const char
state = STATE_EXEC_COMMAND_PATH;
break;
case STATE_EXEC_COMMAND_PATH:
- path = arg;
- arg = NULL;
+ path = TAKE_PTR(arg);
state = STATE_EXEC_COMMAND_ARGS;
if (!path_is_absolute(path))
diff --git a/src/core/unit.c b/src/core/unit.c
index 52851b6ffc..90ec73231b 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -709,10 +709,8 @@ static int set_complete_move(Set **s, Set **other) {
if (*s)
return set_move(*s, *other);
- else {
- *s = *other;
- *other = NULL;
- }
+ else
+ *s = TAKE_PTR(*other);
return 0;
}
@@ -726,10 +724,8 @@ static int hashmap_complete_move(Hashmap **s, Hashmap **other) {
if (*s)
return hashmap_move(*s, *other);
- else {
- *s = *other;
- *other = NULL;
- }
+ else
+ *s = TAKE_PTR(*other);
return 0;
}
@@ -4017,8 +4013,7 @@ static int user_from_unit_name(Unit *u, char **ret) {
return r;
if (valid_user_group_name(n)) {
- *ret = n;
- n = NULL;
+ *ret = TAKE_PTR(n);
return 0;
}
@@ -4220,7 +4215,7 @@ char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) {
char* unit_concat_strv(char **l, UnitWriteFlags flags) {
_cleanup_free_ char *result = NULL;
size_t n = 0, allocated = 0;
- char **i, *ret;
+ char **i;
/* Takes a list of strings, escapes them, and concatenates them. This may be used to format command lines in a
* way suitable for ExecStart= stanzas */
@@ -4255,10 +4250,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags) {
result[n] = 0;
- ret = result;
- result = NULL;
-
- return ret;
+ return TAKE_PTR(result);
}
int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) {