From 52de97e28072cdb02958efe4cc0b2f0104f54ba9 Mon Sep 17 00:00:00 2001 From: Brooks Prumo Date: Tue, 21 Sep 2021 15:09:04 -0500 Subject: [PATCH] Add type alias for Uid (#20075) --- bucket_map/src/bucket.rs | 4 ++-- bucket_map/src/bucket_storage.rs | 20 +++++++++++--------- bucket_map/src/index_entry.rs | 4 ++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/bucket_map/src/bucket.rs b/bucket_map/src/bucket.rs index 5a91bcf8f..bb646fb29 100644 --- a/bucket_map/src/bucket.rs +++ b/bucket_map/src/bucket.rs @@ -1,7 +1,7 @@ use crate::bucket_item::BucketItem; use crate::bucket_map::BucketMapError; 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::{MaxSearch, RefCount}; use rand::thread_rng; @@ -141,7 +141,7 @@ impl Bucket { fn bucket_create_key( index: &BucketStorage, key: &Pubkey, - elem_uid: u64, + elem_uid: Uid, random: u64, ref_count: u64, ) -> Result { diff --git a/bucket_map/src/bucket_storage.rs b/bucket_map/src/bucket_storage.rs index 59bd3e534..2a68cdb5c 100644 --- a/bucket_map/src/bucket_storage.rs +++ b/bucket_map/src/bucket_storage.rs @@ -31,25 +31,27 @@ use std::sync::Arc; */ 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)] struct Header { lock: AtomicU64, } -/// A Header UID of 0 indicates that the header is unlocked -pub(crate) const UID_UNLOCKED: u64 = 0; - impl Header { - fn try_lock(&self, uid: u64) -> bool { + fn try_lock(&self, uid: Uid) -> bool { Ok(UID_UNLOCKED) == self .lock .compare_exchange(UID_UNLOCKED, uid, Ordering::Acquire, Ordering::Relaxed) } - fn unlock(&self) -> u64 { + fn unlock(&self) -> Uid { self.lock.swap(UID_UNLOCKED, Ordering::Release) } - fn uid(&self) -> u64 { + fn uid(&self) -> Uid { 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() { 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() { panic!("allocate: bad index size"); } @@ -153,7 +155,7 @@ impl BucketStorage { e } - pub fn free(&self, ix: u64, uid: u64) { + pub fn free(&self, ix: u64, uid: Uid) { if ix >= self.num_cells() { panic!("free: bad index size"); } diff --git a/bucket_map/src/index_entry.rs b/bucket_map/src/index_entry.rs index fd9ac23f0..492eff100 100644 --- a/bucket_map/src/index_entry.rs +++ b/bucket_map/src/index_entry.rs @@ -1,5 +1,5 @@ use crate::bucket::Bucket; -use crate::bucket_storage::BucketStorage; +use crate::bucket_storage::{BucketStorage, Uid}; use crate::RefCount; use solana_sdk::clock::Slot; use solana_sdk::pubkey::Pubkey; @@ -54,7 +54,7 @@ impl IndexEntry { }; Some((slice, self.ref_count)) } - pub fn key_uid(key: &Pubkey) -> u64 { + pub fn key_uid(key: &Pubkey) -> Uid { let mut s = DefaultHasher::new(); key.hash(&mut s); s.finish().max(1u64)