mmtk/util/analysis/
obj_num.rs

1use crate::util::analysis::RtAnalysis;
2use crate::util::statistics::counter::EventCounter;
3use crate::vm::VMBinding;
4use std::sync::{Arc, Mutex};
5
6/**
7 * Simple analysis routine that counts the number of objects allocated
8 */
9pub struct ObjectCounter {
10    running: bool,
11    counter: Arc<Mutex<EventCounter>>,
12}
13
14impl ObjectCounter {
15    pub fn new(running: bool, counter: Arc<Mutex<EventCounter>>) -> Self {
16        Self { running, counter }
17    }
18}
19
20impl<VM: VMBinding> RtAnalysis<VM> for ObjectCounter {
21    fn alloc_hook(&mut self, _size: usize, _align: usize, _offset: usize) {
22        if self.running {
23            // The analysis routine simply updates the counter when the allocation hook is called
24            self.counter.lock().unwrap().inc();
25        }
26    }
27
28    fn set_running(&mut self, running: bool) {
29        self.running = running;
30    }
31}