monitor/mon/space/
handle.rs

1use std::sync::Arc;
2
3use monitor_api::MappedObjectAddrs;
4use twizzler_abi::object::{ObjID, 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 fn id(&self) -> ObjID {
43        self.info.id
44    }
45
46    pub unsafe fn object_handle(&self) -> ObjectHandle {
47        ObjectHandle::new(
48            self.info.id,
49            core::ptr::null_mut(),
50            self.map.start as *mut _,
51            self.map.meta as *mut _,
52            self.info.flags,
53            MAX_SIZE - NULLPAGE_SIZE * 2,
54        )
55    }
56}
57
58impl Drop for MapHandleInner {
59    fn drop(&mut self) {
60        // Toss this work onto a background thread.
61        let monitor = get_monitor();
62        if let Some(unmapper) = monitor.unmapper.get() {
63            unmapper.background_unmap_info(self.info);
64        }
65    }
66}