pub trait Realloc: Grow + Shrink {
// Provided methods
unsafe fn realloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, Self::Error> { ... }
unsafe fn rezalloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, Self::Error> { ... }
}Expand description
A memory allocation interface which can arbitrarily resize allocations.
Provided Methods§
Sourceunsafe fn realloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, Self::Error>
unsafe fn realloc( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, Self::Error>
Reallocates a block, growing or shrinking as needed.
On grow, preserves existing contents up to old_layout.size(), and on
shrink, truncates to new_layout.size().
On failure, the original memory will not be deallocated.
Returns a dangling pointer if layout.size() ==
0.
§Safety
The caller must ensure:
ptrpoints to a block previously allocated with this allocator.old_layoutdescribes exactly the same block.
§Errors
Errors are implementation-defined, refer to [Self::Error] and Error.
The standard implementations may return:
Err(Error::AllocFailed(layout, cause))if allocation fails.causeis typicallyCause::Unknown. If theos_err_reportingfeature is enabled, it will beCause::OSErr. In this case,oserrwill be the error fromIOErr::last_os_error.raw_os_error().
Sourceunsafe fn rezalloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, Self::Error>
unsafe fn rezalloc( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, Self::Error>
Reallocates a block, growing or shrinking as needed, with extra bytes being zeroed.
On grow, preserves existing contents up to old_layout.size(), and on
shrink, truncates to new_layout.size().
On failure, the original memory will not be deallocated.
Returns a dangling pointer if layout.size() ==
0.
§Safety
The caller must ensure:
ptrpoints to a block previously allocated with this allocator.old_layoutdescribes exactly the same block.
§Errors
Errors are implementation-defined, refer to [Self::Error] and Error.
The standard implementations may return:
Err(Error::AllocFailed(layout, cause))if allocation fails.causeis typicallyCause::Unknown. If theos_err_reportingfeature is enabled, it will beCause::OSErr. In this case,oserrwill be the error fromIOErr::last_os_error.raw_os_error().
Implementations on Foreign Types§
impl Realloc for Global
no_alloc only.impl Realloc for System
std and non-crate feature no_alloc only.