Skip to main content

DeviceMemRegistry

Struct DeviceMemRegistry 

Source
pub struct DeviceMemRegistry { /* private fields */ }
Expand description

Instance-scoped device-memory registry.

Implementations§

Source§

impl DeviceMemRegistry

Source

pub fn new() -> Self

Construct an empty registry.

Handles start at 1 so callers may reserve 0 as a sentinel; they are otherwise sequential. Cross-instance forgery is prevented by the owner-InstanceId check in Self::lookup / Self::free, so the handle space does not need the randomised-seed treatment the kernel registry uses for its ids.

Source

pub fn with_budget(budget: Arc<DeviceMemBudget>) -> Self

Construct an empty registry charging the given process-wide DeviceMemBudget instead of the shared process_device_budget singleton.

Intended for tests that need an isolated aggregate so the process-wide caps can be exercised without the global singleton bleeding state across test cases (which run in the same process). Production code should use Self::new so every instance shares the one ceiling.

Source

pub fn insert(&self, entry: DeviceMemEntry) -> Result<u64, AbiError>

Reserve aggregate-bytes budget for a new allocation, returning the freshly-assigned handle on success.

Enforces MAX_DEVICE_ALLOCS_PER_INSTANCE and MAX_TOTAL_DEVICE_BYTES (the per-instance caps) and the process-wide MAX_PROCESS_DEVICE_BYTES / MAX_PROCESS_DEVICE_ALLOCS caps via the shared DeviceMemBudget (all returning AbiError::QuotaExceeded) before inserting the entry. The per-call MAX_DEVICE_ALLOC_BYTES cap is the caller’s responsibility (the host function checks it before any driver call) — this method only enforces the aggregate caps so the check + add stays atomic against concurrent allocations.

The per-instance caps are checked first (defence in depth: a single runaway instance trips its own cap before it can charge the shared budget), then the process-wide budget. If the process budget refuses, the per-instance byte reservation is rolled back so the two counters never drift.

Source

pub fn lookup( &self, handle: u64, owner: InstanceId, ) -> Result<DeviceMemHandle, AbiError>

Look up an allocation by handle, returning an independent handle copy.

Returns Err(AbiError::InvalidHandle) if the handle is unknown or belongs to a different instance.

Source

pub fn free( &self, handle: u64, owner: InstanceId, ) -> Result<DeviceMemEntry, AbiError>

Remove an allocation owned by owner, returning its entry.

Returns Err(AbiError::InvalidHandle) when the handle is unknown or belongs to another instance — a guest cannot free a buffer it does not own. On success both the per-instance aggregate-bytes counter and the shared process-wide DeviceMemBudget are credited back.

Source

pub fn len(&self) -> usize

Number of currently-live allocations.

Source

pub fn is_empty(&self) -> bool

True if there are no live allocations.

Source

pub fn total_device_bytes(&self) -> u64

Aggregate device bytes currently retained. Visible for metrics and tests.

Source

pub fn budget(&self) -> &Arc<DeviceMemBudget>

Borrow the process-wide DeviceMemBudget this registry charges. Visible for metrics and tests.

Trait Implementations§

Source§

impl Default for DeviceMemRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more