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!
Implementations§
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 moreSource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
impl AllocatorClone for Global
impl AllocatorNightly for Global
impl Copy for Global
impl StaticAllocator for Global
impl TrivialClone for Global
Auto Trait Implementations§
impl Freeze for Global
impl RefUnwindSafe for Global
impl Send for Global
impl Sync for Global
impl Unpin for Global
impl UnsafeUnpin for Global
impl UnwindSafe for Global
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Printable for T
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type #102070)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read more