Skip to content

Commit

Permalink
Simplify RetiredPtrs::reclaim
Browse files Browse the repository at this point in the history
  • Loading branch information
skogseth committed Sep 27, 2023
1 parent ecda041 commit 1c13c5a
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,7 @@ impl<T> RetiredPtrs<T> {
}

pub fn reclaim(&mut self, hzrd_ptrs: &HzrdPtrs) {
let mut still_active = Vec::new();
while let Some(retired_ptr) = self.0.pop() {
if hzrd_ptrs.contains(retired_ptr.as_ptr() as usize) {
still_active.push(retired_ptr);
continue;
}
}

self.0 = still_active;
self.0.retain(|p| hzrd_ptrs.contains(p.as_ptr() as usize));
}

pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -268,4 +260,27 @@ mod tests {
unsafe { hzrd_ptr.free() };
unsafe { hzrd_ptr.store(&mut value) };
}

#[test]
fn retirement() {
let value = Box::into_raw(Box::new([1,2,3]));
let value = unsafe { NonNull::new_unchecked(value) };

let mut hzrd_ptrs = HzrdPtrs::new();
let mut retired = RetiredPtrs::new();

let hzrd_ptr = unsafe { hzrd_ptrs.get().as_ref() };
unsafe { hzrd_ptr.store(value.as_ptr()) };

retired.add(RetiredPtr::new(value));
assert_eq!(retired.len(), 1);

retired.reclaim(&hzrd_ptrs);
assert_eq!(retired.len(), 1);

unsafe { hzrd_ptr.clear() };
retired.reclaim(&hzrd_ptrs);
assert_eq!(retired.len(), 0);
assert!(retired.is_empty());
}
}

0 comments on commit 1c13c5a

Please sign in to comment.