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
+2 -4
View File
@@ -253,10 +253,8 @@ int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_callback_t c
if (message_type == 0)
return -ENOMSG;
if (_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE)) {
*_error_message = error_message;
error_message = NULL;
}
if (_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE))
*_error_message = TAKE_PTR(error_message);
return message_type;
}
+1 -2
View File
@@ -617,8 +617,7 @@ int dhcp6_option_parse_domainname(const uint8_t *optval, uint16_t optlen, char *
idx++;
}
*str_arr = names;
names = NULL;
*str_arr = TAKE_PTR(names);
return idx;
+6 -10
View File
@@ -276,10 +276,9 @@ int config_parse_ifalias(const char *unit,
}
free(*s);
if (*n) {
*s = n;
n = NULL;
} else
if (*n)
*s = TAKE_PTR(n);
else
*s = NULL;
return 0;
@@ -437,8 +436,7 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
size++;
}
*ret = addresses;
addresses = NULL;
*ret = TAKE_PTR(addresses);
return size;
}
@@ -491,8 +489,7 @@ int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
size++;
}
*ret = addresses;
addresses = NULL;
*ret = TAKE_PTR(addresses);
return size;
}
@@ -585,8 +582,7 @@ int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t
*ret_size = size;
*ret_allocated = allocated;
*ret = routes;
routes = NULL;
*ret = TAKE_PTR(routes);
return 0;
}
+1 -2
View File
@@ -807,8 +807,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d
pos = next_chunk;
}
*domains = names;
names = NULL;
*domains = TAKE_PTR(names);
return cnt;
}