pub struct Dlmalloc<A = System>(/* private fields */);
Expand description
An allocator instance
Instances of this type are used to allocate blocks of memory. For best
results only use one of these. Currently doesn’t implement Drop
to release
lingering memory back to the OS. That may happen eventually though!
Implementations§
Source§impl<A> Dlmalloc<A>
impl<A> Dlmalloc<A>
Sourcepub const fn new_with_allocator(sys_allocator: A) -> Dlmalloc<A>
pub const fn new_with_allocator(sys_allocator: A) -> Dlmalloc<A>
Creates a new instance of an allocator
Source§impl<A: Allocator> Dlmalloc<A>
impl<A: Allocator> Dlmalloc<A>
Sourcepub unsafe fn malloc(&mut self, size: usize, align: usize) -> *mut u8
pub unsafe fn malloc(&mut self, size: usize, align: usize) -> *mut u8
Allocates size
bytes with align
align.
Returns a null pointer if allocation fails. Returns a valid pointer otherwise.
Safety and contracts are largely governed by the GlobalAlloc::alloc
method contracts.
Sourcepub unsafe fn calloc(&mut self, size: usize, align: usize) -> *mut u8
pub unsafe fn calloc(&mut self, size: usize, align: usize) -> *mut u8
Same as malloc
, except if the allocation succeeds it’s guaranteed to
point to size
bytes of zeros.
Sourcepub unsafe fn free(&mut self, ptr: *mut u8, size: usize, align: usize)
pub unsafe fn free(&mut self, ptr: *mut u8, size: usize, align: usize)
Deallocates a ptr
with size
and align
as the previous request used
to allocate it.
Safety and contracts are largely governed by the GlobalAlloc::dealloc
method contracts.
Sourcepub unsafe fn realloc(
&mut self,
ptr: *mut u8,
old_size: usize,
old_align: usize,
new_size: usize,
) -> *mut u8
pub unsafe fn realloc( &mut self, ptr: *mut u8, old_size: usize, old_align: usize, new_size: usize, ) -> *mut u8
Reallocates ptr
, a previous allocation with old_size
and
old_align
, to have new_size
and the same alignment as before.
Returns a null pointer if the memory couldn’t be reallocated, but ptr
is still valid. Returns a valid pointer and frees ptr
if the request
is satisfied.
Safety and contracts are largely governed by the GlobalAlloc::realloc
method contracts.
Sourcepub unsafe fn trim(&mut self, pad: usize) -> bool
pub unsafe fn trim(&mut self, pad: usize) -> bool
If possible, gives memory back to the system if there is unused memory at the high end of the malloc pool or in unused segments.
You can call this after freeing large blocks of memory to potentially reduce the system-level memory requirements of a program. However, it cannot guarantee to reduce memory. Under some allocation patterns, some large free blocks of memory will be locked between two used chunks, so they cannot be given back to the system.
The pad
argument represents the amount of free trailing space to
leave untrimmed. If this argument is zero, only the minimum amount of
memory to maintain internal data structures will be left. Non-zero
arguments can be supplied to maintain enough trailing space to service
future expected allocations without having to re-obtain memory from the
system.
Returns true
if it actually released any memory, else false
.