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:
| Item | Description |
|---|---|
align | Returned allocations must have at least Layout::align alignment. |
size | Returned allocations must be valid to read and/or write for at least Layout::size bytes. |
pin | Returned 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. |
compatible | Returned 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) |
exclusive | Returned allocations belong exclusively to the caller. Double allocating the same pool slot without a free would be undefined behavior. |
exceptions | System 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. |
threads | Allocators are typically implicitly Send+Sync, which means the underlying FFI calls must be thread safe too. |
zeroed | Allocations 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.) |
preserved | Allocations returned by Realloc must preserve their previous contents (although truncation if new_size is smaller than the old size is OK) |
Provided Methods§
Sourceunsafe fn realloc_uninit(
&self,
ptr: NonNull<MaybeUninit<u8>>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<MaybeUninit<u8>>, Self::Error>
unsafe fn realloc_uninit( &self, ptr: NonNull<MaybeUninit<u8>>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<MaybeUninit<u8>>, Self::Error>
Sourceunsafe fn realloc_zeroed(
&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>
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
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
impl<'a, A: Realloc> Realloc for &'a A
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>
Implementors§
impl Realloc for AlignedMalloc
| Safety Item | Description |
|---|---|
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… |