Struct GlobalBlinkAlloc

Source
pub struct GlobalBlinkAlloc<A: Allocator = System> { /* private fields */ }
Expand description

GlobalAlloc implementation based on SyncBlinkAlloc.

§Example

use blink_alloc::GlobalBlinkAlloc;

#[global_allocator]
static GLOBAL_ALLOC: GlobalBlinkAlloc = GlobalBlinkAlloc::new();

fn main() {
    let _ = Box::new(42);
    let _ = vec![1, 2, 3];
}

Implementations§

Source§

impl GlobalBlinkAlloc<System>

Source

pub const fn new() -> Self

Create a new GlobalBlinkAlloc.

Const function can be used to initialize a static variable.

§Example
use blink_alloc::GlobalBlinkAlloc;

#[global_allocator]
static GLOBAL_ALLOC: GlobalBlinkAlloc = GlobalBlinkAlloc::new();

fn main() {
    let _ = Box::new(42);
    let _ = vec![1, 2, 3];
}
Source

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

Create a new GlobalBlinkAlloc.

This method allows to specify initial chunk size.

Const function can be used to initialize a static variable.

§Example
use blink_alloc::GlobalBlinkAlloc;

#[global_allocator]
static GLOBAL_ALLOC: GlobalBlinkAlloc = GlobalBlinkAlloc::new();

fn main() {
    let _ = Box::new(42);
    let _ = vec![1, 2, 3];
}
Source§

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

Source

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

Create a new GlobalBlinkAlloc with specified underlying allocator.

Const function can be used to initialize a static variable.

§Example
use blink_alloc::GlobalBlinkAlloc;

#[global_allocator]
static GLOBAL_ALLOC: GlobalBlinkAlloc<std::alloc::System> = GlobalBlinkAlloc::new_in(std::alloc::System);

fn main() {
    let _ = Box::new(42);
    let _ = vec![1, 2, 3];
}
Source

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

Create a new GlobalBlinkAlloc with specified underlying allocator.

This method allows to specify initial chunk size.

Const function can be used to initialize a static variable.

§Example
use blink_alloc::GlobalBlinkAlloc;

#[global_allocator]
static GLOBAL_ALLOC: GlobalBlinkAlloc<std::alloc::System> = GlobalBlinkAlloc::new_in(std::alloc::System);

fn main() {
    let _ = Box::new(42);
    let _ = vec![1, 2, 3];
}
Source

pub unsafe fn reset(&self)

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

§Safety

Memory allocated from this allocator in blink mode becomes invalidated. The user is responsible to ensure that previously allocated memory won’t be used after reset.

§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;

#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc = unsafe { UnsafeGlobalBlinkAlloc::new() };

unsafe { GLOBAL_ALLOC.blink_mode() };

let b = Box::new(42);
let v = vec![1, 2, 3];
drop(b);
drop(v);

// Safety: memory allocated in blink mode won't be used after reset.
unsafe {
    GLOBAL_ALLOC.reset();
    GLOBAL_ALLOC.direct_mode();
};

Switches allocator to blink mode. All allocations will be served by blink-allocator.

The type is created in direct mode. When used as global allocator, user may manually switch into blink mode in main or at any point later.

However user must switch back to direct mode before returning from main.

§Safety

Must be externally synchronized with other threads accessing this allocator. Memory allocated in direct mode must not be deallocated while in blink mode.

Source

pub unsafe fn direct_mode(&self)

Switches allocator to direct mode. All allocations will be served by underlying allocator.

The type is created in direct mode. When used as global allocator, user may manually switch into blink mode in main or at any point later.

However user must switch back to direct mode before returning from main.

§Safety

Must be externally synchronized with other threads accessing this allocator. Memory allocated in blink mode must not be deallocated while in direct mode.

Source

pub fn local(&self) -> LocalBlinkAlloc<'_, A>

Creates a new thread-local blink allocator proxy that borrows from this multi-threaded allocator.

The local proxy allocator works faster and allows more consistent memory reuse. It can be recreated without resetting the multi-threaded allocator, allowing SyncBlinkAlloc to be warm-up and serve all allocations from a single chunk without ever blocking.

Best works for fork-join style of parallelism. Create a local allocator for each thread/task. Reset after all threads/tasks are finished.

§Examples
static BLINK: GlobalBlinkAlloc = GlobalBlinkAlloc::new();

for _ in 0..3 {
    for i in 0..16 {
        let mut blink = BLINK.local(); // Sendable and 'static.
        std::thread::scope(move |_| {
            let mut vec = Vec::new_in(&blink);
            vec.push(i);
            for j in i*2..i*30 {
                vec.push(j); // Proxy will allocate enough memory to grow vec without reallocating on 2nd iteration and later.
            }
            drop(vec); // Without this line it will fail to borrow mutable on next line.
            blink.reset();
        });

        // Safety: Proxies and allocations are dropped.
        unsafe { BLINK.reset() };
    }
}

Trait Implementations§

Source§

impl<A> GlobalAlloc for GlobalBlinkAlloc<A>
where A: Allocator,

Source§

unsafe fn alloc(&self, layout: Layout) -> *mut u8

Allocates memory as described by the given layout. Read more
Source§

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)

Deallocates the block of memory at the given ptr pointer with the given layout. Read more
Source§

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8

Behaves like alloc, but also ensures that the contents are set to zero before being returned. Read more
Source§

unsafe fn realloc( &self, ptr: *mut u8, layout: Layout, new_size: usize, ) -> *mut u8

Shrinks or grows a block of memory to the given new_size in bytes. The block is described by the given ptr pointer and layout. Read more
Source§

impl<A: Allocator + Send> Send for GlobalBlinkAlloc<A>

Source§

impl<A: Allocator + Sync> Sync for GlobalBlinkAlloc<A>

Auto Trait Implementations§

§

impl<A = System> !Freeze for GlobalBlinkAlloc<A>

§

impl<A = System> !RefUnwindSafe for GlobalBlinkAlloc<A>

§

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

§

impl<A> UnwindSafe for GlobalBlinkAlloc<A>
where A: UnwindSafe,

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.