Trait vectorscan::alloc::MallocLikeAllocator
source · pub trait MallocLikeAllocator {
// Required methods
fn allocate(&self, size: usize) -> Option<NonNull<u8>>;
fn deallocate(&self, ptr: NonNull<u8>);
}
Available on crate feature
alloc
only.Expand description
The alloc/free interface required by vectorscan methods.
This is named “malloc-like” because it mirrors the interface provided by
libc::malloc()
and libc::free()
. This differs from GlobalAlloc
in two ways:
- no
Layout
argument is provided to the deallocate method, so the implementation must record the size of each allocation somewhere. - only a
size
argument is provided to the allocate method, with the alignment requirements of the memory being relegated to comments and docstrings.
Required Methods§
sourcefn allocate(&self, size: usize) -> Option<NonNull<u8>>
fn allocate(&self, size: usize) -> Option<NonNull<u8>>
Allocate a region of memory of at least size
bytes.
The alignment of the returned memory region is specified by the
implementation. Returns None
if allocation fails for any reason.
sourcefn deallocate(&self, ptr: NonNull<u8>)
fn deallocate(&self, ptr: NonNull<u8>)
Free up a previously-allocated memory region at ptr
.
The value must be the exact same location returned by a previous call to
Self::allocate()
. The behavior if this method is called more than once
for a given ptr
is unspecified.
Implementors§
impl MallocLikeAllocator for LayoutTracker
LayoutTracker
implements three additional guarantees over the base
MallocLikeAllocator
:
- 0-size allocation requests always return
None
instead of allocating. - all allocations are aligned to at least 8 bytes.
- attempted double frees will panic instead.