_core_rustc_static/
allocations_table.rs
1use heapdump_vmo::allocations_table_v1::{AllocationsTableWriter, ResourceKey};
6use zx::{self as zx, AsHandleRef, HandleBased};
7
8const VMO_SIZE: usize = 1 << 31;
11
12const VMO_NAME: zx::Name = zx::Name::new_lossy("heapdump-allocations");
13
14pub struct AllocationsTable {
16 vmo: zx::Vmo,
17 writer: AllocationsTableWriter,
18}
19
20impl Default for AllocationsTable {
21 fn default() -> AllocationsTable {
22 let vmo = zx::Vmo::create(VMO_SIZE as u64).expect("failed to create allocations VMO");
23 vmo.set_name(&VMO_NAME).expect("failed to set VMO name");
24
25 let writer = AllocationsTableWriter::new(&vmo).expect("failed to create writer");
26 AllocationsTable { vmo, writer }
27 }
28}
29
30impl AllocationsTable {
31 pub fn share_vmo(&self) -> zx::Vmo {
33 self.vmo.duplicate_handle(zx::Rights::SAME_RIGHTS).expect("failed to share allocations VMO")
34 }
35
36 pub fn snapshot_vmo(&self) -> zx::Vmo {
38 self.vmo
39 .create_child(
40 zx::VmoChildOptions::SNAPSHOT | zx::VmoChildOptions::NO_WRITE,
41 0,
42 VMO_SIZE as u64,
43 )
44 .expect("failed to snapshot allocations VMO")
45 }
46
47 pub fn try_record_allocation(
48 &mut self,
49 address: u64,
50 size: u64,
51 thread_info_key: ResourceKey,
52 stack_trace_key: ResourceKey,
53 timestamp: zx::MonotonicInstant,
54 ) -> bool {
55 self.writer
56 .insert_allocation(address, size, thread_info_key, stack_trace_key, timestamp)
57 .expect("out of space")
58 }
59
60 pub fn update_allocation(
61 &mut self,
62 address: u64,
63 size: u64,
64 thread_info_key: ResourceKey,
65 stack_trace_key: ResourceKey,
66 timestamp: zx::MonotonicInstant,
67 ) -> u64 {
68 if let Some(old_size) = self
69 .writer
70 .replace_allocation(address, size, thread_info_key, stack_trace_key, timestamp)
71 .expect("out of space")
72 {
73 old_size
74 } else {
75 panic!("Block 0x{:x} was not allocated", address);
76 }
77 }
78
79 pub fn forget_allocation(&mut self, address: u64) -> u64 {
80 if let Some(size) = self.writer.erase_allocation(address) {
81 size
82 } else {
83 panic!("Block 0x{:x} was not allocated", address);
84 }
85 }
86}