pub struct CountingAlloc<A, T = Untagged> { /* private fields */ }Expand description
A global allocator around inner that counts what is asked of it: the
bytes live, their peak, and the calls to alloc, alloc_zeroed and
realloc. It counts each Layout’s size, never the inner
allocator’s own overhead.
Make it a static, with #[global_allocator] to count a whole
program, or called directly as a test does. A program with
shuyi-app or shuyi-cli already has one (the global module says
which).
use shuyi_alloc_count::CountingAlloc;
use std::alloc::System;
static HEAP: CountingAlloc<System> = CountingAlloc::new(System);
assert_eq!(HEAP.live_bytes(), 0);CountingAlloc::new counts the whole; CountingAlloc::with_tags
also counts by memory tag (tags), with a header in front of each
block (Tagged).
On a target with atomics each count is one atomic read-modify-write, so any thread may allocate; on one without (an old ARM console) the counts are plain cells, and the target has one thread.
Implementations§
Source§impl<A> CountingAlloc<A, Tagged>
impl<A> CountingAlloc<A, Tagged>
Source§impl<A> CountingAlloc<A>
impl<A> CountingAlloc<A>
Source§impl<A, T> CountingAlloc<A, T>
impl<A, T> CountingAlloc<A, T>
Sourcepub fn live_bytes(&self) -> usize
pub fn live_bytes(&self) -> usize
The bytes allocated and not yet freed.
Sourcepub fn peak_bytes(&self) -> usize
pub fn peak_bytes(&self) -> usize
The most bytes live at once since the start or the last
reset_peak.
Sourcepub fn reset_peak(&self)
pub fn reset_peak(&self)
Starts the peak again from the bytes live now. A tag’s peak
(TagCounts::peak_bytes) stays.
Sourcepub fn take_allocs(&self) -> u32
pub fn take_allocs(&self) -> u32
The calls to alloc, alloc_zeroed and realloc since the last
take (or the start), read and reset in one step; at most
u32::MAX.
Sourcepub fn counts(&self) -> Counts
pub fn counts(&self) -> Counts
All three: the live bytes, their peak, and the calls, taken
(take_allocs).
Trait Implementations§
Source§impl<A: GlobalAlloc> GlobalAlloc for CountingAlloc<A, Tagged>
impl<A: GlobalAlloc> GlobalAlloc for CountingAlloc<A, Tagged>
Source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout. Read moreSource§unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8
alloc, but also ensures that the contents
are set to zero before being returned. Read moreSource§impl<A: GlobalAlloc> GlobalAlloc for CountingAlloc<A>
impl<A: GlobalAlloc> GlobalAlloc for CountingAlloc<A>
Source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout. Read moreSource§unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8
alloc, but also ensures that the contents
are set to zero before being returned. Read more