Skip to main content

Allocator

Struct Allocator 

Source
pub struct Allocator {
    size: u32,
    alignment: u32,
    max_nodes: u32,
    free_storage: u32,
    used_bins_top: u32,
    used_bins: [u8; 32],
    bin_indices: [Option<NodeIndex>; 256],
    nodes: Vec<Node>,
    free_nodes: Vec<NodeIndex>,
    num_free_nodes: u32,
    tail: Option<NodeIndex>,
}
Expand description

An allocator that manages a single contiguous chunk of space and hands out portions of it as requested.

Allocations never move, and every offset it returns is a multiple of the alignment it was constructed with.

§Alignment invariant

Every node — allocated or free — starts at a multiple of the alignment. The initial node starts at zero, and Allocator::allocate rounds the size it charges up to the alignment, so the remainder it splits off also starts at a multiple of it; Allocator::free merges neighbours without moving any start. An allocation’s offset is the start of its node, so it is aligned by construction rather than by aligning an offset up and padding in front.

Fields§

§size: u32

The total size of the managed chunk, in units.

§alignment: u32

The alignment, in units, that every allocation starts at.

§max_nodes: u32

The maximum number of ranges — allocated or free — the allocator tracks.

Every allocation needs at least one node, and the free space that remains after it needs one more, so this bounds the number of live allocations to max_nodes - 1.

§free_storage: u32

The total size of the free ranges, in units.

Fragmentation and rounding mean this is not always allocatable at once, but while it is non-zero a one-unit allocation is always possible.

§used_bins_top: u32

A bit per top-level bin, set when that bin holds at least one range.

§used_bins: [u8; 32]

A bit per leaf bin of each top-level bin, set when that leaf bin holds at least one range.

§bin_indices: [Option<NodeIndex>; 256]

The first node of each leaf bin’s linked list of free ranges.

§nodes: Vec<Node>

Every range, allocated or free, indexed by NodeIndex.

§free_nodes: Vec<NodeIndex>

The indexes of the nodes that no range uses, as a stack.

§num_free_nodes: u32

How many entries of free_nodes are part of the stack.

§tail: Option<NodeIndex>

The last node of the neighbor list, i.e. the one that ends the chunk.

Tracked so that Allocator::extend can append to the chunk in O(1).

Implementations§

Source§

impl Allocator

Source

pub fn new(size: u32) -> Self

Creates an allocator over size units, with unit alignment and the default node limit.

Source

pub fn with_max_nodes_and_alignment( size: u32, max_nodes: u32, alignment: NonZeroU32, ) -> Self

Creates an allocator over size units where every allocation starts at a multiple of alignment, tracking at most max_nodes ranges.

§Panics

Panics if max_nodes is zero or leaves no room for the node index bias.

Source

pub fn reset(&mut self)

Clears out all allocations, returning the allocator to its initial state.

Source

pub fn allocate(&mut self, size: u32) -> Option<Allocation>

Allocates a range of size units and returns its allocation.

The range starts at a multiple of the allocator’s alignment, and the size charged for it is size rounded up to that alignment; see Allocator::allocation_size. Returns None if no free range is large enough, or if the allocator has no node left to describe the allocation.

Source

pub fn free(&mut self, allocation: Allocation)

Frees an allocation, returning the data to the allocator.

§Panics

Panics if the allocation was already freed.

Source

pub fn extend(&mut self, additional: u32) -> bool

Grows the managed chunk by additional units, appended at its end.

The new space starts as one free range. Existing allocations keep their offsets, which is what lets a caller grow the buffer behind the allocator — copying its contents to a larger one — without reallocating anything.

Returns false without changing anything if the allocator has no free node to describe the new range, or if the size would overflow a u32. The added size is rounded up to the alignment, so the appended range starts aligned like every other node.

Source

fn rebin_node(&mut self, node_index: NodeIndex, size: u32)

Moves a free node to the bin for its new size, keeping its place in the neighbor list.

Used to grow a free range in place when the space behind it is added to the chunk.

Source

fn insert_node_into_bin(&mut self, size: u32, data_offset: u32) -> NodeIndex

Creates a free node and inserts it at the head of the bin for its size.

The caller is responsible for linking the node into the neighbor list.

Source

fn remove_node_from_bin(&mut self, node_index: NodeIndex)

Removes a node from its bin and puts it back into the free list.

The caller is responsible for fixing up the neighbor list, and must read anything it still needs from the node before calling this: the node may be handed out again by the next Allocator::insert_node_into_bin.

Source

pub fn allocation_size(&self, allocation: Allocation) -> u32

Returns the size an allocation reserved, in units.

This is the size requested at allocation time rounded up to the allocator’s alignment, which may be larger than what the caller asked for.

Source

pub fn size(&self) -> u32

The total size of the managed chunk, in units.

This grows with Allocator::extend.

Source

pub fn alignment(&self) -> u32

The alignment, in units, that every allocation starts at.

Source

pub fn storage_report(&self) -> StorageReport

Returns a summary of the free space remaining, and of the largest allocation that can be made in one piece.

Trait Implementations§

Source§

impl Debug for Allocator

Source§

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

Formats the value using the given formatter. 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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