Skip to main content

Allocator

Struct Allocator 

Source
pub struct Allocator { /* private fields */ }
Expand description

Arena allocator for Vize.

A per-compile allocation handle. Template structures, retained oxc ASTs and arena strings all allocate from the same pool, so a &'a Allocator hands out &'a references that share one lifetime, and Allocator::reset clears all of them at once — nothing allocated here survives a compile boundary.

§Example

use vize_l0::{Allocator, Box, Vec};

let allocator = Allocator::default();
let allocator = &allocator;

let s = allocator.alloc_str("hello");
assert_eq!(s, "hello");

let boxed = Box::new_in(42, &allocator);
assert_eq!(*boxed, 42);

let mut vec = Vec::new_in(&allocator);
vec.push(1);
assert_eq!(vec.len(), 1);

Implementations§

Source§

impl Allocator

Source

pub fn new() -> Allocator

Creates a new allocator. The pool starts empty and reserves lazily.

Source

pub fn with_capacity(capacity: usize) -> Allocator

Creates a new allocator with the specified capacity reserved.

Source

pub fn alloc_str(&self, s: &str) -> &str

Allocates a string slice in the arena.

Source

pub fn alloc_owned<T>(&self, value: T) -> &T
where T: Any + Send,

Parks an owned value for the rest of the compile and borrows it at the arena’s lifetime.

The only callers are the compile entry points that receive an owned cross-compile summary (Croquis) by value and must hand the transform a reference at the arena’s lifetime — a local cannot produce one, because 'a is chosen by the caller and outlives the whole body.

P1-10 implemented this by parking a ManuallyDrop<T> in the arena itself, reproducing what bumpalo::Bump::alloc did before: the arena never runs destructors, so the summary’s own heap allocations were never released. Per compile that is a leak, and P1-11’s batch pool compiles thousands of files per process, so the leak became the peak-RSS story. The value therefore lives in a Box owned by the allocator now: the borrow still ends at the arena’s lifetime, and Allocator::reset (and dropping the allocator) runs the real destructor.

Arena-resident nodes stay Drop-free — the container const assertions in crate::Box / crate::Vec keep them that way — and this entry point does not widen for them: the Any bound implies 'static, so a parked value cannot hold an arena reference in the first place. It takes owned values built outside the arena, nothing else.

Source

pub fn as_oxc(&self) -> &Allocator

Returns the arena oxc parses retained ASTs into.

Identical to the pool everything else uses; the accessor is kept because oxc_parser takes &oxc_allocator::Allocator by name.

Source

pub fn reset(&mut self)

Resets the allocator, freeing all allocated memory.

This allows reusing the allocator for a new compilation without deallocating the underlying memory — the batch pool (crate::pool) resets between files instead of building a new arena per file.

Everything allocated since the last reset is invalidated: arena bytes are recycled (oxc keeps the current chunk and returns the rest to the global allocator) and every value parked by Allocator::alloc_owned is dropped. Taking &mut self is what makes that sound — no arena-backed borrow can be alive here — and in debug builds the arena’s generation advances, so a stamp taken before this call (Allocator::stamp) panics when validated afterwards.

Source

pub fn allocated_bytes(&self) -> usize

Returns the number of bytes currently allocated.

Arena bytes only: values parked by Allocator::alloc_owned own heap allocations outside the arena and are not counted.

Source

pub fn stamp(&self) -> ArenaStamp

Stamps the arena’s current identity and generation.

The stamp is the escape check for values that leave the borrow checker’s sight (a pointer round-trip, an owned park, a handle stored in a side table): validate it with Allocator::assert_stamp_current before reading, and a value that outlived a Allocator::reset panics instead of returning another file’s bytes. Carries no data in release builds, where validation is a no-op.

Source

pub fn assert_stamp_current(&self, stamp: ArenaStamp)

Panics when stamp did not come from this arena’s current generation.

No-op in release builds. See Allocator::stamp.

Methods from Deref<Target = Allocator>§

Source

pub fn alloc<T>(&self, val: T) -> &mut T

Allocate an object in this Allocator and return an exclusive reference to it.

§Panics

Panics if reserving space for T fails.

§Examples
use oxc_allocator::Allocator;

let allocator = Allocator::default();
let x = allocator.alloc([1u8; 20]);
assert_eq!(x, &[1u8; 20]);
Source

pub fn alloc_str<'alloc>(&'alloc self, src: &str) -> &'alloc str

Copy a string slice into this Allocator and return a reference to it.

§Panics

Panics if reserving space for the string fails.

§Examples
use oxc_allocator::Allocator;
let allocator = Allocator::default();
let hello = allocator.alloc_str("hello world");
assert_eq!(hello, "hello world");
Source

pub fn alloc_slice_copy<T>(&self, src: &[T]) -> &mut [T]
where T: Copy,

Copy a slice into this Allocator and return an exclusive reference to the copy.

§Panics

Panics if reserving space for the slice fails.

