8000 feat: introduce segment fifo eviction policy by MrCroxx · Pull Request #35 · foyer-rs/foyer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: introduce segment fifo eviction policy #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2023
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 8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 83 additions & 5 deletions foyer-intrusive/src/core/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ pub unsafe trait KeyAdapter: Adapter {
) -> *const Self::Key;
}

/// # Safety
///
/// Pointer operations MUST be valid.
///
/// [`PriorityAdapter`] is recommanded to be generated by macro `priority_adapter!`.
pub unsafe trait PriorityAdapter: Adapter {
type Priority: PartialEq + Eq + PartialOrd + Ord + Clone + Copy + Into<usize>;

/// # Safety
///
/// Pointer operations MUST be valid.
unsafe fn item2priority(
&self,
item: *const <Self::PointerOps as PointerOps>::Item,
) -> *const Self::Priority;
}

/// Macro to generate an implementation of [`Adapter`] for instrusive container and items.
///
/// The basic syntax to create an adapter is:
Expand Down Expand Up @@ -234,14 +251,75 @@ macro_rules! key_adapter {
};
}

macro_rules! t {
/// Macro to generate an implementation of [`PriorityAdapter`] for instrusive container and items.
/// ///
/// The basic syntax to create an adapter is:
///
/// ```rust,ignore
/// priority_adapter! { Adapter = Item { priority_field: PriorityType } }
/// ```
///
/// # Generics
///
/// This macro supports generic arguments:
///
/// Note that due to macro parsing limitations, `T: Trait` bounds are not
/// supported in the generic argument list. You must list any trait bounds in
/// a separate `where` clause at the end of the macro.
///
/// # Examples
///
/// ```
/// use foyer_intrusive::{intrusive_adapter, priority_adapter};
/// use foyer_intrusive::core::adapter::{Adapter, PriorityAdapter, Link};
/// use foyer_intrusive::core::pointer::PointerOps;
/// use foyer_intrusive::eviction::EvictionPolicy;
/// use std::sync::Arc;
///
/// #[derive(Debug)]
/// pub struct Item<L>
/// where
/// L: Link
/// {
/// link: L,
/// priority: usize,
/// }
///
/// intrusive_adapter! { ItemAdapter<L> = Arc<Item<L>>: Item<L> { link: L} where L: Link }
/// priority_adapter! { ItemAdapter<L> = Item<L> { priority: usize } where L: Link }
/// ```
#[macro_export]
macro_rules! priority_adapter {
(@impl
$adapter:ident ($($args:tt),*) = $item:ty { $field:ident: $priority:ty } $($where_:tt)*
) => {
unsafe impl<$($args),*> $crate::core::adapter::PriorityAdapter for $adapter<$($args),*> $($where_)*{
type Priority = $priority;

unsafe fn item2priority(
&self,
item: *const <Self::PointerOps as PointerOps>::Item,
) -> *const Self::Priority {
(item as *const u8).add($crate::offset_of!($item, $field)) as *const _
}
}
};
(
$name:ident = $($rest:tt)*
) => {
priority_adapter! {@impl
$name () = $($rest)*
}
};
(
<$($args:tt),*>
) => {};
$name:ident<$($args:tt),*> = $($rest:tt)*
) => {
priority_adapter! {@impl
$name ($($args),*) = $($rest)*
}
};
}

t! { <A, B, C> }

#[cfg(test)]
mod tests {
use itertools::Itertools;
Expand Down
Loading
0