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 a raw mutable pointer to an [`OpaquePointer`].
31 pub fn from_mut_ptr<T>(ptr: *mut T) -> Self {
32 OpaquePointer(ptr as *mut c_void)
33 }
34
35 /// Cast the opaque pointer to a raw mutable pointer.
36 pub fn as_mut_ptr<T>(self) -> *mut T {
37 self.0 as *mut T
38 }
39
40 /// Cast the opaque pointer to an [`Address`] type.
41 pub fn to_address(self) -> Address {
42 Address::from_mut_ptr(self.0)
43 }
44
45 /// Is this opaque pointer null?
46 pub fn is_null(self) -> bool {
47 self.0.is_null()
48 }
49}
50
51/// A VMThread is an opaque pointer that can uniquely identify a thread in the VM.
52/// A VM binding may use thread pointers or thread IDs as VMThreads. MMTk does not make any assumption on this.
53/// This is used as arguments in the VM->MMTk APIs, and MMTk may store it and pass it back through the MMTk->VM traits,
54/// so the VM knows the context.
55/// A VMThread may be a VMMutatorThread, a VMWorkerThread, or any VMThread.
56#[repr(transparent)]
57#[derive(Copy, Clone, Debug, Eq, PartialEq)]
58pub struct VMThread(pub OpaquePointer);
59
60impl VMThread {
61 /// Represents an uninitialized value for [`VMThread`].
62 pub const UNINITIALIZED: Self = Self(OpaquePointer::UNINITIALIZED);
63}
64
65/// A VMMutatorThread is a VMThread that associates with a [`crate::plan::Mutator`].
66/// When a VMMutatorThread is used as an argument or a field of a type, it generally means
67/// the function or the functions for the type is executed in the context of the mutator thread.
68#[repr(transparent)]
69#[derive(Copy, Clone, Debug, Eq, PartialEq)]
70pub struct VMMutatorThread(pub VMThread);
71
72/// A VMWorkerThread is a VMThread that is associates with a [`crate::scheduler::GCWorker`].
73/// When a VMWorkerThread is used as an argument or a field of a type, it generally means
74/// the function or the functions for the type is executed in the context of the mutator thread.
75#[repr(transparent)]
76#[derive(Copy, Clone, Debug, Eq, PartialEq)]
77pub struct VMWorkerThread(pub VMThread);