Struct fixed_bump::DynamicBump
source · pub struct DynamicBump(_);
Expand description
Implementations§
source§impl DynamicBump
impl DynamicBump
sourcepub fn new(layout: Layout) -> Self
pub fn new(layout: Layout) -> Self
Creates a new DynamicBump
. layout
specifies the size and
alignment of the chunks allocated internally by the allocator.
sourcepub fn allocate(&self, layout: Layout) -> Option<NonNull<[u8]>>
pub fn allocate(&self, layout: Layout) -> Option<NonNull<[u8]>>
Tries to allocate memory with a size and alignment matching layout
.
Returns a pointer to the memory on success, or None
on failure.
The memory is valid until the DynamicBump
is dropped. Note that the
returned memory could be larger than layout.size()
.
This method is similar to Allocator::allocate
, except it returns an
Option
instead of a Result
.
Allocation is guaranteed to succeed, assuming the global allocator
succeeds, if layout.size()
is less than or equal to
self.layout().size()
and layout.align()
is less
than or equal to self.layout().align()
. See
Self::can_allocate
.
sourcepub fn alloc_value<T>(&self, value: T) -> &mut T
pub fn alloc_value<T>(&self, value: T) -> &mut T
Allocates a value of type T
.
The memory is initialized with value
and a reference to the value is
returned. Note that the value’s destructor will not be called
automatically.
Panics
Panics if this allocator cannot allocate memory matching
Layout::new::<T>()
(see Self::can_allocate
). Note that if the
global allocator fails, handle_alloc_error
is called instead of
panicking.
For an equivalent that doesn’t panic or call handle_alloc_error
,
see Self::try_alloc_value
.
sourcepub fn try_alloc_value<T>(&self, value: T) -> Result<&mut T, T>
pub fn try_alloc_value<T>(&self, value: T) -> Result<&mut T, T>
Tries to allocate a value of type T
.
If the allocation succeeds, the memory is initialized with value
and
a reference to the value is returned. Note that the value’s destructor
will not be called automatically.
Allocation succeeds if and only if Self::allocate
is able to
allocate memory matching Layout::new::<T>()
. See Self::allocate
for details regarding the circumstances in which allocation can fail.
Errors
If allocation fails, Err(value)
is returned.
sourcepub fn can_allocate(&self, layout: Layout) -> bool
pub fn can_allocate(&self, layout: Layout) -> bool
Returns whether this allocator can allocate memory matching layout
.
This is guaranteed to return true if layout.size()
is less than or
equal to self.layout().size()
and layout.align()
is less than or equal to self.layout().align()
. It
may return true if the alignment is bigger, but never if the size is.
Trait Implementations§
source§impl Allocator for DynamicBump
Available on crate features allocator_api
or allocator-fallback
only.
impl Allocator for DynamicBump
allocator_api
or allocator-fallback
only.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 more