From 9b2fd9b551cdf6250a3220ef6b7e0cd29ed78e30 Mon Sep 17 00:00:00 2001 From: ZhongkaiXu <3605832858@qq.com> Date: Wed, 15 Jan 2025 09:49:56 +0800 Subject: [PATCH] Update frame allocation algorithm to accelerate stream table construction --- src/memory/frame.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/memory/frame.rs b/src/memory/frame.rs index e14be83..6c588aa 100644 --- a/src/memory/frame.rs +++ b/src/memory/frame.rs @@ -128,22 +128,33 @@ impl Frame { } /// allocate contigugous frames, and you can specify the alignment, set the lower `align_log2` bits to 0. - pub fn new_contiguous_with_base( - frame_count: usize, - align_log2: usize, - ) -> HvResult { + pub fn new_contiguous_with_base(frame_count: usize, align_log2: usize) -> HvResult { + let align_mask = (1 << align_log2) - 1; // Create a vector to keep track of attempted frames let mut attempted_frames = Vec::new(); - loop{ - if let Ok(frame) = Frame::new_contiguous(frame_count, 0){ - if frame.start_paddr() & ((1 << align_log2) - 1) == 0{ - info!("new contiguous success!!! start_paddr:0x{:x}",frame.start_paddr()); + loop { + if let Ok(frame) = Frame::new_contiguous(frame_count, 0) { + if frame.start_paddr() & align_mask == 0 { + info!( + "new contiguous success!!! start_paddr:0x{:x}", + frame.start_paddr() + ); return Ok(frame); - }else{ + } else { + let start_paddr = frame.start_paddr(); + let next_aligned_addr = (start_paddr + align_mask) & !align_mask; + let temp_frame_count = (next_aligned_addr - start_paddr) / PAGE_SIZE; drop(frame); - attempted_frames.push(Frame::new_zero()); + attempted_frames.push(Frame::new_contiguous(temp_frame_count, 0)); + if let Ok(frame) = Frame::new_contiguous(frame_count, 0) { + info!( + "new contiguous success!!! start_paddr:0x{:x}", + frame.start_paddr() + ); + return Ok(frame); + } } - }else{ + } else { return Err(hv_err!(ENOMEM)); } }