diff --git a/src/system/kernel/vm/vm.cpp b/src/system/kernel/vm/vm.cpp index 6c21a286eb..009c698a46 100644 --- a/src/system/kernel/vm/vm.cpp +++ b/src/system/kernel/vm/vm.cpp @@ -4341,19 +4341,18 @@ vm_allocate_early_physical_page(kernel_args* args) return 0; } + // Try expanding the existing physical ranges upwards. for (uint32 i = 0; i < args->num_physical_allocated_ranges; i++) { - phys_addr_t nextPage; - - nextPage = args->physical_allocated_range[i].start + phys_addr_t nextPage = args->physical_allocated_range[i].start + args->physical_allocated_range[i].size; - // see if the page after the next allocated paddr run can be allocated - if (i + 1 < args->num_physical_allocated_ranges - && args->physical_allocated_range[i + 1].size != 0) { - // see if the next page will collide with the next allocated range - if (nextPage >= args->physical_allocated_range[i+1].start) + + // make sure the next page does not collide with the next allocated range + if ((i + 1) < args->num_physical_allocated_ranges + && args->physical_allocated_range[i + 1].size != 0) { + if (nextPage >= args->physical_allocated_range[i + 1].start) continue; } - // see if the next physical page fits in the memory block + // see if the next page fits in the memory block if (is_page_in_physical_memory_range(args, nextPage)) { // we got one! args->physical_allocated_range[i].size += B_PAGE_SIZE; @@ -4363,14 +4362,12 @@ vm_allocate_early_physical_page(kernel_args* args) // Expanding upwards didn't work, try going downwards. for (uint32 i = 0; i < args->num_physical_allocated_ranges; i++) { - phys_addr_t nextPage; + phys_addr_t nextPage = args->physical_allocated_range[i].start - B_PAGE_SIZE; - nextPage = args->physical_allocated_range[i].start - B_PAGE_SIZE; - // see if the page after the prev allocated paddr run can be allocated - if (i > 0 && args->physical_allocated_range[i - 1].size != 0) { - // see if the next page will collide with the next allocated range - if (nextPage < args->physical_allocated_range[i-1].start - + args->physical_allocated_range[i-1].size) + // make sure the next page does not collide with the previous allocated range + if ((i > 0) && args->physical_allocated_range[i - 1].size != 0) { + if (nextPage < args->physical_allocated_range[i - 1].start + + args->physical_allocated_range[i - 1].size) continue; } // see if the next physical page fits in the memory block