Skip to content

Commit

Permalink
bucket_map: Moves get_from_bytes() to index_entry.rs (solana-labs#31443)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored May 2, 2023
1 parent 9b6cadc commit 1a6c75a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
12 changes: 0 additions & 12 deletions bucket_map/src/bucket_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,6 @@ impl<O: BucketOccupied> BucketStorage<O> {
unsafe { slice.get_unchecked_mut(0) }
}

pub(crate) fn get_mut_from_parts<T>(item_slice: &mut [u8]) -> &mut T {
debug_assert!(std::mem::size_of::<T>() <= item_slice.len());
let item = item_slice.as_mut_ptr() as *mut T;
unsafe { &mut *item }
}

pub(crate) fn get_from_parts<T>(item_slice: &[u8]) -> &T {
debug_assert!(std::mem::size_of::<T>() <= item_slice.len());
let item = item_slice.as_ptr() as *const T;
unsafe { &*item }
}

pub(crate) fn get_slice<T>(&self, ix: u64, len: u64, header: IncludeHeader) -> &[T] {
let start = self.get_start_offset(ix, header);
let slice = {
Expand Down
21 changes: 15 additions & 6 deletions bucket_map/src/index_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ pub struct BucketWithHeader {
impl BucketOccupied for BucketWithHeader {
fn occupy(&mut self, element: &mut [u8], ix: usize) {
assert!(self.is_free(element, ix));
let entry: &mut DataBucketRefCountOccupiedHeader =
BucketStorage::<BucketWithHeader>::get_mut_from_parts(element);
let entry = get_mut_from_bytes::<DataBucketRefCountOccupiedHeader>(element);
entry.packed_ref_count.set_occupied(OCCUPIED_OCCUPIED);
}
fn free(&mut self, element: &mut [u8], ix: usize) {
assert!(!self.is_free(element, ix));
let entry: &mut DataBucketRefCountOccupiedHeader =
BucketStorage::<BucketWithHeader>::get_mut_from_parts(element);
let entry = get_mut_from_bytes::<DataBucketRefCountOccupiedHeader>(element);
entry.packed_ref_count.set_occupied(OCCUPIED_FREE);
}
fn is_free(&self, element: &[u8], _ix: usize) -> bool {
let entry: &DataBucketRefCountOccupiedHeader =
BucketStorage::<BucketWithHeader>::get_from_parts(element);
let entry = get_from_bytes::<DataBucketRefCountOccupiedHeader>(element);
entry.packed_ref_count.occupied() == OCCUPIED_FREE
}
fn offset_to_first_data() -> usize {
Expand Down Expand Up @@ -426,6 +423,18 @@ impl<T: Copy + 'static> IndexEntryPlaceInBucket<T> {
}
}

fn get_from_bytes<T>(item_slice: &[u8]) -> &T {
debug_assert!(std::mem::size_of::<T>() <= item_slice.len());
let item = item_slice.as_ptr() as *const T;
unsafe { &*item }
}

fn get_mut_from_bytes<T>(item_slice: &mut [u8]) -> &mut T {
debug_assert!(std::mem::size_of::<T>() <= item_slice.len());
let item = item_slice.as_mut_ptr() as *mut T;
unsafe { &mut *item }
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 1a6c75a

Please sign in to comment.