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/core | |
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/core')
-rw-r--r-- | src/core/execute.c | 4 | ||||
-rw-r--r-- | src/core/job.c | 12 | ||||
-rw-r--r-- | src/core/load-fragment.c | 18 | ||||
-rw-r--r-- | src/core/service.c | 6 | ||||
-rw-r--r-- | src/core/smack-setup.c | 4 | ||||
-rw-r--r-- | src/core/transaction.c | 4 | ||||
-rw-r--r-- | src/core/unit.c | 6 |
7 files changed, 27 insertions, 27 deletions
diff --git a/src/core/execute.c b/src/core/execute.c index 6a1e6c8429..ebd1418c8c 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -1967,7 +1967,7 @@ static int build_environment( static int build_pass_environment(const ExecContext *c, char ***ret) { _cleanup_strv_free_ char **pass_env = NULL; - size_t n_env = 0, n_bufsize = 0; + size_t n_env = 0; char **i; STRV_FOREACH(i, c->pass_environment) { @@ -1981,7 +1981,7 @@ static int build_pass_environment(const ExecContext *c, char ***ret) { if (!x) return -ENOMEM; - if (!GREEDY_REALLOC(pass_env, n_bufsize, n_env + 2)) + if (!GREEDY_REALLOC(pass_env, n_env + 2)) return -ENOMEM; pass_env[n_env++] = TAKE_PTR(x); diff --git a/src/core/job.c b/src/core/job.c index 57829d185a..c626ddca91 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -1498,8 +1498,8 @@ static size_t sort_job_list(Job **list, size_t n) { int job_get_before(Job *j, Job*** ret) { _cleanup_free_ Job** list = NULL; - size_t n = 0, n_allocated = 0; Unit *other = NULL; + size_t n = 0; void *v; /* Returns a list of all pending jobs that need to finish before this job may be started. */ @@ -1518,7 +1518,7 @@ int job_get_before(Job *j, Job*** ret) { if (job_compare(j, other->job, UNIT_AFTER) <= 0) continue; - if (!GREEDY_REALLOC(list, n_allocated, n+1)) + if (!GREEDY_REALLOC(list, n+1)) return -ENOMEM; list[n++] = other->job; } @@ -1529,7 +1529,7 @@ int job_get_before(Job *j, Job*** ret) { if (job_compare(j, other->job, UNIT_BEFORE) <= 0) continue; - if (!GREEDY_REALLOC(list, n_allocated, n+1)) + if (!GREEDY_REALLOC(list, n+1)) return -ENOMEM; list[n++] = other->job; } @@ -1543,8 +1543,8 @@ int job_get_before(Job *j, Job*** ret) { int job_get_after(Job *j, Job*** ret) { _cleanup_free_ Job** list = NULL; - size_t n = 0, n_allocated = 0; Unit *other = NULL; + size_t n = 0; void *v; assert(j); @@ -1562,7 +1562,7 @@ int job_get_after(Job *j, Job*** ret) { if (job_compare(j, other->job, UNIT_BEFORE) >= 0) continue; - if (!GREEDY_REALLOC(list, n_allocated, n+1)) + if (!GREEDY_REALLOC(list, n+1)) return -ENOMEM; list[n++] = other->job; } @@ -1577,7 +1577,7 @@ int job_get_after(Job *j, Job*** ret) { if (job_compare(j, other->job, UNIT_AFTER) >= 0) continue; - if (!GREEDY_REALLOC(list, n_allocated, n+1)) + if (!GREEDY_REALLOC(list, n+1)) return -ENOMEM; list[n++] = other->job; } diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 3dd1e869eb..cd37314542 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -712,7 +712,7 @@ int config_parse_exec( bool ignore = false, separate_argv0 = false; _cleanup_free_ ExecCommand *nce = NULL; _cleanup_strv_free_ char **n = NULL; - size_t nlen = 0, nbufsize = 0; + size_t nlen = 0; const char *f; semicolon = false; @@ -799,7 +799,7 @@ int config_parse_exec( if (!separate_argv0) { char *w = NULL; - if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) + if (!GREEDY_REALLOC(n, nlen + 2)) return log_oom(); w = strdup(path); @@ -831,7 +831,7 @@ int config_parse_exec( p += 2; p += strspn(p, WHITESPACE); - if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) + if (!GREEDY_REALLOC(n, nlen + 2)) return log_oom(); w = strdup(";"); @@ -856,7 +856,7 @@ int config_parse_exec( return ignore ? 0 : -ENOEXEC; } - if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) + if (!GREEDY_REALLOC(n, nlen + 2)) return log_oom(); n[nlen++] = TAKE_PTR(resolved); @@ -2691,9 +2691,9 @@ int config_parse_pass_environ( void *userdata) { _cleanup_strv_free_ char **n = NULL; - size_t nlen = 0, nbufsize = 0; - char*** passenv = data; const Unit *u = userdata; + char*** passenv = data; + size_t nlen = 0; int r; assert(filename); @@ -2737,7 +2737,7 @@ int config_parse_pass_environ( continue; } - if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) + if (!GREEDY_REALLOC(n, nlen + 2)) return log_oom(); n[nlen++] = TAKE_PTR(k); @@ -2766,9 +2766,9 @@ int config_parse_unset_environ( void *userdata) { _cleanup_strv_free_ char **n = NULL; - size_t nlen = 0, nbufsize = 0; char*** unsetenv = data; const Unit *u = userdata; + size_t nlen = 0; int r; assert(filename); @@ -2812,7 +2812,7 @@ int config_parse_unset_environ( continue; } - if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) + if (!GREEDY_REALLOC(n, nlen + 2)) return log_oom(); n[nlen++] = TAKE_PTR(k); diff --git a/src/core/service.c b/src/core/service.c index 085715b65a..c34030644a 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -2566,10 +2566,10 @@ static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, ExecC static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command) { _cleanup_free_ char *args = NULL, *p = NULL; - size_t allocated = 0, length = 0; Service *s = SERVICE(u); const char *type, *key; ServiceExecCommand id; + size_t length = 0; unsigned idx; char **arg; @@ -2598,7 +2598,7 @@ static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command return log_oom(); n = strlen(e); - if (!GREEDY_REALLOC(args, allocated, length + 2 + n + 2)) + if (!GREEDY_REALLOC(args, length + 2 + n + 2)) return log_oom(); if (length > 0) @@ -2610,7 +2610,7 @@ static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command args[length++] = '"'; } - if (!GREEDY_REALLOC(args, allocated, length + 1)) + if (!GREEDY_REALLOC(args, length + 1)) return log_oom(); args[length++] = 0; diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c index 8cc1696a4f..58ad085dcd 100644 --- a/src/core/smack-setup.c +++ b/src/core/smack-setup.c @@ -247,7 +247,7 @@ static int write_onlycap_list(void) { _cleanup_close_ int onlycap_fd = -1; _cleanup_free_ char *list = NULL; _cleanup_fclose_ FILE *f = NULL; - size_t len = 0, allocated = 0; + size_t len = 0; int r; f = fopen("/etc/smack/onlycap", "re"); @@ -272,7 +272,7 @@ static int write_onlycap_list(void) { continue; l = strlen(buf); - if (!GREEDY_REALLOC(list, allocated, len + l + 1)) + if (!GREEDY_REALLOC(list, len + l + 1)) return log_oom(); stpcpy(list + len, buf)[0] = ' '; diff --git a/src/core/transaction.c b/src/core/transaction.c index 1d3cf8f1fc..4a55c075b4 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -330,11 +330,11 @@ _pure_ static bool unit_matters_to_anchor(Unit *u, Job *j) { static char* merge_unit_ids(const char* unit_log_field, char **pairs) { char **unit_id, **job_type, *ans = NULL; - size_t alloc = 0, size = 0, next; + size_t size = 0, next; STRV_FOREACH_PAIR(unit_id, job_type, pairs) { next = strlen(unit_log_field) + strlen(*unit_id); - if (!GREEDY_REALLOC(ans, alloc, size + next + 1)) + if (!GREEDY_REALLOC(ans, size + next + 1)) return mfree(ans); sprintf(ans + size, "%s%s", unit_log_field, *unit_id); diff --git a/src/core/unit.c b/src/core/unit.c index 67d811d7dd..fc63f3bee5 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -4004,7 +4004,7 @@ char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) { char* unit_concat_strv(char **l, UnitWriteFlags flags) { _cleanup_free_ char *result = NULL; - size_t n = 0, allocated = 0; + size_t n = 0; char **i; /* Takes a list of strings, escapes them, and concatenates them. This may be used to format command lines in a @@ -4021,7 +4021,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags) { return NULL; a = (n > 0) + 1 + strlen(p) + 1; /* separating space + " + entry + " */ - if (!GREEDY_REALLOC(result, allocated, n + a + 1)) + if (!GREEDY_REALLOC(result, n + a + 1)) return NULL; q = result + n; @@ -4035,7 +4035,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags) { n += a; } - if (!GREEDY_REALLOC(result, allocated, n + 1)) + if (!GREEDY_REALLOC(result, n + 1)) return NULL; result[n] = 0; |