Skip to main content

Crate ps_alloc

Crate ps_alloc 

Source
Expand description

A reasonably safe allocator: a thin, checked wrapper over std::alloc exposing C-style alloc, free, and realloc.

Every allocation is prefixed with a hidden header storing a marker and the allocation’s size, which lets the crate detect some misuse (double frees, corrupted or foreign pointers) at runtime, on a best-effort basis. All allocations are aligned to HEADER_SIZE (16) bytes.

Any error other than NullPtr returned by free or realloc (or realloc’s recoverable NewAllocationFailed) indicates that the program is already in an undefined state.

§Example

let ptr = ps_alloc::alloc(64)?;

unsafe {
    ptr.write(42);
    assert_eq!(ptr.read(), 42);

    ps_alloc::free(ptr).expect("the pointer came from alloc");
}

Enums§

AllocationError
Errors returned by crate::alloc.
DeallocationError
Errors returned by crate::free.
ReallocationError
Errors returned by crate::realloc.

Constants§

HEADER_SIZE
The size in bytes of the hidden header prefixed to every allocation; also the alignment of every allocation produced by this crate.

Functions§

alloc
A reasonably safe implementation of alloc.
free
This function is the counterpart to crate::alloc.
realloc
Reallocates memory allocated by crate::alloc.