1use std::marker::PhantomData;
8#[cfg(miri)]
9use {crate::loom::sync::Mutex, std::collections::BTreeMap};
10
11pub(crate) struct PtrExposeDomain<T> {
12 #[cfg(miri)]
13 map: Mutex<BTreeMap<usize, *const T>>,
14 _phantom: PhantomData<T>,
15}
16
17unsafe impl<T> Sync for PtrExposeDomain<T> {}
19
20impl<T> PtrExposeDomain<T> {
21 pub(crate) const fn new() -> Self {
22 Self {
23 #[cfg(miri)]
24 map: Mutex::const_new(BTreeMap::new()),
25 _phantom: PhantomData,
26 }
27 }
28
29 #[inline]
30 pub(crate) fn expose_provenance(&self, ptr: *const T) -> usize {
31 #[cfg(miri)]
32 {
33 let addr: usize = unsafe { std::mem::transmute(ptr) };
36 self.map.lock().insert(addr, ptr);
37 addr
38 }
39
40 #[cfg(not(miri))]
41 {
42 ptr as usize
43 }
44 }
45
46 #[inline]
47 #[allow(clippy::wrong_self_convention)] pub(crate) fn from_exposed_addr(&self, addr: usize) -> *const T {
49 #[cfg(miri)]
50 {
51 let maybe_ptr = self.map.lock().get(&addr).copied();
52
53 unsafe { maybe_ptr.unwrap_unchecked() }
56 }
57
58 #[cfg(not(miri))]
59 {
60 addr as *const T
61 }
62 }
63
64 #[inline]
65 pub(crate) fn unexpose_provenance(&self, _ptr: *const T) {
66 #[cfg(miri)]
67 {
68 let addr: usize = unsafe { std::mem::transmute(_ptr) };
70 let maybe_ptr = self.map.lock().remove(&addr);
71
72 unsafe { maybe_ptr.unwrap_unchecked() };
75 }
76 }
77}