summaryrefslogtreecommitdiffstats
path: root/src/shared
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/shared
parentman/udevadm: remove superfluous --version from subcommands (#8549) (diff)
downloadsystemd-ae2a15bc14bc448e625ad93fd044bc077ede4b3f.tar.xz
systemd-ae2a15bc14bc448e625ad93fd044bc077ede4b3f.zip
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/shared')
-rw-r--r--src/shared/ask-password-api.c3
-rw-r--r--src/shared/bus-util.c3
-rw-r--r--src/shared/cgroup-show.c3
-rw-r--r--src/shared/dns-domain.c6
-rw-r--r--src/shared/efivars.c6
-rw-r--r--src/shared/fdset.c4
-rw-r--r--src/shared/fstab-util.c9
-rw-r--r--src/shared/import-util.c3
-rw-r--r--src/shared/install-printf.c6
-rw-r--r--src/shared/install.c9
-rw-r--r--src/shared/machine-image.c6
-rw-r--r--src/shared/nsflags.c3
-rw-r--r--src/shared/path-lookup.c15
-rw-r--r--src/shared/specifier.c3
14 files changed, 25 insertions, 54 deletions
diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c
index 4fa9188957..88cafd4940 100644
--- a/src/shared/ask-password-api.c
+++ b/src/shared/ask-password-api.c
@@ -730,8 +730,7 @@ int ask_password_agent(
if (keyname)
(void) add_to_keyring_and_log(keyname, flags, l);
- *ret = l;
- l = NULL;
+ *ret = TAKE_PTR(l);
r = 0;
finish:
diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c
index 8061af1865..803f75335c 100644
--- a/src/shared/bus-util.c
+++ b/src/shared/bus-util.c
@@ -1094,8 +1094,7 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_
return r;
strv_free(*p);
- *p = l;
- l = NULL;
+ *p = TAKE_PTR(l);
return 0;
}
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
index 0ddae95434..326a398075 100644
--- a/src/shared/cgroup-show.c
+++ b/src/shared/cgroup-show.c
@@ -189,8 +189,7 @@ int show_cgroup_by_path(
free(last);
}
- last = k;
- k = NULL;
+ last = TAKE_PTR(k);
}
if (r < 0)
diff --git a/src/shared/dns-domain.c b/src/shared/dns-domain.c
index 8c807e0e23..89d18431e4 100644
--- a/src/shared/dns-domain.c
+++ b/src/shared/dns-domain.c
@@ -295,8 +295,7 @@ int dns_label_escape_new(const char *p, size_t l, char **ret) {
if (r < 0)
return r;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return r;
}
@@ -601,8 +600,7 @@ int dns_name_endswith(const char *name, const char *suffix) {
/* Not the same, let's jump back, and try with the next label again */
s = suffix;
- n = saved_n;
- saved_n = NULL;
+ n = TAKE_PTR(saved_n);
}
}
}
diff --git a/src/shared/efivars.c b/src/shared/efivars.c
index 9ca51cf750..d31cf2d860 100644
--- a/src/shared/efivars.c
+++ b/src/shared/efivars.c
@@ -249,8 +249,7 @@ int efi_get_variable(
((char*) buf)[st.st_size - 4] = 0;
((char*) buf)[st.st_size - 4 + 1] = 0;
- *value = buf;
- buf = NULL;
+ *value = TAKE_PTR(buf);
*size = (size_t) st.st_size - 4;
if (attribute)
@@ -563,8 +562,7 @@ int efi_get_boot_order(uint16_t **order) {
l / sizeof(uint16_t) > INT_MAX)
return -EINVAL;
- *order = buf;
- buf = NULL;
+ *order = TAKE_PTR(buf);
return (int) (l / sizeof(uint16_t));
}
diff --git a/src/shared/fdset.c b/src/shared/fdset.c
index 9ce1295223..845c19b1cf 100644
--- a/src/shared/fdset.c
+++ b/src/shared/fdset.c
@@ -25,6 +25,7 @@
#include "sd-daemon.h"
+#include "alloc-util.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "fdset.h"
@@ -168,8 +169,7 @@ int fdset_new_fill(FDSet **_s) {
}
r = 0;
- *_s = s;
- s = NULL;
+ *_s = TAKE_PTR(s);
finish:
/* We won't close the fds here! */
diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c
index bcd7b43084..1a3bc46d2a 100644
--- a/src/shared/fstab-util.c
+++ b/src/shared/fstab-util.c
@@ -169,10 +169,8 @@ answer:
*filtered = f;
}
- if (value) {
- *value = v;
- v = NULL;
- }
+ if (value)
+ *value = TAKE_PTR(v);
return !!n;
}
@@ -201,8 +199,7 @@ int fstab_extract_values(const char *opts, const char *name, char ***values) {
return r;
}
- *values = res;
- res = NULL;
+ *values = TAKE_PTR(res);
return !!*values;
}
diff --git a/src/shared/import-util.c b/src/shared/import-util.c
index 07ba216e93..5f71add506 100644
--- a/src/shared/import-util.c
+++ b/src/shared/import-util.c
@@ -153,8 +153,7 @@ int raw_strip_suffixes(const char *p, char **ret) {
break;
}
- *ret = q;
- q = NULL;
+ *ret = TAKE_PTR(q);
return 0;
}
diff --git a/src/shared/install-printf.c b/src/shared/install-printf.c
index aaab2e6665..5135d0671e 100644
--- a/src/shared/install-printf.c
+++ b/src/shared/install-printf.c
@@ -50,10 +50,8 @@ static int specifier_prefix_and_instance(char specifier, void *data, void *userd
if (!ans)
return -ENOMEM;
*ret = ans;
- } else {
- *ret = prefix;
- prefix = NULL;
- }
+ } else
+ *ret = TAKE_PTR(prefix);
return 0;
}
diff --git a/src/shared/install.c b/src/shared/install.c
index ed5f51cc46..7506018d41 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1422,8 +1422,7 @@ static int unit_file_search(
r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags);
if (r >= 0) {
- info->path = path;
- path = NULL;
+ info->path = TAKE_PTR(path);
result = r;
found_unit = true;
break;
@@ -1446,8 +1445,7 @@ static int unit_file_search(
r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags);
if (r >= 0) {
- info->path = path;
- path = NULL;
+ info->path = TAKE_PTR(path);
result = r;
found_unit = true;
break;
@@ -1754,8 +1752,7 @@ static int install_info_symlink_wants(
if (r < 0)
return r;
- path = instance.path;
- instance.path = NULL;
+ path = TAKE_PTR(instance.path);
if (instance.type == UNIT_FILE_TYPE_MASKED) {
unit_file_changes_add(changes, n_changes, -ERFKILL, path, NULL);
diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c
index 66eefb3036..f48b6c693c 100644
--- a/src/shared/machine-image.c
+++ b/src/shared/machine-image.c
@@ -80,7 +80,6 @@ Image *image_unref(Image *i) {
static char **image_settings_path(Image *image) {
_cleanup_strv_free_ char **l = NULL;
- char **ret;
const char *fn, *s;
unsigned i = 0;
@@ -104,10 +103,7 @@ static char **image_settings_path(Image *image) {
if (!l[i])
return NULL;
- ret = l;
- l = NULL;
-
- return ret;
+ return TAKE_PTR(l);
}
static char *image_roothash_path(Image *image) {
diff --git a/src/shared/nsflags.c b/src/shared/nsflags.c
index 05ec9feb8d..a8ddc9a57f 100644
--- a/src/shared/nsflags.c
+++ b/src/shared/nsflags.c
@@ -114,8 +114,7 @@ int namespace_flag_to_string_many(unsigned long flags, char **ret) {
return -ENOMEM;
}
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 0;
}
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
index 5f31831c99..a472e80aec 100644
--- a/src/shared/path-lookup.c
+++ b/src/shared/path-lookup.c
@@ -181,7 +181,6 @@ static char** user_dirs(
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
_cleanup_free_ char *data_home = NULL;
_cleanup_strv_free_ char **res = NULL;
- char **tmp;
int r;
r = xdg_user_dirs(&config_dirs, &data_dirs);
@@ -242,10 +241,7 @@ static char** user_dirs(
if (path_strv_make_absolute_cwd(res) < 0)
return NULL;
- tmp = res;
- res = NULL;
-
- return tmp;
+ return TAKE_PTR(res);
}
bool path_is_user_data_dir(const char *path) {
@@ -374,8 +370,7 @@ static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **ru
*runtime = NULL;
}
- *persistent = a;
- a = NULL;
+ *persistent = TAKE_PTR(a);
return 0;
@@ -413,8 +408,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r
if (!b)
return -ENOMEM;
- *runtime = b;
- b = NULL;
+ *runtime = TAKE_PTR(b);
break;
}
@@ -443,8 +437,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r
assert_not_reached("Hmm, unexpected scope value.");
}
- *persistent = a;
- a = NULL;
+ *persistent = TAKE_PTR(a);
return 0;
}
diff --git a/src/shared/specifier.c b/src/shared/specifier.c
index 23aaa88c4b..98d95eef8e 100644
--- a/src/shared/specifier.c
+++ b/src/shared/specifier.c
@@ -117,8 +117,7 @@ int specifier_printf(const char *text, const Specifier table[], void *userdata,
*(t++) = '%';
*t = 0;
- *_ret = ret;
- ret = NULL;
+ *_ret = TAKE_PTR(ret);
return 0;
}