Skip to content
CryoCryo home
Stdliballoc

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

PropertyType
slotLayout
slots_per_blocku64
free_listFreeNode*
blocksBlock*

Constructors

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;

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>;

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;

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;