1
1
use crate :: cmp;
2
- use crate :: convert:: TryFrom ;
3
2
use crate :: fmt;
4
3
use crate :: mem;
5
4
use crate :: num:: NonZeroUsize ;
@@ -303,18 +302,10 @@ impl Layout {
303
302
// > must not overflow isize (i.e., the rounded value must be
304
303
// > less than or equal to `isize::MAX`)
305
304
let padded_size = self . size ( ) + self . padding_needed_for ( self . align ( ) ) ;
306
- // Size manipulation is done in isize space to avoid overflowing isize.
307
- let n = isize:: try_from ( n) . map_err ( |_| LayoutError ) ?;
308
- let alloc_size = ( padded_size as isize ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
305
+ let alloc_size = padded_size. checked_mul ( n) . ok_or ( LayoutError ) ?;
309
306
310
- // SAFETY: self.align is already known to be valid and alloc_size has been
311
- // padded already.
312
- unsafe {
313
- Ok ( (
314
- Layout :: from_size_align_unchecked ( alloc_size as usize , self . align ( ) ) ,
315
- padded_size as usize ,
316
- ) )
317
- }
307
+ // The safe constructor is called here to enforce the isize size limit.
308
+ Layout :: from_size_align ( alloc_size, self . align ( ) ) . map ( |layout| ( layout, padded_size) )
318
309
}
319
310
320
311
/// Creates a layout describing the record for `self` followed by
@@ -368,12 +359,12 @@ impl Layout {
368
359
let new_align = cmp:: max ( self . align ( ) , next. align ( ) ) ;
369
360
let pad = self . padding_needed_for ( next. align ( ) ) ;
370
361
371
- // Size manipulation is done in isize space to avoid overflowing isize.
372
- let offset = ( self . size ( ) as isize ) . checked_add ( pad as isize ) . ok_or ( LayoutError ) ?;
373
- let new_size = offset. checked_add ( next. size ( ) as isize ) . ok_or ( LayoutError ) ?;
362
+ let offset = self . size ( ) . checked_add ( pad) . ok_or ( LayoutError ) ?;
363
+ let new_size = offset. checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
374
364
375
- let layout = Layout :: from_size_align ( new_size as usize , new_align) ?;
376
- Ok ( ( layout, offset as usize ) )
365
+ // The safe constructor is called here to enforce the isize size limit.
366
+ let layout = Layout :: from_size_align ( new_size, new_align) ?;
367
+ Ok ( ( layout, offset) )
377
368
}
378
369
379
370
/// Creates a layout describing the record for `n` instances of
@@ -391,9 +382,8 @@ impl Layout {
391
382
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
392
383
#[ inline]
393
384
pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
394
- // Size manipulation is done in isize space to avoid overflowing isize.
395
- let n = isize:: try_from ( n) . map_err ( |_| LayoutError ) ?;
396
- let size = ( self . size ( ) as isize ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
385
+ let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
386
+ // The safe constructor is called here to enforce the isize size limit.
397
387
Layout :: from_size_align ( size as usize , self . align ( ) )
398
388
}
399
389
@@ -406,10 +396,9 @@ impl Layout {
406
396
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
407
397
#[ inline]
408
398
pub fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
409
- // Size manipulation is done in isize space to avoid overflowing isize.
410
- let new_size =
411
- ( self . size ( ) as isize ) . checked_add ( next. size ( ) as isize ) . ok_or ( LayoutError ) ?;
412
- Layout :: from_size_align ( new_size as usize , self . align ( ) )
399
+ let new_size = self . size ( ) . checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
400
+ // The safe constructor is called here to enforce the isize size limit.
401
+ Layout :: from_size_align ( new_size, self . align ( ) )
413
402
}
414
403
415
404
/// Creates a layout describing the record for a `[T; n]`.
@@ -418,19 +407,9 @@ impl Layout {
418
407
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
419
408
#[ inline]
420
409
pub fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
421
- // Size manipulation is done in isize space to avoid overflowing isize.
422
- let n = isize:: try_from ( n) . map_err ( |_| LayoutError ) ?;
423
- let array_size = ( mem:: size_of :: < T > ( ) as isize ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
424
-
425
- // SAFETY:
426
- // - Size: `array_size` cannot be too big because `size_of::<T>()` must
427
- // be a multiple of `align_of::<T>()`. Therefore, `array_size`
428
- // rounded up to the nearest multiple of `align_of::<T>()` is just
429
- // `array_size`. And `array_size` cannot be too big because it was
430
- // just checked by the `checked_mul()`.
431
- // - Alignment: `align_of::<T>()` will always give an acceptable
432
- // (non-zero, power of two) alignment.
433
- Ok ( unsafe { Layout :: from_size_align_unchecked ( array_size as usize , mem:: align_of :: < T > ( ) ) } )
410
+ let array_size = mem:: size_of :: < T > ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
411
+ // The safe constructor is called here to enforce the isize size limit.
412
+ Layout :: from_size_align ( array_size, mem:: align_of :: < T > ( ) )
434
413
}
435
414
}
436
415
0 commit comments