Add type alias for Uid (#20075)

This commit is contained in:
Brooks Prumo 2021-09-21 15:09:04 -05:00 committed by GitHub
parent 0b6e9d861e
commit 52de97e280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 13 deletions

View File

@ -1,7 +1,7 @@
use crate::bucket_item::BucketItem; use crate::bucket_item::BucketItem;
use crate::bucket_map::BucketMapError; use crate::bucket_map::BucketMapError;
use crate::bucket_stats::BucketMapStats; use crate::bucket_stats::BucketMapStats;
use crate::bucket_storage::{BucketStorage, UID_UNLOCKED}; use crate::bucket_storage::{BucketStorage, Uid, UID_UNLOCKED};
use crate::index_entry::IndexEntry; use crate::index_entry::IndexEntry;
use crate::{MaxSearch, RefCount}; use crate::{MaxSearch, RefCount};
use rand::thread_rng; use rand::thread_rng;
@ -141,7 +141,7 @@ impl<T: Clone + Copy> Bucket<T> {
fn bucket_create_key( fn bucket_create_key(
index: &BucketStorage, index: &BucketStorage,
key: &Pubkey, key: &Pubkey,
elem_uid: u64, elem_uid: Uid,
random: u64, random: u64,
ref_count: u64, ref_count: u64,
) -> Result<u64, BucketMapError> { ) -> Result<u64, BucketMapError> {

View File

@ -31,25 +31,27 @@ use std::sync::Arc;
*/ */
const DEFAULT_CAPACITY_POW2: u8 = 5; const DEFAULT_CAPACITY_POW2: u8 = 5;
/// A Header UID of 0 indicates that the header is unlocked
pub(crate) const UID_UNLOCKED: Uid = 0;
pub(crate) type Uid = u64;
#[repr(C)] #[repr(C)]
struct Header { struct Header {
lock: AtomicU64, lock: AtomicU64,
} }
/// A Header UID of 0 indicates that the header is unlocked
pub(crate) const UID_UNLOCKED: u64 = 0;
impl Header { impl Header {
fn try_lock(&self, uid: u64) -> bool { fn try_lock(&self, uid: Uid) -> bool {
Ok(UID_UNLOCKED) Ok(UID_UNLOCKED)
== self == self
.lock .lock
.compare_exchange(UID_UNLOCKED, uid, Ordering::Acquire, Ordering::Relaxed) .compare_exchange(UID_UNLOCKED, uid, Ordering::Acquire, Ordering::Relaxed)
} }
fn unlock(&self) -> u64 { fn unlock(&self) -> Uid {
self.lock.swap(UID_UNLOCKED, Ordering::Release) self.lock.swap(UID_UNLOCKED, Ordering::Release)
} }
fn uid(&self) -> u64 { fn uid(&self) -> Uid {
self.lock.load(Ordering::Relaxed) self.lock.load(Ordering::Relaxed)
} }
} }
@ -120,7 +122,7 @@ impl BucketStorage {
) )
} }
pub fn uid(&self, ix: u64) -> u64 { pub fn uid(&self, ix: u64) -> Uid {
if ix >= self.num_cells() { if ix >= self.num_cells() {
panic!("bad index size"); panic!("bad index size");
} }
@ -132,7 +134,7 @@ impl BucketStorage {
} }
} }
pub fn allocate(&self, ix: u64, uid: u64) -> Result<(), BucketStorageError> { pub fn allocate(&self, ix: u64, uid: Uid) -> Result<(), BucketStorageError> {
if ix >= self.num_cells() { if ix >= self.num_cells() {
panic!("allocate: bad index size"); panic!("allocate: bad index size");
} }
@ -153,7 +155,7 @@ impl BucketStorage {
e e
} }
pub fn free(&self, ix: u64, uid: u64) { pub fn free(&self, ix: u64, uid: Uid) {
if ix >= self.num_cells() { if ix >= self.num_cells() {
panic!("free: bad index size"); panic!("free: bad index size");
} }

View File

@ -1,5 +1,5 @@
use crate::bucket::Bucket; use crate::bucket::Bucket;
use crate::bucket_storage::BucketStorage; use crate::bucket_storage::{BucketStorage, Uid};
use crate::RefCount; use crate::RefCount;
use solana_sdk::clock::Slot; use solana_sdk::clock::Slot;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
@ -54,7 +54,7 @@ impl IndexEntry {
}; };
Some((slice, self.ref_count)) Some((slice, self.ref_count))
} }
pub fn key_uid(key: &Pubkey) -> u64 { pub fn key_uid(key: &Pubkey) -> Uid {
let mut s = DefaultHasher::new(); let mut s = DefaultHasher::new();
key.hash(&mut s); key.hash(&mut s);
s.finish().max(1u64) s.finish().max(1u64)