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
impl FrameAllocator
Sourcepub fn fallback() -> Self
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.
Sourcepub fn new_vec<T>(self) -> Vec<T, FrameAllocator>
pub fn new_vec<T>(self) -> Vec<T, FrameAllocator>
Shorthand for creating a FrameVec.
Sourcepub fn new_vec_with_capacity<T>(self, cap: usize) -> Vec<T, FrameAllocator>
pub fn new_vec_with_capacity<T>(self, cap: usize) -> Vec<T, FrameAllocator>
Shorthand for creating a FrameVec.
fn allocate_impl( mem: *mut FrameInnerAllocator, layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
unsafe fn deallocate_impl( mem: *mut FrameInnerAllocator, ptr: NonNull<u8>, layout: Layout, )
unsafe fn grow_impl( mem: *mut FrameInnerAllocator, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink_impl( mem: *mut FrameInnerAllocator, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
fn allocate_fallback(layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn deallocate_fallback(ptr: NonNull<u8>, layout: Layout)
fn grow_fallback( ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
fn check_frame_id(&self)
Trait Implementations§
Source§impl Allocator for FrameAllocator
impl Allocator for FrameAllocator
Source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Source§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
ptr
. Read moreSource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
Source§unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
Source§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocate
, but also ensures that the returned memory is zero-initialized. Read moreSource§impl Clone for FrameAllocator
impl Clone for FrameAllocator
Source§impl Drop for FrameAllocator
impl Drop for FrameAllocator
Source§impl Serialize for FrameAllocator
impl Serialize for FrameAllocator
impl Send for FrameAllocator
Auto Trait Implementations§
impl Freeze for FrameAllocator
impl RefUnwindSafe for FrameAllocator
impl !Sync for FrameAllocator
impl Unpin for FrameAllocator
impl UnwindSafe for FrameAllocator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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