Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ where
self.memo_ingredient_indices.get(ingredient_index),
)
});
std::mem::take(&mut self.deleted_entries);

self.deleted_entries.clear();
}

fn fmt_index(&self, index: crate::Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
18 changes: 9 additions & 9 deletions src/function/delete.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::ptr::NonNull;

use crossbeam_queue::SegQueue;

use super::memo::Memo;
use super::Configuration;

/// Stores the list of memos that have been deleted so they can be freed
/// once the next revision starts. See the comment on the field
/// `deleted_entries` of [`FunctionIngredient`][] for more details.
pub(super) struct DeletedEntries<C: Configuration> {
seg_queue: SegQueue<SharedBox<Memo<C::Output<'static>>>>,
memos: boxcar::Vec<SharedBox<Memo<C::Output<'static>>>>,
}

unsafe impl<T: Send> Send for SharedBox<T> {}
Expand All @@ -18,30 +16,32 @@ unsafe impl<T: Sync> Sync for SharedBox<T> {}
impl<C: Configuration> Default for DeletedEntries<C> {
fn default() -> Self {
Self {
seg_queue: Default::default(),
memos: Default::default(),
}
}
}

impl<C: Configuration> DeletedEntries<C> {
/// # Safety
///
/// The memo must be valid and safe to free when the `DeletedEntries` list is dropped.
/// The memo must be valid and safe to free when the `DeletedEntries` list is cleared or dropped.
pub(super) unsafe fn push(&self, memo: NonNull<Memo<C::Output<'_>>>) {
let memo = unsafe {
std::mem::transmute::<NonNull<Memo<C::Output<'_>>>, NonNull<Memo<C::Output<'static>>>>(
memo,
)
};

self.seg_queue.push(SharedBox(memo));
self.memos.push(SharedBox(memo));
}

/// Free all deleted memos, keeping the list available for reuse.
pub(super) fn clear(&mut self) {
self.memos.clear();
}
}

/// A wrapper around `NonNull` that frees the allocation when it is dropped.
///
/// `crossbeam::SegQueue` does not expose mutable accessors so we have to create
/// a wrapper to run code during `Drop`.
struct SharedBox<T>(NonNull<T>);

impl<T> Drop for SharedBox<T> {
Expand Down