Skip to content

Commit d5e0925

Browse files
authored
Merge pull request #235 from japaric/well-aligned-dangling-pointer
CAS Pool: make dangling Ptr well aligned
2 parents 9563b35 + 46d1361 commit d5e0925

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/pool/cas.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ impl<T> Ptr<T> {
177177
}
178178

179179
pub fn dangling() -> Self {
180-
unsafe { Self::from_parts(initial_tag_value(), 1) }
180+
// `anchor()` returns a well-aligned pointer so an offset of 0 will also produce a well-aligned pointer
181+
unsafe { Self::from_parts(initial_tag_value(), 0) }
181182
}
182183

183184
pub unsafe fn as_ref(&self) -> &T {

src/pool/singleton.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,25 @@ mod tests {
354354
assert_eq!(*A::alloc().unwrap().init(1), 1);
355355
}
356356

357+
#[test]
358+
fn boxed_zst_is_well_aligned() {
359+
#[repr(align(2))]
360+
pub struct Zst2;
361+
362+
pool!(A: Zst2);
363+
364+
let x = A::alloc().unwrap().init(Zst2);
365+
assert_eq!(0, &*x as *const Zst2 as usize % 2);
366+
367+
#[repr(align(4096))]
368+
pub struct Zst4096;
369+
370+
pool!(B: Zst4096);
371+
372+
let x = B::alloc().unwrap().init(Zst4096);
373+
assert_eq!(0, &*x as *const Zst4096 as usize % 4096);
374+
}
375+
357376
#[test]
358377
fn destructors() {
359378
static COUNT: AtomicUsize = AtomicUsize::new(0);

0 commit comments

Comments
 (0)