From 319a4f4bc46b230fc660321e99aaac1bc449deea Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 18 May 2021 23:01:32 +0200 Subject: 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.) --- src/journal-remote/journal-remote.c | 9 ++++----- src/journal-remote/journal-remote.h | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'src/journal-remote') diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 13461dbe41..6e5aebdc48 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -171,7 +171,7 @@ static int get_source_for_fd(RemoteServer *s, assert(fd >= 0); assert(source); - if (!GREEDY_REALLOC0(s->sources, s->sources_size, fd + 1)) + if (!GREEDY_REALLOC0(s->sources, fd + 1)) return log_oom(); r = journal_remote_get_writer(s, name, &writer); @@ -197,7 +197,7 @@ static int remove_source(RemoteServer *s, int fd) { RemoteSource *source; assert(s); - assert(fd >= 0 && fd < (ssize_t) s->sources_size); + assert(fd >= 0 && fd < (ssize_t) MALLOC_ELEMENTSOF(s->sources)); source = s->sources[fd]; if (source) { @@ -352,8 +352,7 @@ void journal_remote_server_destroy(RemoteServer *s) { hashmap_free_with_destructor(s->daemons, MHDDaemonWrapper_free); #endif - assert(s->sources_size == 0 || s->sources); - for (i = 0; i < s->sources_size; i++) + for (i = 0; i < MALLOC_ELEMENTSOF(s->sources); i++) remove_source(s, i); free(s->sources); @@ -388,7 +387,7 @@ int journal_remote_handle_raw_source( * 0 if data is currently exhausted, negative on error. */ - assert(fd >= 0 && fd < (ssize_t) s->sources_size); + assert(fd >= 0 && fd < (ssize_t) MALLOC_ELEMENTSOF(s->sources)); source = s->sources[fd]; assert(source->importer.fd == fd); diff --git a/src/journal-remote/journal-remote.h b/src/journal-remote/journal-remote.h index 247ffa9765..45176e964d 100644 --- a/src/journal-remote/journal-remote.h +++ b/src/journal-remote/journal-remote.h @@ -23,7 +23,6 @@ struct MHDDaemonWrapper { struct RemoteServer { RemoteSource **sources; - size_t sources_size; size_t active; sd_event *events; -- cgit v1.2.1