Skip to content

Commit

Permalink
AcctIdx: better types for AccountInfo (solana-labs#21893)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington authored Dec 14, 2021
1 parent 8d980f0 commit e694aca
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions runtime/src/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::{
/// offset within an append vec to account data
pub type Offset = usize;

/// bytes used to store this account in append vec
pub type StoredSize = usize;

/// specify where account data is located
#[derive(Debug)]
pub enum StorageLocation {
Expand Down Expand Up @@ -61,11 +64,11 @@ pub struct AccountInfo {
/// needed to track shrink candidacy in bytes. Used to update the number
/// of alive bytes in an AppendVec as newer slots purge outdated entries
/// Note that highest bit is used for ZERO_LAMPORT_BIT
stored_size: usize,
stored_size: StoredSize,
}

/// presence of this bit in stored_size indicates this account info references an account with zero lamports
const ZERO_LAMPORT_BIT: usize = 1 << (usize::BITS - 1);
const ZERO_LAMPORT_BIT: StoredSize = 1 << (StoredSize::BITS - 1);

impl ZeroLamport for AccountInfo {
fn is_zero_lamport(&self) -> bool {
Expand All @@ -86,7 +89,11 @@ impl IsCached for StorageLocation {
}

impl AccountInfo {
pub fn new(storage_location: StorageLocation, mut stored_size: usize, lamports: u64) -> Self {
pub fn new(
storage_location: StorageLocation,
mut stored_size: StoredSize,
lamports: u64,
) -> Self {
let (store_id, offset) = match storage_location {
StorageLocation::AppendVec(store_id, offset) => (store_id, offset),
StorageLocation::Cached => (CACHE_VIRTUAL_STORAGE_ID, CACHE_VIRTUAL_OFFSET),
Expand All @@ -102,15 +109,15 @@ impl AccountInfo {
}
}

pub fn store_id(&self) -> usize {
pub fn store_id(&self) -> AppendVecId {
self.store_id
}

pub fn offset(&self) -> usize {
pub fn offset(&self) -> Offset {
self.offset
}

pub fn stored_size(&self) -> usize {
pub fn stored_size(&self) -> StoredSize {
// elminate the special bit that indicates the info references an account with zero lamports
self.stored_size & !ZERO_LAMPORT_BIT
}
Expand Down

0 comments on commit e694aca

Please sign in to comment.