pub struct BulkAlloc<B>where
    B: GlobalAlloc,{ /* private fields */ }
Expand description

BulkAlloc is an implementation of GlobalAlloc holding memory cache. This struct acquires bulk memories from the backend, and frees them on the drop at once for the performance.

Each instance has 2 kinds of caches: some forward linked lists to store a specific sized pointers, and one red black tree to store arbitrary sized memory block.

The balanced tree cache merges 2 holding pointers if they are placed next to each other. BulkAlloc stores a pointer into the tree cache if the size is large enough or if the pointer can be merged into another; otherwise, it is stored into the linked list cache for the size.

Safety

Instance drop releases all the memories acquired from the backend. All the pointers allocated via the instance will be invalid after then. Accessing such a pointer may lead to memory unsafety even if the pointer itself is not deallocated.

See also

Implementations§

source§

impl<B> BulkAlloc<B>where B: GlobalAlloc,

source

pub const MAX_CACHE_SIZE: usize = 65_528usize

The max byte size that BulkAlloc can cache.

source§

impl<B> BulkAlloc<B>where B: GlobalAlloc,

source

pub const fn new(backend: B) -> Self

Creates a new instance.

Trait Implementations§

source§

impl<B> Drop for BulkAlloc<B>where B: GlobalAlloc,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<B> GlobalAlloc for BulkAlloc<B>where B: GlobalAlloc,

source§

unsafe fn alloc(&self, layout: Layout) -> *mut u8

Method alloc delegates the request to the backend if layout is too large (i.e. the size is greater than MAX_CACHE_SIZE or the align is greater than align_of::<usize>(). Note that usually, the alignment of Layout is less than or equals to the value unless the caller dares to enlarge it.)

Otherwise, alloc searches the cache for a larger or the same size memory in the cache. If no proper memory is found, it acquires a MEMORY_CHUNK_SIZE bytes chunk from the backend allocator at first. Then, it takes a pointer from the memory block to return and caches the rest again.

Safety

All the pointers allocated via the instance will be invalid after the instance drop. Accessing such a pointer may lead to memory unsafety even if the pointer itself is not deallocated.

read more

source§

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)

Method dealloc delegates the request to the backend if layout is too large (i.e. the size is greater than MAX_CACHE_SIZE or the align is greater than align_of::<usize>(). Note that usually, the alignment of Layout is less than or equals to the value unless the caller dare to enlarge it.)

Otherwise, dealloc stores the passed pointer into the proper cache. It is when the instance is dropped when the pointer is released.

Safety

All the pointers allocated via the instance will be invalid after the instance drop. Accessing such a pointer may lead to memory unsafety even if the pointer itself is not deallocated.

read more

1.28.0 · source§

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8

Behaves like alloc, but also ensures that the contents are set to zero before being returned. Read more
1.28.0 · source§

unsafe fn realloc( &self, ptr: *mut u8, layout: Layout, new_size: usize ) -> *mut u8

Shrink or grow a block of memory to the given new_size in bytes. The block is described by the given ptr pointer and layout. Read more

Auto Trait Implementations§

§

impl<B> !RefUnwindSafe for BulkAlloc<B>

§

impl<B> !Send for BulkAlloc<B>

§

impl<B> !Sync for BulkAlloc<B>

§

impl<B> Unpin for BulkAlloc<B>where B: Unpin,

§

impl<B> UnwindSafe for BulkAlloc<B>where B: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.