Skip to content

Commit

Permalink
feat(allocator): add Allocator::new and with_capacity methods
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jan 20, 2025
1 parent 787aaad commit 7b82475
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ pub struct Allocator {
}

impl Allocator {
/// Create a new [`Allocator`] with no initial capacity.
///
/// This method does not reserve any memory to back the allocator. Memory for allocator's initial
/// chunk will be reserved lazily, when you make the first allocation into this [`Allocator`]
/// (e.g. with [`Allocator::alloc`], [`Box::new_in`], [`Vec::new_in`], [`HashMap::new_in`]).
///
/// If you can estimate the amount of memory the allocator will require to fit what you intend to
/// allocate into it, it is generally preferable to create that allocator with [`with_capacity`]
/// which reserves that amount of memory upfront. This will avoid further system calls to allocate
/// further chunks later on.
///
/// [`with_capacity`]: Allocator::with_capacity
//
// `#[inline(always)]` because just delegates to `bumpalo` method
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn new() -> Self {
Self { bump: Bump::new() }
}

/// Create a new [`Allocator`] with specified capacity.
//
// `#[inline(always)]` because just delegates to `bumpalo` method
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn with_capacity(capacity: usize) -> Self {
Self { bump: Bump::with_capacity(capacity) }
}

/// Allocate an object in this [`Allocator`] and return an exclusive reference to it.
///
/// # Panics
Expand Down

0 comments on commit 7b82475

Please sign in to comment.