webrender::frame_allocator

Struct FrameAllocator

Source
pub struct FrameAllocator {
    inner: *mut FrameInnerAllocator,
    frame_id: Option<FrameId>,
}
Expand description

A memory allocator for allocations that have the same lifetime as a built frame.

A custom allocator is used because:

  • The frame is created on a thread and dropped on another thread, which causes lock contention in jemalloc.
  • Since all allocations have a very similar lifetime, we can implement much faster allocation and deallocation with a specialized allocator than can be achieved with a general purpose allocator.

If the allocator is created using FrameAllocator::fallback(), it is not attached to a FrameMemory and simply falls back to the global allocator. This should only be used to handle deserialization (for wrench replays) and tests.

§Safety

None of the safety restrictions below apply if the allocator is created using FrameAllocator::fallback.

FrameAllocator can move between thread if and only if it does so along with the FrameMemory it is associated to (if any). The opposite is also true: it is safe to move FrameMemory between threads if and only if all live frame allocators associated to it move along with it.

FrameAllocator must be dropped before the FrameMemory it is associated to.

In other words, FrameAllocator should only be used for containers that are in the Frame data structure and not stored elsewhere. The Frame holds on to its FrameMemory, allowing it all to be sent from the frame builder thread to the renderer thread together.

Another way to think of it is that the frame is a large self-referential data structure, holding on to its memory and a large number of containers that point into the memory.

Fields§

§inner: *mut FrameInnerAllocator§frame_id: Option<FrameId>

Implementations§

Source§

impl FrameAllocator

Source

pub fn fallback() -> Self

Creates a FrameAllocator that defaults to the global allocator.

Should only be used for testing purposes or desrialization in wrench replays.

Source

pub fn new_vec<T>(self) -> Vec<T, FrameAllocator>

Shorthand for creating a FrameVec.

Source

pub fn new_vec_with_capacity<T>(self, cap: usize) -> Vec<T, FrameAllocator>

Shorthand for creating a FrameVec.

Source

fn allocate_impl( mem: *mut FrameInnerAllocator, layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Source

unsafe fn deallocate_impl( mem: *mut FrameInnerAllocator, ptr: NonNull<u8>, layout: Layout, )

Source

unsafe fn grow_impl( mem: *mut FrameInnerAllocator, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Source

unsafe fn shrink_impl( mem: *mut FrameInnerAllocator, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Source

fn allocate_fallback(layout: Layout) -> Result<NonNull<[u8]>, AllocError>

Source

fn deallocate_fallback(ptr: NonNull<u8>, layout: Layout)

Source

fn grow_fallback( ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Source

fn check_frame_id(&self)

Trait Implementations§

Source§

impl Allocator for FrameAllocator

Source§

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>

Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)

Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Attempts to extend the memory block. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Attempts to shrink the memory block. Read more
Source§

fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>

Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl Clone for FrameAllocator

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for FrameAllocator

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Serialize for FrameAllocator

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Send for FrameAllocator

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.