summaryrefslogtreecommitdiffstats
path: root/src/libsystemd
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
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')
-rw-r--r--src/libsystemd/sd-bus/bus-objects.c3
-rw-r--r--src/libsystemd/sd-bus/sd-bus.c3
-rw-r--r--src/libsystemd/sd-daemon/sd-daemon.c3
-rw-r--r--src/libsystemd/sd-device/sd-device.c3
-rw-r--r--src/libsystemd/sd-login/sd-login.c28
-rw-r--r--src/libsystemd/sd-netlink/netlink-message.c3
-rw-r--r--src/libsystemd/sd-network/sd-network.c15
7 files changed, 20 insertions, 38 deletions
diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c
index 6e00255b20..08015a99ef 100644
--- a/src/libsystemd/sd-bus/bus-objects.c
+++ b/src/libsystemd/sd-bus/bus-objects.c
@@ -1470,8 +1470,7 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
return NULL;
n->parent = parent;
- n->path = s;
- s = NULL; /* do not free */
+ n->path = TAKE_PTR(s);
r = hashmap_put(bus->nodes, n->path, n);
if (r < 0) {
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index 2f5e483ae2..8c022fce1b 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -1342,8 +1342,7 @@ int bus_set_address_user(sd_bus *b) {
if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT, ee) < 0)
return -ENOMEM;
- b->address = s;
- s = NULL;
+ b->address = TAKE_PTR(s);
return 0;
}
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
index 1334498ca4..0da0ca5890 100644
--- a/src/libsystemd/sd-daemon/sd-daemon.c
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
@@ -141,8 +141,7 @@ _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
return r;
}
- *names = l;
- l = NULL;
+ *names = TAKE_PTR(l);
return n_fds;
}
diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c
index 1297dfa911..fd2821b1a7 100644
--- a/src/libsystemd/sd-device/sd-device.c
+++ b/src/libsystemd/sd-device/sd-device.c
@@ -1297,8 +1297,7 @@ int device_get_id_filename(sd_device *device, const char **ret) {
}
}
- device->id_filename = id;
- id = NULL;
+ device->id_filename = TAKE_PTR(id);
}
*ret = device->id_filename;
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;
}
diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c
index af3d13edcd..3a5f9346aa 100644
--- a/src/libsystemd/sd-netlink/netlink-message.c
+++ b/src/libsystemd/sd-netlink/netlink-message.c
@@ -803,8 +803,7 @@ static int netlink_container_parse(sd_netlink_message *m,
attributes[type].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
}
- container->attributes = attributes;
- attributes = NULL;
+ container->attributes = TAKE_PTR(attributes);
container->n_attributes = count;
return 0;
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;
}