fixed_bump

Struct Bump

Source
pub struct Bump<Size, Align = Size>(/* private fields */);
Expand description

A bump allocator that allocates memory in non-amortized O(1) (constant) time.

The allocator internally uses fixed-size chunks of memory. The size and alignment of each chunk of memory is determined by the type parameters Size and Align: the size is mem::size_of::<Size>() and the alignment is mem::align_of::<Align>(). The default value of Align is Size, so you can specify both the size and alignment with a single type parameter.

A common use of this type, and the most space-efficient way to use it, is to allocate many values of the same type (or at least the same size and alignment). In this case, it may be convenient to specify the chunk size using an array type: to use properly aligned chunks large enough to allocate n values of type T, pass [T; n] as the Size parameter, which will also be the Align parameter by default.

Implementations§

Source§

impl<Size, Align> Bump<Size, Align>

Source

pub fn new() -> Self

Creates a new Bump.

Source

pub fn allocate(&self, layout: Layout) -> Option<NonNull<[u8]>>

Tries to allocate memory with a size and alignment matching layout.

Returns a pointer to the memory on success, or None on failure. The memory is valid until the Bump is dropped. Note that the returned memory could be larger than layout.size().

This method is similar to Allocator::allocate, except it returns an Option instead of a Result.

Allocation is guaranteed to succeed, assuming the global allocator succeeds, if layout.size() is less than or equal to mem::size_of::<Size>() and layout.align() is less than or equal to mem::align_of::<Align>(). See Self::can_allocate.

Source

pub fn alloc_value<T>(&self, value: T) -> &mut T

Allocates a value of type T.

The memory is initialized with value and a reference to the value is returned. Note that the value’s destructor will not be called automatically.

§Panics

Panics if this allocator cannot allocate memory matching Layout::new::<T>() (see Self::can_allocate). Note that if the global allocator fails, handle_alloc_error is called instead of panicking.

For an equivalent that doesn’t panic or call handle_alloc_error, see Self::try_alloc_value.

Source

pub fn try_alloc_value<T>(&self, value: T) -> Result<&mut T, T>

Tries to allocate a value of type T.

If the allocation succeeds, the memory is initialized with value and a reference to the value is returned. Note that the value’s destructor will not be called automatically.

Allocation succeeds if and only if Self::allocate is able to allocate memory matching Layout::new::<T>(). See Self::allocate for details regarding the circumstances in which allocation can fail.

§Errors

If allocation fails, Err(value) is returned.

Source

pub fn can_allocate(&self, layout: Layout) -> bool

Returns whether this allocator can allocate memory matching layout.

This is guaranteed to return true if layout.size() is less than or equal to mem::size_of::<Size>() and layout.align() is less than or equal to mem::align_of::<Align>(). It may return true if the alignment is bigger, but never if the size is.

Trait Implementations§

Source§

impl<Size, Align> Allocator for Bump<Size, Align>

Available on crate features allocator_api or allocator-fallback only.
Source§

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout)

🔬This is a nightly-only experimental API. (allocator_api)
Deallocates the memory referenced by ptr. Read more
Source§

fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to shrink the memory block. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

🔬This is a nightly-only experimental API. (allocator_api)
Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl<Size, Align> Default for Bump<Size, Align>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Size, Align = Size> !Freeze for Bump<Size, Align>

§

impl<Size, Align = Size> !RefUnwindSafe for Bump<Size, Align>

§

impl<Size, Align = Size> !Send for Bump<Size, Align>

§

impl<Size, Align = Size> !Sync for Bump<Size, Align>

§

impl<Size, Align> Unpin for Bump<Size, Align>

§

impl<Size, Align> UnwindSafe for Bump<Size, Align>

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<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, 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.