disk_buckets: factor out unsafe code (#22028)
This commit is contained in:
committed by
GitHub
parent
d896ff74ec
commit
84eaaae062
@ -126,29 +126,29 @@ impl BucketStorage {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uid(&self, ix: u64) -> Uid {
|
/// return ref to header of item 'ix' in mmapped file
|
||||||
assert!(ix < self.capacity(), "bad index size");
|
fn header_ptr(&self, ix: u64) -> &Header {
|
||||||
let ix = (ix * self.cell_size) as usize;
|
let ix = (ix * self.cell_size) as usize;
|
||||||
let hdr_slice: &[u8] = &self.mmap[ix..ix + std::mem::size_of::<Header>()];
|
let hdr_slice: &[u8] = &self.mmap[ix..ix + std::mem::size_of::<Header>()];
|
||||||
unsafe {
|
unsafe {
|
||||||
let hdr = hdr_slice.as_ptr() as *const Header;
|
let hdr = hdr_slice.as_ptr() as *const Header;
|
||||||
return hdr.as_ref().unwrap().uid();
|
hdr.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn uid(&self, ix: u64) -> Uid {
|
||||||
|
assert!(ix < self.capacity(), "bad index size");
|
||||||
|
self.header_ptr(ix).uid()
|
||||||
|
}
|
||||||
|
|
||||||
/// 'is_resizing' true if caller is resizing the index (so don't increment count)
|
/// 'is_resizing' true if caller is resizing the index (so don't increment count)
|
||||||
/// 'is_resizing' false if caller is adding an item to the index (so increment count)
|
/// 'is_resizing' false if caller is adding an item to the index (so increment count)
|
||||||
pub fn allocate(&self, ix: u64, uid: Uid, is_resizing: bool) -> Result<(), BucketStorageError> {
|
pub fn allocate(&self, ix: u64, uid: Uid, is_resizing: bool) -> Result<(), BucketStorageError> {
|
||||||
assert!(ix < self.capacity(), "allocate: bad index size");
|
assert!(ix < self.capacity(), "allocate: bad index size");
|
||||||
assert!(UID_UNLOCKED != uid, "allocate: bad uid");
|
assert!(UID_UNLOCKED != uid, "allocate: bad uid");
|
||||||
let mut e = Err(BucketStorageError::AlreadyAllocated);
|
let mut e = Err(BucketStorageError::AlreadyAllocated);
|
||||||
let ix = (ix * self.cell_size) as usize;
|
|
||||||
//debug!("ALLOC {} {}", ix, uid);
|
//debug!("ALLOC {} {}", ix, uid);
|
||||||
let hdr_slice: &[u8] = &self.mmap[ix..ix + std::mem::size_of::<Header>()];
|
if self.header_ptr(ix).try_lock(uid) {
|
||||||
if unsafe {
|
|
||||||
let hdr = hdr_slice.as_ptr() as *const Header;
|
|
||||||
hdr.as_ref().unwrap().try_lock(uid)
|
|
||||||
} {
|
|
||||||
e = Ok(());
|
e = Ok(());
|
||||||
if !is_resizing {
|
if !is_resizing {
|
||||||
self.count.fetch_add(1, Ordering::Relaxed);
|
self.count.fetch_add(1, Ordering::Relaxed);
|
||||||
@ -160,13 +160,7 @@ impl BucketStorage {
|
|||||||
pub fn free(&self, ix: u64, uid: Uid) {
|
pub fn free(&self, ix: u64, uid: Uid) {
|
||||||
assert!(ix < self.capacity(), "bad index size");
|
assert!(ix < self.capacity(), "bad index size");
|
||||||
assert!(UID_UNLOCKED != uid, "free: bad uid");
|
assert!(UID_UNLOCKED != uid, "free: bad uid");
|
||||||
let ix = (ix * self.cell_size) as usize;
|
let previous_uid = self.header_ptr(ix).unlock();
|
||||||
//debug!("FREE {} {}", ix, uid);
|
|
||||||
let hdr_slice: &[u8] = &self.mmap[ix..ix + std::mem::size_of::<Header>()];
|
|
||||||
unsafe {
|
|
||||||
let hdr = hdr_slice.as_ptr() as *const Header;
|
|
||||||
//debug!("FREE uid: {}", hdr.as_ref().unwrap().uid());
|
|
||||||
let previous_uid = hdr.as_ref().unwrap().unlock();
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
previous_uid, uid,
|
previous_uid, uid,
|
||||||
"free: unlocked a header with a differet uid: {}",
|
"free: unlocked a header with a differet uid: {}",
|
||||||
@ -174,7 +168,6 @@ impl BucketStorage {
|
|||||||
);
|
);
|
||||||
self.count.fetch_sub(1, Ordering::Relaxed);
|
self.count.fetch_sub(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get<T: Sized>(&self, ix: u64) -> &T {
|
pub fn get<T: Sized>(&self, ix: u64) -> &T {
|
||||||
assert!(ix < self.capacity(), "bad index size");
|
assert!(ix < self.capacity(), "bad index size");
|
||||||
|
Reference in New Issue
Block a user