Struct bulk_allocator::BulkAlloc
source · 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,
impl<B> BulkAlloc<B>where B: GlobalAlloc,
sourcepub const MAX_CACHE_SIZE: usize = 65_528usize
pub const MAX_CACHE_SIZE: usize = 65_528usize
The max byte size that BulkAlloc
can cache.
Trait Implementations§
source§impl<B> Drop for BulkAlloc<B>where
B: GlobalAlloc,
impl<B> Drop for BulkAlloc<B>where B: GlobalAlloc,
source§impl<B> GlobalAlloc for BulkAlloc<B>where
B: GlobalAlloc,
impl<B> GlobalAlloc for BulkAlloc<B>where B: GlobalAlloc,
source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
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.
source§unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)
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.