pub struct ReallocGuard<const N: usize> { /* private fields */ }Expand description
Per-instruction realloc budget guard.
N is the maximum number of accounts to track (const generic, stack-allocated).
Typical values: 4, 8, or 16.
Implementations§
Source§impl<const N: usize> ReallocGuard<N>
impl<const N: usize> ReallocGuard<N>
Sourcepub const fn new(budget: u32) -> ReallocGuard<N>
pub const fn new(budget: u32) -> ReallocGuard<N>
Create a new guard with the given growth budget (bytes).
Sourcepub fn register(&mut self, slot: usize, size: usize) -> Result<(), ProgramError>
pub fn register(&mut self, slot: usize, size: usize) -> Result<(), ProgramError>
Register an account’s original size for tracking.
slot is the local tracking index (0..N-1), not the account index.
size is the current data length.
Returns Err if slot >= N.
Sourcepub fn check_growth(
&self,
slot: usize,
new_size: usize,
) -> Result<(), ProgramError>
pub fn check_growth( &self, slot: usize, new_size: usize, ) -> Result<(), ProgramError>
Check if growing account slot to new_size is within budget.
Does NOT commit the growth – call commit_growth after the
actual realloc succeeds.
Sourcepub fn commit_growth(
&mut self,
slot: usize,
new_size: usize,
) -> Result<(), ProgramError>
pub fn commit_growth( &mut self, slot: usize, new_size: usize, ) -> Result<(), ProgramError>
Commit a growth after successful realloc.
Must be called after the actual safe_realloc succeeds. This repeats
the checked growth accounting instead of trusting callers to have run
Self::check_growth first, so the commit step is safe on its own.
Returns Err if slot is not registered or the budget would be exceeded.
Sourcepub fn slot_growth(&self, slot: usize) -> i64
pub fn slot_growth(&self, slot: usize) -> i64
Growth of a specific slot from its original size (bytes).