CAS Pool: make dangling Ptr well aligned

the old logic didn't consider the pointee's alignment when creating a dangling pointer

dangling pointers are used in pools of ZST (Zero Sized Types). the old logic resulted in these Boxed
ZST not being well aligned. the test added in this PR was failing
This commit is contained in:
Jorge Aparicio 2021-08-26 13:14:34 +02:00
parent 9563b3546c
commit 959fc9b683
2 changed files with 12 additions and 1 deletions

View File

@ -177,7 +177,7 @@ impl<T> Ptr<T> {
}
pub fn dangling() -> Self {
unsafe { Self::from_parts(initial_tag_value(), 1) }
unsafe { Self::from_parts(initial_tag_value(), core::mem::align_of::<T>() as i32) }
}
pub unsafe fn as_ref(&self) -> &T {

View File

@ -354,6 +354,17 @@ mod tests {
assert_eq!(*A::alloc().unwrap().init(1), 1);
}
#[test]
fn boxed_zst_is_well_aligned() {
#[repr(align(2))]
pub struct Zst2;
pool!(A: Zst2);
let x = A::alloc().unwrap().init(Zst2);
assert_eq!(0, &*x as *const Zst2 as usize % 2);
}
#[test]
fn destructors() {
static COUNT: AtomicUsize = AtomicUsize::new(0);