From 58b06ac1ab8d6a9dbb9d51b09cad203aef4e5fc5 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 5 Sep 2022 21:56:49 +0900 Subject: [PATCH] repart: split out free_area_{current,min}_end() from free_area_available_for_new_partitions() No actual code changes, just preparation for later commits. --- src/partition/repart.c | 51 ++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 5b999d5ba9..004022f6da 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -571,35 +571,42 @@ static uint64_t free_area_available(const FreeArea *a) { return a->size - a->allocated; } -static uint64_t free_area_available_for_new_partitions(Context *context, const FreeArea *a) { - uint64_t avail; +static uint64_t free_area_current_end(Context *context, const FreeArea *a) { + assert(context); + assert(a); + if (!a->after) + return free_area_available(a); + + assert(a->after->offset != UINT64_MAX); + assert(a->after->current_size != UINT64_MAX); + + /* Calculate where the free area ends, based on the offset of the partition preceding it. */ + return round_up_size(a->after->offset + a->after->current_size, context->grain_size) + free_area_available(a); +} + +static uint64_t free_area_min_end(Context *context, const FreeArea *a) { + assert(context); + assert(a); + + if (!a->after) + return 0; + + assert(a->after->offset != UINT64_MAX); + assert(a->after->current_size != UINT64_MAX); + + /* Calculate where the partition would end when we give it as much as it needs. */ + return round_up_size(a->after->offset + partition_min_size_with_padding(context, a->after), context->grain_size); +} + +static uint64_t free_area_available_for_new_partitions(Context *context, const FreeArea *a) { assert(context); assert(a); /* Similar to free_area_available(), but takes into account that the required size and padding of the * preceding partition is honoured. */ - avail = free_area_available(a); - if (a->after) { - uint64_t need, space_end, new_end; - - need = partition_min_size_with_padding(context, a->after); - - assert(a->after->offset != UINT64_MAX); - assert(a->after->current_size != UINT64_MAX); - - /* Calculate where the free area ends, based on the offset of the partition preceding it */ - space_end = round_up_size(a->after->offset + a->after->current_size, context->grain_size) + avail; - - /* Calculate where the partition would end when we give it as much as it needs */ - new_end = round_up_size(a->after->offset + need, context->grain_size); - - /* Calculate saturated difference of the two: that's how much we have free for other partitions */ - return LESS_BY(space_end, new_end); - } - - return avail; + return LESS_BY(free_area_current_end(context, a), free_area_min_end(context, a)); } static int free_area_compare(FreeArea *const *a, FreeArea *const*b, Context *context) {