Skip to main content

Statistics

Struct Statistics 

Source
pub struct Statistics<I> { /* private fields */ }
Expand description

Wrapper that records allocation activity in AllocStats.

Send + Sync if I: Send + Sync. Atomic counters are themselves Sync.

§Accounting invariant

Statistics records bytes by the layout the outer caller passed in. Wrappers below Statistics that pass the layout through unchanged (PoisonOnFree, Quarantine, Watermark) preserve this — the bytes counted equal the bytes the caller requested. Wrappers below Statistics that inflate the inner layout (Canary, CacheJitter, HugePageAligned, SplitMetadata) consume more inner-allocator bytes than the counter reports; the counter therefore reflects “bytes the user asked for”, not “bytes the underlying region holds”. If you need the latter, wrap Statistics INSIDE the layout-inflating wrapper: Canary<Statistics<Slab<T>>> counts what Slab actually carved. The recommended position for Statistics (Statistics<PoisonOnFree<Slab>>) treats “bytes the user asked for” as the right number to surface to operators; flip the nesting only if you specifically need physical accounting.

§API-misuse compile-failures (pinned)

Statistics<I> inherits the Sync property of its inner allocator. Wrapping a !Sync allocator (such as Slab, whose UnsafeCell free list head makes it !Sync by design) does not silently upgrade it to Sync. Calling stats() on a shared reference across threads when the inner is !Sync is therefore rejected at compile time:

// FAILS TO COMPILE: `Slab` is `!Sync` (UnsafeCell on the freelist
// head), so `Statistics<Slab<...>>` is also `!Sync`, and
// `assert_sync` rejects it.
use forge_alloc::InlineBacked;
use forge_alloc::Statistics;
use forge_alloc::Slab;
fn assert_sync<T: Sync>() {}
assert_sync::<Statistics<Slab<u64, InlineBacked<512>>>>();

Implementations§

Source§

impl<I> Statistics<I>

Source

pub const fn new(inner: I) -> Self

Wrap.

Source

pub fn stats(&self) -> &AllocStats

Borrow the counters. Read-only access — counters are updated by the allocator’s own methods.

Source

pub fn inner(&self) -> &I

Borrow the inner allocator.

Trait Implementations§

Source§

impl<I: Allocator> Allocator for Statistics<I>

Source§

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

Allocate a block satisfying layout. The returned slice’s length is at least layout.size() but may be larger.
Source§

unsafe fn usable_size( &self, ptr: NonNull<u8>, layout: NonZeroLayout, ) -> Option<usize>

Usable size of an existing allocation, if the allocator can report it. Defaults to None — implementors that track usable size override. Read more
Source§

fn capacity_bytes(&self) -> Option<usize>

Total bytes this allocator can issue, if bounded. None for unbounded allocators like System. Used by Watermark to compute thresholds.
Source§

fn corruption_events(&self) -> u64

Detected freelist / metadata corruption events observed by this allocator since construction. Read more
Source§

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

Allocate a zero-initialized block.
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Grow an allocation in place if possible, otherwise allocate-copy-free. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old: NonZeroLayout, new: NonZeroLayout, ) -> Result<NonNull<[u8]>, AllocError>

Shrink an allocation in place if possible, otherwise allocate-copy-free. Read more
Source§

fn reset(&mut self) -> Result<(), AllocError>

Reclaim everything previously allocated. Default impl returns AllocError — only arena-style allocators implement a meaningful reset. Read more
Source§

impl<I: Allocator> Deallocator for Statistics<I>

Source§

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

Release a previously allocated block. Read more
Source§

impl<I: FixedRange> FixedRange for Statistics<I>

Source§

fn commit(&self, offset: usize, len: usize) -> Result<(), AllocError>

Pass-through forward so a commit-aware consumer reaches the inner backing when this wrapper sits over a lazy_commit MmapBacked.

Source§

fn base(&self) -> NonNull<u8>

First byte of the owned address range. Read more
Source§

fn size(&self) -> usize

Length in bytes of the owned address range. Read more
Source§

fn contains(&self, ptr: NonNull<u8>) -> bool

Whether ptr lies within [base, base + size). Read more

Auto Trait Implementations§

§

impl<I> !Freeze for Statistics<I>

§

impl<I> RefUnwindSafe for Statistics<I>
where I: RefUnwindSafe,

§

impl<I> Send for Statistics<I>
where I: Send,

§

impl<I> Sync for Statistics<I>
where I: Sync,

§

impl<I> Unpin for Statistics<I>
where I: Unpin,

§

impl<I> UnsafeUnpin for Statistics<I>
where I: UnsafeUnpin,

§

impl<I> UnwindSafe for Statistics<I>
where I: 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 = 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.