Struct WeakAlloc

Source
pub struct WeakAlloc<A> { /* private fields */ }
Expand description

A custom allocator that can be given ownership of data, returning a WeakRef.

Implementations§

Source§

impl<A> WeakAlloc<A>

Source

pub const fn new(alloc: A) -> Self

Source§

impl<A> WeakAlloc<A>
where A: GlobalAlloc + Clone,

Source

pub fn give<T: Send + Sync + 'static>(&self, element: T) -> WeakRef<T, A>

Give ownership of a value to the allocator. The value may be deallocated at any time if no other strong references to it exist.

Returns a WeakRef that can be used to get back an Arc in the future using the upgrade method, if the value still exists.

Because of the way Arc is implemented, prefer giving T where the size of T is small. Otherwise the backing allocation will not be deallocated when dropping the Arc, it will only be deallocated after all the weak references go out of scope. So [u8; 1000] is bad, but Box<[u8; 1000]> and Vec<[u8; 1000]> are good.

Source

pub fn give_and_upgrade<T: Send + Sync + 'static>( &self, element: T, ) -> ArcRef<T, A>

Alternative to A.give(x).upgrade().unwrap() that never fails. There is a race condition in that snippet if another thread fills the memory causing the allocator to call WEAK_LIST.pop_lru() after the call to give but before the call to upgrade. That race condition is avoided in this method by holding the lock slightly longer, so that no other thread can modify the list before we upgrade to an ArcRef.

Source

pub fn upgrade<T: Send + Sync + 'static>( &self, w: &WeakRef<T, A>, ) -> Option<ArcRef<T, A>>

Source

pub fn clear(&self)

Remove all the weak references from the WeakAlloc. This will deallocate all the WeakRefs that do not have an active ArcRef.

Source

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

Try to allocate some memory without freeing any existing weak allocations. This can be used to implement the equivalent to try_give: try to allocate a box with some contents but only if there is enough memory. If there isn’t enough memory this method will return a null pointer and the code needed to initialize the box can be skipped (using give forces you to initialize the value).

§Safety

The same restrictions as GlobalAlloc::alloc.

Source

pub fn inner(&self) -> &A

Returns a reference to the inner allocator.

Trait Implementations§

Source§

impl<A: Clone> Clone for WeakAlloc<A>

Source§

fn clone(&self) -> WeakAlloc<A>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A> GlobalAlloc for WeakAlloc<A>
where A: GlobalAlloc,

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
1.28.0 · 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
1.28.0 · 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

Auto Trait Implementations§

§

impl<A> Freeze for WeakAlloc<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for WeakAlloc<A>
where A: RefUnwindSafe,

§

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

§

impl<A> Sync for WeakAlloc<A>
where A: Sync,

§

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

§

impl<A> UnwindSafe for WeakAlloc<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.