memkit 0.2.0-beta.1

Deterministic, intent-driven memory allocation for systems requiring predictable performance
Documentation
//! Scoped frame guards.

use crate::allocator::MkAllocator;

/// A guard that represents a frame scope.
///
/// When dropped, the frame arena is reset to its state when the guard was created.
/// This is useful for sub-frame temporary allocations.
///
/// # Example
///
/// ```rust,ignore
/// let alloc = MkAllocator::with_defaults();
/// alloc.begin_frame();
///
/// {
///     let _scope = alloc.scope();
///     let temp = alloc.frame_alloc::<[u8; 1024]>();
///     // temp is valid here
/// } // temp is invalidated, memory reclaimed
///
/// // Can allocate more in the same frame
/// let other = alloc.frame_alloc::<u32>();
///
/// alloc.end_frame();
/// ```
pub struct MkScope<'a> {
    alloc: &'a MkAllocator,
    saved_head: usize,
}

impl<'a> MkScope<'a> {
    /// Create a new scope guard.
    pub(crate) fn new(alloc: &'a MkAllocator) -> Self {
        let saved_head = alloc.frame_head();
        Self { alloc, saved_head }
    }

    /// Get the saved head position.
    pub fn saved_head(&self) -> usize {
        self.saved_head
    }
}

impl<'a> Drop for MkScope<'a> {
    fn drop(&mut self) {
        self.alloc.reset_frame_to(self.saved_head);
    }
}