Skip to main content

Realloc

Trait Realloc 

Source
pub unsafe trait Realloc: Alloc + Free {
    // Provided methods
    unsafe fn realloc_uninit(
        &self,
        ptr: NonNull<MaybeUninit<u8>>,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> { ... }
    unsafe fn realloc_zeroed(
        &self,
        ptr: NonNull<MaybeUninit<u8>>,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<NonNull<MaybeUninit<u8>>, Self::Error> { ... }
}
Expand description

Reallocation function:
realloc_uninit(ptr: NonNull<_>, old: Layout, new: Layout) -> Result<NonNull<_>, _>
realloc_zeroed(ptr: NonNull<_>, old: Layout, new: Layout) -> Result<NonNull<_>, _>

§Safety

Invariants that Realloc must uphold for the soundness of various unsafe code include:

ItemDescription
alignReturned allocations must have at least Layout::align alignment.
sizeReturned allocations must be valid to read and/or write for at least Layout::size bytes.
pinReturned allocations must remain valid at their initial address for the lifetime of Self (typically 'static!), or until freed by Free or successful Realloc - whichever comes first. In some cases (e.g. structs allocating from arrays on self without heap indirection), this might mean that the traits can only be implemented on shared references, rather than on the structs themselves.
compatibleReturned allocations must be compatible with all other thin traits implemented on the same allocator (at minimum in the sense that they must not cause undefined behavior when passed pointers to said allocations)
exclusiveReturned allocations belong exclusively to the caller. Double allocating the same pool slot without a free would be undefined behavior.
exceptionsSystem allocators typically use extern "C" FFI, which is not safe to unwind exceptions through. Ensure you catch any expected exceptions such as C++’s std::bad_alloc and SEH exceptions like STATUS_HEAP_CORRUPTION (e.g. don’t use HEAP_GENERATE_EXCEPTIONS) on the C/C++ side before returning into Rust code.
threadsAllocators are typically implicitly Send+Sync, which means the underlying FFI calls must be thread safe too.
zeroedAllocations returned by Realloc::realloc_zeroed must be zeroed between their old and new actualized size - which might be for more than new_size (especially if thin::SizeOfDebug is implemented.)
preservedAllocations returned by Realloc must preserve their previous contents (although truncation if new_size is smaller than the old size is OK)

Provided Methods§

Source

unsafe fn realloc_uninit( &self, ptr: NonNull<MaybeUninit<u8>>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<MaybeUninit<u8>>, Self::Error>

Reallocate an existing allocation, ptr, belonging to self.

§Safety
  • ptr must belong to self
  • ptr will no longer be accessible after a succesful realloc (realloc_uninit returns Ok(…))
  • old_layout must exactly match the Layout last used to successfully (re)allocate ptr
Source

unsafe fn realloc_zeroed( &self, ptr: NonNull<MaybeUninit<u8>>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<MaybeUninit<u8>>, Self::Error>

Reallocate an existing allocation, ptr, belonging to self.

Any newly allocated memory will be zeroed. Any memory beyond old_layout.size() may or may not be zeroed. This might be no new memory, even if new_layout.size() > old_layout.size() - the implementation could’ve rounded both the old and new sizes up to the same value. As such, it’s almost certainly a bug to use this to (re)allocate memory that came from {,re}alloc_uninit. Reallocate memory that came from {,re}alloc_zeroed instead.

§Safety
  • ptr must belong to self
  • ptr will no longer be accessible after a succesful realloc (realloc_uninit returns Ok(…))
  • old_layout must exactly match the Layout last used to successfully (re)allocate ptr

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'a, A: Realloc> Realloc for &'a A

Source§

unsafe fn realloc_uninit( &self, ptr: NonNull<MaybeUninit<u8>>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<MaybeUninit<u8>>, Self::Error>

Source§

unsafe fn realloc_zeroed( &self, ptr: NonNull<MaybeUninit<u8>>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<MaybeUninit<u8>>, Self::Error>

Implementors§

Source§

impl Realloc for AlignedMalloc

Safety ItemDescription
align⚠️ untested, but should be safe if thin::Alloc was
size⚠️ untested, but should be safe if thin::Alloc was
pin✔️ AlignedMalloc is 'static - reallocations by _aligned_realloc (MSVC) / free+aligned_alloc (C11) live until _aligned_free (MSVC) / freed (C89)
compatible⚠️ AlignedMalloc uses exclusively intercompatible fns - see type-level docs for details, especially “FFI Safety Caveats”
exclusive✔️ Allocations by _aligned_realloc / free+aligned_alloc are exclusive/unique
exceptions✔️ _aligned_realloc / free+aligned_alloc throw no exceptions (C API) and return null on error (possibly setting errno)
threads⚠️ thread-unsafe stdlibs existed once upon a time. I consider linking them in a multithreaded program defacto undefined behavior beyond the scope of this to guard against.
zeroed⚠️
preserved⚠️ untested, but should be the case…
Source§

impl Realloc for CoTaskMem

Source§

impl Realloc for CryptMem

Source§

impl Realloc for ialloc::allocator::alloc::Global

Source§

impl Realloc for ialloc::allocator::win32::Global

Source§

impl Realloc for Heap

Source§

impl Realloc for HeapNoSerialize

Source§

impl Realloc for IMalloc

Source§

impl Realloc for Local

Source§

impl Realloc for Malloc

Source§

impl Realloc for NewDelete

Source§

impl Realloc for NewDeleteAligned

Source§

impl Realloc for NewDeleteArray

Source§

impl Realloc for NewDeleteArrayAligned

Source§

impl Realloc for Null

Source§

impl Realloc for ProcessHeap

Source§

impl Realloc for StdAllocator<c_char>

Source§

impl Realloc for VirtualCommit

Source§

impl<'a, const A: usize, const B: usize, const N: usize> Realloc for &'a FixedPoolLinearProbe<A, B, N>
where [(); A]: ValidAlignLessThan1GiB,

Source§

impl<'a> Realloc for Bump<'a>

Source§

impl<A: Realloc> Realloc for AllocZst<A>

Source§

impl<A: Realloc> Realloc for DangleZst<A>

Source§

impl<A: Realloc> Realloc for PanicOverAlign<A>