Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle partially-overlapping frames #954

Draft
wants to merge 1 commit into
base: theseus_main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions kernel/frame_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,12 +875,12 @@ fn contains_any(
///
/// Currently, this function adds no new frames at all if any frames within the given `frames` list
/// overlap any existing regions at all.
/// TODO: handle partially-overlapping regions by extending existing regions on either end.
fn add_reserved_region(
list: &mut StaticArrayRBTree<Chunk>,
frames: FrameRange,
) -> Result<FrameRange, &'static str> {

let mut overlapped = false;
let mut frames = frames;
// Check whether the reserved region overlaps any existing regions.
match &mut list.0 {
Inner::Array(ref mut arr) => {
Expand All @@ -902,20 +902,30 @@ fn add_reserved_region(
break;
}
if let Some(_overlap) = chunk.overlap(&frames) {
// trace!("Failed to add reserved region {:?} due to overlap {:?} with existing chunk {:?}",
// frames, _overlap, chunk
// );
return Err("Failed to add reserved region that overlapped with existing reserved regions (RBTree).");
trace!("Failed to add reserved region {:?} due to overlap {:?} with existing chunk {:?}",
frames, _overlap, chunk
);
frames = FrameRange::new(
min(*chunk.start(), *frames.start()),
max(*chunk.end(), *frames.end()),
);
let _removed_chunk = cursor_mut.replace_with(Wrapper::new_link(
Chunk { typ: MemoryRegionType::Reserved, frames: frames.clone() }
)).expect("BUG: frame_allocator failed to replace the current chunk while merging contiguous chunks.");
// we can't remove chunk here and insert below because in this loop we might replace/merge multiple times
overlapped = true;
}
cursor_mut.move_next();
}
}
}

list.insert(Chunk {
typ: MemoryRegionType::Reserved,
frames: frames.clone(),
}).map_err(|_c| "BUG: Failed to insert non-overlapping frames into list.")?;
if !overlapped {
list.insert(Chunk {
typ: MemoryRegionType::Reserved,
frames: frames.clone(),
}).map_err(|_c| "BUG: Failed to insert non-overlapping frames into list.")?;
}

Ok(frames)
}
Expand Down