@@ -72,7 +72,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
7272 RawVec :: allocate_in ( capacity, true , a)
7373 }
7474
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 {
7676 unsafe {
7777 let elem_size = mem:: size_of :: < T > ( ) ;
7878
@@ -87,7 +87,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
8787 let layout = Layout :: from_size_align ( alloc_size, align) . unwrap ( ) ;
8888 let result = if zeroed { a. alloc_zeroed ( layout) } else { a. alloc ( layout) } ;
8989 match result {
90- Ok ( ptr) => ptr. cast ( ) ,
90+ Ok ( ( ptr, size) ) => {
91+ capacity = size / elem_size;
92+ ptr. cast ( )
93+ }
9194 Err ( _) => handle_alloc_error ( layout) ,
9295 }
9396 } ;
@@ -280,7 +283,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
280283 // 0, getting to here necessarily means the `RawVec` is overfull.
281284 assert ! ( elem_size != 0 , "capacity overflow" ) ;
282285
283- let ( new_cap , ptr ) = match self . current_layout ( ) {
286+ let ( ptr , new_cap ) = match self . current_layout ( ) {
284287 Some ( cur) => {
285288 // Since we guarantee that we never allocate more than
286289 // `isize::MAX` bytes, `elem_size * self.cap <= isize::MAX` as
@@ -297,7 +300,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
297300 alloc_guard ( new_size) . unwrap_or_else ( |_| capacity_overflow ( ) ) ;
298301 let ptr_res = self . a . realloc ( NonNull :: from ( self . ptr ) . cast ( ) , cur, new_size) ;
299302 match ptr_res {
300- Ok ( ptr) => ( new_cap , ptr ) ,
303+ Ok ( ( ptr, new_size ) ) => ( ptr , new_size / elem_size ) ,
301304 Err ( _) => handle_alloc_error ( Layout :: from_size_align_unchecked (
302305 new_size,
303306 cur. align ( ) ,
@@ -310,7 +313,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
310313 let new_cap = if elem_size > ( !0 ) / 8 { 1 } else { 4 } ;
311314 let layout = Layout :: array :: < T > ( new_cap) . unwrap ( ) ;
312315 match self . a . alloc ( layout) {
313- Ok ( ptr) => ( new_cap , ptr ) ,
316+ Ok ( ( ptr, new_size ) ) => ( ptr , new_size / elem_size ) ,
314317 Err ( _) => handle_alloc_error ( layout) ,
315318 }
316319 }
@@ -598,7 +601,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
598601 let align = mem:: align_of :: < T > ( ) ;
599602 let old_layout = Layout :: from_size_align_unchecked ( old_size, align) ;
600603 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 ( ) ,
602605 Err ( _) => {
603606 handle_alloc_error ( Layout :: from_size_align_unchecked ( new_size, align) )
604607 }
@@ -631,6 +634,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
631634 fallibility : Fallibility ,
632635 strategy : ReserveStrategy ,
633636 ) -> Result < ( ) , TryReserveError > {
637+ let elem_size = mem:: size_of :: < T > ( ) ;
638+
634639 unsafe {
635640 // NOTE: we don't early branch on ZSTs here because we want this
636641 // to actually catch "asking for more than usize::MAX" in that case.
@@ -662,15 +667,15 @@ impl<T, A: AllocRef> RawVec<T, A> {
662667 None => self . a . alloc ( new_layout) ,
663668 } ;
664669
665- let ptr = match ( res, fallibility) {
670+ let ( ptr, new_cap ) = match ( res, fallibility) {
666671 ( Err ( AllocErr ) , Infallible ) => handle_alloc_error ( new_layout) ,
667672 ( Err ( AllocErr ) , Fallible ) => {
668673 return Err ( TryReserveError :: AllocError {
669674 layout : new_layout,
670675 non_exhaustive : ( ) ,
671676 } ) ;
672677 }
673- ( Ok ( ptr) , _) => ptr,
678+ ( Ok ( ( ptr, new_size ) ) , _) => ( ptr, new_size / elem_size ) ,
674679 } ;
675680
676681 self . ptr = ptr. cast ( ) . into ( ) ;
0 commit comments