Skip to main content

VirtualBlock

Struct VirtualBlock 

Source
pub struct VirtualBlock<'callbacks> { /* private fields */ }
Expand description

Virtual TLSF or linear allocator; owns host metadata and no native objects.

Mutations require exclusive access. Drop releases host metadata only. Callback construction borrows its allocation/free record through Drop; raw callback user data remains the unsafe constructor caller’s responsibility. This is not a Vulkan memory block or a pool with GPU lifetime tracking.

Implementations§

Source§

impl<'callbacks> VirtualBlock<'callbacks>

Source

pub fn is_empty(&self) -> bool

Observe checked virtual emptiness in constant time.

Source

pub fn set_user_data( &mut self, allocation: &VirtualAllocation, data: usize, ) -> Result<(), AllocError>

Set opaque per-allocation metadata, with no native pointer dereference or ownership.

Source

pub fn detailed_statistics(&self) -> VirtualBlockDetails

Calculate explicit virtual allocation/free-span extrema.

Source

pub fn write_statistics_json( &self, output: &mut impl Write, detailed: bool, ) -> Result<(), AllocError>

Stream virtual JSON diagnostics; no native memory is accessed.

Source

pub fn new(size: u64) -> Result<Self, AllocError>

Source

pub fn new_with_algorithm( size: u64, algorithm: AllocationAlgorithm, ) -> Result<Self, AllocError>

Create TLSF or linear host-only placement. No native storage is acquired.

use vk::{VirtualBlock, mem::{AllocationAlgorithm, PlacementStrategy}};
let mut block = VirtualBlock::new_with_algorithm(4096, AllocationAlgorithm::Linear)?;
let mut lower = block.allocate(64, 16, PlacementStrategy::Default)?;
let mut upper = block.allocate_upper(64, 16)?;
assert_eq!(block.allocation_info(&lower)?.offset, 0);
assert_eq!(block.allocation_info(&upper)?.offset, 4032);
block.free(&mut upper)?;
block.free(&mut lower)?;
§Errors

Rejects a zero size or failed host metadata reservation.

Source

pub unsafe fn new_with_callbacks( size: u64, algorithm: AllocationAlgorithm, callbacks: &'callbacks AllocationCallbacks, ) -> Result<Self, AllocError>

Create host-only placement whose metadata uses borrowed Vulkan allocation callbacks.

TLSF/linear buffers, growth and detailed-JSON scratch storage use only this allocation/free pair, with SystemAllocationScope::OBJECT. The block value stays inline; tokens need no reference-count allocation. Reallocation and driver internal-allocation notifications are not invoked. Drop releases host metadata only; no Vulkan device or native memory is involved. The callback record cannot expire while the block still owns storage:

ⓘ
let mut block;
{
    let callbacks = vk::AllocationCallbacks::new();
    // SAFETY: demonstration of record lifetime; null functions are rejected at runtime.
    block = unsafe {
        VirtualBlock::new_with_callbacks(1024, AllocationAlgorithm::Tlsf, &callbacks)
    }.unwrap();
}
block.clear();
§Errors

Rejects zero size, a missing allocation/free function or failed host storage.

§Safety

Callbacks must return uniquely owned memory of the requested size/alignment, or null on failure, and free only their own allocations. They must not unwind or reenter this block. Their code and user data must remain valid through block Drop and support calls from any thread, including concurrent independent blocks. Borrowing retains the callback record, not ownership of its raw user data.

Source

pub fn allocate( &mut self, size: u64, alignment: u64, strategy: PlacementStrategy, ) -> Result<VirtualAllocation, AllocError>

Allocate an aligned region. Linear placement ignores the TLSF search strategy.

Source

pub fn allocate_upper( &mut self, size: u64, alignment: u64, ) -> Result<VirtualAllocation, AllocError>

Allocate downward in a linear block. Active ring mode rejects this request.

§Errors

Rejects TLSF, invalid extents/alignment, exhausted space or host metadata.

Source

pub fn free( &mut self, allocation: &mut VirtualAllocation, ) -> Result<(), AllocError>

Release a matching live identity. No native resource is destroyed or waited on.

Source

pub fn allocation_info( &self, allocation: &VirtualAllocation, ) -> Result<VirtualAllocationInfo, AllocError>

Validate identity/generation and return the current offset/size snapshot.

Source

pub fn clear(&mut self)

Invalidate all tokens and restore the full range, retaining host capacity. Users of previously returned offsets must stop using them before reuse.

Source

pub fn statistics(&self) -> VirtualBlockStats

Calculate detailed free-span statistics; this is not a constant-time hot path.

Auto Trait Implementations§

§

impl<'callbacks> Forget for VirtualBlock<'callbacks>

§

impl<'callbacks> Freeze for VirtualBlock<'callbacks>

§

impl<'callbacks> Move for VirtualBlock<'callbacks>

§

impl<'callbacks> RefUnwindSafe for VirtualBlock<'callbacks>

§

impl<'callbacks> Send for VirtualBlock<'callbacks>

§

impl<'callbacks> Sync for VirtualBlock<'callbacks>

§

impl<'callbacks> Unpin for VirtualBlock<'callbacks>

§

impl<'callbacks> UnsafeUnpin for VirtualBlock<'callbacks>

§

impl<'callbacks> UnwindSafe for VirtualBlock<'callbacks>

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.