§Examples
use oxc_allocator::Allocator;
let allocator = Allocator::default();
let x = allocator.alloc_slice_copy(&[1, 2, 3]);
assert_eq!(x, &[1, 2, 3]);
Source

pub fn alloc_layout(&self, layout: Layout) -> NonNull<u8>

Allocate space for an object with the given Layout.

The returned pointer points at uninitialized memory, and should be initialized with std::ptr::write.

§Panics

Panics if reserving space matching layout fails.

Source

pub fn alloc_concat_strs_array<'a, const N: usize>( &'a self, strings: [&str; N], ) -> &'a str

Create new &str from a fixed-size array of &strs concatenated together, allocated in the given allocator.

§Panics

Panics if the sum of length of all strings exceeds isize::MAX.

§Example
use oxc_allocator::Allocator;

let allocator = Allocator::new();
let s = allocator.alloc_concat_strs_array(["hello", " ", "world", "!"]);
assert_eq!(s, "hello world!");
Source

pub fn capacity(&self) -> usize

Calculate the total capacity of this Allocator including all chunks, in bytes.

Note: This is the total amount of memory the Allocator owns NOT the total size of data that’s been allocated in it. If you want the latter, use used_bytes instead.

§Examples
use oxc_allocator::Allocator;

let capacity = 64 * 1024; // 64 KiB
let mut allocator = Allocator::with_capacity(capacity);
allocator.alloc(123u64); // 8 bytes

// Result is the capacity (64 KiB), not the size of allocated data (8 bytes).
// `Allocator::with_capacity` may allocate a bit more than requested.
assert!(allocator.capacity() >= capacity);
Source

pub fn used_bytes(&self) -> usize

Calculate the total size of data used in this Allocator, in bytes.

This is the total amount of memory that has been used in the Allocator, NOT the amount of memory the Allocator owns. If you want the latter, use capacity instead.

The result includes:

  1. Padding bytes between objects which have been allocated to preserve alignment of types where they have different alignments or have larger-than-typical alignment.
  2. Excess capacity in Vecs, StringBuilders and HashMaps.
  3. Objects which were allocated but later dropped. Allocator does not re-use allocations, so anything which is allocated into arena continues to take up “dead space”, even after it’s no longer referenced anywhere.
  4. “Dead space” left over where a Vec, StringBuilder or HashMap has grown and had to make a new allocation to accommodate its new larger size. Its old allocation continues to take up “dead” space in the allocator, unless it was the most recent allocation.

In practice, this almost always means that the result returned from this function will be an over-estimate vs the amount of “live” data in the arena.

However, if you are using the result of this method to create a new Allocator to clone an AST into, it is theoretically possible (though very unlikely) that it may be a slight under-estimate of the capacity required in new allocator to clone the AST into, depending on the order that &strs were allocated into arena in parser vs the order they get allocated during cloning. The order allocations are made in affects the amount of padding bytes required.

§Examples
use oxc_allocator::{Allocator, Vec};

let capacity = 64 * 1024; // 64 KiB
let mut allocator = Allocator::with_capacity(capacity);

allocator.alloc(1u8); // 1 byte with alignment 1
allocator.alloc(2u8); // 1 byte with alignment 1
allocator.alloc(3u64); // 8 bytes with alignment 8

// Only 10 bytes were allocated, but 16 bytes were used, in order to align `3u64` on 8
assert_eq!(allocator.used_bytes(), 16);

allocator.reset();

let mut vec = Vec::<u64>::with_capacity_in(2, &&allocator);

// Allocate something else, so `vec`'s allocation is not the most recent
allocator.alloc(123u64);

// `vec` has to grow beyond it's initial capacity
vec.extend([1, 2, 3, 4]);

// `vec` takes up 32 bytes, and `123u64` takes up 8 bytes = 40 total.
// But there's an additional 16 bytes consumed for `vec`'s original capacity of 2,
// which is still using up space
assert_eq!(allocator.used_bytes(), 56);

Trait Implementations§

Source§

impl AsRef<Allocator> for Allocator

Source§

fn as_ref(&self) -> &Allocator

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Default for Allocator

Source§

fn default() -> Allocator

Returns the “default value” for a type. Read more
Source§

impl Deref for Allocator

Source§

type Target = Allocator

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Allocator as Deref>::Target

Dereferences the value.
Source§

impl<'a> GetAllocator<'a> for &'a Allocator

Lets &Allocator stand in wherever oxc’s arena containers want an arena, so Box::new_in(value, &allocator) / Vec::new_in(&allocator) accept the Vize handle directly.

Source§

fn allocator(&self) -> &'a Allocator

Get the underlying allocator.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'a, T> FromIn<'a, T> for T

Source§

fn from_in(t: T, _: &'a Allocator) -> T

Converts to this type from the input type within the given allocator.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<'a, T, U> IntoIn<'a, U> for T
where U: FromIn<'a, T>,

Source§

fn into_in(self, allocator: &'a Allocator) -> U

Converts this type into the (usually inferred) input type within the given allocator.
Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.