Struct AllocatorGuard

Source
pub struct AllocatorGuard<'alloc_pool> { /* private fields */ }
Expand description

A guard object representing exclusive access to an Allocator from the pool.

On drop, the Allocator is reset and returned to the pool.

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: Copy>(&self, src: &[T]) -> &mut [T]

Copy a slice into this Bump 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 Deref for AllocatorGuard<'_>

Source§

type Target = Allocator

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Drop for AllocatorGuard<'_>

Source§

fn drop(&mut self)

Return Allocator back to the pool.

Auto Trait Implementations§

§

impl<'alloc_pool> !Freeze for AllocatorGuard<'alloc_pool>

§

impl<'alloc_pool> !RefUnwindSafe for AllocatorGuard<'alloc_pool>

§

impl<'alloc_pool> Send for AllocatorGuard<'alloc_pool>

§

impl<'alloc_pool> Sync for AllocatorGuard<'alloc_pool>

§

impl<'alloc_pool> Unpin for AllocatorGuard<'alloc_pool>

§

impl<'alloc_pool> !UnwindSafe for AllocatorGuard<'alloc_pool>

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<'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<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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.