Skip to main content

SystemAllocator

Struct SystemAllocator 

Source
pub struct SystemAllocator;
Expand description

An Allocator that delegates directly to the operating system.

SystemAllocator is a zero-sized type; it holds no state and is safe to share across threads. It is the typical backing allocator for arena, pool, and bump allocators when running on a hosted OS.

§Platform Notes

  • Unix — Uses posix_memalign to satisfy arbitrary alignment requests. The effective alignment is always at least sizeof(void*) (usually 8 or 16 bytes) because posix_memalign requires it.
  • Windows — Uses _aligned_malloc / _aligned_free. The effective alignment is at least sizeof(void*).
  • Otheralloc always returns None.

Trait Implementations§

Source§

impl Allocator for SystemAllocator

Source§

unsafe fn alloc(&self, layout: Layout) -> Option<NonNull<u8>>

Allocates a block of memory according to layout.

Returns Some(ptr) where ptr is aligned to at least layout.align() and valid for layout.size() bytes. Returns None if the OS call fails or if the target platform is unsupported.

§Safety
  • layout.size() must be greater than zero; zero-sized allocations are rejected by posix_memalign and _aligned_malloc.
  • The returned pointer must eventually be passed to dealloc with the same layout.
Source§

unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout)

Releases a block previously allocated by this SystemAllocator.

§Safety
  • ptr must have been returned by a prior successful call to alloc on this same SystemAllocator.
  • layout must exactly match the layout passed to that alloc call.
  • After dealloc returns, ptr is invalid and must not be accessed.
  • Calling dealloc twice for the same pointer is undefined behaviour.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.