Skip to content

Commit 67cb78d

Browse files
Merge pull request #98 from ZhongkaiXu/dev
update frame-allocation algorithm to accelerate stream table construction
2 parents d77982d + ccbf30f commit 67cb78d

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/memory/frame.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,33 @@ impl Frame {
128128
}
129129

130130
/// allocate contigugous frames, and you can specify the alignment, set the lower `align_log2` bits to 0.
131-
pub fn new_contiguous_with_base(
132-
frame_count: usize,
133-
align_log2: usize,
134-
) -> HvResult<Self> {
131+
pub fn new_contiguous_with_base(frame_count: usize, align_log2: usize) -> HvResult<Self> {
132+
let align_mask = (1 << align_log2) - 1;
135133
// Create a vector to keep track of attempted frames
136134
let mut attempted_frames = Vec::new();
137-
loop{
138-
if let Ok(frame) = Frame::new_contiguous(frame_count, 0){
139-
if frame.start_paddr() & ((1 << align_log2) - 1) == 0{
140-
info!("new contiguous success!!! start_paddr:0x{:x}",frame.start_paddr());
135+
loop {
136+
if let Ok(frame) = Frame::new_contiguous(frame_count, 0) {
137+
if frame.start_paddr() & align_mask == 0 {
138+
info!(
139+
"new contiguous success!!! start_paddr:0x{:x}",
140+
frame.start_paddr()
141+
);
141142
return Ok(frame);
142-
}else{
143+
} else {
144+
let start_paddr = frame.start_paddr();
145+
let next_aligned_addr = (start_paddr + align_mask) & !align_mask;
146+
let temp_frame_count = (next_aligned_addr - start_paddr) / PAGE_SIZE;
143147
drop(frame);
144-
attempted_frames.push(Frame::new_zero());
148+
attempted_frames.push(Frame::new_contiguous(temp_frame_count, 0));
149+
if let Ok(frame) = Frame::new_contiguous(frame_count, 0) {
150+
info!(
151+
"new contiguous success!!! start_paddr:0x{:x}",
152+
frame.start_paddr()
153+
);
154+
return Ok(frame);
155+
}
145156
}
146-
}else{
157+
} else {
147158
return Err(hv_err!(ENOMEM));
148159
}
149160
}

0 commit comments

Comments
 (0)