[go: up one dir, main page]

Struct blink_alloc::BlinkAlloc

source ·
pub struct BlinkAlloc<A: Allocator = Global> { /* private fields */ }
Expand description

Single-threaded blink allocator.

Blink-allocator is arena-based allocator that allocates memory in growing chunks and serve allocations from them. When chunk is exhausted a new larger chunk is allocated.

Deallocation is no-op. BlinkAlloc can be reset to free all chunks except the last one, that will be reused.

Blink allocator aims to allocate a chunk large enough to serve all allocations between resets.

A shared and mutable reference to the BlinkAlloc implement Allocator trait. When “nightly” feature is enabled, Allocator trait is core::alloc::Allocator. Otherwise it is duplicated trait defined in allocator-api2.

Resetting blink allocator requires mutable borrow, so it is not possible to do while shared borrow is alive. That matches requirement of Allocator trait - while Allocator instance (a shared reference to BlinkAlloc) or any of its clones are alive, allocated memory must be valid.

This version of blink-allocator is single-threaded. It is possible to send to another thread, but cannot be shared. Internally it uses Cell for interior mutability and requires that state cannot be changed from another thread.

For multi-threaded version see SyncBlinkAlloc. Requires "sync" feature.

Example


let mut blink = BlinkAlloc::new();
let layout = std::alloc::Layout::new::<[u32; 8]>();
let ptr = blink.allocate(layout).unwrap();
let ptr = NonNull::new(ptr.as_ptr() as *mut u8).unwrap(); // Method for this is unstable.

unsafe {
    std::ptr::write(ptr.as_ptr().cast(), [1, 2, 3, 4, 5, 6, 7, 8]);
    blink.deallocate(ptr, layout);
}

blink.reset();

Example that uses nightly’s allocator_api

let mut blink = BlinkAlloc::new();
let mut vec = Vec::new_in(&blink);
vec.push(1);
vec.extend(1..3);
vec.extend(3..10);
drop(vec);
blink.reset();

Implementations§

source§

impl BlinkAlloc<Global>

source

pub const fn new() -> Self

Creates new blink allocator that uses global allocator to allocate memory chunks.

See BlinkAlloc::new_in for using custom allocator.

source

pub const fn with_chunk_size(chunk_size: usize) -> Self

Creates new blink allocator that uses global allocator to allocate memory chunks. With this method you can specify initial chunk size.

See BlinkAlloc::new_in for using custom allocator.

source§

impl<A> BlinkAlloc<A>where A: Allocator,

source

pub const fn new_in(allocator: A) -> Self

Creates new blink allocator that uses provided allocator to allocate memory chunks.

See BlinkAlloc::new for using global allocator.

source

pub const fn with_chunk_size_in(chunk_size: usize, allocator: A) -> Self

Creates new blink allocator that uses global allocator to allocate memory chunks. With this method you can specify initial chunk size.

See BlinkAlloc::new_in for using custom allocator.

source

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

Allocates memory with specified layout from this allocator. If needed it will allocate new chunk using underlying allocator. If chunk allocation fails, it will return Err.

source

pub fn reset(&mut self)

Resets this allocator, deallocating all chunks except the last one. Last chunk will be reused. With steady memory usage after few iterations chunk size should be sufficient for all allocations between resets.

Trait Implementations§

source§

impl<A> Allocator for &mut BlinkAlloc<A>where A: Allocator,

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) -> &Selfwhere 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<A> Allocator for BlinkAlloc<A>where A: Allocator,

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 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 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_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) -> &Selfwhere 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<A> BlinkAllocator for BlinkAlloc<A>where A: Allocator,

source§

fn reset(&mut self)

Resets allocator potentially invalidating all allocations made from this instance. This is no-op if allocator is Clone (typically shared reference to blink-allocator). Read more
source§

impl<A> Default for BlinkAlloc<A>where A: Allocator + Default,

source§

fn default() -> Self

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

impl<A> Drop for BlinkAlloc<A>where A: Allocator,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<A = Global> !RefUnwindSafe for BlinkAlloc<A>

§

impl<A> Send for BlinkAlloc<A>where A: Send,

§

impl<A = Global> !Sync for BlinkAlloc<A>

§

impl<A> Unpin for BlinkAlloc<A>where A: Unpin,

§

impl<A = Global> !UnwindSafe for BlinkAlloc<A>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.