pub struct Global;Expand description
A wrapper for the global allocator.
This type implements the Allocator trait by forwarding calls
to the allocator registered with the #[global_allocator] attribute
if there is one, or the std crate’s default.
Global is not “the global allocator”, but a wrapper for it.
If the global allocator used Global, it would call itself recursively.
To avoid this, Global does not implement GlobalAlloc.
Similar to alloc, dealloc, and the other global allocation functions,
when and how calls are forwarded to the allocator registered with
#[global_allocator] is unspecified. See their safety docs for more information.
In particular, even if you know which allocator was registered
as the global allocator, calling it directly is not the same as calling
Global. You cannot deallocate memory from one using the other.
Global must be treated like an opaque allocator that only guarantees the contract
described in the docs of Allocator, as well as the following:
- All instances of
Globalare equivalent. - Allocations from
Globalare only invalidated by calls to de-/reallocating functions. If no such call is made, then the allocation will live for the rest of the program. - The global allocation functions in the
allocmodule are equivalent to the methods onGlobal, except that they disallow zero-sized allocations, and implicitly ignore any returned excess size. In particular, you may deallocate memory fromalloc::allocusingGlobal.deallocateand vice-versa.
Note that the current implementation of Global does not take advantage of
some features of Allocator, such as zero-sized allocations (which currently
return a dangling pointer) and overallocating.
This may change in the future. You must not rely on it for correctness!
Trait Implementations§
1.100.0 (const: unstable) · Source§impl Allocator for Global
impl Allocator for Global
Source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Source§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocate, but also ensures that the returned memory is zero-initialized. Read moreSource§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
ptr. Read more