Skip to main content

TalcCell

Struct TalcCell 

Source
pub struct TalcCell<S: Source, B: Binning> { /* private fields */ }
Expand description

TalcCell implements GlobalAlloc and Allocator without locking, but is !Sync.

This type has similar semantics to a Cell.

§Example


use allocator_api2::alloc::{Allocator, Layout};
use allocator_api2::vec::Vec;
use talc::{TalcCell, source::*};

static mut HEAP: [u8; 2048] = [0; 2048];

let talc = TalcCell::new(unsafe { Claim::array(&raw mut HEAP) });

let mut my_vec = Vec::<u32, _>::with_capacity_in(42, &talc);
my_vec.push(123);

§Safety

TalcCell’s API does not expose references to the inner Talc within an UnsafeCell and is !Sync, so it’s safe to mutate the inner data through a shared reference.

There is an exception to this; a reference to the inner Talc is exposed to sources. Source is thus an unsafe trait to implement, and the implementation must uphold that they don’t use the TalcCell/TalcLock directly or indirectly (e.g. calling dbg! in Source::resize when TalcLock is the global allocator) in the implementation. This requirement is not unique to TalcCell. If TalcLock is used in the source impl, it’ll deadlock.

To help catch bad Source implementations, TalcCell tracks borrows when debug_assertions are enabled, similar to a RefCell.

Implementations§

Source§

impl<S: Source, B: Binning> TalcCell<S, B>

Source

pub const fn new(source: S) -> Self

Create a new TalcCell.

Source

pub fn get_mut(&mut self) -> &mut Talc<S, B>

Returns a mutable reference to the inner Talc.

Source

pub fn into_inner(self) -> Talc<S, B>

Consumes the TalcCell, returning the inner Talc.

Source

pub fn replace_source(&self, source: S) -> S

Swaps out the source for another.

If you just want to clone the source, see TalcCell::clone_source.

Source

pub fn counters(&self) -> Counters

Available on crate feature counters only.

Obtain the inner allocation statistics.

Source

pub unsafe fn reserved(&self, heap_end: NonNull<u8>) -> Reserved

See Talc::reserved for documentation details.

Source

pub unsafe fn claim(&self, base: *mut u8, size: usize) -> Option<NonNull<u8>>

See Talc::claim for documentation details.

Source

pub unsafe fn extend( &self, heap_end: NonNull<u8>, new_end: *mut u8, ) -> NonNull<u8>

See Talc::extend for documentation details.

Source

pub unsafe fn truncate( &self, heap_end: NonNull<u8>, new_end: *mut u8, ) -> Option<NonNull<u8>>

See Talc::truncate for documentation details.

Source

pub unsafe fn resize( &self, heap_end: NonNull<u8>, new_end: *mut u8, ) -> Option<NonNull<u8>>

See Talc::resize for documentation details.

Source§

impl<S: Source + Clone, B: Binning> TalcCell<S, B>

Source

pub fn clone_source(&self) -> S

Returns a clone of Talc’s source.

To set the source instead, use TalcCell::replace_source.

Trait Implementations§

Source§

impl<S: Source, B: Binning> Allocator for TalcCell<S, B>

Source§

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

Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)

Deallocates the memory referenced by ptr. Read more
Source§

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

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>

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

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

Attempts to extend the memory block. Read more
Source§

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

Attempts to shrink the memory block. Read more
Source§

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

Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl<S: Debug + Source, B: Debug + Binning> Debug for TalcCell<S, B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Source, B: Binning> GlobalAlloc for TalcCell<S, B>

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

Auto Trait Implementations§

§

impl<S, B> !Freeze for TalcCell<S, B>

§

impl<S, B> !RefUnwindSafe for TalcCell<S, B>

§

impl<S, B> !Sync for TalcCell<S, B>

§

impl<S, B> Send for TalcCell<S, B>
where UnsafeCell<Talc<S, B>>: Send,

§

impl<S, B> Unpin for TalcCell<S, B>
where UnsafeCell<Talc<S, B>>: Unpin,

§

impl<S, B> UnsafeUnpin for TalcCell<S, B>
where UnsafeCell<Talc<S, B>>: UnsafeUnpin,

§

impl<S, B> UnwindSafe for TalcCell<S, B>
where UnsafeCell<Talc<S, B>>: 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.