summaryrefslogtreecommitdiff
path: root/src/shared/bitmap.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/bitmap.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/bitmap.c')
-rw-r--r--src/shared/bitmap.c5
1 files changed, 2 insertions, 3 deletions
diff --git a/src/shared/bitmap.c b/src/shared/bitmap.c
index dcf67fbbd8..6cf08b898f 100644
--- a/src/shared/bitmap.c
+++ b/src/shared/bitmap.c
@@ -39,7 +39,7 @@ Bitmap* bitmap_copy(Bitmap *b) {
if (!ret->bitmaps)
return mfree(ret);
- ret->n_bitmaps = ret->bitmaps_allocated = b->n_bitmaps;
+ ret->n_bitmaps = b->n_bitmaps;
return ret;
}
@@ -81,7 +81,7 @@ int bitmap_set(Bitmap *b, unsigned n) {
offset = BITMAP_NUM_TO_OFFSET(n);
if (offset >= b->n_bitmaps) {
- if (!GREEDY_REALLOC0(b->bitmaps, b->bitmaps_allocated, offset + 1))
+ if (!GREEDY_REALLOC0(b->bitmaps, offset + 1))
return -ENOMEM;
b->n_bitmaps = offset + 1;
@@ -147,7 +147,6 @@ void bitmap_clear(Bitmap *b) {
b->bitmaps = mfree(b->bitmaps);
b->n_bitmaps = 0;
- b->bitmaps_allocated = 0;
}
bool bitmap_iterate(const Bitmap *b, Iterator *i, unsigned *n) {