mmtk/util/
opaque_pointer.rs

1use crate::util::Address;
2use libc::c_void;
3
4/// OpaquePointer represents pointers that MMTk needs to know about but will not deferefence it.
5/// For example, a pointer to the thread or the thread local storage is an opaque pointer for MMTK.
6/// The type does not provide any method for dereferencing.
7#[repr(transparent)]
8#[derive(Copy, Clone, Debug, Eq, PartialEq)]
9pub struct OpaquePointer(*mut c_void);
10
11// We never really dereference an opaque pointer in mmtk-core.
12unsafe impl Sync for OpaquePointer {}
13unsafe impl Send for OpaquePointer {}
14
15impl Default for OpaquePointer {
16    fn default() -> Self {
17        Self::UNINITIALIZED
18    }
19}
20
21impl OpaquePointer {
22    /// Represents an uninitialized value for [`OpaquePointer`].
23    pub const UNINITIALIZED: Self = Self(std::ptr::null_mut::<c_void>());
24
25    /// Cast an [`Address`] type to an [`OpaquePointer`].
26    pub fn from_address(addr: Address) -> Self {
27        OpaquePointer(addr.to_mut_ptr::<c_void>())
28    }
29
30    /// Cast the opaque pointer to an [`Address`] type.
31    pub fn to_address(self) -> Address {
32        Address::from_mut_ptr(self.0)
33    }
34
35    /// Is this opaque pointer null?
36    pub fn is_null(self) -> bool {
37        self.0.is_null()
38    }
39}
40
41/// A VMThread is an opaque pointer that can uniquely identify a thread in the VM.
42/// A VM binding may use thread pointers or thread IDs as VMThreads. MMTk does not make any assumption on this.
43/// This is used as arguments in the VM->MMTk APIs, and MMTk may store it and pass it back through the MMTk->VM traits,
44/// so the VM knows the context.
45/// A VMThread may be a VMMutatorThread, a VMWorkerThread, or any VMThread.
46#[repr(transparent)]
47#[derive(Copy, Clone, Debug, Eq, PartialEq)]
48pub struct VMThread(pub OpaquePointer);
49
50impl VMThread {
51    /// Represents an uninitialized value for [`VMThread`].
52    pub const UNINITIALIZED: Self = Self(OpaquePointer::UNINITIALIZED);
53}
54
55/// A VMMutatorThread is a VMThread that associates with a [`crate::plan::Mutator`].
56/// When a VMMutatorThread is used as an argument or a field of a type, it generally means
57/// the function or the functions for the type is executed in the context of the mutator thread.
58#[repr(transparent)]
59#[derive(Copy, Clone, Debug, Eq, PartialEq)]
60pub struct VMMutatorThread(pub VMThread);
61
62/// A VMWorkerThread is a VMThread that is associates with a [`crate::scheduler::GCWorker`].
63/// When a VMWorkerThread is used as an argument or a field of a type, it generally means
64/// the function or the functions for the type is executed in the context of the mutator thread.
65#[repr(transparent)]
66#[derive(Copy, Clone, Debug, Eq, PartialEq)]
67pub struct VMWorkerThread(pub VMThread);