summaryrefslogtreecommitdiffstats
path: root/src/analyze
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-05-18 23:01:32 +0200
committerLennart Poettering <lennart@poettering.net>2021-05-19 16:42:37 +0200
commit319a4f4bc46b230fc660321e99aaac1bc449deea (patch)
tree9eca2e1352df29aeeef91e4fd4bcc12ad4ea44e9 /src/analyze
parentalloc-util: add MALLOC_ELEMENTSOF() helper (diff)
downloadsystemd-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/analyze')
-rw-r--r--src/analyze/analyze-security.c4
-rw-r--r--src/analyze/analyze.c4
2 files changed, 4 insertions, 4 deletions
diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c
index fc5af11f55..f20606c17c 100644
--- a/src/analyze/analyze-security.c
+++ b/src/analyze/analyze-security.c
@@ -2116,7 +2116,7 @@ int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_strv_free_ char **list = NULL;
- size_t allocated = 0, n = 0;
+ size_t n = 0;
char **i;
r = sd_bus_call_method(
@@ -2148,7 +2148,7 @@ int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags) {
if (!endswith(info.id, ".service"))
continue;
- if (!GREEDY_REALLOC(list, allocated, n + 2))
+ if (!GREEDY_REALLOC(list, n + 2))
return log_oom();
copy = strdup(info.id);
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index 1ad3731852..62c0ccbdfe 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -342,7 +342,7 @@ static int acquire_time_data(sd_bus *bus, UnitTimes **out) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(unit_times_free_arrayp) UnitTimes *unit_times = NULL;
BootTimes *boot_times = NULL;
- size_t allocated = 0, c = 0;
+ size_t c = 0;
UnitInfo u;
int r;
@@ -361,7 +361,7 @@ static int acquire_time_data(sd_bus *bus, UnitTimes **out) {
while ((r = bus_parse_unit_info(reply, &u)) > 0) {
UnitTimes *t;
- if (!GREEDY_REALLOC(unit_times, allocated, c + 2))
+ if (!GREEDY_REALLOC(unit_times, c + 2))
return log_oom();
unit_times[c + 1].has_data = false;