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.)
This commit is contained in:
@@ -254,7 +254,6 @@ static int boot_entries_find(
|
||||
size_t *n_entries) {
|
||||
|
||||
_cleanup_strv_free_ char **files = NULL;
|
||||
size_t n_allocated = *n_entries;
|
||||
char **f;
|
||||
int r;
|
||||
|
||||
@@ -268,7 +267,7 @@ static int boot_entries_find(
|
||||
return log_error_errno(r, "Failed to list files in \"%s\": %m", dir);
|
||||
|
||||
STRV_FOREACH(f, files) {
|
||||
if (!GREEDY_REALLOC0(*entries, n_allocated, *n_entries + 1))
|
||||
if (!GREEDY_REALLOC0(*entries, *n_entries + 1))
|
||||
return log_oom();
|
||||
|
||||
r = boot_entry_load(root, *f, *entries + *n_entries);
|
||||
@@ -463,7 +462,6 @@ static int boot_entries_find_unified(
|
||||
size_t *n_entries) {
|
||||
|
||||
_cleanup_(closedirp) DIR *d = NULL;
|
||||
size_t n_allocated = *n_entries;
|
||||
struct dirent *de;
|
||||
int r;
|
||||
|
||||
@@ -491,7 +489,7 @@ static int boot_entries_find_unified(
|
||||
if (!endswith_no_case(de->d_name, ".efi"))
|
||||
continue;
|
||||
|
||||
if (!GREEDY_REALLOC0(*entries, n_allocated, *n_entries + 1))
|
||||
if (!GREEDY_REALLOC0(*entries, *n_entries + 1))
|
||||
return log_oom();
|
||||
|
||||
fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
|
||||
@@ -749,7 +747,6 @@ int boot_entries_augment_from_loader(
|
||||
"auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
|
||||
};
|
||||
|
||||
size_t n_allocated;
|
||||
char **i;
|
||||
|
||||
assert(config);
|
||||
@@ -757,8 +754,6 @@ int boot_entries_augment_from_loader(
|
||||
/* Let's add the entries discovered by the boot loader to the end of our list, unless they are
|
||||
* already included there. */
|
||||
|
||||
n_allocated = config->n_entries;
|
||||
|
||||
STRV_FOREACH(i, found_by_loader) {
|
||||
_cleanup_free_ char *c = NULL, *t = NULL, *p = NULL;
|
||||
char **a, **b;
|
||||
@@ -785,7 +780,7 @@ int boot_entries_augment_from_loader(
|
||||
if (!p)
|
||||
return log_oom();
|
||||
|
||||
if (!GREEDY_REALLOC0(config->entries, n_allocated, config->n_entries + 1))
|
||||
if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1))
|
||||
return log_oom();
|
||||
|
||||
config->entries[config->n_entries++] = (BootEntry) {
|
||||
|
||||
Reference in New Issue
Block a user