Skip to content

Commit

Permalink
require NonNull arg to retire
Browse files Browse the repository at this point in the history
  • Loading branch information
wyang5 committed Oct 10, 2024
1 parent 476fa0a commit 4534edd
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/smr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ impl<'a> ThreadContext<'a> {
None
}

#[allow(clippy::missing_safety_doc)]
pub unsafe fn retire(&self, ptr: *mut u8, f: fn(*mut u8), birth_era: u64) {
pub fn retire(&self, ptr: NonNull<u8>, f: fn(NonNull<u8>), birth_era: u64) {
if self.cleanup_freq == 0 {
panic!("cannot retire using this context: cleanup_freq is 0.")
}
Expand Down Expand Up @@ -356,8 +355,8 @@ impl<'a, 'b: 'a, T> Drop for Guard<'a, 'b, T> {
}

struct RetiredFn {
ptr: *mut u8,
f: fn(*mut u8),
ptr: NonNull<u8>,
f: fn(NonNull<u8>),
span: (u64, u64),
}

Expand All @@ -370,6 +369,7 @@ impl Drop for RetiredFn {
#[cfg(test)]
mod tests {
use std::mem::zeroed;
use std::ptr::NonNull;
use std::sync::atomic::Ordering::{Relaxed, SeqCst};
use std::sync::atomic::{AtomicPtr, AtomicUsize};
use std::thread;
Expand Down Expand Up @@ -410,15 +410,20 @@ mod tests {
birth_era: r.load_era(),
};
let swapped = x.swap(Box::into_raw(Box::new(obj)), SeqCst);
if !swapped.is_null() {
if let Some(to_retire) = NonNull::<u8>::new(swapped as *mut u8) {
unsafe {
ctx.retire(
swapped as *mut u8,
to_retire,
dealloc_boxed_ptr::<Obj<usize>>,
(*swapped).birth_era,
);
}
}
if !swapped.is_null() {
unsafe {

}
}
r.increment_era();
}
});
Expand All @@ -435,9 +440,9 @@ mod tests {
}
}

fn dealloc_boxed_ptr<T>(p: *mut u8) {
fn dealloc_boxed_ptr<T>(p: NonNull<u8>) {
unsafe {
drop(Box::from_raw(p as *mut T));
drop(Box::from_raw(p.as_ptr() as *mut T));
}
}

Expand Down

0 comments on commit 4534edd

Please sign in to comment.