Struct blink_alloc::BlinkAlloc
source · pub struct BlinkAlloc<A: Allocator = Global> { /* private fields */ }
Expand description
Single-threaded blink allocator.
Blink-allocator is arena-based allocator that allocates memory in growing chunks and serve allocations from them. When chunk is exhausted a new larger chunk is allocated.
Deallocation is no-op. BlinkAlloc
can be reset
to free all chunks except the last one, that will be reused.
Blink allocator aims to allocate a chunk large enough to serve all allocations between resets.
A shared and mutable reference to the BlinkAlloc
implement
Allocator
trait.
When “nightly” feature is enabled, Allocator
trait is
core::alloc::Allocator
. Otherwise it is duplicated trait defined
in allocator-api2
.
Resetting blink allocator requires mutable borrow, so it is not possible
to do while shared borrow is alive. That matches requirement of
Allocator
trait - while Allocator
instance
(a shared reference to BlinkAlloc
) or any of its clones are alive,
allocated memory must be valid.
This version of blink-allocator is single-threaded. It is possible
to send to another thread, but cannot be shared.
Internally it uses Cell
for interior mutability and requires
that state cannot be changed from another thread.
For multi-threaded version see SyncBlinkAlloc
.
Requires "sync"
feature.
Example
let mut blink = BlinkAlloc::new();
let layout = std::alloc::Layout::new::<[u32; 8]>();
let ptr = blink.allocate(layout).unwrap();
let ptr = NonNull::new(ptr.as_ptr() as *mut u8).unwrap(); // Method for this is unstable.
unsafe {
std::ptr::write(ptr.as_ptr().cast(), [1, 2, 3, 4, 5, 6, 7, 8]);
blink.deallocate(ptr, layout);
}
blink.reset();
Example that uses nightly’s allocator_api
let mut blink = BlinkAlloc::new();
let mut vec = Vec::new_in(&blink);
vec.push(1);
vec.extend(1..3);
vec.extend(3..10);
drop(vec);
blink.reset();
Implementations§
source§impl BlinkAlloc<Global>
impl BlinkAlloc<Global>
sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates new blink allocator that uses global allocator to allocate memory chunks.
See BlinkAlloc::new_in
for using custom allocator.
sourcepub const fn with_chunk_size(chunk_size: usize) -> Self
pub const fn with_chunk_size(chunk_size: usize) -> Self
Creates new blink allocator that uses global allocator to allocate memory chunks. With this method you can specify initial chunk size.
See BlinkAlloc::new_in
for using custom allocator.
source§impl<A> BlinkAlloc<A>where
A: Allocator,
impl<A> BlinkAlloc<A>where A: Allocator,
sourcepub const fn new_in(allocator: A) -> Self
pub const fn new_in(allocator: A) -> Self
Creates new blink allocator that uses provided allocator to allocate memory chunks.
See BlinkAlloc::new
for using global allocator.
sourcepub const fn with_chunk_size_in(chunk_size: usize, allocator: A) -> Self
pub const fn with_chunk_size_in(chunk_size: usize, allocator: A) -> Self
Creates new blink allocator that uses global allocator to allocate memory chunks. With this method you can specify initial chunk size.
See BlinkAlloc::new_in
for using custom allocator.
Trait Implementations§
source§impl<A> Allocator for &mut BlinkAlloc<A>where
A: Allocator,
impl<A> Allocator for &mut BlinkAlloc<A>where A: Allocator,
source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)source§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
allocator_api
)ptr
. Read moresource§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)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>
allocator_api
)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>
allocator_api
)grow
, but also ensures that the new contents are set to zero before being
returned. Read moresource§impl<A> Allocator for BlinkAlloc<A>where
A: Allocator,
impl<A> Allocator for BlinkAlloc<A>where A: Allocator,
source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)source§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>
allocator_api
)source§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
allocator_api
)ptr
. Read moresource§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)allocate
, but also ensures that the returned memory is zero-initialized. Read moresource§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>
allocator_api
)grow
, but also ensures that the new contents are set to zero before being
returned. Read more