@@ -51,6 +51,39 @@ impl MemorySet {
5151 pub fn token ( & self ) -> usize {
5252 self . page_table . token ( )
5353 }
54+ /// Check whether there is overlap with already mapped pages
55+ pub fn check_mmap_area ( & self , start_vpn : VirtPageNum , end_vpn : VirtPageNum ) -> bool {
56+ for area in & self . areas {
57+ if !( end_vpn <= area. vpn_range . get_start ( )
58+ || start_vpn >= area. vpn_range . get_end ( ) )
59+ {
60+ return true ;
61+ }
62+ }
63+ false
64+ }
65+ /// Remove
66+ pub fn remove_framed_area (
67+ & mut self ,
68+ start_va : VirtAddr ,
69+ end_va : VirtAddr ,
70+ ) -> isize {
71+ if let Some ( pos) = self
72+ . areas
73+ . iter ( )
74+ . position ( |area| {
75+ area. vpn_range . get_start ( ) == start_va. floor ( )
76+ && area. vpn_range . get_end ( ) == end_va. ceil ( )
77+ } )
78+ {
79+ let mut area = self . areas . remove ( pos) ;
80+ area. unmap ( & mut self . page_table ) ;
81+ 0
82+ } else {
83+ -1
84+ }
85+ }
86+
5487 /// Assume that no conflicts.
5588 pub fn insert_framed_area (
5689 & mut self ,
@@ -63,13 +96,24 @@ impl MemorySet {
6396 None ,
6497 ) ;
6598 }
66- fn push ( & mut self , mut map_area : MapArea , data : Option < & [ u8 ] > ) {
99+ pub fn push ( & mut self , mut map_area : MapArea , data : Option < & [ u8 ] > ) {
67100 map_area. map ( & mut self . page_table ) ;
68101 if let Some ( data) = data {
69102 map_area. copy_data ( & mut self . page_table , data) ;
70103 }
71104 self . areas . push ( map_area) ;
72105 }
106+ pub fn remove_area ( & mut self , start_vpn : VirtPageNum , end_vpn : VirtPageNum ) -> isize {
107+ if let Some ( pos) = self . areas . iter ( ) . position ( |area| {
108+ area. vpn_range . get_start ( ) == start_vpn && area. vpn_range . get_end ( ) == end_vpn
109+ } ) {
110+ let mut area = self . areas . remove ( pos) ;
111+ area. unmap ( & mut self . page_table ) ;
112+ 0
113+ } else {
114+ -1
115+ }
116+ }
73117 /// Mention that trampoline is not collected by areas.
74118 fn map_trampoline ( & mut self ) {
75119 self . page_table . map (
@@ -262,6 +306,20 @@ impl MemorySet {
262306 false
263307 }
264308 }
309+
310+ pub fn unmap_range ( & mut self , start_vpn : VirtPageNum , end_vpn : VirtPageNum ) -> isize {
311+
312+ for vpn in VPNRange :: new ( start_vpn, end_vpn) {
313+ if let Some ( area) = self . areas . iter_mut ( ) . find ( |area| {
314+ vpn >= area. vpn_range . get_start ( ) && end_vpn < area. vpn_range . get_end ( )
315+ } ) {
316+ area. unmap_one ( & mut self . page_table , vpn) ;
317+ area. data_frames . remove ( & vpn) ;
318+
319+ }
320+ }
321+ 0
322+ }
265323}
266324/// map area structure, controls a contiguous piece of virtual memory
267325pub struct MapArea {
@@ -409,4 +467,4 @@ pub fn remap_test() {
409467 . unwrap( )
410468 . executable( ) , ) ;
411469 println ! ( "remap_test passed!" ) ;
412- }
470+ }
0 commit comments