summaryrefslogtreecommitdiff
path: root/src/shared/local-addresses.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-05-18 23:01:32 +0200
committerLennart Poettering <lennart@poettering.net>2021-05-19 16:42:37 +0200
commit319a4f4bc46b230fc660321e99aaac1bc449deea (patch)
tree9eca2e1352df29aeeef91e4fd4bcc12ad4ea44e9 /src/shared/local-addresses.c
parent99480504d47c0a6bbb1ac230cc33df12bbd36c54 (diff)
downloadsystemd-319a4f4bc46b230fc660321e99aaac1bc449deea.tar.gz
alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()
We recently started making more use of malloc_usable_size() and rely on it (see the string_erase() story). Given that we don't really support sytems where malloc_usable_size() cannot be trusted beyond statistics anyway, let's go fully in and rework GREEDY_REALLOC() on top of it: instead of passing around and maintaining the currenly allocated size everywhere, let's just derive it automatically from malloc_usable_size(). I am mostly after this for the simplicity this brings. It also brings minor efficiency improvements I guess, but things become so much nicer to look at if we can avoid these allocation size variables everywhere. Note that the malloc_usable_size() man page says relying on it wasn't "good programming practice", but I think it does this for reasons that don't apply here: the greedy realloc logic specifically doesn't rely on the returned extra size, beyond the fact that it is equal or larger than what was requested. (This commit was supposed to be a quick patch btw, but apparently we use the greedy realloc stuff quite a bit across the codebase, so this ends up touching *a*lot* of code.)
Diffstat (limited to 'src/shared/local-addresses.c')
-rw-r--r--src/shared/local-addresses.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/src/shared/local-addresses.c b/src/shared/local-addresses.c
index 9dec7a7e1a..66e452adad 100644
--- a/src/shared/local-addresses.c
+++ b/src/shared/local-addresses.c
@@ -67,7 +67,7 @@ int local_addresses(
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_free_ struct local_address *list = NULL;
- size_t n_list = 0, n_allocated = 0;
+ size_t n_list = 0;
sd_netlink_message *m;
int r;
@@ -121,7 +121,7 @@ int local_addresses(
if (flags & IFA_F_DEPRECATED)
continue;
- if (!GREEDY_REALLOC0(list, n_allocated, n_list+1))
+ if (!GREEDY_REALLOC0(list, n_list+1))
return -ENOMEM;
a = list + n_list;
@@ -175,7 +175,6 @@ int local_addresses(
static int add_local_gateway(
struct local_address **list,
size_t *n_list,
- size_t *n_allocated,
int af,
int ifindex,
uint32_t metric,
@@ -183,13 +182,12 @@ static int add_local_gateway(
assert(list);
assert(n_list);
- assert(n_allocated);
assert(via);
if (af != AF_UNSPEC && af != via->family)
return 0;
- if (!GREEDY_REALLOC(*list, *n_allocated, *n_list + 1))
+ if (!GREEDY_REALLOC(*list, *n_list + 1))
return -ENOMEM;
(*list)[(*n_list)++] = (struct local_address) {
@@ -211,7 +209,7 @@ int local_gateways(
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_free_ struct local_address *list = NULL;
- size_t n_list = 0, n_allocated = 0;
+ size_t n_list = 0;
int r;
if (context)
@@ -299,7 +297,7 @@ int local_gateways(
if (r >= 0) {
via.family = family;
via.address = gateway;
- r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &via);
+ r = add_local_gateway(&list, &n_list, af, ifi, metric, &via);
if (r < 0)
return r;
@@ -313,7 +311,7 @@ int local_gateways(
if (r < 0 && r != -ENODATA)
return r;
if (r >= 0) {
- r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &via);
+ r = add_local_gateway(&list, &n_list, af, ifi, metric, &via);
if (r < 0)
return r;
@@ -335,7 +333,7 @@ int local_gateways(
if (ifindex > 0 && mr->ifindex != ifindex)
continue;
- r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &mr->gateway);
+ r = add_local_gateway(&list, &n_list, af, ifi, metric, &mr->gateway);
if (r < 0)
return r;
}
@@ -358,7 +356,7 @@ int local_outbounds(
struct local_address **ret) {
_cleanup_free_ struct local_address *list = NULL, *gateways = NULL;
- size_t n_list = 0, n_allocated = 0;
+ size_t n_list = 0;
int r, n_gateways;
/* Determines our default outbound addresses, i.e. the "primary" local addresses we use to talk to IP
@@ -457,7 +455,7 @@ int local_outbounds(
if (in4_addr_is_null(&sa.in.sin_addr)) /* Auto-binding didn't work. :-( */
continue;
- if (!GREEDY_REALLOC(list, n_allocated, n_list+1))
+ if (!GREEDY_REALLOC(list, n_list+1))
return -ENOMEM;
list[n_list++] = (struct local_address) {
@@ -472,7 +470,7 @@ int local_outbounds(
if (in6_addr_is_null(&sa.in6.sin6_addr))
continue;
- if (!GREEDY_REALLOC(list, n_allocated, n_list+1))
+ if (!GREEDY_REALLOC(list, n_list+1))
return -ENOMEM;
list[n_list++] = (struct local_address) {