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:
Lennart Poettering
2021-05-19 16:42:37 +02:00
parent 99480504d4
commit 319a4f4bc4
127 changed files with 658 additions and 688 deletions
+6 -6
View File
@@ -19,7 +19,7 @@
int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
_cleanup_free_ char *s = NULL;
size_t allocated = 0, sz = 0;
size_t sz = 0;
char quote = 0; /* 0 or ' or " */
bool backslash = false; /* whether we've just seen a backslash */
char c;
@@ -42,7 +42,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
* the pointer *p at the first invalid character. */
if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
if (!GREEDY_REALLOC(s, allocated, sz+1))
if (!GREEDY_REALLOC(s, sz+1))
return -ENOMEM;
for (;; (*p)++, c = **p) {
@@ -57,7 +57,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
/* We found a non-blank character, so we will always
* want to return a string (even if it is empty),
* allocate it here. */
if (!GREEDY_REALLOC(s, allocated, sz+1))
if (!GREEDY_REALLOC(s, sz+1))
return -ENOMEM;
break;
}
@@ -65,7 +65,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
for (;; (*p)++, c = **p) {
if (backslash) {
if (!GREEDY_REALLOC(s, allocated, sz+7))
if (!GREEDY_REALLOC(s, sz+7))
return -ENOMEM;
if (c == 0) {
@@ -128,7 +128,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
backslash = true;
break;
} else {
if (!GREEDY_REALLOC(s, allocated, sz+2))
if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;
@@ -160,7 +160,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
goto finish;
} else {
if (!GREEDY_REALLOC(s, allocated, sz+2))
if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;