summaryrefslogtreecommitdiff
path: root/src/systemctl
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/systemctl
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/systemctl')
-rw-r--r--src/systemctl/systemctl.c29
1 files changed, 11 insertions, 18 deletions
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 064d593131..d23bddc99a 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -1924,8 +1924,7 @@ static int get_machine_list(
return log_oom();
machine_infos[c].is_host = true;
- machine_infos[c].name = hn;
- hn = NULL;
+ machine_infos[c].name = TAKE_PTR(hn);
(void) get_machine_properties(bus, &machine_infos[c]);
c++;
@@ -2462,10 +2461,9 @@ static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **un
if (r < 0)
return log_error_errno(r, "Failed to access path '%s': %m", path);
- if (unit_path) {
- *unit_path = lpath;
- lpath = NULL;
- }
+ if (unit_path)
+ *unit_path = TAKE_PTR(lpath);
+
return 1;
}
@@ -2497,10 +2495,9 @@ static int unit_find_template_path(
if (r < 0)
return r;
- if (template) {
- *template = _template;
- _template = NULL;
- }
+ if (template)
+ *template = TAKE_PTR(_template);
+
return r;
}
@@ -6671,8 +6668,7 @@ static int create_edit_temp_file(const char *new_path, const char *original_path
} else if (r < 0)
return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path);
- *ret_tmp_fn = t;
- t = NULL;
+ *ret_tmp_fn = TAKE_PTR(t);
return 0;
}
@@ -6703,12 +6699,9 @@ static int get_file_to_edit(
return -EEXIST;
}
- *ret_path = run;
- run = NULL;
- } else {
- *ret_path = path;
- path = NULL;
- }
+ *ret_path = TAKE_PTR(run);
+ } else
+ *ret_path = TAKE_PTR(path);
return 0;
}