pub unsafe trait Deallocator {
// Required method
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: NonZeroLayout);
}Expand description
The deallocation half of an allocator. Every type that issues memory must be able to take it back at drop time.
For arena allocators (BumpArena, parsers), deallocate is a no-op —
reclaim happens via reset() (see Allocator::reset).
§Safety
Trait-level invariants implementors must uphold — an implementor of this trait declares the following to all callers:
deallocateaccepts any pointer previously returned by the same allocation domain’sAllocator::allocate(or any other method of this trait pair that issued memory —allocate_zeroed,grow,shrink), paired with the sameNonZeroLayoutused to obtain it. For stateful allocators (Slab,BumpArena,StackAlloc,ExtendableSlab, etc.) the “domain” is the very&selfthat issued the pointer — pointers from a sibling instance of the same type are UB. For stateless allocators that delegate to a global backing (System,MmapBacked— the OS manages the per-mapping state — any ZST forwarder), the “domain” extends to any instance of the same type, since the OS / global heap is the actual owner. Implementors that are unclear should document explicitly; conservatively, callers should pass the very instance that issued the pointer.- After
deallocatereturns the pointer is invalid; the caller may not read, write, or compare its address to any new pointer (the integer value may be reused by the allocator). Implementors may additionally poison, quarantine, or return memory to the OS — those are defense-in-depth choices made by hardening wrappers, not requirements of the trait. - Calling
deallocatewith a pointer that did not originate from this allocator, with alayoutthat does not match the originalallocatecall, or with a pointer that has already beendeallocated, is undefined behavior. deallocatemay be called from any thread that holds the appropriate&selfreference. If the implementor is!Syncthe receiver&selfitself enforces single-thread access; ifSyncthe implementor must internally serialize concurrent calls.
Required Methods§
Sourceunsafe fn deallocate(&self, ptr: NonNull<u8>, layout: NonZeroLayout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: NonZeroLayout)
Release a previously allocated block.
§Safety
All four of:
ptrmust have been returned by a previous call to the same allocation domain’sallocate/allocate_zeroed/grow/shrink(see the trait-level safety doc — for stateful allocators that domain is the same&self; for stateless forwarders to a global backing it is any instance of the same type).layoutmust equal the layout supplied to the call that producedptr— samesizeand samealign. (Wrappers that transparently inflate the layout, e.g.Canary, must inverse-transform before forwarding.)ptrmust not have been previouslydeallocated. Double-free is UB.- After the call, the caller must not read, write, or compare
against new pointers the value of
ptr.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<A> Deallocator for &Awhere
A: Deallocator + ?Sized,
impl<A> Deallocator for &Awhere
A: Deallocator + ?Sized,
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: NonZeroLayout)
Implementors§
impl Deallocator for BumpDeallocator<'_>
impl Deallocator for HugePageBacked
impl Deallocator for LockedMmapBacked
impl Deallocator for MmapBacked
impl Deallocator for System
impl<B: Allocator + FixedRange, const CLASSES: usize> Deallocator for SizeClassed<B, CLASSES>
impl<B: FixedRange> Deallocator for BumpArena<B>
impl<B: FixedRange> Deallocator for StackAlloc<B>
impl<I: Allocator, H: WatermarkHandler> Deallocator for Watermark<I, H>
Available on
target_has_atomic=ptr only.