From c6f3be2bdad826440889a079b5b2a8bcfb67738e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Fatay=20Yi=C4=9Fit=20=C5=9Eahin?= Date: Tue, 1 Oct 2024 13:52:32 +0300 Subject: [PATCH] find_kernel: improve the iterator handling Instead of checking if it is the first iteration of the loop in every iteration, handle the first iteration of the modules iterator outside the loop. --- src/arch/x86_64/multiboot.rs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/arch/x86_64/multiboot.rs b/src/arch/x86_64/multiboot.rs index 59a680a..112650b 100644 --- a/src/arch/x86_64/multiboot.rs +++ b/src/arch/x86_64/multiboot.rs @@ -108,23 +108,22 @@ pub fn find_kernel() -> &'static [u8] { // Iterate through all modules. // Collect the start address of the first module and the highest end address of all modules. - let modules = multiboot + let mut module_iter = multiboot .modules() .expect("Could not find a memory map in the Multiboot information"); - let mut found_module = false; - let mut start_address = 0; - let mut end_address = 0; - - for m in modules { - found_module = true; - - if start_address == 0 { - start_address = m.start as usize; - } - - if m.end as usize > end_address { - end_address = m.end as usize; - } + let start_address; + let mut end_address; + + if let Some(first_module) = module_iter.next() { + start_address = first_module.start as usize; + info!("Found an ELF module at {:#x}", start_address); + end_address = first_module.end as usize; + } else { + panic!("Could not find a single module in the Multiboot information") + } + // Find the maximum end address from the remaining modules + for m in module_iter { + end_address = usize::max(end_address, m.end as usize); } info!("Found module: [{:#x} - {:#x}]", start_address, end_address); @@ -139,12 +138,6 @@ pub fn find_kernel() -> &'static [u8] { PhysAlloc::init(free_memory_address); // Identity-map the ELF header of the first module. - assert!( - found_module, - "Could not find a single module in the Multiboot information" - ); - assert!(start_address > 0); - info!("Found an ELF module at {:#x}", start_address); paging::map_range::( start_address, start_address,