Files
std/allocators/pool.sakura
T
2026-11-10 12:26:50 -06:00

223 lines
8.4 KiB
Plaintext

#! @file pool.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the `pool` allocator, which hands out blocks of one size from chunks
#! of memory it owns, and takes each back to hand out again.
module std::allocators;
#! An allocator handing out blocks of one size, carved from chunks of memory it
#! owns, and taking each back to hand out again: for values made and released
#! often, as the nodes of a list or a tree.
#!
#! A block given back goes on a list the next block is taken from, so handing one
#! out or taking one back costs a few instructions, and nothing goes back to the
#! program's heap until the pool is closed. `reset` takes back every block at
#! once, keeping the chunks to hand out again. What a block holds at first may be
#! anything.
#!
#! As an `allocator` it hands out blocks no larger and no more aligned than its
#! own, and gives `null` for any other, so a container given a pool holds one
#! element in each block it asks for, as a node does. `make` and `unmake` hand out
#! and take back values of a type.
#! @remarks A pool must outlive every block it handed out, and must not move while
#! a container refers to it.
pub struct pool : allocator, owning {
var _size: u64; #! The bytes of each block, a multiple of `_align`.
var _align: u64; #! What each block's address is a multiple of.
var _per: u64; #! How many blocks each chunk holds.
var _first: *mut pool::chunk; #! The chunks it took, the oldest first.
var _chunk: *mut pool::chunk; #! The chunk blocks are carved from, or null.
var _at: *mut u8; #! Where the next block carved begins.
var _end: *mut u8; #! Where the chunk's blocks end.
var _free: *mut u8; #! The last block given back, whose first word holds the address of the one before, or null.
var _out: u64; #! How many blocks are handed out.
}
# A chunk: this header, then its blocks.
struct pool::chunk {
var next: *mut pool::chunk; # The chunk taken after it, or null.
var size: u64; # The bytes of memory after the header.
}
# The bytes of a chunk before its blocks, which keeps them aligned as the heap
# aligns blocks.
def pool::header(): u64 {
return align(size_of::<pool::chunk>(), max_align);
}
#! Makes a pool handing out blocks of a size, aligned to a power of two, from
#! chunks of 64 KiB, or of one block where a block is larger.
#! @param size How many bytes each block holds, raised to a word, so a block
#! given back holds the address of the next.
#! @param alignment What each block's address is a multiple of, a power of two
#! no wider than `max_align`, raised to a word.
#! @returns The pool, which holds no memory yet.
pub def pool::new(size: u64, alignment: u64): pool {
val aligned := alignment < 8 ? 8 : alignment;
if aligned > max_align || (aligned & (aligned - 1)) != 0 {
fault("The alignment is not a power of two, or is wider than `max_align`.");
}
val each := align(size < 8 ? 8 : size, aligned);
val per := 65536 / each;
return pool { _size = each, _align = aligned, _per = per == 0 ? 1 : per };
}
#! Makes a pool handing out blocks for values of a type.
#! @tparam T The type.
#! @returns The pool, which holds no memory yet.
pub def pool::of<T>(): pool {
return pool::new(size_of::<T>(), align_of::<T>());
}
#! Gives back every chunk the pool took, which frees every block it handed out.
#! @param this The pool to close.
#! @remarks Values `make` put in blocks still handed out are not closed. The pool
#! is left holding nothing, so it may hand out blocks again.
pub def pool::close(&mut this): void {
var chunk := this._first;
while chunk != null {
val next := (*chunk).next;
raw_release(chunk as *mut u8);
chunk = next;
}
this._first = null;
this._chunk = null;
this._at = null;
this._end = null;
this._free = null;
this._out = 0;
}
#! The bytes each block holds.
#! @param this The pool.
#! @returns The bytes.
pub def pool::block_size(&this): u64 {
return this._size;
}
#! How many blocks are handed out and not yet given back.
#! @param this The pool.
#! @returns How many.
pub def pool::count(&this): u64 {
return this._out;
}
#! Hands out a block: the last given back, or one carved from a chunk, taking
#! another chunk from the program's heap where every one is carved.
#! @param this The pool to acquire from.
#! @param size The number of bytes the block holds, no more than the pool's.
#! @param align What the address of its first byte is a multiple of, no wider
#! than the pool's.
#! @returns The block, which may hold anything at first, or `null` where it is
#! larger or more aligned than the pool's.
pub def pool::acquire(&mut this, size: u64, align: u64): *mut u8 {
if size > this._size || align > this._align {
return null;
}
this._out++;
if this._free != null {
val made := this._free;
this._free = (made as *mut *mut u8)[0];
return made;
}
if this._at == this._end {
this.next_chunk();
}
val made := this._at;
this._at = this._at + this._size;
return made;
}
# Carves the chunk after the one carved, taking a chunk from the program's heap
# where there is none after it.
def pool::next_chunk(&mut this): void {
var next := this._chunk == null ? this._first : (*this._chunk).next;
if next == null {
val bytes := this._per * this._size;
next = raw_allocate(pool::header() + bytes) as *mut pool::chunk;
(*next).next = null;
(*next).size = bytes;
# Only the last chunk has none after it, and only a pool with none has none
# carved.
if this._chunk == null {
this._first = next;
} else {
(*this._chunk).next = next;
}
}
this._chunk = next;
this._at = next as *mut u8 + pool::header();
this._end = this._at + (*next).size;
}
#! Takes back a block, which the next block handed out is.
#! @param this The pool the block came from.
#! @param addr The block, or `null`, which takes back nothing.
#! @param size The number of bytes it was acquired with.
pub def pool::dispose(&mut this, addr: *mut u8, size: u64): void {
return if addr == null;
(addr as *mut *mut u8)[0] = this._free;
this._free = addr;
this._out--;
}
#! Hands out a block of another size holding what a block it handed out held,
#! which is the block itself, as every block holds the pool's size.
#! @param this The pool the block came from.
#! @param addr The block to resize, or `null` to acquire one.
#! @param old The number of bytes it was acquired with.
#! @param size The number of bytes the new block holds.
#! @param align What the address of its first byte is a multiple of.
#! @returns The block, or `null` where the size or alignment is larger than the
#! pool's, leaving the block as it was.
pub def pool::resize(&mut this, addr: *mut u8, old: u64, size: u64, align: u64): *mut u8 {
if size > this._size || align > this._align {
return null;
}
return addr == null ? this.acquire(size, align) : addr;
}
#! Takes back every block the pool handed out, keeping its chunks to hand out
#! again.
#! @param this The pool to reset.
#! @remarks Values `make` put in blocks still handed out are not closed.
pub def pool::reset(&mut this): void {
this._chunk = null;
this._at = null;
this._end = null;
this._free = null;
this._out = 0;
}
#! Moves a value into a block the pool hands out.
#! @tparam T The value's type, no larger and no more aligned than the pool's
#! blocks.
#! @param this The pool.
#! @param value The value, which the block then holds.
#! @returns The value's address, until `unmake` takes it back.
pub def pool::make<T>(&mut this, value: T): *mut T {
val at := this.acquire(size_of::<T>(), align_of::<T>()) as *mut T;
if at == null {
fault("The value is larger, or more aligned, than the pool's blocks.");
}
if const T is closeable {
memset(at as *mut u8, 0, size_of::<T>());
}
at[0] = move(&mut value);
return at;
}
#! Closes a value `make` put in a block, and takes the block back.
#! @tparam T The value's type.
#! @param this The pool the value came from.
#! @param at The value's address, or `null`, which takes back nothing.
pub def pool::unmake<T>(&mut this, at: *mut T): void {
return if at == null;
close(through(at));
this.dispose(at as *mut u8, size_of::<T>());
}