summaryrefslogtreecommitdiff
path: root/src/pstore
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/pstore
parent99480504d47c0a6bbb1ac230cc33df12bbd36c54 (diff)
downloadsystemd-319a4f4bc46b230fc660321e99aaac1bc449deea.tar.gz
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/pstore')
-rw-r--r--src/pstore/pstore.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/src/pstore/pstore.c b/src/pstore/pstore.c
index 6ce38f10a3..81538d9cb3 100644
--- a/src/pstore/pstore.c
+++ b/src/pstore/pstore.c
@@ -101,7 +101,6 @@ typedef struct PStoreEntry {
typedef struct PStoreList {
PStoreEntry *entries;
- size_t n_allocated;
size_t n_entries;
} PStoreList;
@@ -208,7 +207,7 @@ static int write_dmesg(const char *dmesg, size_t size, const char *id) {
static void process_dmesg_files(PStoreList *list) {
/* Move files, reconstruct dmesg.txt */
_cleanup_free_ char *dmesg = NULL, *dmesg_id = NULL;
- size_t dmesg_size = 0, dmesg_allocated = 0;
+ size_t dmesg_size = 0;
bool dmesg_bad = false;
PStoreEntry *pe;
@@ -303,7 +302,7 @@ static void process_dmesg_files(PStoreList *list) {
/* Reconstruction of dmesg is done as a useful courtesy: do not fail, but don't write garbled
* output either. */
size_t needed = strlen(pe->dirent.d_name) + strlen(":\n") + pe->content_size + 1;
- if (!GREEDY_REALLOC(dmesg, dmesg_allocated, dmesg_size + needed)) {
+ if (!GREEDY_REALLOC(dmesg, dmesg_size + needed)) {
log_oom();
dmesg_bad = true;
continue;
@@ -348,7 +347,7 @@ static int list_files(PStoreList *list, const char *sourcepath) {
continue;
}
- if (!GREEDY_REALLOC(list->entries, list->n_allocated, list->n_entries + 1))
+ if (!GREEDY_REALLOC(list->entries, list->n_entries + 1))
return log_oom();
list->entries[list->n_entries++] = (PStoreEntry) {