diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-05-18 23:01:32 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-05-19 16:42:37 +0200 |
commit | 319a4f4bc46b230fc660321e99aaac1bc449deea (patch) | |
tree | 9eca2e1352df29aeeef91e4fd4bcc12ad4ea44e9 /src/libsystemd-network/sd-dhcp-lease.c | |
parent | 99480504d47c0a6bbb1ac230cc33df12bbd36c54 (diff) | |
download | systemd-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/libsystemd-network/sd-dhcp-lease.c')
-rw-r--r-- | src/libsystemd-network/sd-dhcp-lease.c | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 6d88c88e6b..095a4ee683 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -440,14 +440,13 @@ static int lease_parse_sip_server(const uint8_t *option, size_t len, struct in_a static int lease_parse_routes( const uint8_t *option, size_t len, - struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) { + struct sd_dhcp_route **routes, size_t *routes_size) { struct in_addr addr; assert(option || len <= 0); assert(routes); assert(routes_size); - assert(routes_allocated); if (len <= 0) return 0; @@ -455,7 +454,7 @@ static int lease_parse_routes( if (len % 8 != 0) return -EINVAL; - if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + (len / 8))) + if (!GREEDY_REALLOC(*routes, *routes_size + (len / 8))) return -ENOMEM; while (len >= 8) { @@ -486,12 +485,11 @@ static int lease_parse_routes( /* parses RFC3442 Classless Static Route Option */ static int lease_parse_classless_routes( const uint8_t *option, size_t len, - struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) { + struct sd_dhcp_route **routes, size_t *routes_size) { assert(option || len <= 0); assert(routes); assert(routes_size); - assert(routes_allocated); if (len <= 0) return 0; @@ -502,7 +500,7 @@ static int lease_parse_classless_routes( uint8_t dst_octets; struct sd_dhcp_route *route; - if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + 1)) + if (!GREEDY_REALLOC(*routes, *routes_size + 1)) return -ENOMEM; route = *routes + *routes_size; @@ -616,7 +614,7 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void break; case SD_DHCP_OPTION_STATIC_ROUTE: - r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size, &lease->static_route_allocated); + r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size); if (r < 0) log_debug_errno(r, "Failed to parse static routes, ignoring: %m"); break; @@ -678,8 +676,7 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void r = lease_parse_classless_routes( option, len, &lease->static_route, - &lease->static_route_size, - &lease->static_route_allocated); + &lease->static_route_size); if (r < 0) log_debug_errno(r, "Failed to parse classless routes, ignoring: %m"); break; @@ -747,7 +744,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d while (pos < len) { _cleanup_free_ char *name = NULL; - size_t n = 0, allocated = 0; + size_t n = 0; size_t jump_barrier = pos, next_chunk = 0; bool first = true; @@ -767,7 +764,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d if (pos >= len) return -EBADMSG; - if (!GREEDY_REALLOC(name, allocated, n + !first + DNS_LABEL_ESCAPED_MAX)) + if (!GREEDY_REALLOC(name, n + !first + DNS_LABEL_ESCAPED_MAX)) return -ENOMEM; if (first) @@ -806,7 +803,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d return -EBADMSG; } - if (!GREEDY_REALLOC(name, allocated, n + 1)) + if (!GREEDY_REALLOC(name, n + 1)) return -ENOMEM; name[n] = 0; @@ -1232,7 +1229,6 @@ int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) { r = deserialize_dhcp_routes( &lease->static_route, &lease->static_route_size, - &lease->static_route_allocated, routes); if (r < 0) log_debug_errno(r, "Failed to parse DHCP routes %s, ignoring: %m", routes); |