]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: split out free_area_{current,min}_end() from free_area_available_for_new_part...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 5 Sep 2022 12:56:49 +0000 (21:56 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 8 Sep 2022 20:49:00 +0000 (05:49 +0900)
No actual code changes, just preparation for later commits.

src/partition/repart.c

index 5b999d5ba9231ff2ed7414678ee41425e115b180..004022f6dabe0c4b99d7af30684428bf8f2814f8 100644 (file)
@@ -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);
 
-        /* Similar to free_area_available(), but takes into account that the required size and padding of the
-         * preceding partition is honoured. */
+        if (!a->after)
+                return free_area_available(a);
 
-        avail = free_area_available(a);
-        if (a->after) {
-                uint64_t need, space_end, new_end;
+        assert(a->after->offset != UINT64_MAX);
+        assert(a->after->current_size != UINT64_MAX);
 
-                need = partition_min_size_with_padding(context, a->after);
+        /* 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);
+}
 
-                assert(a->after->offset != UINT64_MAX);
-                assert(a->after->current_size != UINT64_MAX);
+static uint64_t free_area_min_end(Context *context, const FreeArea *a) {
+        assert(context);
+        assert(a);
 
-                /* 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;
+        if (!a->after)
+                return 0;
 
-                /* 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);
+        assert(a->after->offset != UINT64_MAX);
+        assert(a->after->current_size != UINT64_MAX);
 
-                /* Calculate saturated difference of the two: that's how much we have free for other partitions */
-                return LESS_BY(space_end, new_end);
-        }
+        /* 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. */
 
-        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) {