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
4 changes: 2 additions & 2 deletions src/hashbrown/control/bitmask.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::group::BitMaskWord;
use super::group::NonZeroBitMaskWord;
use super::group::BITMASK_ITER_MASK;
use super::group::BITMASK_STRIDE;
use super::group::BitMaskWord;
use super::group::NonZeroBitMaskWord;

/// A bit mask which contains the result of a `Match` operation on a `Group` and
/// allows iterating through them.
Expand Down
4 changes: 2 additions & 2 deletions src/hashbrown/control/group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ cfg_if::cfg_if! {
use generic as imp;
}
}
pub(super) use self::imp::BITMASK_ITER_MASK;
pub(super) use self::imp::BITMASK_STRIDE;
pub(super) use self::imp::BitMaskWord;
pub(crate) use self::imp::Group;
pub(super) use self::imp::NonZeroBitMaskWord;
pub(super) use self::imp::BITMASK_ITER_MASK;
pub(super) use self::imp::BITMASK_STRIDE;
55 changes: 27 additions & 28 deletions src/internal/linked_list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::alloc::Layout;
use std::alloc::alloc;
use std::alloc::dealloc;
use std::alloc::handle_alloc_error;
use std::alloc::Layout;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::ptr::{self};
use std::ptr;

/// Maximum number of nodes parked on the free list
const FREE_LIST_CAPACITY: usize = 256;
Expand Down Expand Up @@ -41,7 +40,7 @@ pub struct Node<T> {

/// A doubly-linked list with an internal, bounded free-list for node reuse.
pub struct LinkedList<T> {
sentinel: NonNull<Links>,
sentinel: ptr::NonNull<Links>,
free_head: *mut Links,
free_len: usize,
len: usize,
Expand All @@ -60,7 +59,7 @@ impl<T> LinkedList<T> {
/// - The caller must not use `node` again as an "unlinked" node until it is
/// unlinked again.
#[inline]
unsafe fn push_front_node(&mut self, node: NonNull<Links>) {
unsafe fn push_front_node(&mut self, node: ptr::NonNull<Links>) {
let s = self.sentinel.as_ptr();
let n = node.as_ptr();
unsafe {
Expand All @@ -79,7 +78,7 @@ impl<T> LinkedList<T> {
///
/// Same contract as [`push_front_node`](Self::push_front_node).
#[inline]
unsafe fn push_back_node(&mut self, node: NonNull<Links>) {
unsafe fn push_back_node(&mut self, node: ptr::NonNull<Links>) {
let s = self.sentinel.as_ptr();
let n = node.as_ptr();
unsafe {
Expand All @@ -94,7 +93,7 @@ impl<T> LinkedList<T> {

/// Removes and returns the node at the front of the list, if any.
#[inline]
fn pop_front_node(&mut self) -> Option<NonNull<Links>> {
fn pop_front_node(&mut self) -> Option<ptr::NonNull<Links>> {
if self.len == 0 {
return None;
}
Expand All @@ -109,12 +108,12 @@ impl<T> LinkedList<T> {
};
self.len -= 1;
// SAFETY: read from a valid node above, so non-null.
Some(unsafe { NonNull::new_unchecked(node) })
Some(unsafe { ptr::NonNull::new_unchecked(node) })
}

/// Removes and returns the node at the back of the list, if any.
#[inline]
fn pop_back_node(&mut self) -> Option<NonNull<Links>> {
fn pop_back_node(&mut self) -> Option<ptr::NonNull<Links>> {
if self.len == 0 {
return None;
}
Expand All @@ -129,7 +128,7 @@ impl<T> LinkedList<T> {
};
self.len -= 1;
// SAFETY: read from a valid node above, so non-null.
Some(unsafe { NonNull::new_unchecked(node) })
Some(unsafe { ptr::NonNull::new_unchecked(node) })
}

/// Unlinks `node` from the list, without deallocating or reading its element.
Expand All @@ -138,7 +137,7 @@ impl<T> LinkedList<T> {
///
/// `node` must point to a node currently linked into this list (not the sentinel).
#[inline]
unsafe fn unlink_node(&mut self, node: NonNull<Links>) {
unsafe fn unlink_node(&mut self, node: ptr::NonNull<Links>) {
let n = node.as_ptr();
unsafe {
// SAFETY: `n` linked per fn contract; `prev`/`next` valid.
Expand Down Expand Up @@ -181,7 +180,7 @@ impl<T> LinkedList<T> {
/// `node` must point to a node whose `element` is initialized and which is
/// no longer linked into the list (already unlinked by the caller).
#[inline]
unsafe fn take_element_and_recycle(&mut self, node: NonNull<Links>) -> T {
unsafe fn take_element_and_recycle(&mut self, node: ptr::NonNull<Links>) -> T {
let node_ptr = node.as_ptr() as *mut Node<T>;
// SAFETY: `element` initialized per fn contract; `ptr::read` doesn't drop.
let element = unsafe { ptr::read(&(*node_ptr).element) };
Expand All @@ -196,7 +195,7 @@ impl<T> LinkedList<T> {
///
/// `node` must point to a node currently linked into this list
#[inline]
unsafe fn remove_node(&mut self, node: NonNull<Links>) -> T {
unsafe fn remove_node(&mut self, node: ptr::NonNull<Links>) -> T {
// SAFETY: caller guarantees `node` is linked.
unsafe { self.unlink_node(node) };
self.len -= 1;
Expand Down Expand Up @@ -304,7 +303,7 @@ impl<T> LinkedList<T> {
p
};
// SAFETY: non-null checked above (or diverged).
let sentinel = unsafe { NonNull::new_unchecked(raw) };
let sentinel = unsafe { ptr::NonNull::new_unchecked(raw) };
LinkedList {
sentinel,
free_head: ptr::null_mut(),
Expand Down Expand Up @@ -350,7 +349,7 @@ impl<T> LinkedList<T> {
let node = unsafe { (*s).next };
// SAFETY: non-null above; layout-compatible (see `Node` docs).
Some(Cursor(unsafe {
NonNull::new_unchecked(node as *mut Node<T>)
ptr::NonNull::new_unchecked(node as *mut Node<T>)
}))
}

Expand All @@ -366,7 +365,7 @@ impl<T> LinkedList<T> {
let node = unsafe { (*s).prev };
// SAFETY: same as `cursor_front`.
Some(Cursor(unsafe {
NonNull::new_unchecked(node as *mut Node<T>)
ptr::NonNull::new_unchecked(node as *mut Node<T>)
}))
}

Expand All @@ -375,13 +374,13 @@ impl<T> LinkedList<T> {
pub fn push_front(&mut self, elt: T) -> Cursor<T> {
let node = self.get_node(elt);
// SAFETY: `get_node` never returns null.
let links = unsafe { NonNull::new_unchecked(node as *mut Links) };
let links = unsafe { ptr::NonNull::new_unchecked(node as *mut Links) };

// SAFETY: freshly obtained node, unlinked (see `push_front_node` contract).
unsafe { self.push_front_node(links) };
self.len += 1;
// SAFETY: same pointer validated above.
Cursor(unsafe { NonNull::new_unchecked(node) })
Cursor(unsafe { ptr::NonNull::new_unchecked(node) })
}

/// Removes and returns the front element, or `None` if the list is empty.
Expand All @@ -399,14 +398,14 @@ impl<T> LinkedList<T> {
pub fn push_back(&mut self, elt: T) -> Cursor<T> {
let node = self.get_node(elt);
// SAFETY: `get_node` always returns a non-null, freshly-usable `Node<T>` pointer.
let links = unsafe { NonNull::new_unchecked(node as *mut Links) };
let links = unsafe { ptr::NonNull::new_unchecked(node as *mut Links) };

// SAFETY: `links` refers to a node that was just obtained and is
// not linked into any list yet.
unsafe { self.push_back_node(links) };
self.len += 1;
// SAFETY: `node` is the same non-null pointer validated above.
Cursor(unsafe { NonNull::new_unchecked(node) })
Cursor(unsafe { ptr::NonNull::new_unchecked(node) })
}

/// Removes and returns the back element, or `None` if the list is empty.
Expand Down Expand Up @@ -476,7 +475,7 @@ impl<T> Drop for LinkedList<T> {
/// later access or reposition that node via [`Cursor::element`],
/// [`Cursor::move_to_front`], [`Cursor::move_to_back`], or [`Cursor::unlink`].
///
/// `#[repr(transparent)]` over `NonNull<Node<T>>` means a `Cursor<T>` has the
/// `#[repr(transparent)]` over `ptr::NonNull<Node<T>>` means a `Cursor<T>` has the
/// exact same layout as the raw pointer it wraps — no extra state is tracked,
/// so the cursor does *not* know which `LinkedList` it came from, whether the
/// node is still linked, or whether other `Cursor`s alias the same node. All
Expand All @@ -487,9 +486,9 @@ impl<T> Drop for LinkedList<T> {
/// equality (`PartialEq`/`Eq`) compares the underlying pointer, i.e. identity
/// of the node, not the value of the element.
#[repr(transparent)]
pub struct Cursor<T>(NonNull<Node<T>>);
pub struct Cursor<T>(ptr::NonNull<Node<T>>);

// `NonNull<Node<T>>` is just a pointer; copying it is always safe.
// `ptr::NonNull<Node<T>>` is just a pointer; copying it is always safe.
impl<T> Clone for Cursor<T> {
#[inline]
fn clone(&self) -> Self {
Expand All @@ -514,10 +513,10 @@ impl<T> Cursor<T> {
/// Relies on `#[repr(C)]` on `Node<T>` placing `links` at offset 0, so the
/// cast is always valid regardless of `T`.
#[inline]
fn links(&self) -> NonNull<Links> {
fn links(&self) -> ptr::NonNull<Links> {
let ptr = self.0.as_ptr() as *mut Links;
// SAFETY: `self.0` is `NonNull`, so the reinterpreted pointer is non-null too.
unsafe { NonNull::new_unchecked(ptr) }
// SAFETY: `self.0` is `ptr::NonNull`, so the reinterpreted pointer is non-null too.
unsafe { ptr::NonNull::new_unchecked(ptr) }
}

/// Returns a reference to the node's element, with a caller-chosen lifetime.
Expand Down Expand Up @@ -619,7 +618,7 @@ impl<T> Cursor<T> {
pub struct RawIter<T> {
next: *mut Links,
len: usize,
_marker: PhantomData<NonNull<Node<T>>>,
_marker: PhantomData<ptr::NonNull<Node<T>>>,
}

impl<T> Iterator for RawIter<T> {
Expand All @@ -639,7 +638,7 @@ impl<T> Iterator for RawIter<T> {
self.next = unsafe { (*node).next };
// SAFETY: `node` is non-null (came from a valid linked `Links`), and
// by `#[repr(C)]` on `Node<T>` a `*mut Links` is a valid `*mut Node<T>`.
let cursor = Cursor(unsafe { NonNull::new_unchecked(node as *mut Node<T>) });
let cursor = Cursor(unsafe { ptr::NonNull::new_unchecked(node as *mut Node<T>) });
Some(cursor)
}

Expand Down
2 changes: 1 addition & 1 deletion src/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ macro_rules! implement_view_guard {
let cache = self.cache.get();
let inner = cache.0.get()?;

let policy = inner.policy_py_attached(py);
let policy = inner.policy(py);

if self.initial_gv == inner.shared().generation_version().get() {
return Ok(policy);
Expand Down
9 changes: 5 additions & 4 deletions src/policies/fifopolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ impl FIFOPolicy {

#[inline]
fn decrement_indexes(&mut self, start: usize, end: usize) {
const MAX_FRONT_OFFSET: usize = cfg_select! {
feature = "use-small-offset" => u8::MAX as usize,
not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize,
};
const MAX_FRONT_OFFSET: usize =
cfg_select! {
feature = "use-small-offset" => u8::MAX as usize,
not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize,
};

// Fast path: shifting the entire front is a single counter increment.
// Guard against overflow; the full-normalization path below handles that case.
Expand Down
9 changes: 5 additions & 4 deletions src/policies/ttlpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,11 @@ impl TTLPolicy {

#[inline]
fn decrement_indexes(&mut self, start: usize, end: usize) {
const MAX_FRONT_OFFSET: usize = cfg_select! {
feature = "use-small-offset" => u8::MAX as usize,
not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize,
};
const MAX_FRONT_OFFSET: usize =
cfg_select! {
feature = "use-small-offset" => u8::MAX as usize,
not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize,
};

// Fast path: shifting the entire front is a single counter increment.
// Guard against overflow; the full-normalization path below handles that case.
Expand Down
Loading
Loading