Function hard::mem::malloc[][src]

pub unsafe fn malloc<T>() -> Result<NonNull<T>, HardError>
Expand description

Allocate sufficient hardened memory to store a value of type T, returning a pointer to the start of the allocated memory.

Uses the Sodium function sodium_malloc to securely allocate a region of memory, which will be mlocked, and surrounded with guard pages.

Returns Ok(ptr), where ptr is a pointer to the newly-allocated memory, if allocation was successful, otherwise returns a HardError.

Safety

This function returns a pointer to uninitialised memory, allocated outside of Rust’s memory management. As such, all the issues associated with manual memory management in languages like C apply: Memory must be initialised before use, it must be freed exactly once, and not used after having been freed. Memory allocated with this function should be freed using free from this module, rather than any other memory management tool, to preserve Sodium’s security invariants.

You must call super::init before using this function.