@@ -3,19 +3,21 @@ use axhal::paging::{MappingFlags, PageSize};
33use axmm:: backend:: Backend ;
44use axtask:: current;
55use memory_addr:: { VirtAddr , align_up_4k} ;
6- use starry_core:: task:: AsThread ;
7- use starry_core:: config:: { USER_HEAP_BASE , USER_HEAP_SIZE , USER_HEAP_SIZE_MAX } ;
6+ use starry_core:: {
7+ config:: { USER_HEAP_BASE , USER_HEAP_SIZE , USER_HEAP_SIZE_MAX } ,
8+ task:: AsThread ,
9+ } ;
810
911pub fn sys_brk ( addr : usize ) -> AxResult < isize > {
1012 let curr = current ( ) ;
1113 let proc_data = & curr. as_thread ( ) . proc_data ;
1214 let current_top = proc_data. get_heap_top ( ) as usize ;
1315 let heap_limit = USER_HEAP_BASE + USER_HEAP_SIZE_MAX ;
14-
16+
1517 if addr == 0 {
1618 return Ok ( current_top as isize ) ;
1719 }
18-
20+
1921 if addr < USER_HEAP_BASE || addr > heap_limit {
2022 return Ok ( current_top as isize ) ;
2123 }
@@ -24,23 +26,27 @@ pub fn sys_brk(addr: usize) -> AxResult<isize> {
2426 let current_top_aligned = align_up_4k ( current_top) ;
2527 // Initial heap region end address (already mapped during ELF loading)
2628 let initial_heap_end = USER_HEAP_BASE + USER_HEAP_SIZE ;
27-
29+
2830 // Only map new pages when expanding beyond already mapped region
2931 // Expansion start should be the greater of initial_heap_end and current_top_aligned
3032 if new_top_aligned > current_top_aligned {
3133 let expand_start = VirtAddr :: from ( initial_heap_end. max ( current_top_aligned) ) ;
3234 let expand_size = new_top_aligned. saturating_sub ( expand_start. as_usize ( ) ) ;
3335
34- if expand_size > 0 {
35- if proc_data. aspace . lock ( ) . map (
36- expand_start,
37- expand_size,
38- MappingFlags :: READ | MappingFlags :: WRITE | MappingFlags :: USER ,
39- false ,
40- Backend :: new_alloc ( expand_start, PageSize :: Size4K ) ,
41- ) . is_err ( ) {
42- return Ok ( current_top as isize ) ;
43- }
36+ if expand_size > 0
37+ && proc_data
38+ . aspace
39+ . lock ( )
40+ . map (
41+ expand_start,
42+ expand_size,
43+ MappingFlags :: READ | MappingFlags :: WRITE | MappingFlags :: USER ,
44+ false ,
45+ Backend :: new_alloc ( expand_start, PageSize :: Size4K ) ,
46+ )
47+ . is_err ( )
48+ {
49+ return Ok ( current_top as isize ) ;
4450 }
4551 }
4652
0 commit comments