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)
This commit is contained in:
Lennart Poettering
2018-03-22 20:21:42 +01:00
parent 1147eef0b6
commit ae2a15bc14
93 changed files with 215 additions and 379 deletions
+1 -2
View File
@@ -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) {
+1 -2
View File
@@ -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;
}
+1 -2
View File
@@ -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;
}
+1 -2
View File
@@ -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;
+10 -18
View File
@@ -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;
}
+1 -2
View File
@@ -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;
+5 -10
View File
@@ -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;
}