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/test/test-nss-hosts.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/test/test-nss-hosts.c')
-rw-r--r-- | src/test/test-nss-hosts.c | 7 |
1 files changed, 2 insertions, 5 deletions
diff --git a/src/test/test-nss-hosts.c b/src/test/test-nss-hosts.c index 42ed53adc3..eddb047951 100644 --- a/src/test/test-nss-hosts.c +++ b/src/test/test-nss-hosts.c @@ -354,15 +354,13 @@ static void test_byaddr(void *handle, static int make_addresses(struct local_address **addresses) { int n; - size_t n_alloc; _cleanup_free_ struct local_address *addrs = NULL; n = local_addresses(NULL, 0, AF_UNSPEC, &addrs); if (n < 0) log_info_errno(n, "Failed to query local addresses: %m"); - n_alloc = n; /* we _can_ do that */ - assert_se(GREEDY_REALLOC(addrs, n_alloc, n + 3)); + assert_se(GREEDY_REALLOC(addrs, n + 3)); addrs[n++] = (struct local_address) { .family = AF_INET, .address.in = { htobe32(0x7F000001) } }; @@ -406,7 +404,6 @@ static int parse_argv(int argc, char **argv, _cleanup_strv_free_ char **modules = NULL, **names = NULL; _cleanup_free_ struct local_address *addrs = NULL; - size_t n_allocated = 0; const char *p; int r, n = 0; @@ -446,7 +443,7 @@ static int parse_argv(int argc, char **argv, if (r < 0) return r; } else { - assert_se(GREEDY_REALLOC0(addrs, n_allocated, n + 1)); + assert_se(GREEDY_REALLOC0(addrs, n + 1)); addrs[n++] = (struct local_address) { .family = family, .address = address }; |