@@ -181,16 +181,14 @@ unsafe impl AllocRef for System {
181
181
) ;
182
182
183
183
// 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.
184
185
// Other conditions must be upheld by the caller
185
186
unsafe {
186
187
match layout. size ( ) {
187
- old_size if old_size == new_size => {
188
- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
189
- }
190
188
0 => self . alloc ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
191
189
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) ;
194
192
let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
195
193
let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
196
194
Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
@@ -212,16 +210,14 @@ unsafe impl AllocRef for System {
212
210
) ;
213
211
214
212
// 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.
215
214
// Other conditions must be upheld by the caller
216
215
unsafe {
217
216
match layout. size ( ) {
218
- old_size if old_size == new_size => {
219
- Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
220
- }
221
217
0 => self . alloc_zeroed ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
222
218
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) ;
225
221
let raw_ptr = GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size) ;
226
222
raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
227
223
let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
@@ -244,11 +240,8 @@ unsafe impl AllocRef for System {
244
240
"`new_size` must be smaller than or equal to `layout.size()`"
245
241
) ;
246
242
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
252
245
unsafe {
253
246
self . dealloc ( ptr, layout) ;
254
247
}
@@ -257,8 +250,8 @@ unsafe impl AllocRef for System {
257
250
// SAFETY: new_size is not zero,
258
251
// Other conditions must be upheld by the caller
259
252
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) ;
262
255
GlobalAlloc :: realloc ( & System , ptr. as_ptr ( ) , layout, new_size)
263
256
} ;
264
257
NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?
0 commit comments