Skip to content

Commit

Permalink
Fix fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed May 17, 2024
1 parent b2cce16 commit 7c982f3
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 17 deletions.
15 changes: 12 additions & 3 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,13 @@ impl Arena {
}

#[inline]
pub(super) fn alloc<T>(&self, size: u32, value_size: u32, align: u32, overflow: u32) -> Result<(u32, u32), ArenaError> {
pub(super) fn alloc<T>(
&self,
size: u32,
value_size: u32,
align: u32,
overflow: u32,
) -> Result<(u32, u32), ArenaError> {
let trailer_size = mem::size_of::<T>();
let trailer_align = mem::align_of::<T>();

Expand All @@ -376,7 +382,9 @@ impl Arena {
let trailer_padded = trailer_size as u64 + trailer_align as u64 - 1;
let header = self.header();
let mut current_allocated = header.allocated.load(Ordering::Acquire);
if current_allocated + padded + overflow as u64 + trailer_padded + value_size as u64 > self.cap as u64 {
if current_allocated + padded + overflow as u64 + trailer_padded + value_size as u64
> self.cap as u64
{
return Err(ArenaError);
}

Expand All @@ -394,7 +402,8 @@ impl Arena {
let node_offset = (allocated as u32 - size) & !(align - 1);

let allocated_for_trailer = allocated + trailer_padded;
let value_offset = (allocated_for_trailer as u32 - trailer_size as u32) & !(trailer_align as u32 - 1);
let value_offset =
(allocated_for_trailer as u32 - trailer_size as u32) & !(trailer_align as u32 - 1);

return Ok((node_offset, value_offset));
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a> Drop for OccupiedValue<'a> {
}

/// A trait for extra information that can be stored with entry in the skiplist.
///
///
/// # Safety
/// The implementors must ensure that they can be reconstructed from a byte slice directly.
/// e.g. struct includes `*const T` cannot be used as the trailer, because the pointer cannot be reconstructed from a byte slice directly.
Expand Down
8 changes: 6 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,9 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
let old = EntryRef::from_node(node_ptr, self);

if upsert {
node_ptr.as_ptr().set_value(&self.arena, trailer, value_size, f)?;
node_ptr
.as_ptr()
.set_value(&self.arena, trailer, value_size, f)?;
}

return Ok(if old.is_removed() { None } else { Some(old) });
Expand Down Expand Up @@ -772,7 +774,9 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
let old = EntryRef::from_node(node_ptr, self);

if upsert {
node_ptr.as_ptr().set_value(&self.arena, trailer, value_size, f)?;
node_ptr
.as_ptr()
.set_value(&self.arena, trailer, value_size, f)?;
}

return Ok(if old.is_removed() { None } else { Some(old) });
Expand Down
2 changes: 1 addition & 1 deletion src/map/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
pub fn get<'a, 'b: 'a>(&'a self, version: u64, key: &'b [u8]) -> Option<EntryRef<'a, T, C>> {
unsafe {
let (n, eq) = self.find_near(version, key, false, true); // findLessOrEqual.

let n = n?;
let node = n.as_ptr();
let node_key = node.get_key(&self.arena);
Expand Down
14 changes: 10 additions & 4 deletions src/map/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ pub(super) struct Node<T> {
pub(super) key_size: u16,
pub(super) height: u16,
pub(super) trailer: PhantomData<T>,

// // Immutable. No need to lock to access value.
// pub(super) value_size: u32,
// // Immutable. No need to lock to access
Expand Down Expand Up @@ -294,7 +293,8 @@ impl<T: Trailer> Node<T> {
let unused_size = (MAX_HEIGHT as u32 - height) * (Link::SIZE as u32);
let node_size = (Self::MAX_NODE_SIZE as u32) - unused_size;

let (node_offset, value_offset) = arena.alloc::<T>(node_size + key_size as u32, 0, Self::ALIGN, unused_size)?;
let (node_offset, value_offset) =
arena.alloc::<T>(node_size + key_size as u32, 0, Self::ALIGN, unused_size)?;

unsafe {
// Safety: we have check the offset is valid
Expand Down Expand Up @@ -366,15 +366,21 @@ impl<T: Copy> Node<T> {
///
/// - The caller must ensure that the node is allocated by the arena.
#[inline]
pub(super) unsafe fn get_value_and_trailer<'a, 'b: 'a>(&'a self, arena: &'b Arena) -> (T, Option<&'b [u8]>) {
pub(super) unsafe fn get_value_and_trailer<'a, 'b: 'a>(
&'a self,
arena: &'b Arena,
) -> (T, Option<&'b [u8]>) {
let (offset, len) = self.value.load(Ordering::Acquire);
let ptr = arena.get_pointer(offset as usize);
let trailer = ptr::read(ptr as *const T);
if len == u32::MAX {
return (trailer, None);
}

(trailer, Some(arena.get_bytes(offset as usize + mem::size_of::<T>(), len as usize)))
(
trailer,
Some(arena.get_bytes(offset as usize + mem::size_of::<T>(), len as usize)),
)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/map/node/align4vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ impl ValuePointer {
offset: inner.offset,
len: u32::MAX,
};
let _ = self.0.compare_exchange(inner, new_inner, Ordering::SeqCst, Ordering::Relaxed);
let _ = self
.0
.compare_exchange(inner, new_inner, Ordering::SeqCst, Ordering::Relaxed);
}
}
6 changes: 4 additions & 2 deletions src/map/node/align8vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ impl ValuePointer {
let old = self.0.load(Ordering::Acquire);
let (offset, _) = decode_value(old);
let new = encode_value(offset, u32::MAX);
let _ = self.0.compare_exchange(old, new, Ordering::SeqCst, Ordering::Relaxed);
let _ = self
.0
.compare_exchange(old, new, Ordering::SeqCst, Ordering::Relaxed);
}
}

Expand All @@ -53,4 +55,4 @@ const fn decode_value(value: u64) -> (u32, u32) {
let offset = value as u32;
let val_size = (value >> 32) as u32;
(offset, val_size)
}
}
6 changes: 4 additions & 2 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,11 @@ impl<T, C> SkipSet<T, C> {

fn new_in(arena: Arena, cmp: C, ro: bool) -> Result<Self, Error> {
if ro {
let (ptr, offset) = arena.head_ptr::<T>(Node::<T>::MAX_NODE_SIZE as u32, Node::<T>::alignment());
let (ptr, offset) =
arena.head_ptr::<T>(Node::<T>::MAX_NODE_SIZE as u32, Node::<T>::alignment());
let head = NodePtr::new(ptr, offset);
let (ptr, offset) = arena.tail_ptr::<T>(Node::<T>::MAX_NODE_SIZE as u32, Node::<T>::alignment());
let (ptr, offset) =
arena.tail_ptr::<T>(Node::<T>::MAX_NODE_SIZE as u32, Node::<T>::alignment());
let tail = NodePtr::new(ptr, offset);

return Ok(Self::construct(arena, head, tail, ro, cmp));
Expand Down
7 changes: 6 additions & 1 deletion src/set/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@ impl<T: Trailer> Node<T> {
let unused_size = (MAX_HEIGHT as u32 - height) * (Link::SIZE as u32);
let node_size = (Self::MAX_NODE_SIZE as u32) - unused_size;

let (node_offset, _) = arena.alloc::<T>(node_size + key_size as u32, 0, Self::alignment(), unused_size)?;
let (node_offset, _) = arena.alloc::<T>(
node_size + key_size as u32,
0,
Self::alignment(),
unused_size,
)?;

unsafe {
// Safety: we have check the offset is valid
Expand Down

0 comments on commit 7c982f3

Please sign in to comment.