@@ -181,16 +181,14 @@ unsafe impl AllocRef for System {
181181 ) ;
182182
183183 // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
184+ // If `new_size` is zero, then `old_size` has to be zero as well.
184185 // Other conditions must be upheld by the caller
185186 unsafe {
186187 match layout. size ( ) {
187- old_size if old_size == new_size => {
188- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
189- }
190188 0 => self . alloc ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
191189 old_size => {
192- // `realloc` probably checks for `new_size > size` or something similar.
193- intrinsics:: assume ( new_size > old_size) ;
190+ // `realloc` probably checks for `new_size >= size` or something similar.
191+ intrinsics:: assume ( new_size >= old_size) ;
194192 let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
195193 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
196194 Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
@@ -212,16 +210,14 @@ unsafe impl AllocRef for System {
212210 ) ;
213211
214212 // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
213+ // If `new_size` is zero, then `old_size` has to be zero as well.
215214 // Other conditions must be upheld by the caller
216215 unsafe {
217216 match layout. size ( ) {
218- old_size if old_size == new_size => {
219- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
220- }
221217 0 => self . alloc_zeroed ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
222218 old_size => {
223- // `realloc` probably checks for `new_size > size` or something similar.
224- intrinsics:: assume ( new_size > old_size) ;
219+ // `realloc` probably checks for `new_size >= size` or something similar.
220+ intrinsics:: assume ( new_size >= old_size) ;
225221 let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
226222 raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
227223 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
@@ -244,11 +240,8 @@ unsafe impl AllocRef for System {
244240 "`new_size` must be smaller than or equal to `layout.size()`"
245241 ) ;
246242
247- let ptr = if new_size == old_size {
248- ptr
249- } else if new_size == 0 {
250- // SAFETY: `layout` is non-zero in size as `old_size` != `new_size`
251- // Other conditions must be upheld by the caller
243+ let ptr = if new_size == 0 {
244+ // SAFETY: conditions must be upheld by the caller
252245 unsafe {
253246 self . dealloc ( ptr, layout) ;
254247 }
@@ -257,8 +250,8 @@ unsafe impl AllocRef for System {
257250 // SAFETY: new_size is not zero,
258251 // Other conditions must be upheld by the caller
259252 let raw_ptr = unsafe {
260- // `realloc` probably checks for `new_size < old_size` or something similar.
261- intrinsics:: assume ( new_size < old_size) ;
253+ // `realloc` probably checks for `new_size <= old_size` or something similar.
254+ intrinsics:: assume ( new_size <= old_size) ;
262255 GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size)
263256 } ;
264257 NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?
0 commit comments