heap
import std::alloc::heap; · source
alloc::heap is the library's own process heap: a segment allocator that maps its memory straight from the OS. It is what GlobalAllocator is backed by when a build sets [low_level] native_alloc, and NativeHeap names it directly, so a container can keep its storage there whatever GlobalAllocator is backed by.
All memory lives in 4 MiB, 4 MiB-aligned mappings ("segments"), so the segment that owns any block is found by masking the block's address — freeing never needs the caller to supply a size. Small requests are served from about forty size classes on a 16-byte granule, one class per segment, from a free list of returned blocks and then a bump frontier over unused space; a segment whose last block comes back is unmapped, so memory returns to the OS at once. Large or over-aligned requests get a mapping of their own. One global spinlock guards the metadata, so every allocating thread contends for the same lock. Freeing a pointer the heap never produced aborts with a message rather than corrupting its metadata.
Types
NativeHeap
type struct NativeHeap {}
A handle to the mmap-backed heap, holding no state. Every instance draws on the one process heap, so a block may be released through any of them. Its Allocator implementation lives in alloc::allocator, because allocator imports this module and the module graph rejects cycles.
import std::alloc::heap::{ NativeHeap };
import std::collections::array::{ Array };
mut xs: Array<i64, NativeHeap> = Array::<i64, NativeHeap>::new_in(NativeHeap::new());
xs.push(1);
Constructors
static new() -> NativeHeap;
static new() -> NativeHeap;A handle to the process's mmap-backed heap. Holds no state.
Trait implementations
implement trait Allocator for struct NativeHeap
std::alloc::allocator
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct NativeHeap {}The mmap-backed heap as an allocator: Array<T, NativeHeap>, String<NativeHeap> and the rest keep their storage here whatever GlobalAllocator is backed by. Every instance draws on the one process heap, so a block may be released through any of them.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct NativeHeap {}The mmap-backed heap as an allocator: Array<T, NativeHeap>, String<NativeHeap> and the rest keep their storage here whatever GlobalAllocator is backed by. Every instance draws on the one process heap, so a block may be released through any of them.
namespace std::collections::array;Array<T, A>: growable, heap-backed, contiguous sequence.
module pagetype struct Array<T, A = GlobalAllocator> where A: Allocator {}A growable sequence of Ts stored contiguously, owning its elements; A is the allocator its storage comes from. Indexing out of range panics; get answers None instead.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut xs: Array<i64, NativeHeap>static Array::new_in(alloc: A) -> Array<T, A>Empty array with a caller-supplied allocator.
std::collections::arraystatic NativeHeap::new() -> NativeHeapA handle to the process's mmap-backed heap. Holds no state.
std::alloc::heapmut xs: Array<i64, NativeHeap>Array::push(mut &this, value: T) -> voidAdd value after the last element, growing the storage when it is full. Panics on allocation failure; try_push returns the error instead.
type struct GlobalAllocator {}Zero-sized handle to the process-wide allocator. Construct with GlobalAllocator::new(); all instances dispatch through the same underlying backend.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocationError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocationError>;
}A source of heap memory: hands out blocks described by a Layout and takes them back. Collections, Box and String take an A: Allocator type parameter, so the storage strategy is the caller's choice; GlobalAllocator is the default.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference