diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-03-22 16:53:26 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-03-22 20:21:42 +0100 |
commit | ae2a15bc14bc448e625ad93fd044bc077ede4b3f (patch) | |
tree | e503e6cf3b571d0a150dc2cea7d1838f55aaa6ab /src/shared/path-lookup.c | |
parent | 1147eef0b6a5937526247ff81ca1e5e45205ed16 (diff) | |
download | systemd-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/shared/path-lookup.c')
-rw-r--r-- | src/shared/path-lookup.c | 15 |
1 files changed, 4 insertions, 11 deletions
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; } |