pub struct OneRingAlloc;
Expand description
Global ring-allocator.
This allocator uses global allocator to allocate memory chunks.
Allocator uses ring buffer of chunks to allocate memory in front chunk, moving it to back if chunk is full. If next chunk is still occupied by previous allocation, allocator will allocate new chunk.
This type is ZST and data is stored in static variables, removing size overhead in collections.
Each thread will use thread-local rings to rotate over chunks. On thread exit all unused chunks are freed and the rest is moved to global ring.
When thread-local ring cannot allocate memory it will steal global ring or allocate new chunk from global allocator if global ring is empty.
Implementations§
Source§impl OneRingAlloc
impl OneRingAlloc
Sourcepub fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
pub fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Attempts to allocate a block of memory with global ring-allocator. Returns a pointer to the beginning of the block if successful.
Sourcepub unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
pub unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
Deallocates the memory referenced by ptr
.
§Safety
ptr
must denote a block of memory currently allocated via [allocate
] orOneRingAlloc::allocate
, andlayout
must fit that block of memory.
Sourcepub fn clean_global(&self)
pub fn clean_global(&self)
Cleans global shared rings.
When thread exists it frees all chunks that it allocated, except those that are still in use by currently allocated blocks. Chunks that are still in use are put to global shared rings.
Those get stolen by thread if thread needs new chunk.
This function frees all chunks that are in global shared rings if they are not in use by currently allocated blocks.
This function may reduce memory overhead if threads exist and blocks allocated by them is freed later, while all other threads are warm.
Sourcepub fn clean_local(&self)
pub fn clean_local(&self)
Cleans local rings.
Thread frees chunks that it allocated when it exists. While thread is running chunks are stored in thread-local rings and reused in circular manner without deallocation.
This method frees all chunks that are not in use by currently allocated blocks in the local rings. Call this when thread’s memory usage drops significantly and you want to reduce memory overhead.
Trait Implementations§
Source§impl Allocator for OneRingAlloc
impl Allocator for OneRingAlloc
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§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§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 grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
grow
, but also ensures that the new contents are set to zero before being
returned. Read moreSource§impl Clone for OneRingAlloc
impl Clone for OneRingAlloc
Source§fn clone(&self) -> OneRingAlloc
fn clone(&self) -> OneRingAlloc
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more