[][src]Struct alloc_compose::Proxy

pub struct Proxy<A, C> {
    pub alloc: A,
    pub callbacks: C,
}

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: Acallbacks: C

Trait Implementations

impl<A: AllocRef, C: CallbackRef> AllocRef for Proxy<A, C>[src]

impl<A: AllocateAll, C: CallbackRef> AllocateAll for Proxy<A, C>[src]

impl<A: Clone, C: Clone> Clone for Proxy<A, C>[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]

impl<A: Ord, C: Ord> Ord for Proxy<A, C>[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]

impl<A: PartialOrd, C: PartialOrd> PartialOrd<Proxy<A, C>> for Proxy<A, C>[src]

impl<A: ReallocateInPlace, C: CallbackRef> ReallocateInPlace for Proxy<A, C>[src]

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

impl<A, C> Sync for Proxy<A, C> where
    A: Sync,
    C: Sync

impl<A, C> Unpin for Proxy<A, C> where
    A: Unpin,
    C: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.