@@ -22,6 +22,7 @@ extern crate alloc;
22
22
use rustc_data_structures:: cold_path;
23
23
use smallvec:: SmallVec ;
24
24
25
+ use std:: alloc:: Layout ;
25
26
use std:: cell:: { Cell , RefCell } ;
26
27
use std:: cmp;
27
28
use std:: intrinsics;
@@ -363,13 +364,15 @@ impl DroplessArena {
363
364
}
364
365
}
365
366
366
- /// Allocates a byte slice with specified size and alignment from the
367
- /// current memory chunk. Returns `None` if there is no free space left to
368
- /// satisfy the request.
367
+ /// Allocates a byte slice with specified layout from the current memory
368
+ /// chunk. Returns `None` if there is no free space left to satisfy the
369
+ /// request.
369
370
#[ inline]
370
- fn alloc_raw_without_grow ( & self , bytes : usize , align : usize ) -> Option < * mut u8 > {
371
+ fn alloc_raw_without_grow ( & self , layout : Layout ) -> Option < * mut u8 > {
371
372
let ptr = self . ptr . get ( ) as usize ;
372
373
let end = self . end . get ( ) as usize ;
374
+ let align = layout. align ( ) ;
375
+ let bytes = layout. size ( ) ;
373
376
// The allocation request fits into the current chunk iff:
374
377
//
375
378
// let aligned = align_to(ptr, align);
@@ -390,23 +393,23 @@ impl DroplessArena {
390
393
}
391
394
392
395
#[ inline]
393
- pub fn alloc_raw ( & self , bytes : usize , align : usize ) -> * mut u8 {
394
- assert ! ( bytes != 0 ) ;
396
+ pub fn alloc_raw ( & self , layout : Layout ) -> * mut u8 {
397
+ assert ! ( layout . size ( ) != 0 ) ;
395
398
loop {
396
- if let Some ( a) = self . alloc_raw_without_grow ( bytes , align ) {
399
+ if let Some ( a) = self . alloc_raw_without_grow ( layout ) {
397
400
break a;
398
401
}
399
402
// No free space left. Allocate a new chunk to satisfy the request.
400
403
// On failure the grow will panic or abort.
401
- self . grow ( bytes ) ;
404
+ self . grow ( layout . size ( ) ) ;
402
405
}
403
406
}
404
407
405
408
#[ inline]
406
409
pub fn alloc < T > ( & self , object : T ) -> & mut T {
407
410
assert ! ( !mem:: needs_drop:: <T >( ) ) ;
408
411
409
- let mem = self . alloc_raw ( mem :: size_of :: < T > ( ) , mem :: align_of :: < T > ( ) ) as * mut T ;
412
+ let mem = self . alloc_raw ( Layout :: for_value :: < T > ( & object ) ) as * mut T ;
410
413
411
414
unsafe {
412
415
// Write into uninitialized memory.
@@ -431,7 +434,7 @@ impl DroplessArena {
431
434
assert ! ( mem:: size_of:: <T >( ) != 0 ) ;
432
435
assert ! ( !slice. is_empty( ) ) ;
433
436
434
- let mem = self . alloc_raw ( slice . len ( ) * mem :: size_of :: < T > ( ) , mem :: align_of :: < T > ( ) ) as * mut T ;
437
+ let mem = self . alloc_raw ( Layout :: for_value :: < [ T ] > ( slice ) ) as * mut T ;
435
438
436
439
unsafe {
437
440
mem. copy_from_nonoverlapping ( slice. as_ptr ( ) , slice. len ( ) ) ;
@@ -477,8 +480,8 @@ impl DroplessArena {
477
480
if len == 0 {
478
481
return & mut [ ] ;
479
482
}
480
- let size = len . checked_mul ( mem :: size_of :: < T > ( ) ) . unwrap ( ) ;
481
- let mem = self . alloc_raw ( size , mem :: align_of :: < T > ( ) ) as * mut T ;
483
+
484
+ let mem = self . alloc_raw ( Layout :: array :: < T > ( len ) . unwrap ( ) ) as * mut T ;
482
485
unsafe { self . write_from_iter ( iter, len, mem) }
483
486
}
484
487
( _, _) => {
@@ -491,9 +494,8 @@ impl DroplessArena {
491
494
// the content of the SmallVec
492
495
unsafe {
493
496
let len = vec. len ( ) ;
494
- let start_ptr = self
495
- . alloc_raw ( len * mem:: size_of :: < T > ( ) , mem:: align_of :: < T > ( ) )
496
- as * mut T ;
497
+ let start_ptr =
498
+ self . alloc_raw ( Layout :: for_value :: < [ T ] > ( vec. as_slice ( ) ) ) as * mut T ;
497
499
vec. as_ptr ( ) . copy_to_nonoverlapping ( start_ptr, len) ;
498
500
vec. set_len ( 0 ) ;
499
501
slice:: from_raw_parts_mut ( start_ptr, len)
@@ -537,7 +539,7 @@ pub struct DropArena {
537
539
impl DropArena {
538
540
#[ inline]
539
541
pub unsafe fn alloc < T > ( & self , object : T ) -> & mut T {
540
- let mem = self . arena . alloc_raw ( mem :: size_of :: < T > ( ) , mem :: align_of :: < T > ( ) ) as * mut T ;
542
+ let mem = self . arena . alloc_raw ( Layout :: new :: < T > ( ) ) as * mut T ;
541
543
// Write into uninitialized memory.
542
544
ptr:: write ( mem, object) ;
543
545
let result = & mut * mem;
@@ -557,10 +559,7 @@ impl DropArena {
557
559
}
558
560
let len = vec. len ( ) ;
559
561
560
- let start_ptr = self
561
- . arena
562
- . alloc_raw ( len. checked_mul ( mem:: size_of :: < T > ( ) ) . unwrap ( ) , mem:: align_of :: < T > ( ) )
563
- as * mut T ;
562
+ let start_ptr = self . arena . alloc_raw ( Layout :: array :: < T > ( len) . unwrap ( ) ) as * mut T ;
564
563
565
564
let mut destructors = self . destructors . borrow_mut ( ) ;
566
565
// Reserve space for the destructors so we can't panic while adding them
0 commit comments