@@ -6,14 +6,19 @@ use core::arch::wasm64 as wasm;
66use core:: ptr;
77use core:: sync:: atomic:: { AtomicBool , Ordering } ;
88
9+ #[ cfg( target_os = "unknown" ) ]
910extern "C" {
1011 static __heap_base: u8 ;
1112 static __heap_end: u8 ;
1213}
1314
1415static PREEXISTING_USED : AtomicBool = AtomicBool :: new ( false ) ;
1516
16- fn preexisting_chunk ( size : usize , heap_base : usize , heap_end : usize ) -> Option < ( usize , usize ) > {
17+ fn preexisting_chunk_from_bounds (
18+ size : usize ,
19+ heap_base : usize ,
20+ heap_end : usize ,
21+ ) -> Option < ( usize , usize ) > {
1722 let start = heap_base;
1823 if start == 0 {
1924 return None ;
@@ -31,6 +36,18 @@ fn preexisting_chunk(size: usize, heap_base: usize, heap_end: usize) -> Option<(
3136 Some ( ( start, len) )
3237}
3338
39+ #[ cfg( target_os = "unknown" ) ]
40+ fn preexisting_chunk_from_linker ( size : usize ) -> Option < ( usize , usize ) > {
41+ let heap_base = unsafe { & __heap_base as * const u8 as usize } ;
42+ let heap_end = unsafe { & __heap_end as * const u8 as usize } ;
43+ preexisting_chunk_from_bounds ( size, heap_base, heap_end)
44+ }
45+
46+ #[ cfg( not( target_os = "unknown" ) ) ]
47+ fn preexisting_chunk_from_linker ( _size : usize ) -> Option < ( usize , usize ) > {
48+ None
49+ }
50+
3451fn try_donate_preexisting (
3552 state : & AtomicBool ,
3653 chunk : Option < ( usize , usize ) > ,
@@ -84,10 +101,7 @@ unsafe impl Allocator for System {
84101 let page_size = self . page_size ( ) ;
85102
86103 if size != 0 {
87- let heap_base = unsafe { & __heap_base as * const u8 as usize } ;
88- let heap_end = unsafe { & __heap_end as * const u8 as usize } ;
89-
90- let chunk = preexisting_chunk ( size, heap_base, heap_end) ;
104+ let chunk = preexisting_chunk_from_linker ( size) ;
91105 if let Some ( ( base, len) ) = try_donate_preexisting ( & PREEXISTING_USED , chunk) {
92106 return ( base as * mut u8 , len, 0 ) ;
93107 }
@@ -123,7 +137,7 @@ unsafe impl Allocator for System {
123137
124138#[ cfg( test) ]
125139mod tests {
126- use super :: { preexisting_chunk , try_donate_preexisting} ;
140+ use super :: { preexisting_chunk_from_bounds , try_donate_preexisting} ;
127141 use core:: sync:: atomic:: { AtomicBool , Ordering } ;
128142
129143 fn legacy_grow_only (
@@ -144,7 +158,7 @@ mod tests {
144158 let heap_base = page_size;
145159 let heap_end = page_size * 4 ;
146160
147- let chunk = preexisting_chunk ( 16 , heap_base, heap_end) . unwrap ( ) ;
161+ let chunk = preexisting_chunk_from_bounds ( 16 , heap_base, heap_end) . unwrap ( ) ;
148162 let state = AtomicBool :: new ( false ) ;
149163 let new_behavior = try_donate_preexisting ( & state, Some ( chunk) ) ;
150164 let legacy_behavior = legacy_grow_only ( 16 , page_size, usize:: MAX ) ;
@@ -160,7 +174,7 @@ mod tests {
160174 let heap_base = page_size;
161175 let heap_end = page_size * 4 ;
162176
163- let chunk = preexisting_chunk ( 16 , heap_base, heap_end) . unwrap ( ) ;
177+ let chunk = preexisting_chunk_from_bounds ( 16 , heap_base, heap_end) . unwrap ( ) ;
164178 assert_eq ! ( chunk, ( page_size, page_size * 3 ) ) ;
165179 }
166180
@@ -171,12 +185,12 @@ mod tests {
171185 let heap_end = page_size * 2 ;
172186 let state = AtomicBool :: new ( false ) ;
173187
174- let first = preexisting_chunk ( page_size * 3 , heap_base, heap_end) ;
188+ let first = preexisting_chunk_from_bounds ( page_size * 3 , heap_base, heap_end) ;
175189 assert_eq ! ( first, None ) ;
176190 assert_eq ! ( try_donate_preexisting( & state, first) , None ) ;
177191 assert ! ( state. load( Ordering :: Relaxed ) ) ;
178192
179- let second = preexisting_chunk ( 16 , heap_base, heap_end) ;
193+ let second = preexisting_chunk_from_bounds ( 16 , heap_base, heap_end) ;
180194 assert_eq ! ( second, Some ( ( page_size, page_size) ) ) ;
181195 assert_eq ! ( try_donate_preexisting( & state, second) , None ) ;
182196 }
@@ -202,7 +216,7 @@ mod tests {
202216 let heap_base = 0 ;
203217 let heap_end = page_size * 4 ;
204218
205- assert_eq ! ( preexisting_chunk ( 16 , heap_base, heap_end) , None ) ;
219+ assert_eq ! ( preexisting_chunk_from_bounds ( 16 , heap_base, heap_end) , None ) ;
206220 }
207221}
208222
0 commit comments