twizzler/
object.rs

1//! Traits and types for working with objects.
2
3use twizzler_abi::object::{MAX_SIZE, NULLPAGE_SIZE};
4
5use crate::{marker::BaseType, ptr::Ref};
6
7mod builder;
8mod fot;
9mod meta;
10mod mutable;
11mod object;
12mod tx;
13
14pub use builder::*;
15pub use fot::*;
16pub use meta::*;
17pub use mutable::MutObject;
18pub use object::Object;
19pub use twizzler_rt_abi::object::{MapFlags, ObjID, ObjectHandle};
20pub use tx::TxObject;
21
22/// Operations common to structured objects.
23pub trait TypedObject {
24    /// The base type of this object.
25    type Base: BaseType;
26
27    /// Returns a resolved reference to the object's base.
28    fn base_ref(&self) -> Ref<'_, Self::Base>;
29
30    fn base(&self) -> &Self::Base;
31}
32
33/// Operations common to all objects, with raw pointers.
34pub trait RawObject {
35    /// Get the underlying runtime handle for this object.
36    fn handle(&self) -> &ObjectHandle;
37
38    /// Get the object ID.
39    fn id(&self) -> ObjID {
40        self.handle().id()
41    }
42
43    /// Get a const pointer to the object base.
44    fn base_ptr<T>(&self) -> *const T {
45        self.lea(NULLPAGE_SIZE, size_of::<T>()).unwrap().cast()
46    }
47
48    /// Get a mut pointer to the object base.
49    fn base_mut_ptr<T>(&self) -> *mut T {
50        self.lea_mut(NULLPAGE_SIZE, size_of::<T>()).unwrap().cast()
51    }
52
53    /// Get a const pointer to the object metadata.
54    fn meta_ptr(&self) -> *const MetaInfo {
55        self.handle().meta().cast()
56    }
57
58    /// Get a mut pointer to the object metadata.
59    fn meta_mut_ptr(&self) -> *mut MetaInfo {
60        self.handle().meta().cast()
61    }
62
63    /// Get a const pointer to a given FOT entry.
64    fn fote_ptr(&self, idx: usize) -> Option<*const FotEntry> {
65        let offset: isize = (1 + idx).try_into().ok()?;
66        unsafe { Some((self.meta_ptr() as *const FotEntry).offset(-offset)) }
67    }
68
69    /// Get a mut pointer to a given FOT entry.
70    fn fote_ptr_mut(&self, idx: usize) -> Option<*mut FotEntry> {
71        let offset: isize = (1 + idx).try_into().ok()?;
72        unsafe { Some((self.meta_mut_ptr() as *mut FotEntry).offset(-offset)) }
73    }
74
75    /// Get a const pointer to given range of the object.
76    fn lea(&self, offset: usize, _len: usize) -> Option<*const u8> {
77        Some(unsafe { self.handle().start().add(offset) as *const u8 })
78    }
79
80    /// Get a mut pointer to given range of the object.
81    fn lea_mut(&self, offset: usize, _len: usize) -> Option<*mut u8> {
82        Some(unsafe { self.handle().start().add(offset) as *mut u8 })
83    }
84
85    /// If the pointer is local to this object, return the offset into the object. Otherwise, return
86    /// None.
87    fn ptr_local(&self, ptr: *const u8) -> Option<usize> {
88        if ptr.addr() >= self.handle().start().addr()
89            && ptr.addr() < self.handle().start().addr() + MAX_SIZE
90        {
91            Some(ptr.addr() - self.handle().start().addr())
92        } else {
93            None
94        }
95    }
96}
97
98impl RawObject for ObjectHandle {
99    fn handle(&self) -> &ObjectHandle {
100        self
101    }
102}