pub struct Scratch;
Expand description
Memory allocator that will certainly be cleaned up in the next Janet Garbage Collection cycle.
If this crate are build with the nightly
feature enabled, this type also implements
the Allocator
trait. That means that with the nightly
feature set it’s possible to use this allocator with Rust types that uses allocator as
parameter, like Box
.
Implementations§
source§impl Scratch
impl Scratch
sourcepub fn malloc(&self, layout: Layout) -> Option<NonNull<[u8]>>
pub fn malloc(&self, layout: Layout) -> Option<NonNull<[u8]>>
Attempts to allocate a block of memory.
On success, returns a NonNull<[u8]>
meeting the size and alignment
guarantees of layout
.
The returned block may have a larger size than specified by layout.size()
, and
may or may not have its contents initialized.
sourcepub fn calloc(&self, layout: Layout) -> Option<NonNull<[u8]>>
pub fn calloc(&self, layout: Layout) -> Option<NonNull<[u8]>>
Behaves like allocate
, but also ensures that the returned memory is
zero-initialized.
sourcepub unsafe fn realloc(
&self,
ptr: NonNull<[u8]>,
layout: Layout,
new_size: usize
) -> Option<NonNull<[u8]>>
pub unsafe fn realloc( &self, ptr: NonNull<[u8]>, layout: Layout, new_size: usize ) -> Option<NonNull<[u8]>>
Shrink or grow a block of memory to the given new_size
.
The block is described by the given ptr
pointer and layout
.
§Safety
This function is unsafe because undefined behavior can result if the caller does not ensure all of the following:
ptr
must be currently allocated via this allocator,layout
must be the same layout that was used to allocate that block of memory,new_size
must be greater than zero.new_size
, when rounded up to the nearest multiple oflayout.align()
, must not overflow (i.e., the rounded value must be less thanusize::MAX
).
Trait Implementations§
source§impl Allocator for Scratch
Available on crate feature nightly
only.
impl Allocator for Scratch
nightly
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