[−][src]Struct alloc_compose::Proxy
Calls the provided callbacks when invoking methods on AllocRef
.
A typical use case for a Proxy
allocator is collecting statistics. alloc-compose
provides
different implementations for CallbackRef
.
Examples
#![feature(allocator_api, slice_ptr_get)] use alloc_compose::{stats, CallbackRef, Proxy}; use std::alloc::{AllocRef, Layout, System}; let counter = stats::Counter::default(); let mut alloc = Proxy { alloc: System, callbacks: counter.by_ref(), }; unsafe { let memory = alloc.alloc(Layout::new::<u32>())?; alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<u32>()); } assert_eq!(counter.num_allocs(), 1); assert_eq!(counter.num_deallocs(), 1);
If more information is needed, one can either implement CallbackRef
itself or use a more
fine-grained callback:
use alloc_compose::{ region::Region, stats::{AllocInitFilter, ResultFilter}, }; use core::mem::MaybeUninit; let counter = stats::FilteredCounter::default(); let mut data = [MaybeUninit::new(0); 32]; let mut alloc = Proxy { alloc: Region::new(&mut data), callbacks: counter.by_ref(), }; unsafe { let memory = alloc.alloc(Layout::new::<u32>())?; alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<u32>()); alloc.alloc_zeroed(Layout::new::<[u32; 64]>()).unwrap_err(); } assert_eq!(counter.num_allocates(), 2); assert_eq!( counter.num_allocates_filter(AllocInitFilter::None, ResultFilter::Ok), 1 ); assert_eq!( counter.num_allocates_filter(AllocInitFilter::Zeroed, ResultFilter::Err), 1 );
Fields
alloc: A
callbacks: C
Trait Implementations
impl<A: AllocRef, C: CallbackRef> AllocRef for Proxy<A, C>
[src]
pub fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
[src]
pub fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
[src]
pub unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout)
[src]
pub unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
[src]
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
pub unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
[src]
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
pub unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
[src]
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
pub fn by_ref(&self) -> &Self
[src]
impl<A: AllocateAll, C: CallbackRef> AllocateAll for Proxy<A, C>
[src]
pub fn allocate_all(&self) -> Result<NonNull<[u8]>, AllocError>
[src]
pub fn allocate_all_zeroed(&self) -> Result<NonNull<[u8]>, AllocError>
[src]
pub fn deallocate_all(&self)
[src]
pub fn capacity(&self) -> usize
[src]
pub fn capacity_left(&self) -> usize
[src]
pub fn is_empty(&self) -> bool
[src]
pub fn is_full(&self) -> bool
[src]
impl<A: Clone, C: Clone> Clone for Proxy<A, C>
[src]
pub fn clone(&self) -> Proxy<A, C>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<A: Copy, C: Copy> Copy for Proxy<A, C>
[src]
impl<A: Debug, C: Debug> Debug for Proxy<A, C>
[src]
impl<A: Eq, C: Eq> Eq for Proxy<A, C>
[src]
impl<A: Hash, C: Hash> Hash for Proxy<A, C>
[src]
pub fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<A: Ord, C: Ord> Ord for Proxy<A, C>
[src]
pub fn cmp(&self, other: &Proxy<A, C>) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<A: Owns, C: CallbackRef> Owns for Proxy<A, C>
[src]
impl<A: PartialEq, C: PartialEq> PartialEq<Proxy<A, C>> for Proxy<A, C>
[src]
pub fn eq(&self, other: &Proxy<A, C>) -> bool
[src]
pub fn ne(&self, other: &Proxy<A, C>) -> bool
[src]
impl<A: PartialOrd, C: PartialOrd> PartialOrd<Proxy<A, C>> for Proxy<A, C>
[src]
pub fn partial_cmp(&self, other: &Proxy<A, C>) -> Option<Ordering>
[src]
pub fn lt(&self, other: &Proxy<A, C>) -> bool
[src]
pub fn le(&self, other: &Proxy<A, C>) -> bool
[src]
pub fn gt(&self, other: &Proxy<A, C>) -> bool
[src]
pub fn ge(&self, other: &Proxy<A, C>) -> bool
[src]
impl<A: ReallocateInPlace, C: CallbackRef> ReallocateInPlace for Proxy<A, C>
[src]
pub unsafe fn grow_in_place(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<usize, AllocError>
[src]
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<usize, AllocError>
pub unsafe fn grow_in_place_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<usize, AllocError>
[src]
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<usize, AllocError>
pub unsafe fn shrink_in_place(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<usize, AllocError>
[src]
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<usize, AllocError>
impl<A, C> StructuralEq for Proxy<A, C>
[src]
impl<A, C> StructuralPartialEq for Proxy<A, C>
[src]
Auto Trait Implementations
impl<A, C> Send for Proxy<A, C> where
A: Send,
C: Send,
A: Send,
C: Send,
impl<A, C> Sync for Proxy<A, C> where
A: Sync,
C: Sync,
A: Sync,
C: Sync,
impl<A, C> Unpin for Proxy<A, C> where
A: Unpin,
C: Unpin,
A: Unpin,
C: Unpin,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,