Merge pull request #235 from japaric/well-aligned-dangling-pointer

CAS Pool: make dangling Ptr well aligned
This commit is contained in:
Emil Fresk 2021-08-26 15:42:49 +02:00 committed by GitHub
commit d5e092575b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -177,7 +177,8 @@ impl<T> Ptr<T> {
}
pub fn dangling() -> Self {
unsafe { Self::from_parts(initial_tag_value(), 1) }
// `anchor()` returns a well-aligned pointer so an offset of 0 will also produce a well-aligned pointer
unsafe { Self::from_parts(initial_tag_value(), 0) }
}
pub unsafe fn as_ref(&self) -> &T {

View File

@ -354,6 +354,25 @@ 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);
#[repr(align(4096))]
pub struct Zst4096;
pool!(B: Zst4096);
let x = B::alloc().unwrap().init(Zst4096);
assert_eq!(0, &*x as *const Zst4096 as usize % 4096);
}
#[test]
fn destructors() {
static COUNT: AtomicUsize = AtomicUsize::new(0);