summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-login
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-login
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-login')
-rw-r--r--src/libsystemd/sd-login/sd-login.c28
1 files changed, 10 insertions, 18 deletions
diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c
index 6601bcd6be..2a4eede9f8 100644
--- a/src/libsystemd/sd-login/sd-login.c
+++ b/src/libsystemd/sd-login/sd-login.c
@@ -325,8 +325,7 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
if (isempty(s))
return -ENODATA;
- *session = s;
- s = NULL;
+ *session = TAKE_PTR(s);
return 0;
}
@@ -355,8 +354,7 @@ static int file_of_seat(const char *seat, char **_p) {
if (!p)
return -ENOMEM;
- *_p = p;
- p = NULL;
+ *_p = TAKE_PTR(p);
return 0;
}
@@ -529,8 +527,7 @@ _public_ int sd_session_get_state(const char *session, char **state) {
if (isempty(s))
return -EIO;
- *state = s;
- s = NULL;
+ *state = TAKE_PTR(s);
return 0;
}
@@ -575,8 +572,7 @@ static int session_get_string(const char *session, const char *field, char **val
if (isempty(s))
return -ENODATA;
- *value = s;
- s = NULL;
+ *value = TAKE_PTR(s);
return 0;
}
@@ -681,10 +677,8 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
return r;
}
- if (session && s) {
- *session = s;
- s = NULL;
- }
+ if (session && s)
+ *session = TAKE_PTR(s);
return 0;
}
@@ -909,10 +903,9 @@ _public_ int sd_get_machine_names(char ***machines) {
*b = NULL;
}
- if (machines) {
- *machines = l;
- l = NULL;
- }
+ if (machines)
+ *machines = TAKE_PTR(l);
+
return r;
}
@@ -933,8 +926,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
if (!c)
return -EIO;
- *class = c;
- c = NULL;
+ *class = TAKE_PTR(c);
return 0;
}