summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-network/sd-network.c
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/libsystemd/sd-network/sd-network.c
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/libsystemd/sd-network/sd-network.c')
-rw-r--r--src/libsystemd/sd-network/sd-network.c15
1 files changed, 5 insertions, 10 deletions
diff --git a/src/libsystemd/sd-network/sd-network.c b/src/libsystemd/sd-network/sd-network.c
index 8f4814019e..1d3e9b30f5 100644
--- a/src/libsystemd/sd-network/sd-network.c
+++ b/src/libsystemd/sd-network/sd-network.c
@@ -51,8 +51,7 @@ _public_ int sd_network_get_operational_state(char **state) {
if (isempty(s))
return -ENODATA;
- *state = s;
- s = NULL;
+ *state = TAKE_PTR(s);
return 0;
}
@@ -81,8 +80,7 @@ static int network_get_strv(const char *key, char ***ret) {
strv_uniq(a);
r = strv_length(a);
- *ret = a;
- a = NULL;
+ *ret = TAKE_PTR(a);
return r;
}
@@ -121,8 +119,7 @@ static int network_link_get_string(int ifindex, const char *field, char **ret) {
if (isempty(s))
return -ENODATA;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 0;
}
@@ -154,8 +151,7 @@ static int network_link_get_strv(int ifindex, const char *key, char ***ret) {
strv_uniq(a);
r = strv_length(a);
- *ret = a;
- a = NULL;
+ *ret = TAKE_PTR(a);
return r;
}
@@ -263,8 +259,7 @@ static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) {
if (ifis)
ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice */
- *ret = ifis;
- ifis = NULL;
+ *ret = TAKE_PTR(ifis);
return c;
}