allocator_tracer/
event.rs

1use std::time::Duration;
2
3
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[cfg_attr(feature = "serde", serde(tag="type", rename_all="camelCase"))]
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum AllocationEvent {
8    Alloc {
9        size: usize,
10        align: usize,
11        time: Duration,
12        ptr: usize
13    },
14    Dealloc {
15        size: usize,
16        align: usize,
17        time: Duration,
18        ptr: usize
19    },
20    Realloc {
21        size: usize,
22        align: usize,
23        time: Duration,
24        ptr: usize,
25        new_size: usize,
26        new_ptr: usize,
27    }
28}
29
30impl AllocationEvent {
31    #[inline]
32    pub fn time(&self) -> Duration {
33        match self {
34            AllocationEvent::Alloc { time, .. } => *time,
35            AllocationEvent::Dealloc { time, .. } => *time,
36            AllocationEvent::Realloc { time, .. } => *time,
37        }
38    }
39
40    pub fn allocated_bytes(&self) -> isize {
41        match self {
42            AllocationEvent::Alloc { size, .. } => *size as isize,
43            AllocationEvent::Dealloc { size, .. } => -(*size as isize),
44            AllocationEvent::Realloc { size, new_size, .. } => *new_size as isize - *size as isize,
45        }
46    }
47
48    pub fn is_alloc(&self) -> bool {
49        matches!(self, AllocationEvent::Alloc { .. })
50    }
51
52    pub fn is_dealloc(&self) -> bool {
53        matches!(self, AllocationEvent::Dealloc { .. })
54    }
55
56    pub fn is_realloc(&self) -> bool {
57        matches!(self, AllocationEvent::Realloc { .. })
58    }
59}