Skip to content

Commit ad22768

Browse files
committed
replace int_to_ptr_with_provenance by map_addr-style function
1 parent f92b4a7 commit ad22768

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

crossbeam-epoch/src/atomic.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,26 @@ fn ensure_aligned<T: ?Sized + Pointable>(raw: *mut ()) {
6363
/// `tag` is truncated to fit into the unused bits of the pointer to `T`.
6464
#[inline]
6565
fn compose_tag<T: ?Sized + Pointable>(ptr: *mut (), tag: usize) -> *mut () {
66-
int_to_ptr_with_provenance(
67-
(ptr as usize & !low_bits::<T>()) | (tag & low_bits::<T>()),
68-
ptr,
69-
)
66+
map_addr(ptr, |a| (a & !low_bits::<T>()) | (tag & low_bits::<T>()))
7067
}
7168

7269
/// Decomposes a tagged pointer `data` into the pointer and the tag.
7370
#[inline]
7471
fn decompose_tag<T: ?Sized + Pointable>(ptr: *mut ()) -> (*mut (), usize) {
7572
(
76-
int_to_ptr_with_provenance(ptr as usize & !low_bits::<T>(), ptr),
73+
map_addr(ptr, |a| a & !low_bits::<T>()),
7774
ptr as usize & low_bits::<T>(),
7875
)
7976
}
8077

81-
// HACK: https://github.com/rust-lang/miri/issues/1866#issuecomment-985802751
78+
// FIXME: This is exactly <https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.map_addr-1>,
79+
// which we cannot use yet due to the MSRV.
8280
#[inline]
83-
fn int_to_ptr_with_provenance<T>(addr: usize, prov: *mut T) -> *mut T {
84-
let ptr = prov.cast::<u8>();
85-
ptr.wrapping_add(addr.wrapping_sub(ptr as usize)).cast()
81+
fn map_addr<T>(ptr: *mut T, f: impl FnOnce(usize) -> usize) -> *mut T {
82+
let new_addr = f(ptr as usize);
83+
ptr.cast::<u8>()
84+
.wrapping_add(new_addr.wrapping_sub(ptr as usize))
85+
.cast::<T>()
8686
}
8787

8888
/// Types that are pointed to by a single word.
@@ -640,9 +640,7 @@ impl<T: ?Sized + Pointable> Atomic<T> {
640640
let fetch_order = strongest_failure_ordering(order);
641641
Shared::from_ptr(
642642
self.data
643-
.fetch_update(order, fetch_order, |x| {
644-
Some(int_to_ptr_with_provenance(x as usize & val, x))
645-
})
643+
.fetch_update(order, fetch_order, |x| Some(map_addr(x, |a| a & val)))
646644
.unwrap(),
647645
)
648646
}
@@ -687,9 +685,7 @@ impl<T: ?Sized + Pointable> Atomic<T> {
687685
let fetch_order = strongest_failure_ordering(order);
688686
Shared::from_ptr(
689687
self.data
690-
.fetch_update(order, fetch_order, |x| {
691-
Some(int_to_ptr_with_provenance(x as usize | val, x))
692-
})
688+
.fetch_update(order, fetch_order, |x| Some(map_addr(x, |a| a | val)))
693689
.unwrap(),
694690
)
695691
}
@@ -734,9 +730,7 @@ impl<T: ?Sized + Pointable> Atomic<T> {
734730
let fetch_order = strongest_failure_ordering(order);
735731
Shared::from_ptr(
736732
self.data
737-
.fetch_update(order, fetch_order, |x| {
738-
Some(int_to_ptr_with_provenance(x as usize ^ val, x))
739-
})
733+
.fetch_update(order, fetch_order, |x| Some(map_addr(x, |a| a ^ val)))
740734
.unwrap(),
741735
)
742736
}

0 commit comments

Comments
 (0)