Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/oxc_allocator/src/vec2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ use core::slice;
// #[cfg(feature = "std")]
// use std::io;

use bumpalo::collections::CollectionAllocErr;

use oxc_data_structures::assert_unchecked;

use crate::alloc::Alloc;

mod raw_vec;
use raw_vec::RawVec;
use raw_vec::{AllocError, RawVec};

unsafe fn arith_offset<T>(p: *const T, offset: isize) -> *const T {
p.offset(offset)
Expand Down Expand Up @@ -921,7 +919,7 @@ impl<'a, T: 'a, A: Alloc> Vec<'a, T, A> {
/// vec.try_reserve(10).unwrap();
/// assert!(vec.capacity() >= 11);
/// ```
pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> {
pub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError> {
self.buf.try_reserve(self.len_u32(), additional)
}

Expand All @@ -948,7 +946,7 @@ impl<'a, T: 'a, A: Alloc> Vec<'a, T, A> {
/// vec.try_reserve_exact(10).unwrap();
/// assert!(vec.capacity() >= 11);
/// ```
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> {
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), AllocError> {
self.buf.try_reserve_exact(self.len_u32(), additional)
}

Expand Down
46 changes: 25 additions & 21 deletions crates/oxc_allocator/src/vec2/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ use core::cmp;
use core::mem;
use core::ptr::{self, NonNull};

use bumpalo::collections::CollectionAllocErr::{self, AllocErr, CapacityOverflow};

use crate::alloc::Alloc;

/// Error type for fallible methods:
/// [`RawVec::try_reserve`], [`RawVec::try_reserve_exact`].
pub enum AllocError {
AllocErr,
CapacityOverflow,
}

// use boxed::Box;

/// A low-level utility for more ergonomically allocating, reallocating, and deallocating
Expand Down Expand Up @@ -353,11 +358,7 @@ impl<'a, T, A: Alloc> RawVec<'a, T, A> {
*/

/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
pub fn try_reserve_exact(
&mut self,
len: u32,
additional: usize,
) -> Result<(), CollectionAllocErr> {
pub fn try_reserve_exact(&mut self, len: u32, additional: usize) -> Result<(), AllocError> {
if self.needs_to_grow(len, additional) {
self.grow_exact(len, additional)?
}
Expand Down Expand Up @@ -393,7 +394,7 @@ impl<'a, T, A: Alloc> RawVec<'a, T, A> {
}

/// The same as `reserve`, but returns on errors instead of panicking or aborting.
pub fn try_reserve(&mut self, len: u32, additional: usize) -> Result<(), CollectionAllocErr> {
pub fn try_reserve(&mut self, len: u32, additional: usize) -> Result<(), AllocError> {
if self.needs_to_grow(len, additional) {
self.grow_amortized(len, additional)?;
}
Expand Down Expand Up @@ -637,7 +638,7 @@ impl<T, A: Alloc> RawVec<'_, T, A> {

/// Helper method to reserve additional space, reallocating the backing memory.
/// The caller is responsible for confirming that there is not already enough space available.
fn grow_exact(&mut self, len: u32, additional: usize) -> Result<(), CollectionAllocErr> {
fn grow_exact(&mut self, len: u32, additional: usize) -> Result<(), AllocError> {
unsafe {
// NOTE: we don't early branch on ZSTs here because we want this
// to actually catch "asking for more than u32::MAX" in that case.
Expand All @@ -646,8 +647,10 @@ impl<T, A: Alloc> RawVec<'_, T, A> {

// `len as usize` is safe because `len` is `u32`, so it must be
// less than `usize::MAX`.
let new_cap = (len as usize).checked_add(additional).ok_or(CapacityOverflow)?;
let new_layout = Layout::array::<T>(new_cap).map_err(|_| CapacityOverflow)?;
let new_cap =
(len as usize).checked_add(additional).ok_or(AllocError::CapacityOverflow)?;
let new_layout =
Layout::array::<T>(new_cap).map_err(|_| AllocError::CapacityOverflow)?;

self.ptr = self.finish_grow(new_layout)?.cast();

Expand All @@ -663,7 +666,7 @@ impl<T, A: Alloc> RawVec<'_, T, A> {

/// Helper method to reserve additional space, reallocating the backing memory.
/// The caller is responsible for confirming that there is not already enough space available.
fn grow_amortized(&mut self, len: u32, additional: usize) -> Result<(), CollectionAllocErr> {
fn grow_amortized(&mut self, len: u32, additional: usize) -> Result<(), AllocError> {
unsafe {
// NOTE: we don't early branch on ZSTs here because we want this
// to actually catch "asking for more than u32::MAX" in that case.
Expand All @@ -673,7 +676,8 @@ impl<T, A: Alloc> RawVec<'_, T, A> {
// Nothing we can really do about these checks, sadly.
// `len as usize` is safe because `len` is `u32`, so it must be
// less than `usize::MAX`.
let required_cap = (len as usize).checked_add(additional).ok_or(CapacityOverflow)?;
let required_cap =
(len as usize).checked_add(additional).ok_or(AllocError::CapacityOverflow)?;

// This guarantees exponential growth. The doubling cannot overflow
// because `cap <= isize::MAX` and the type of `cap` is `u32`.
Expand Down Expand Up @@ -707,7 +711,7 @@ impl<T, A: Alloc> RawVec<'_, T, A> {
// }
// let cap = cmp::max(Self::MIN_NON_ZERO_CAP, cap);

let new_layout = Layout::array::<T>(cap).map_err(|_| CapacityOverflow)?;
let new_layout = Layout::array::<T>(cap).map_err(|_| AllocError::CapacityOverflow)?;

self.ptr = self.finish_grow(new_layout)?.cast();

Expand All @@ -723,7 +727,7 @@ impl<T, A: Alloc> RawVec<'_, T, A> {

// Given a new layout, completes the grow operation.
#[inline]
fn finish_grow(&self, new_layout: Layout) -> Result<NonNull<u8>, CollectionAllocErr> {
fn finish_grow(&self, new_layout: Layout) -> Result<NonNull<u8>, AllocError> {
alloc_guard(new_layout.size())?;

let new_ptr = match self.current_layout() {
Expand Down Expand Up @@ -772,13 +776,13 @@ impl<T, A: Alloc> RawVec<'_, T, A> {
// running on a platform which can use all 4GB in user-space. e.g. PAE or x32

#[inline]
fn alloc_guard(alloc_size: usize) -> Result<(), CollectionAllocErr> {
fn alloc_guard(alloc_size: usize) -> Result<(), AllocError> {
if mem::size_of::<usize>() < 8 {
if alloc_size > ::core::isize::MAX as usize {
return Err(CapacityOverflow);
return Err(AllocError::CapacityOverflow);
}
} else if alloc_size > u32::MAX as usize {
return Err(CapacityOverflow);
return Err(AllocError::CapacityOverflow);
}
Ok(())
}
Expand All @@ -796,11 +800,11 @@ fn capacity_overflow() -> ! {
// to make the call site function as small as possible, so it can be inlined.
#[inline(never)]
#[cold]
fn handle_error(error: CollectionAllocErr) -> ! {
fn handle_error(error: AllocError) -> ! {
match error {
CapacityOverflow => capacity_overflow(),
AllocError::CapacityOverflow => capacity_overflow(),
// TODO: call `handle_alloc_error` instead of `panic!` once the AllocErr stored a Layout,
AllocErr => panic!("encountered allocation error"),
AllocError::AllocErr => panic!("encountered allocation error"),
}
}

Expand Down
Loading