Do not allocate on unneeded Vec

This commit is contained in:
Wildan M
2026-03-07 22:23:12 +07:00
committed by Jeremy Soller
parent 547722767a
commit fd9c651410
7 changed files with 30 additions and 22 deletions
+16 -8
View File
@@ -329,7 +329,7 @@ impl AddrSpaceWrapper {
requested_dst_base: Option<Page>,
new_page_count: usize,
new_flags: MapFlags,
notify_files: &mut Vec<UnmapResult>,
mut notify_files_out: Option<&mut Vec<UnmapResult>>,
) -> Result<Page> {
let dst_lock = self;
let mut dst = dst_lock.acquire_write();
@@ -360,13 +360,16 @@ impl AddrSpaceWrapper {
}
Some(base) if new_flags.contains(MapFlags::MAP_FIXED) => {
let unpin = false;
notify_files.append(&mut AddrSpace::munmap_inner(
let notify_files = AddrSpace::munmap_inner(
&mut dst.grants,
&mut dst.table.utable,
&mut dst_flusher,
PageSpan::new(base, new_page_count),
unpin,
)?);
)?;
if let Some(notify_files_out) = notify_files_out.as_mut() {
notify_files_out.extend(notify_files);
}
base
}
@@ -401,7 +404,7 @@ impl AddrSpaceWrapper {
if new_page_count < src_span.count {
let unpin = false;
notify_files.append(&mut AddrSpace::munmap_inner(
let notify_files: Vec<UnmapResult> = AddrSpace::munmap_inner(
src_grants,
src_mapper,
src_flusher,
@@ -410,7 +413,10 @@ impl AddrSpaceWrapper {
src_span.count - new_page_count,
),
unpin,
)?);
)?;
if let Some(notify_files_out) = notify_files_out.as_mut() {
notify_files_out.extend(notify_files);
}
}
let mut remaining_src_span =
@@ -662,7 +668,7 @@ impl AddrSpace {
flags: MapFlags,
map: impl FnOnce(Page, PageFlags<RmmA>, &mut PageMapper, &mut Flusher) -> Result<Grant>,
) -> Result<Page> {
self.mmap(dst_lock, None, page_count, flags, &mut Vec::new(), map)
self.mmap(dst_lock, None, page_count, flags, None, map)
}
pub fn mmap(
&mut self,
@@ -670,7 +676,7 @@ impl AddrSpace {
requested_base_opt: Option<Page>,
page_count: NonZeroUsize,
flags: MapFlags,
notify_files_out: &mut Vec<UnmapResult>,
notify_files_out: Option<&mut Vec<UnmapResult>>,
map: impl FnOnce(Page, PageFlags<RmmA>, &mut PageMapper, &mut Flusher) -> Result<Grant>,
) -> Result<Page> {
debug_assert_eq!(dst_lock.inner.as_mut_ptr(), self as *mut Self);
@@ -695,7 +701,9 @@ impl AddrSpace {
requested_span,
unpin,
)?;
notify_files_out.append(&mut notify_files);
if let Some(notify_files_out) = notify_files_out {
notify_files_out.append(&mut notify_files);
}
requested_span
} else {
+1 -1
View File
@@ -98,7 +98,7 @@ impl MemoryScheme {
(map.address != 0 || fixed).then_some(span.base),
page_count,
map.flags,
&mut notify_files,
Some(&mut notify_files),
|dst_page, flags, mapper, flusher| {
let span = PageSpan::new(dst_page, page_count.get());
if is_phys_contiguous {
+3 -3
View File
@@ -525,7 +525,7 @@ impl KernelScheme for ProcScheme {
requested_dst_base,
src_page_count.get(),
map.flags,
&mut notify_files,
Some(&mut notify_files),
)?
} else {
let mut dst_addrsp_guard = dst_addr_space.acquire_write();
@@ -534,7 +534,7 @@ impl KernelScheme for ProcScheme {
requested_dst_base,
src_page_count,
map.flags,
&mut notify_files,
Some(&mut notify_files),
|dst_page, _, dst_mapper, flusher| {
Grant::borrow(
Arc::clone(addrspace),
@@ -574,7 +574,7 @@ impl KernelScheme for ProcScheme {
None,
NonZeroUsize::new(1).unwrap(),
MapFlags::PROT_READ | MapFlags::PROT_WRITE,
&mut Vec::new(),
None,
|page, flags, mapper, flusher| {
Grant::allocated_shared_one_page(
frame.get(),
+1 -1
View File
@@ -96,7 +96,7 @@ pub fn resource(token: &mut CleanLockToken) -> Result<Vec<u8>> {
None,
page_count,
MapFlags::PROT_READ | MapFlags::PROT_WRITE,
&mut Vec::new(),
None,
|page, flags, mapper, flusher| {
let shared = false;
Ok(Grant::zeroed(
+4 -4
View File
@@ -568,7 +568,7 @@ impl UserInner {
Some(free_span.base),
ONE,
map_flags | MAP_FIXED_NOREPLACE,
&mut Vec::new(),
None,
move |dst_page, page_flags, mapper, flusher| {
let is_pinned = true;
Grant::allocated_shared_one_page(
@@ -606,7 +606,7 @@ impl UserInner {
Some(first_middle_dst_page),
middle_page_count,
map_flags | MAP_FIXED_NOREPLACE,
&mut Vec::new(),
None,
move |dst_page, _, mapper, flusher| {
let eager = true;
@@ -663,7 +663,7 @@ impl UserInner {
Some(tail_dst_page),
ONE,
map_flags | MAP_FIXED_NOREPLACE,
&mut Vec::new(),
None,
move |dst_page, page_flags, mapper, flusher| {
let is_pinned = true;
Grant::allocated_shared_one_page(
@@ -1109,7 +1109,7 @@ impl UserInner {
dst_base,
page_count_nz,
map.flags,
&mut notify_files,
Some(&mut notify_files),
|dst_base, flags, mapper, flusher| {
Grant::borrow_fmap(
PageSpan::new(dst_base, page_count),
+2 -2
View File
@@ -637,7 +637,7 @@ pub fn mremap(
requested_dst_base,
NonZeroUsize::new(1).expect("value specified is not zero"),
map_flags,
&mut Vec::new(),
None,
|page, page_flags, mapper, flusher| {
let frame = raii_frame.take();
// XXX: add_ref(RefKind::Shared) is internally done by borrow_frame_enforce_rw_allocated(src_span.base).
@@ -663,7 +663,7 @@ pub fn mremap(
requested_dst_base,
new_page_count,
map_flags,
&mut Vec::new(),
None,
)?;
Ok(base.start_address().data())
+3 -3
View File
@@ -1,4 +1,4 @@
use alloc::{sync::Arc, vec::Vec};
use alloc::sync::Arc;
use core::{mem, num::NonZeroUsize};
use rmm::Arch;
@@ -117,7 +117,7 @@ pub unsafe fn usermode_bootstrap(bootstrap: &Bootstrap, token: &mut CleanLockTok
Some(base),
page_count,
flags,
&mut Vec::new(),
None,
|page, flags, mapper, flusher| {
let shared = false;
Ok(Grant::zeroed(
@@ -173,7 +173,7 @@ pub unsafe fn usermode_bootstrap(bootstrap: &Bootstrap, token: &mut CleanLockTok
))),
NonZeroUsize::new(KERNEL_METADATA_PAGE_COUNT).unwrap(),
MapFlags::MAP_FIXED_NOREPLACE | MapFlags::PROT_READ | MapFlags::PROT_WRITE,
&mut Vec::new(),
None,
|page, flags, mapper, flusher| {
let shared = false;
Ok(Grant::zeroed(