summaryrefslogtreecommitdiff
path: root/src/libsystemd-network
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-network
parent1147eef0b6a5937526247ff81ca1e5e45205ed16 (diff)
downloadsystemd-ae2a15bc14bc448e625ad93fd044bc077ede4b3f.tar.gz
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-network')
-rw-r--r--src/libsystemd-network/dhcp-option.c6
-rw-r--r--src/libsystemd-network/dhcp6-option.c3
-rw-r--r--src/libsystemd-network/network-internal.c16
-rw-r--r--src/libsystemd-network/sd-dhcp-lease.c3
4 files changed, 10 insertions, 18 deletions
diff --git a/src/libsystemd-network/dhcp-option.c b/src/libsystemd-network/dhcp-option.c
index 0489579e7f..142e39bc66 100644
--- a/src/libsystemd-network/dhcp-option.c
+++ b/src/libsystemd-network/dhcp-option.c
@@ -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;
}
diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c
index df96ad739d..2b3c8ceb59 100644
--- a/src/libsystemd-network/dhcp6-option.c
+++ b/src/libsystemd-network/dhcp6-option.c
@@ -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;
diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c
index 584a1f36ac..00a56c8206 100644
--- a/src/libsystemd-network/network-internal.c
+++ b/src/libsystemd-network/network-internal.c
@@ -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;
}
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index 2e88e39878..9db0a93898 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -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;
}