1
1
#![ unstable( feature = "raw_vec_internals" , reason = "unstable const warnings" , issue = "none" ) ]
2
2
3
- use core:: alloc:: LayoutError ;
4
3
use core:: cmp;
5
4
use core:: intrinsics;
6
5
use core:: mem:: { self , ManuallyDrop , MaybeUninit } ;
@@ -395,10 +394,9 @@ impl<T, A: Allocator> RawVec<T, A> {
395
394
let cap = cmp:: max ( self . cap * 2 , required_cap) ;
396
395
let cap = cmp:: max ( Self :: MIN_NON_ZERO_CAP , cap) ;
397
396
398
- let new_layout = Layout :: array :: < T > ( cap) ;
399
-
400
397
// `finish_grow` is non-generic over `T`.
401
- let ptr = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
398
+ let elem_layout = Layout :: new :: < T > ( ) ;
399
+ let ptr = finish_grow ( cap, elem_layout, self . current_memory ( ) , & mut self . alloc ) ?;
402
400
self . set_ptr ( ptr) ;
403
401
Ok ( ( ) )
404
402
}
@@ -415,10 +413,10 @@ impl<T, A: Allocator> RawVec<T, A> {
415
413
// is called for a zero-sized `T` after `needs_to_grow()` has
416
414
// succeeded, this early return will occur.)
417
415
let cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
418
- let new_layout = Layout :: array :: < T > ( cap) ;
419
416
420
417
// `finish_grow` is non-generic over `T`.
421
- let ptr = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
418
+ let elem_layout = Layout :: new :: < T > ( ) ;
419
+ let ptr = finish_grow ( cap, elem_layout, self . current_memory ( ) , & mut self . alloc ) ?;
422
420
self . set_ptr ( ptr) ;
423
421
Ok ( ( ) )
424
422
}
@@ -446,17 +444,18 @@ impl<T, A: Allocator> RawVec<T, A> {
446
444
// much smaller than the number of `T` types.)
447
445
#[ inline( never) ]
448
446
fn finish_grow < A > (
449
- new_layout : Result < Layout , LayoutError > ,
447
+ cap : usize ,
448
+ elem_layout : Layout ,
450
449
current_memory : Option < ( NonNull < u8 > , Layout ) > ,
451
450
alloc : & mut A ,
452
451
) -> Result < NonNull < [ u8 ] > , TryReserveError >
453
452
where
454
453
A : Allocator ,
455
454
{
456
- // Check for the error here to minimize the size of `RawVec::grow_*`.
457
- let new_layout = new_layout . map_err ( |_| CapacityOverflow ) ?;
455
+ let array_size = elem_layout . size ( ) . checked_mul ( cap ) . ok_or ( CapacityOverflow ) ? ;
456
+ alloc_guard ( array_size ) ?;
458
457
459
- alloc_guard ( new_layout. size ( ) ) ? ;
458
+ let new_layout = unsafe { Layout :: from_size_align_unchecked ( array_size , elem_layout . align ( ) ) } ;
460
459
461
460
let memory = if let Some ( ( ptr, old_layout) ) = current_memory {
462
461
debug_assert_eq ! ( old_layout. align( ) , new_layout. align( ) ) ;
0 commit comments