alloc_cat 1.1.1

a simple allocator for small-to-tiny Wasm projects in rust
Documentation
use core::fmt;

use crate::metrics::SizeInfo;


#[derive(Debug)]
pub struct SimpleAllocatorStats {
    pub regions: usize,
    pub total_words: usize,
    pub allocated_words: usize,
    pub unused_words: usize,     // free contiguous space unallocated
    pub fragment_words: usize,   // free words in bits & pieces
    pub fragments: usize,
}

impl fmt::Display for SimpleAllocatorStats {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f, "SimpleAllocatorStats(regions={}, total_words={}, allocated_words={}, unused_words={}, \
            fragments={}, fragment_words={})",
            self.regions, self.total_words, self.allocated_words, self.unused_words,
            self.fragments, self.fragment_words,
        )
    }
}

impl SimpleAllocatorStats {
    pub(crate) fn add(&mut self, stats: SimpleAllocatorStats) {
        self.regions += stats.regions;
        self.total_words += stats.total_words;
        self.allocated_words += stats.allocated_words;
        self.unused_words += stats.unused_words;
        self.fragment_words += stats.fragment_words;
        self.fragments += stats.fragments;
    }
}


#[derive(Clone, Debug)]
pub struct AllocatorStats {
    // bytes:
    pub in_use: usize,
    pub high_water: usize,
    pub trip_high_water: usize,

    // track count of each allocation size:
    pub size_table: [SizeInfo; 257],
    // track count of each alignment request:
    pub align_table: [usize; 16],

    // these metrics only exist in wasm:
    pub regions: usize,
    pub total_words: usize,
    pub allocated_words: usize,
    pub unused_words: usize,        // free contiguous space unallocated
    pub fragment_words: usize,      // free words in bits & pieces
    pub fragments: usize,
    pub total_large_pages: usize,   // pages allocated to >32KB blocks
    pub unused_large_pages: usize,
    pub allocated_large_pages_words: usize,
    pub heap_start: usize,          // address of start of heap
}

impl fmt::Display for AllocatorStats {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f, "AllocatorStats(regions={}, total_words={}, allocated_words={}, unused_words={}, \
            fragments={}, fragment_words={}, total_large={}, unused_large={}, large_words={}, heap_start={})",
            self.regions, self.total_words, self.allocated_words, self.unused_words,
            self.fragments, self.fragment_words, self.total_large_pages, self.unused_large_pages,
            self.allocated_large_pages_words, self.heap_start,
        )
    }
}

impl Default for AllocatorStats {
    fn default() -> AllocatorStats {
        AllocatorStats {
            in_use: 0,
            high_water: 0,
            trip_high_water: 0,
            size_table: [SizeInfo::default(); 257],
            align_table: [0; 16],
            regions: 0,
            total_words: 0,
            allocated_words: 0,
            unused_words: 0,
            fragment_words: 0,
            fragments: 0,
            total_large_pages: 0,
            unused_large_pages: 0,
            allocated_large_pages_words: 0,
            heap_start: 0,
        }
    }
}

impl AllocatorStats {
    pub(crate) fn from_simple_allocator_stats(stats: SimpleAllocatorStats) -> AllocatorStats {
        AllocatorStats {
            regions: stats.regions,
            total_words: stats.total_words,
            allocated_words: stats.allocated_words,
            unused_words: stats.unused_words,
            fragment_words: stats.fragment_words,
            fragments: stats.fragments,
            ..Default::default()
        }
    }
}


pub trait Statsable {
    fn get_stats(&self) -> AllocatorStats;
    fn reset_trip(&self);
}