Skip to main content

Chunk

Struct Chunk 

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

A wholesale block of memory obtained via direct_alloc out of which the MemoryPool carves the *mut u8 slots handed to consumers.

A Chunk serves allocations in two ways.

  1. Reusing a deallocated section tracked in its FreeList (after defragmenting it).
  2. Bumping the internal cursor over the untouched tail of the block.

The backing block is released via direct_dealloc when the Chunk is dropped.

Implementations§

Source§

impl Chunk

Source

pub fn new(capacity: usize) -> Option<Chunk>

Allocates a new Chunk of capacity bytes using direct_alloc.

Returns None if the system refuses to hand us the memory.

Source

pub fn capacity(&self) -> usize

Source

pub fn remaining(&self) -> usize

Number of bytes remaining in the untouched tail of the Chunk.

Source

pub fn contains(&self, ptr: *const u8) -> bool

Checks if ptr points into the memory block owned by this Chunk.

Source

pub fn can_allocate(&self, size: usize, align: usize) -> bool

Checks if this Chunk has a slot of size bytes aligned to align available, either in its FreeList or in its untouched tail. Call Self::defragment first for best results.

Source

pub fn defragment(&mut self)

Merges adjacent FreeSlot entries back into single continuous sections.

Because the FreeList is kept sorted by address, a single pass suffices. Merging maximizes the odds that a deallocated region can be recycled for a new allocation.

Source

pub fn allocate(&mut self, size: usize, align: usize) -> Option<*mut u8>

Obtains a *mut u8 slot of size bytes aligned to align from this Chunk.

§Order of Operations
  1. Defragment the FreeList via Self::defragment.
  2. Try to recycle a deallocated section via Self::reclaim.
  3. Fall back to bumping the cursor over the untouched tail via Self::bump.

Returns None if no slot is available in this Chunk.

Source

pub fn deallocate(&mut self, ptr: *mut u8, size: usize)

Returns a slot to this Chunk by recording it in the FreeList so a later allocation can recycle it. No memory is returned to the system here.

Trait Implementations§

Source§

impl Debug for Chunk

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Drop for Chunk

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !Send for Chunk

§

impl !Sync for Chunk

§

impl Freeze for Chunk

§

impl RefUnwindSafe for Chunk

§

impl Unpin for Chunk

§

impl UnsafeUnpin for Chunk

§

impl UnwindSafe for Chunk

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 = 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V