pool
import std::alloc::pool; · source
Types
Pool
type struct Pool {
slot: Layout;
slots_per_block: u64;
free_list: FreeNode*;
blocks: Block*;
static new(slot: Layout) -> Pool;
static with_block_size(slot: Layout, slots_per_block: u64) -> Pool;
allocate_slot(mut &this) -> Result<NonNull<u8>, AllocError>;
deallocate_slot(mut &this, ptr: NonNull<u8>) -> void;
}
Fixed-size slots. A pool is configured with one slot layout at construction and refuses anything that doesn't match. Freed slots are threaded onto an intrusive free list, so allocation and deallocation are both O(1). When the free list runs dry it allocates a block and carves slots_per_block fresh slots out of it.
Reach for a pool when you have many values of the same type with churn-heavy lifetimes — game entities, AST nodes under frequent edit, request objects. For mixed sizes, use an arena or the global allocator.
Properties
| Property | Type |
|---|---|
slot | Layout |
slots_per_block | u64 |
free_list | FreeNode* |
blocks | Block* |
Constructors
static new(slot: Layout) -> Pool;
static new(slot: Layout) -> Pool;Create a pool serving slot-sized allocations with the default block granularity. Panics if slot.size() < sizeof(FreeNode*): the intrusive free list needs a pointer's worth of room in each slot.
static with_block_size(slot: Layout, slots_per_block: u64) -> Pool;
static with_block_size(slot: Layout, slots_per_block: u64) -> Pool;Create a pool with a custom number of slots per backing block. Larger blocks reduce malloc frequency at the cost of more unused tail memory for small pools.
Methods
allocate_slot(mut &this) -> Result<NonNull<u8>, AllocError>;
allocate_slot(mut &this) -> Result<NonNull<u8>, AllocError>;Grab a free slot, carving a new block if the free list is empty. Returns Err(OutOfMemory) if the backing allocator refuses the block request.
deallocate_slot(mut &this, ptr: NonNull<u8>) -> void;
deallocate_slot(mut &this, ptr: NonNull<u8>) -> void;Return a slot to the pool. ptr must have come from a previous allocate_slot on this pool and must not have been freed.
Trait implementations
implement trait Drop for struct Pool
implement trait Allocator for struct Pool
Through the Allocator trait, a mismatched layout returns Err(InvalidLayout) — you picked the wrong allocator for that request. drop returns every block; it does not run destructors on whatever the slots held.
Constants
const DEFAULT_SLOTS_PER_BLOCK: u64 = 64;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 Pool {
slot: Layout;
slots_per_block: u64;
free_list: FreeNode*;
blocks: Block*;
}Pool: O(1) allocator for uniformly-sized slots.
Pool.slot: Layouttype struct Layout {
size: u64;
alignment: u64;
}Layout: size + alignment of a memory block.
std::alloc::layoutPool.slots_per_block: u64Pool.free_list: FreeNode*type struct FreeNode {
next: FreeNode*;
}Pool.blocks: Block*type struct Block {
data: u8*;
next: Block*;
}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.
static Pool::new(slot: Layout) -> PoolCreate a pool serving slot-sized allocations with the default block granularity. Panics if slot.size() < sizeof(FreeNode*): the intrusive free list needs a pointer's worth of room in each slot.
(parameter) slot: Layouttype struct Pool {
slot: Layout;
slots_per_block: u64;
free_list: FreeNode*;
blocks: Block*;
}Pool: O(1) allocator for uniformly-sized slots.
std::alloc::poolstatic Pool::with_block_size(slot: Layout, slots_per_block: u64) -> PoolCreate a pool with a custom number of slots per backing block. Larger blocks reduce malloc frequency at the cost of more unused tail memory for small pools.
(parameter) slots_per_block: u64Pool::allocate_slot(mut &this) -> Result<NonNull<u8>, AllocError>Grab a free slot, carving a new block if the free list is empty. Returns Err(OutOfMemory) if the backing allocator refuses the block request.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type struct NonNull<T> {
ptr: T*;
}type struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}Pool::deallocate_slot(mut &this, ptr: NonNull<u8>) -> voidReturn a slot to the pool. ptr must have come from a previous allocate_slot on **this** pool and must not have been freed.
(parameter) ptr: NonNull<u8>const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const DEFAULT_SLOTS_PER_BLOCK: u64 = 64;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).
Pool.slots_per_block: u64Pool.slot: LayoutPool.free_list: FreeNode*Pool.blocks: Block*static Pool::new(slot: Layout) -> PoolCreate a pool serving slot-sized allocations with the default block granularity. Panics if slot.size() < sizeof(FreeNode*): the intrusive free list needs a pointer's worth of room in each slot.
sizeof(T)The size of a type in bytes, as a compile-time constant - usable in static_assert. alignof(T) gives the alignment.
static Pool::with_block_size(slot: Layout, slots_per_block: u64) -> PoolCreate a pool with a custom number of slots per backing block. Larger blocks reduce malloc frequency at the cost of more unused tail memory for small pools.
std::alloc::poolPool::allocate_slot(mut &this) -> Result<NonNull<u8>, AllocError>Grab a free slot, carving a new block if the free list is empty. Returns Err(OutOfMemory) if the backing allocator refuses the block request.
Result::Err(E)AllocErrorKind::OutOfMemoryThe underlying allocator returned null; the system is out of memory or hit an address-space limit.
std::alloc::allocatorPool::deallocate_slot(mut &this, ptr: NonNull<u8>) -> voidReturn a slot to the pool. ptr must have come from a previous allocate_slot on **this** pool and must not have been freed.
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 referencetype trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatorAllocErrorKind::InvalidLayoutThe requested layout is incompatible with this allocator (e.g., the size or alignment doesn't match a Pool's fixed slot). The caller passed the wrong allocator for the requested layout.
std::alloc::allocator