Struct UnsafeGlobalBlinkAlloc

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

GlobalAlloc implementation based on BlinkAlloc.

Implementations§

Source§

impl UnsafeGlobalBlinkAlloc<System>

Source

pub const unsafe fn new() -> Self

Create a new UnsafeGlobalBlinkAlloc.

Const function can be used to initialize a static variable.

§Safety

This method is unsafe because this type is not thread-safe but implements Sync. Allocator returned by this method must not be used concurrently.

For safer alternative see GlobalBlinkAlloc.

§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;

// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc = unsafe { UnsafeGlobalBlinkAlloc::new() };

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

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

Create a new UnsafeGlobalBlinkAlloc.

This method allows to specify initial chunk size.

Const function can be used to initialize a static variable.

§Safety

This method is unsafe because this type is not thread-safe but implements Sync. Allocator returned by this method must not be used concurrently.

For safer alternative see GlobalBlinkAlloc.

§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;

// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc<std::alloc::System> = unsafe { UnsafeGlobalBlinkAlloc::new_in(std::alloc::System) };

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

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

Source

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

Create a new UnsafeGlobalBlinkAlloc with specified underlying allocator.

Const function can be used to initialize a static variable.

§Safety

This method is unsafe because this type is not thread-safe but implements Sync. Allocator returned by this method must not be used concurrently.

For safer alternative see GlobalBlinkAlloc.

§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;

// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc<std::alloc::System> = unsafe { UnsafeGlobalBlinkAlloc::new_in(std::alloc::System) };

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

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

Create a new UnsafeGlobalBlinkAlloc with specified underlying allocator.

This method allows to specify initial chunk size.

Const function can be used to initialize a static variable.

§Safety

This method is unsafe because this type is not thread-safe but implements Sync. Allocator returned by this method must not be used concurrently.

For safer alternative see GlobalBlinkAlloc.

§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;

// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc<std::alloc::System> = unsafe { UnsafeGlobalBlinkAlloc::new_in(std::alloc::System) };

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

Trait Implementations§

Source§

impl<A> GlobalAlloc for UnsafeGlobalBlinkAlloc<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 UnsafeGlobalBlinkAlloc<A>

Source§

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

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