summaryrefslogtreecommitdiff
path: root/src/shared/sleep-config.c
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/shared/sleep-config.c
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/shared/sleep-config.c')
-rw-r--r--src/shared/sleep-config.c6
1 files changed, 2 insertions, 4 deletions
diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c
index 53280cf40a..6bb5d80a95 100644
--- a/src/shared/sleep-config.c
+++ b/src/shared/sleep-config.c
@@ -512,7 +512,6 @@ int read_fiemap(int fd, struct fiemap **ret) {
uint32_t result_extents = 0;
uint64_t fiemap_start = 0, fiemap_length;
const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent));
- size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra;
if (fstat(fd, &statinfo) < 0)
return log_debug_errno(errno, "Cannot determine file size: %m");
@@ -551,8 +550,7 @@ int read_fiemap(int fd, struct fiemap **ret) {
/* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
* the extents for the whole file. Add space for the initial struct fiemap. */
- if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated,
- n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
+ if (!greedy_realloc0((void**) &fiemap, n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
return -ENOMEM;
fiemap->fm_extent_count = fiemap->fm_mapped_extents;
@@ -562,7 +560,7 @@ int read_fiemap(int fd, struct fiemap **ret) {
return log_debug_errno(errno, "Failed to read extents: %m");
/* Resize result_fiemap to allow us to copy in the extents */
- if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated,
+ if (!greedy_realloc((void**) &result_fiemap,
n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
return -ENOMEM;