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/shared/specifier.c | |
parent | alloc-util: add MALLOC_ELEMENTSOF() helper (diff) | |
download | systemd-319a4f4bc46b230fc660321e99aaac1bc449deea.tar.xz systemd-319a4f4bc46b230fc660321e99aaac1bc449deea.zip |
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/specifier.c')
-rw-r--r-- | src/shared/specifier.c | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/src/shared/specifier.c b/src/shared/specifier.c index ef164b3942..f4c73b2741 100644 --- a/src/shared/specifier.c +++ b/src/shared/specifier.c @@ -30,18 +30,18 @@ #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%" int specifier_printf(const char *text, size_t max_length, const Specifier table[], const void *userdata, char **ret) { - size_t l, allocated = 0; _cleanup_free_ char *result = NULL; - char *t; - const char *f; bool percent = false; + const char *f; + size_t l; + char *t; int r; assert(text); assert(table); l = strlen(text); - if (!GREEDY_REALLOC(result, allocated, l + 1)) + if (!GREEDY_REALLOC(result, l + 1)) return -ENOMEM; t = result; @@ -67,7 +67,7 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[ j = t - result; k = strlen(w); - if (!GREEDY_REALLOC(result, allocated, j + k + l + 1)) + if (!GREEDY_REALLOC(result, j + k + l + 1)) return -ENOMEM; memcpy(result + j, w, k); t = result + j + k; @@ -98,13 +98,6 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[ } *(t++) = 0; - /* Try to deallocate unused bytes, but don't sweat it too much */ - if ((size_t)(t - result) < allocated) { - t = realloc(result, t - result); - if (t) - result = t; - } - *ret = TAKE_PTR(result); return 0; } |