monitor/mon/space/
handle.rs

1use std::sync::Arc;
2
3use monitor_api::MappedObjectAddrs;
4use twizzler_abi::object::{MAX_SIZE, NULLPAGE_SIZE};
5use twizzler_rt_abi::object::ObjectHandle;
6
7use super::MapInfo;
8use crate::mon::get_monitor;
9
10/// A handle for an object mapped into the address space. This handle is owning, and when dropped,
11/// the mapping is sent to the background unmapping thread.
12#[derive(Debug)]
13pub struct MapHandleInner {
14    info: MapInfo,
15    map: MappedObjectAddrs,
16}
17
18/// A shared map handle.
19pub type MapHandle = Arc<MapHandleInner>;
20
21impl MapHandleInner {
22    /// Create a new map handle.
23    pub(crate) fn new(info: MapInfo, map: MappedObjectAddrs) -> Self {
24        Self { info, map }
25    }
26
27    /// Get the mapped addresses of this handle.
28    pub fn addrs(&self) -> MappedObjectAddrs {
29        self.map
30    }
31
32    /// Get a pointer to the start address of the object.
33    pub fn monitor_data_start(&self) -> *mut u8 {
34        self.map.start as *mut u8
35    }
36
37    /// Get a pointer to the base address of the object.
38    pub fn monitor_data_base(&self) -> *mut u8 {
39        (self.map.start + NULLPAGE_SIZE) as *mut u8
40    }
41
42    pub unsafe fn object_handle(&self) -> ObjectHandle {
43        ObjectHandle::new(
44            self.info.id,
45            core::ptr::null_mut(),
46            self.map.start as *mut _,
47            self.map.meta as *mut _,
48            self.info.flags,
49            MAX_SIZE - NULLPAGE_SIZE * 2,
50        )
51    }
52}
53
54impl Drop for MapHandleInner {
55    fn drop(&mut self) {
56        // Toss this work onto a background thread.
57        let monitor = get_monitor();
58        if let Some(unmapper) = monitor.unmapper.get() {
59            unmapper.background_unmap_info(self.info);
60        }
61    }
62}