Struct dlmalloc::Dlmalloc

source ·
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 Dlmalloc<System>

source

pub const fn new() -> Dlmalloc<System>

Creates a new instance of an allocator

source§

impl<A> Dlmalloc<A>

source

pub const fn new_with_allocator(sys_allocator: A) -> Dlmalloc<A>

Creates a new instance of an allocator

source§

impl<A: Allocator> Dlmalloc<A>

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub unsafe fn destroy(self) -> usize

Releases all allocations in this allocator back to the system, consuming self and preventing further use.

Returns the number of bytes released to the system.

Auto Trait Implementations§

§

impl<A> RefUnwindSafe for Dlmalloc<A>
where A: RefUnwindSafe,

§

impl<A> Send for Dlmalloc<A>
where A: Send,

§

impl<A = System> !Sync for Dlmalloc<A>

§

impl<A> Unpin for Dlmalloc<A>
where A: Unpin,

§

impl<A> UnwindSafe for Dlmalloc<A>
where A: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.