Struct System

struct System

The default memory allocator provided by the operating system.

This is based on malloc on Unix platforms and HeapAlloc on Windows, plus related functions. However, it is not valid to mix use of the backing system allocator with System, as this implementation may include extra work, such as to serve alignment requests greater than the alignment provided directly by the backing system allocator.

This type implements the GlobalAlloc trait. Currently the default global allocator is unspecified. Libraries, however, like cdylibs and staticlibs are guaranteed to use the System by default and as such work as if they had this definition:

use std::alloc::System;

#[global_allocator]
static A: System = System;

fn main() {
    let a = Box::new(4); // Allocates from the system allocator.
    println!("{a}");
}

You can also define your own wrapper around System if you'd like, such as keeping track of the number of all bytes allocated:

use std::alloc::{System, GlobalAlloc, Layout};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};

struct Counter;

static ALLOCATED: AtomicUsize = AtomicUsize::new(0);

unsafe impl GlobalAlloc for Counter {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let ret = unsafe { System.alloc(layout) };
        if !ret.is_null() {
            ALLOCATED.fetch_add(layout.size(), Relaxed);
        }
        ret
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        unsafe { System.dealloc(ptr, layout); }
        ALLOCATED.fetch_sub(layout.size(), Relaxed);
    }
}

#[global_allocator]
static A: Counter = Counter;

fn main() {
    println!("allocated bytes before main: {}", ALLOCATED.load(Relaxed));
}

It can also be used directly to allocate memory independently of whatever global allocator has been selected for a Rust program. For example if a Rust program opts in to using jemalloc as the global allocator, System will still allocate memory using malloc and HeapAlloc.

Implementations

impl Allocator for System

fn allocate(self: &Self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(self: &Self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
unsafe fn deallocate(self: &Self, ptr: NonNull<u8>, layout: Layout)
unsafe fn grow(self: &Self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed(self: &Self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink(self: &Self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<[u8]>, AllocError>

impl Clone for System

fn clone(self: &Self) -> System

impl Copy for System

impl Debug for System

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl Default for System

fn default() -> System

impl Freeze for System

impl GlobalAlloc for crate::alloc::System

unsafe fn alloc(self: &Self, layout: Layout) -> *mut u8
unsafe fn alloc_zeroed(self: &Self, layout: Layout) -> *mut u8
unsafe fn dealloc(self: &Self, ptr: *mut u8, _layout: Layout)
unsafe fn realloc(self: &Self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8

impl RefUnwindSafe for System

impl Send for System

impl Sync for System

impl Unpin for System

impl UnwindSafe for System

impl<T> Any for System

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for System

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for System

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

impl<T> CloneToUninit for System

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for System

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for System

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for System

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for System

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

impl<T, U> TryInto for System

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