Auto merge of #145750 - btj:drop-alloc-guard, r= · rust-lang/rust@dacfb46 (original) (raw)
`@@ -468,10 +468,6 @@ impl<A: Allocator> RawVecInner {
`
468
468
`return Ok(Self::new_in(alloc, elem_layout.alignment()));
`
469
469
`}
`
470
470
``
471
``
`-
if let Err(err) = alloc_guard(layout.size()) {
`
472
``
`-
return Err(err);
`
473
``
`-
}
`
474
``
-
475
471
`let result = match init {
`
476
472
`AllocInit::Uninitialized => alloc.allocate(layout),
`
477
473
`#[cfg(not(no_global_oom_handling))]
`
`@@ -662,7 +658,7 @@ impl<A: Allocator> RawVecInner {
`
662
658
`let new_layout = layout_array(cap, elem_layout)?;
`
663
659
``
664
660
`let ptr = finish_grow(new_layout, self.current_memory(elem_layout), &mut self.alloc)?;
`
665
``
`` -
// SAFETY: finish_grow would have resulted in a capacity overflow if we tried to allocate more than isize::MAX items
``
``
661
`` +
// SAFETY: layout_array would have resulted in a capacity overflow if we tried to allocate more than isize::MAX items
``
666
662
``
667
663
`unsafe { self.set_ptr_and_cap(ptr, cap) };
`
668
664
`Ok(())
`
`@@ -684,7 +680,7 @@ impl<A: Allocator> RawVecInner {
`
684
680
`let new_layout = layout_array(cap, elem_layout)?;
`
685
681
``
686
682
`let ptr = finish_grow(new_layout, self.current_memory(elem_layout), &mut self.alloc)?;
`
687
``
`` -
// SAFETY: finish_grow would have resulted in a capacity overflow if we tried to allocate more than isize::MAX items
``
``
683
`` +
// SAFETY: layout_array would have resulted in a capacity overflow if we tried to allocate more than isize::MAX items
``
688
684
`unsafe {
`
689
685
`self.set_ptr_and_cap(ptr, cap);
`
690
686
`}
`
`@@ -771,8 +767,6 @@ fn finish_grow(
`
771
767
`where
`
772
768
`A: Allocator,
`
773
769
`{
`
774
``
`-
alloc_guard(new_layout.size())?;
`
775
``
-
776
770
`let memory = if let Some((ptr, old_layout)) = current_memory {
`
777
771
`debug_assert_eq!(old_layout.align(), new_layout.align());
`
778
772
`unsafe {
`
`@@ -799,23 +793,6 @@ fn handle_error(e: TryReserveError) -> ! {
`
799
793
`}
`
800
794
`}
`
801
795
``
802
``
`-
// We need to guarantee the following:
`
803
``
`` -
// * We don't ever allocate > isize::MAX byte-size objects.
``
804
``
`` -
// * We don't overflow usize::MAX and actually allocate too little.
``
805
``
`-
//
`
806
``
`-
// On 64-bit we just need to check for overflow since trying to allocate
`
807
``
`` -
// > isize::MAX bytes will surely fail. On 32-bit and 16-bit we need to add
``
808
``
`-
// an extra guard for this in case we're running on a platform which can use
`
809
``
`-
// all 4GB in user-space, e.g., PAE or x32.
`
810
``
`-
#[inline]
`
811
``
`-
fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> {
`
812
``
`-
if usize::BITS < 64 && alloc_size > isize::MAX as usize {
`
813
``
`-
Err(CapacityOverflow.into())
`
814
``
`-
} else {
`
815
``
`-
Ok(())
`
816
``
`-
}
`
817
``
`-
}
`
818
``
-
819
796
`#[inline]
`
820
797
`fn layout_array(cap: usize, elem_layout: Layout) -> Result<Layout, TryReserveError> {
`
821
798
` elem_layout.repeat(cap).map(|(layout, pad)| layout).map_err(|| CapacityOverflow.into())
`