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>
impl<S: Source, B: Binning> TalcCell<S, B>
Sourcepub fn into_inner(self) -> Talc<S, B>
pub fn into_inner(self) -> Talc<S, B>
Sourcepub fn replace_source(&self, source: S) -> S
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.
Sourcepub fn counters(&self) -> Counters
Available on crate feature counters only.
pub fn counters(&self) -> Counters
counters only.Obtain the inner allocation statistics.
Sourcepub unsafe fn reserved(&self, heap_end: NonNull<u8>) -> Reserved
pub unsafe fn reserved(&self, heap_end: NonNull<u8>) -> Reserved
See Talc::reserved for documentation details.
Sourcepub unsafe fn claim(&self, base: *mut u8, size: usize) -> Option<NonNull<u8>>
pub unsafe fn claim(&self, base: *mut u8, size: usize) -> Option<NonNull<u8>>
See Talc::claim for documentation details.
Sourcepub unsafe fn extend(
&self,
heap_end: NonNull<u8>,
new_end: *mut u8,
) -> NonNull<u8>
pub unsafe fn extend( &self, heap_end: NonNull<u8>, new_end: *mut u8, ) -> NonNull<u8>
See Talc::extend for documentation details.
Source§impl<S: Source + Clone, B: Binning> TalcCell<S, B>
impl<S: Source + Clone, B: Binning> TalcCell<S, B>
Sourcepub fn clone_source(&self) -> S
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>
impl<S: Source, B: Binning> Allocator for TalcCell<S, B>
Source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Source§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
ptr. Read moreSource§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocate, but also ensures that the returned memory is zero-initialized. Read moreSource§unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
grow, but also ensures that the new contents are set to zero before being
returned. Read moreSource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
Source§impl<S: Source, B: Binning> GlobalAlloc for TalcCell<S, B>
impl<S: Source, B: Binning> GlobalAlloc for TalcCell<S, B>
Source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout. Read more