Skip to content
CryoCryo home
Stdliballoc

pool

import std::alloc::pool; · source

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;
}
const DEFAULT_SLOTS_PER_BLOCK: u64 = 64;

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.

MethodNotes
new(slot)Default block granularity (64 slots). Panics if the slot is smaller than a pointer — the free list needs that room.
with_block_size(slot, slots_per_block)Bigger blocks mean fewer allocations and more unused tail.
allocate_slot()Result<NonNull<u8>, AllocError>
deallocate_slot(ptr)The pointer must have come from this pool and not 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.