@@ -72,7 +72,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
72
72
RawVec :: allocate_in ( capacity, true , a)
73
73
}
74
74
75
- fn allocate_in ( capacity : usize , zeroed : bool , mut a : A ) -> Self {
75
+ fn allocate_in ( mut capacity : usize , zeroed : bool , mut a : A ) -> Self {
76
76
unsafe {
77
77
let elem_size = mem:: size_of :: < T > ( ) ;
78
78
@@ -87,7 +87,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
87
87
let layout = Layout :: from_size_align ( alloc_size, align) . unwrap ( ) ;
88
88
let result = if zeroed { a. alloc_zeroed ( layout) } else { a. alloc ( layout) } ;
89
89
match result {
90
- Ok ( ptr) => ptr. cast ( ) ,
90
+ Ok ( ( ptr, size) ) => {
91
+ capacity = size / elem_size;
92
+ ptr. cast ( )
93
+ }
91
94
Err ( _) => handle_alloc_error ( layout) ,
92
95
}
93
96
} ;
@@ -280,7 +283,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
280
283
// 0, getting to here necessarily means the `RawVec` is overfull.
281
284
assert ! ( elem_size != 0 , "capacity overflow" ) ;
282
285
283
- let ( new_cap , ptr ) = match self . current_layout ( ) {
286
+ let ( ptr , new_cap ) = match self . current_layout ( ) {
284
287
Some ( cur) => {
285
288
// Since we guarantee that we never allocate more than
286
289
// `isize::MAX` bytes, `elem_size * self.cap <= isize::MAX` as
@@ -297,7 +300,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
297
300
alloc_guard ( new_size) . unwrap_or_else ( |_| capacity_overflow ( ) ) ;
298
301
let ptr_res = self . a . realloc ( NonNull :: from ( self . ptr ) . cast ( ) , cur, new_size) ;
299
302
match ptr_res {
300
- Ok ( ptr) => ( new_cap , ptr ) ,
303
+ Ok ( ( ptr, new_size ) ) => ( ptr , new_size / elem_size ) ,
301
304
Err ( _) => handle_alloc_error ( Layout :: from_size_align_unchecked (
302
305
new_size,
303
306
cur. align ( ) ,
@@ -310,7 +313,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
310
313
let new_cap = if elem_size > ( !0 ) / 8 { 1 } else { 4 } ;
311
314
let layout = Layout :: array :: < T > ( new_cap) . unwrap ( ) ;
312
315
match self . a . alloc ( layout) {
313
- Ok ( ptr) => ( new_cap , ptr ) ,
316
+ Ok ( ( ptr, new_size ) ) => ( ptr , new_size / elem_size ) ,
314
317
Err ( _) => handle_alloc_error ( layout) ,
315
318
}
316
319
}
@@ -598,7 +601,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
598
601
let align = mem:: align_of :: < T > ( ) ;
599
602
let old_layout = Layout :: from_size_align_unchecked ( old_size, align) ;
600
603
match self . a . realloc ( NonNull :: from ( self . ptr ) . cast ( ) , old_layout, new_size) {
601
- Ok ( p ) => self . ptr = p . cast ( ) . into ( ) ,
604
+ Ok ( ( ptr , _ ) ) => self . ptr = ptr . cast ( ) . into ( ) ,
602
605
Err ( _) => {
603
606
handle_alloc_error ( Layout :: from_size_align_unchecked ( new_size, align) )
604
607
}
@@ -631,6 +634,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
631
634
fallibility : Fallibility ,
632
635
strategy : ReserveStrategy ,
633
636
) -> Result < ( ) , TryReserveError > {
637
+ let elem_size = mem:: size_of :: < T > ( ) ;
638
+
634
639
unsafe {
635
640
// NOTE: we don't early branch on ZSTs here because we want this
636
641
// to actually catch "asking for more than usize::MAX" in that case.
@@ -662,15 +667,15 @@ impl<T, A: AllocRef> RawVec<T, A> {
662
667
None => self . a . alloc ( new_layout) ,
663
668
} ;
664
669
665
- let ptr = match ( res, fallibility) {
670
+ let ( ptr, new_cap ) = match ( res, fallibility) {
666
671
( Err ( AllocErr ) , Infallible ) => handle_alloc_error ( new_layout) ,
667
672
( Err ( AllocErr ) , Fallible ) => {
668
673
return Err ( TryReserveError :: AllocError {
669
674
layout : new_layout,
670
675
non_exhaustive : ( ) ,
671
676
} ) ;
672
677
}
673
- ( Ok ( ptr) , _) => ptr,
678
+ ( Ok ( ( ptr, new_size ) ) , _) => ( ptr, new_size / elem_size ) ,
674
679
} ;
675
680
676
681
self . ptr = ptr. cast ( ) . into ( ) ;
0 commit comments