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/delta | |
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/delta')
-rw-r--r-- | src/delta/delta.c | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/src/delta/delta.c b/src/delta/delta.c index bb38db54a4..8c702459c1 100644 --- a/src/delta/delta.c +++ b/src/delta/delta.c @@ -291,7 +291,7 @@ static int enumerate_dir( _cleanup_closedir_ DIR *d = NULL; struct dirent *de; _cleanup_strv_free_ char **files = NULL, **dirs = NULL; - size_t n_files = 0, allocated_files = 0, n_dirs = 0, allocated_dirs = 0; + size_t n_files = 0, n_dirs = 0; char **t; int r; @@ -314,7 +314,7 @@ static int enumerate_dir( dirent_ensure_type(d, de); if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) { - if (!GREEDY_REALLOC0(dirs, allocated_dirs, n_dirs + 2)) + if (!GREEDY_REALLOC0(dirs, n_dirs + 2)) return -ENOMEM; dirs[n_dirs] = strdup(de->d_name); @@ -326,7 +326,7 @@ static int enumerate_dir( if (!dirent_is_file(de)) continue; - if (!GREEDY_REALLOC0(files, allocated_files, n_files + 2)) + if (!GREEDY_REALLOC0(files, n_files + 2)) return -ENOMEM; files[n_files] = strdup(de->d_name); |