@@ -185,16 +185,14 @@ unsafe impl AllocRef for System {
185
185
) ;
186
186
187
187
// 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.
188
189
// Other conditions must be upheld by the caller
189
190
unsafe {
190
191
match layout. size ( ) {
191
- old_size if old_size == new_size => {
192
- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
193
- }
194
192
0 => self . alloc ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
195
193
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) ;
198
196
let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
199
197
let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
200
198
Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
@@ -216,16 +214,14 @@ unsafe impl AllocRef for System {
216
214
) ;
217
215
218
216
// 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.
219
218
// Other conditions must be upheld by the caller
220
219
unsafe {
221
220
match layout. size ( ) {
222
- old_size if old_size == new_size => {
223
- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
224
- }
225
221
0 => self . alloc_zeroed ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
226
222
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) ;
229
225
let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
230
226
raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
231
227
let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
@@ -248,11 +244,8 @@ unsafe impl AllocRef for System {
248
244
"`new_size` must be smaller than or equal to `layout.size()`"
249
245
) ;
250
246
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
256
249
unsafe {
257
250
self . dealloc ( ptr, layout) ;
258
251
}
@@ -261,8 +254,8 @@ unsafe impl AllocRef for System {
261
254
// SAFETY: new_size is not zero,
262
255
// Other conditions must be upheld by the caller
263
256
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) ;
266
259
GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size)
267
260
} ;
268
261
NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?
0 commit comments