]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - e2fsck/region.c
e2fsck: add optimization for large, fragmented sparse files
[thirdparty/e2fsprogs.git] / e2fsck / region.c
index aaaaa19054cb943ddff98a8f83ca3edb13eef4b4..95d23be4f26c8853408b7c56dfd9c50fe5db9b8f 100644 (file)
@@ -30,6 +30,7 @@ struct region_struct {
        region_addr_t   min;
        region_addr_t   max;
        struct region_el *allocated;
+       struct region_el *last;
 };
 
 region_t region_create(region_addr_t min, region_addr_t max)
@@ -42,6 +43,7 @@ region_t region_create(region_addr_t min, region_addr_t max)
        memset(region, 0, sizeof(struct region_struct));
        region->min = min;
        region->max = max;
+       region->last = NULL;
        return region;
 }
 
@@ -68,6 +70,18 @@ int region_allocate(region_t region, region_addr_t start, int n)
        if (n == 0)
                return 1;
 
+       if (region->last && region->last->end == start &&
+           !region->last->next) {
+               region->last->end = end;
+               return 0;
+       }
+       if (region->last && start > region->last->end &&
+           !region->last->next) {
+               r = NULL;
+               prev = region->last;
+               goto append_to_list;
+       }
+
        /*
         * Search through the linked list.  If we find that it
         * conflicts witih something that's already allocated, return
@@ -92,6 +106,8 @@ int region_allocate(region_t region, region_addr_t start, int n)
                                        r->end = next->end;
                                        r->next = next->next;
                                        free(next);
+                                       if (!r->next)
+                                               region->last = r;
                                        return 0;
                                }
                        }
@@ -104,12 +120,15 @@ int region_allocate(region_t region, region_addr_t start, int n)
        /*
         * Insert a new region element structure into the linked list
         */
+append_to_list:
        new_region = malloc(sizeof(struct region_el));
        if (!new_region)
                return -1;
        new_region->start = start;
        new_region->end = start + n;
        new_region->next = r;
+       if (!new_region->next)
+               region->last = new_region;
        if (prev)
                prev->next = new_region;
        else
@@ -159,10 +178,10 @@ void region_print(region_t region, FILE *f)
        struct region_el        *r;
        int     i = 0;
 
-       fprintf(f, "Printing region (min=%d. max=%d)\n\t", region->min,
+       fprintf(f, "Printing region (min=%llu. max=%llu)\n\t", region->min,
                region->max);
        for (r = region->allocated; r; r = r->next) {
-               fprintf(f, "(%d, %d)  ", r->start, r->end);
+               fprintf(f, "(%llu, %llu)  ", r->start, r->end);
                if (++i >= 8)
                        fprintf(f, "\n\t");
        }
@@ -183,7 +202,7 @@ int main(int argc, char **argv)
                case BCODE_CREATE:
                        start = bcode_program[pc++];
                        end = bcode_program[pc++];
-                       printf("Creating region with args(%d, %d)\n",
+                       printf("Creating region with args(%llu, %llu)\n",
                               start, end);
                        r = region_create(start, end);
                        if (!r) {
@@ -195,7 +214,7 @@ int main(int argc, char **argv)
                        start = bcode_program[pc++];
                        end = bcode_program[pc++];
                        ret = region_allocate(r, start, end);
-                       printf("Region_allocate(%d, %d) returns %d\n",
+                       printf("Region_allocate(%llu, %llu) returns %d\n",
                               start, end, ret);
                        break;
                case BCODE_PRINT: