@@ -185,16 +185,14 @@ unsafe impl AllocRef for System {
185185 ) ;
186186
187187 // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
188+ // If `new_size` is zero, than `old_size` has to be zero as well.
188189 // Other conditions must be upheld by the caller
189190 unsafe {
190191 match layout. size ( ) {
191- old_size if old_size == new_size => {
192- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
193- }
194192 0 => self . alloc ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
195193 old_size => {
196- // `realloc` probably checks for `new_size > size` or something similar.
197- intrinsics:: assume ( new_size > old_size) ;
194+ // `realloc` probably checks for `new_size >= size` or something similar.
195+ intrinsics:: assume ( new_size >= old_size) ;
198196 let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
199197 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
200198 Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
@@ -216,16 +214,14 @@ unsafe impl AllocRef for System {
216214 ) ;
217215
218216 // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
217+ // If `new_size` is zero, than `old_size` has to be zero as well.
219218 // Other conditions must be upheld by the caller
220219 unsafe {
221220 match layout. size ( ) {
222- old_size if old_size == new_size => {
223- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
224- }
225221 0 => self . alloc_zeroed ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
226222 old_size => {
227- // `realloc` probably checks for `new_size > size` or something similar.
228- intrinsics:: assume ( new_size > old_size) ;
223+ // `realloc` probably checks for `new_size >= size` or something similar.
224+ intrinsics:: assume ( new_size >= old_size) ;
229225 let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
230226 raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
231227 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
@@ -248,11 +244,8 @@ unsafe impl AllocRef for System {
248244 "`new_size` must be smaller than or equal to `layout.size()`"
249245 ) ;
250246
251- let ptr = if new_size == old_size {
252- ptr
253- } else if new_size == 0 {
254- // SAFETY: `layout` is non-zero in size as `old_size` != `new_size`
255- // Other conditions must be upheld by the caller
247+ let ptr = if new_size == 0 {
248+ // SAFETY: conditions must be upheld by the caller
256249 unsafe {
257250 self . dealloc ( ptr, layout) ;
258251 }
@@ -261,8 +254,8 @@ unsafe impl AllocRef for System {
261254 // SAFETY: new_size is not zero,
262255 // Other conditions must be upheld by the caller
263256 let raw_ptr = unsafe {
264- // `realloc` probably checks for `new_size < old_size` or something similar.
265- intrinsics:: assume ( new_size < old_size) ;
257+ // `realloc` probably checks for `new_size <= old_size` or something similar.
258+ intrinsics:: assume ( new_size <= old_size) ;
266259 GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size)
267260 } ;
268261 NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?
0 commit comments