Skip to main content

Free

Trait Free 

Source
pub unsafe trait Free: Meta {
    // Provided methods
    unsafe fn free(&self, ptr: NonNull<MaybeUninit<u8>>) { ... }
    unsafe fn free_nullable(&self, ptr: *mut MaybeUninit<u8>) { ... }
}
Expand description

Deallocation function:
free(ptr: NonNull<MaybeUninit<u8>>)

§Safety

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

ItemDescription
compatibleIt must be safe to pass the result of any thin::Alloc or thin::Realloc function, implemented on the same type, to any thin::Free function (once - twice would be an unsound double free.)
exceptionsSystem allocators typically use extern "C" FFI, which is not safe to unwind exceptions through. Avoid C++ or SEH exceptions in favor of error codes or fatal exceptions.
threadsAllocators are typically implicitly Send+Sync, which means the underlying FFI calls must be thread safe too.

Provided Methods§

Source

unsafe fn free(&self, ptr: NonNull<MaybeUninit<u8>>)

Deallocate an allocation, ptr, belonging to self.

§Safety
  • ptr must belong to self
  • ptr will no longer be accessible after free
Examples found in repository?
examples/alloc-tester.rs (line 35)
28    pub fn thin(&mut self) -> &mut Self where A : thin::Alloc + thin::Free {
29        let mut thin = AlignmentRange { min: Alignment::MAX, max: Alignment::MAX };
30        for (dst, min_size) in [(&mut thin.min, 1), (&mut thin.max, 4096)] {
31            for _ in 0 .. 100 {
32                let alloc = (self.create)();
33                let addrs = [(); 4096].into_iter().enumerate().map(|(i, _)| alloc.alloc_uninit(i%16+min_size).unwrap()).collect::<Vec<_>>();
34                let addrbits = addrs[..].iter().copied().map(|addr| addr.as_ptr() as usize).reduce(|x,y| x|y).unwrap();
35                addrs.iter().copied().for_each(|addr| unsafe { alloc.free(addr) });
36                let align = Alignment::new(1 << addrbits.trailing_zeros()).unwrap();
37                *dst = align.min(*dst);
38            }
39        }
40        self.thin = Some(thin);
41        self
42    }
Source

unsafe fn free_nullable(&self, ptr: *mut MaybeUninit<u8>)

Deallocate an allocation, ptr, belonging to self.

§Safety
  • ptr may be null, in which case this is a noop
  • ptr must belong to self
  • ptr will no longer be accessible after free

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: Free> Free for &'a A

Source§

unsafe fn free(&self, ptr: NonNull<MaybeUninit<u8>>)

Source§

unsafe fn free_nullable(&self, ptr: *mut MaybeUninit<u8>)

Implementors§

Source§

impl Free for AlignedMalloc

Safety ItemDescription
compatible⚠️ AlignedMalloc uses exclusively intercompatible fns - see type-level docs for details, especially “FFI Safety Caveats”
exceptions✔️ _aligned_free / free / free_aligned_sized throw no exceptions (C API) and return no errors.
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.
Source§

impl Free for CoTaskMem

Safety ItemDescription
compatible✔️ CoTaskMem uses exclusively intercompatible fns
exceptions✔️ CoTaskMemFree is “infalliable”. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads✔️ CoTaskMemFree uses a no-init gCMalloc::Free → HeapFree with dwFlags=0 (e.g. not using HEAP_NO_SERIALIZE) under the hood
Source§

impl Free for CryptMem

Safety ItemDescription
compatible✔️ CryptMem uses exclusively intercompatible fns
exceptions✔️ CryptMemFree is “infalliable”. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads✔️ CryptMemFree uses LocalFree(…), and Local claims to be thread safe
Source§

impl Free for Global

Safety ItemDescription
compatible✔️ Global uses exclusively intercompatible Global* fns
exceptions✔️ GlobalFree is “infalliable”. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads⚠️ GlobalFree eventually calls HeapFree without HEAP_NO_SERIALIZE, which should be thread safe…
Source§

impl Free for Heap

Safety ItemDescription
compatible✔️ Heap uses exclusively intercompatible Heap* fns
exceptions✔️ HeapFree returns FALSE/0 on error per docs / lack of HEAP_GENERATE_EXCEPTIONS. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads✔️ HEAP_NO_SERIALIZE is not used, making this thread safe.
Source§

impl Free for HeapNoSerialize

Safety ItemDescription
compatible✔️ HeapNoSerialize uses exclusively intercompatible Heap* fns
exceptions✔️ HeapFree returns FALSE/0 on error per docs / lack of HEAP_GENERATE_EXCEPTIONS. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads✔️ HEAP_NO_SERIALIZE is used, making this not thread safe. However, we enforce HeapNoSerialize : !Send + !Sync as validated by AssertNotSendSync.
Source§

impl Free for IMalloc

Safety ItemDescription
compatible✔️ IMalloc uses exclusively intercompatible fns
exceptions✔️ IMalloc::Free returns no errors per docs. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads✔️ IMalloc is !Send + !Sync
Source§

impl Free for Local

Safety ItemDescription
compatible✔️ Local uses exclusively intercompatible Local* fns
exceptions✔️ LocalFree is “infalliable”. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads⚠️ LocalFree eventually calls HeapFree without HEAP_NO_SERIALIZE, which should be thread safe…
Source§

impl Free for Malloc

Safety ItemDescription
compatible✔️ free is compatible with malloc / realloc / calloc (C89 § 7.20.3.2 ¶ 2) and with _recalloc
exceptions✔️ free throws no exceptions (it’s C) and returns no errors (C89 § 7.20.3.2 ¶ 3)
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.
Source§

impl Free for NewDelete

Safety ItemDescription
compatible✔️ NewDelete uses exclusively intercompatible un-arrayed, implicit-alignment operators from the same stdlib.
compatible✔️ ::operator delete(void*) is compatible with ::operator new(size_t, nothrow_t) (C++03 § 18.4.1.1 ¶ 12)
exceptions✔️ ::operator delete(void*) is throw() and returns no errors (C++03 § 18.4.1.1 ¶ 10) / noexcept (C++11+)
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.
Source§

impl Free for NewDeleteArray

Safety ItemDescription
compatible✔️ NewDeleteArray uses exclusively intercompatible arrayed, implicit-alignment operators from the same stdlib.
compatible✔️ ::operator delete[](void*) is compatible with ::operator new[](size_t, nothrow_t) (C++03 § 18.4.1.2 ¶ 11)
exceptions✔️ ::operator delete[](void*) is throw() and returns no errors (C++03 § 18.4.1.2 ¶ 9) / noexcept (C++11+)
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.
Source§

impl Free for Null

Source§

impl Free for ProcessHeap

Source§

impl Free for VirtualCommit

Safety ItemDescription
compatible✔️ VirtualCommit uses exclusively intercompatible Virtual* fns
exceptions✔️ VirtualFree returns FALSE/0 on error per docs, page structures live outside of process memory where they’re “incorruptable”
threads⚠️ As everything builds upon Virtual*, and Microsoft isn’t a bunch of dummies, VirtualFree should should be thread safe, although it’s poorly documented
Source§

impl<A: Free> Free for AllocZst<A>

Source§

impl<A: Free> Free for DangleZst<A>

Source§

impl<A: Free> Free for PanicOverAlign<A>

Source§

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