@@ -262,6 +262,57 @@ impl MemorySet {
262262 false
263263 }
264264 }
265+
266+ // 新加的
267+ /// 公共方法:检查地址范围是否重叠
268+ pub fn check_overlap ( & self , start : VirtAddr , end : VirtAddr ) -> bool {
269+ for area in & self . areas {
270+ let area_start: VirtAddr = area. vpn_range . get_start ( ) . into ( ) ;
271+ let area_end: VirtAddr = area. vpn_range . get_end ( ) . into ( ) ;
272+ if !( end <= area_start || start >= area_end) {
273+ return true ; // 有重叠
274+ }
275+ }
276+ false // 无重叠
277+ }
278+
279+ /// 公共方法:创建内存映射
280+ pub fn mmap ( & mut self , start : VirtAddr , end : VirtAddr , map_type : MapType , perm : MapPermission ) -> bool {
281+ // 检查重叠
282+ if self . check_overlap ( start, end) {
283+ return false ;
284+ }
285+
286+ // 创建映射区域
287+ let map_area = MapArea :: new ( start, end, map_type, perm) ;
288+ // 使用内部的 push 方法(保持 push 私有)
289+ self . push ( map_area, None ) ;
290+ true
291+ }
292+
293+ /// 公共方法:取消内存映射
294+ pub fn munmap ( & mut self , start : VirtAddr , end : VirtAddr ) -> bool {
295+ let start_vpn = start. floor ( ) ;
296+ let end_vpn = end. ceil ( ) ;
297+
298+ // 查找要移除的区域
299+ let mut found_index = None ;
300+ for ( i, area) in self . areas . iter ( ) . enumerate ( ) {
301+ if area. vpn_range . get_start ( ) == start_vpn && area. vpn_range . get_end ( ) == end_vpn {
302+ found_index = Some ( i) ;
303+ break ;
304+ }
305+ }
306+
307+ // 移除并取消映射
308+ if let Some ( index) = found_index {
309+ let mut area = self . areas . remove ( index) ;
310+ area. unmap ( & mut self . page_table ) ;
311+ true
312+ } else {
313+ false
314+ }
315+ }
265316}
266317/// map area structure, controls a contiguous piece of virtual memory
267318pub struct MapArea {
@@ -272,6 +323,7 @@ pub struct MapArea {
272323}
273324
274325impl MapArea {
326+ /// Create a new `MapArea`.
275327 pub fn new (
276328 start_va : VirtAddr ,
277329 end_va : VirtAddr ,
@@ -287,6 +339,7 @@ impl MapArea {
287339 map_perm,
288340 }
289341 }
342+ /// map one page
290343 pub fn map_one ( & mut self , page_table : & mut PageTable , vpn : VirtPageNum ) {
291344 let ppn: PhysPageNum ;
292345 match self . map_type {
@@ -302,32 +355,38 @@ impl MapArea {
302355 let pte_flags = PTEFlags :: from_bits ( self . map_perm . bits ) . unwrap ( ) ;
303356 page_table. map ( vpn, ppn, pte_flags) ;
304357 }
358+
305359 #[ allow( unused) ]
360+ /// unmap one page
306361 pub fn unmap_one ( & mut self , page_table : & mut PageTable , vpn : VirtPageNum ) {
307362 if self . map_type == MapType :: Framed {
308363 self . data_frames . remove ( & vpn) ;
309364 }
310365 page_table. unmap ( vpn) ;
311366 }
367+ /// map the area into the page table
312368 pub fn map ( & mut self , page_table : & mut PageTable ) {
313369 for vpn in self . vpn_range {
314370 self . map_one ( page_table, vpn) ;
315371 }
316372 }
317373 #[ allow( unused) ]
374+ /// unmap the area from the page table
318375 pub fn unmap ( & mut self , page_table : & mut PageTable ) {
319376 for vpn in self . vpn_range {
320377 self . unmap_one ( page_table, vpn) ;
321378 }
322379 }
323380 #[ allow( unused) ]
381+ /// shrink the area to new_end
324382 pub fn shrink_to ( & mut self , page_table : & mut PageTable , new_end : VirtPageNum ) {
325383 for vpn in VPNRange :: new ( new_end, self . vpn_range . get_end ( ) ) {
326384 self . unmap_one ( page_table, vpn)
327385 }
328386 self . vpn_range = VPNRange :: new ( self . vpn_range . get_start ( ) , new_end) ;
329387 }
330388 #[ allow( unused) ]
389+ /// append the area to new_end
331390 pub fn append_to ( & mut self , page_table : & mut PageTable , new_end : VirtPageNum ) {
332391 for vpn in VPNRange :: new ( self . vpn_range . get_end ( ) , new_end) {
333392 self . map_one ( page_table, vpn)
@@ -356,12 +415,15 @@ impl MapArea {
356415 current_vpn. step ( ) ;
357416 }
358417 }
418+
359419}
360420
361421#[ derive( Copy , Clone , PartialEq , Debug ) ]
362422/// map type for memory set: identical or framed
363423pub enum MapType {
424+ /// map virtual address to the identical physical address
364425 Identical ,
426+ /// map virtual address to the allocated frame
365427 Framed ,
366428}
367429
0 commit comments