Initial Commit
This commit is contained in:
1625 files changed
+43618
No files matched your search
@@ -0,0 +1,287 @@
|
||||
#! @file arena.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the `arena` allocator, which hands out blocks of large chunks of
|
||||
#! memory one after another and takes them all back at once.
|
||||
|
||||
module std::allocators;
|
||||
|
||||
|
||||
#! An allocator handing out blocks of chunks of memory one after another, which
|
||||
#! it owns, and taking them all back at once.
|
||||
#!
|
||||
#! A block is handed out where the last one ended, which costs a comparison and
|
||||
#! an addition, and none is given back by itself: they all go back where the
|
||||
#! arena is reset, rewound, or closed. The last block handed out grows and
|
||||
#! shrinks where it is, and goes back where it is disposed of. What a block
|
||||
#! holds at first may be anything.
|
||||
#!
|
||||
#! An arena is for memory whose lifetime is one phase of the work, as what a pass
|
||||
#! of a compiler makes and drops: `reset` frees all of it at once and keeps the
|
||||
#! chunks for the next phase, and `mark` and `rewind` free what was handed out
|
||||
#! after a point, as a stack does. Nodes linked to one another by reference may
|
||||
#! live in one, as long as nothing refers to them past where it frees them.
|
||||
#! @remarks A container given an arena refers to it, so the arena must not move
|
||||
#! while one does, and must not be reset, rewound, or closed while one
|
||||
#! holds memory it handed out.
|
||||
pub struct arena : allocator, owning {
|
||||
var _size: u64; #! How many bytes each chunk it takes holds, at least.
|
||||
var _blocks: arena::run; #! Its blocks, from its chunks.
|
||||
}
|
||||
|
||||
#! Where an arena had handed out to, which `rewind` takes it back to.
|
||||
pub struct arena::point {
|
||||
var _spot: arena::spot; #! Where it had handed out to.
|
||||
}
|
||||
|
||||
# Where a run of blocks had handed out to.
|
||||
struct arena::spot {
|
||||
var chunk: *mut arena::chunk; # The chunk it handed out of, or null.
|
||||
var at: *mut u8; # Where the next block would have begun.
|
||||
}
|
||||
|
||||
# The blocks an arena hands out, from a list of chunks.
|
||||
struct arena::run {
|
||||
var first: *mut arena::chunk; # The first chunk, or null where it has none.
|
||||
var chunk: *mut arena::chunk; # The chunk it hands out of, or null.
|
||||
var at: *mut u8; # Where the next block begins.
|
||||
var end: *mut u8; # Where the chunk's memory ends.
|
||||
var last: *mut u8; # The last block it handed out, while it may grow.
|
||||
}
|
||||
|
||||
# A chunk: this header, then the memory blocks are handed out of.
|
||||
struct arena::chunk {
|
||||
var next: *mut arena::chunk; # The chunk after it, or null.
|
||||
var size: u64; # The bytes of memory after the header.
|
||||
var owned: bool; # Whether the arena took it, and gives it back.
|
||||
}
|
||||
|
||||
#! The fewest bytes a chunk of an arena holds.
|
||||
pub const arena::least: u64 = 256;
|
||||
|
||||
#! Makes an arena that takes chunks of 64 KiB from the program's heap as it needs
|
||||
#! them.
|
||||
#! @returns The arena, which holds no memory yet.
|
||||
pub def arena::new(): arena {
|
||||
return arena::new(65536);
|
||||
}
|
||||
|
||||
#! Makes an arena that takes chunks of at least a size from the program's heap as
|
||||
#! it needs them. A block larger than that has a chunk of its own size.
|
||||
#! @param size How many bytes each chunk holds, at least, which is raised to
|
||||
#! `arena::least`.
|
||||
#! @returns The arena, which holds no memory yet.
|
||||
pub def arena::new(size: u64): arena {
|
||||
return arena { _size = size < arena::least ? arena::least : size };
|
||||
}
|
||||
|
||||
#! Makes an arena that hands out the bytes of a buffer first, then takes chunks
|
||||
#! of 64 KiB from the program's heap once the buffer is full.
|
||||
#! @param buffer The bytes to hand out first, which the arena never gives back
|
||||
#! and which must outlive it.
|
||||
#! @returns The arena.
|
||||
pub def arena::over(buffer: slice<u8>): arena {
|
||||
var made := arena::new();
|
||||
val low := align(buffer.data as u64, max_align);
|
||||
val high := buffer.data as u64 + buffer.length;
|
||||
if high < low || high - low < arena::header() + max_align {
|
||||
return made;
|
||||
}
|
||||
|
||||
val chunk := low as *mut arena::chunk;
|
||||
(*chunk).next = null;
|
||||
(*chunk).size = high - low - arena::header();
|
||||
(*chunk).owned = false;
|
||||
made._blocks.first = chunk;
|
||||
made._blocks.enter(chunk);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Gives back every chunk the arena took, which frees every block it handed out.
|
||||
#! @param this The arena to close.
|
||||
#! @remarks A buffer it was made over is not given back. The arena is left
|
||||
#! holding nothing, so it may hand out blocks again.
|
||||
pub def arena::close(&mut this): void {
|
||||
this._blocks.free();
|
||||
}
|
||||
|
||||
#! Hands out a block after the last it handed out, or at the start of another
|
||||
#! chunk where the chunk has too little left.
|
||||
#! @param this The arena to acquire from.
|
||||
#! @param size The number of bytes the block holds.
|
||||
#! @param align What the address of its first byte is a multiple of, a power of
|
||||
#! two no wider than `max_align`.
|
||||
#! @returns The block, which may hold anything at first.
|
||||
#! @remarks Where the program's heap gives no memory, the program stops, so this
|
||||
#! never gives `null`.
|
||||
pub def arena::acquire(&mut this, size: u64, align: u64): *mut u8 {
|
||||
return this._blocks.take(size, align, this._size);
|
||||
}
|
||||
|
||||
#! Takes back a block, which an arena does only for the last it handed out, so
|
||||
#! that the next block takes its place. Any other goes back where the arena is
|
||||
#! reset, rewound, or closed.
|
||||
#! @param this The arena the block came from.
|
||||
#! @param addr The block.
|
||||
#! @param size The number of bytes it was acquired or last resized with.
|
||||
pub def arena::dispose(&mut this, addr: *mut u8, size: u64): void {
|
||||
this._blocks.give_back(addr, size);
|
||||
}
|
||||
|
||||
#! Hands out a block of another size holding what a block it handed out held.
|
||||
#! The last block it handed out grows or shrinks where it is while its chunk has
|
||||
#! room; any other is copied to a new block.
|
||||
#! @param this The arena the block came from.
|
||||
#! @param addr The block to resize, or `null` to acquire one.
|
||||
#! @param old The number of bytes it was acquired or last resized 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 new block, which may hold anything past what it keeps.
|
||||
pub def arena::resize(&mut this, addr: *mut u8, old: u64, size: u64, align: u64): *mut u8 {
|
||||
return this._blocks.regrow(addr, old, size, align, this._size);
|
||||
}
|
||||
|
||||
#! Frees every block the arena handed out, keeping its chunks to hand out again.
|
||||
#! @param this The arena to reset.
|
||||
pub def arena::reset(&mut this): void {
|
||||
this._blocks.restart();
|
||||
}
|
||||
|
||||
#! Where the arena has handed out to, which `rewind` takes it back to.
|
||||
#! @param this The arena.
|
||||
#! @returns The point.
|
||||
pub def arena::mark(&this): arena::point {
|
||||
return arena::point { _spot = arena::spot { chunk = this._blocks.chunk, at = this._blocks.at } };
|
||||
}
|
||||
|
||||
#! Frees every block the arena handed out after a point `mark` gave, keeping its
|
||||
#! chunks to hand out again.
|
||||
#! @param this The arena to rewind.
|
||||
#! @param to The point, which must be one `mark` gave since the arena was last
|
||||
#! reset, and not one already rewound past.
|
||||
pub def arena::rewind(&mut this, to: arena::point): void {
|
||||
this._blocks.back_to(to._spot);
|
||||
}
|
||||
|
||||
# The bytes of a chunk before its memory, which keeps the memory aligned as the
|
||||
# heap aligns blocks.
|
||||
def arena::header(): u64 {
|
||||
return align(size_of::<arena::chunk>(), max_align);
|
||||
}
|
||||
|
||||
# The first byte of a chunk's memory.
|
||||
def arena::data_of(chunk: *mut arena::chunk): *mut u8 {
|
||||
return (chunk as *mut u8) + arena::header();
|
||||
}
|
||||
|
||||
# Hands out the blocks of a chunk from its start.
|
||||
def arena::run::enter(&mut this, chunk: *mut arena::chunk): void {
|
||||
this.chunk = chunk;
|
||||
this.at = arena::data_of(chunk);
|
||||
this.end = this.at + (*chunk).size;
|
||||
this.last = null;
|
||||
}
|
||||
|
||||
def arena::run::take(&mut this, size: u64, alignment: u64, least: u64): *mut u8 {
|
||||
var start := align(this.at as u64, alignment);
|
||||
if this.chunk == null || start > this.end as u64 || size > this.end as u64 - start {
|
||||
start = this.next_chunk(size, alignment, least);
|
||||
}
|
||||
this.at = (start + size) as *mut u8;
|
||||
this.last = start as *mut u8;
|
||||
return start as *mut u8;
|
||||
}
|
||||
|
||||
# Moves on to a chunk the block fits: one after this chunk, kept from before the
|
||||
# arena was reset or rewound, or a new one put after this chunk. Gives where the
|
||||
# block begins in it.
|
||||
def arena::run::next_chunk(&mut this, size: u64, alignment: u64, least: u64): u64 {
|
||||
var after := this.chunk == null ? this.first : (*this.chunk).next;
|
||||
while after != null {
|
||||
val start := align(arena::data_of(after) as u64, alignment);
|
||||
val end := arena::data_of(after) as u64 + (*after).size;
|
||||
if start <= end && size <= end - start {
|
||||
this.enter(after);
|
||||
return start;
|
||||
}
|
||||
after = (*after).next;
|
||||
}
|
||||
|
||||
val bytes := size + alignment > least ? size + alignment : least;
|
||||
val made := raw_allocate(arena::header() + bytes) as *mut arena::chunk;
|
||||
(*made).size = bytes;
|
||||
(*made).owned = true;
|
||||
if this.chunk == null {
|
||||
(*made).next = this.first;
|
||||
this.first = made;
|
||||
}
|
||||
else {
|
||||
(*made).next = (*this.chunk).next;
|
||||
(*this.chunk).next = made;
|
||||
}
|
||||
this.enter(made);
|
||||
return align(this.at as u64, alignment);
|
||||
}
|
||||
|
||||
# Takes back the last block handed out, where it is that. Gives whether it was.
|
||||
def arena::run::give_back(&mut this, addr: *mut u8, size: u64): bool {
|
||||
if addr == null || addr != this.last || addr + size != this.at {
|
||||
return false;
|
||||
}
|
||||
this.at = addr;
|
||||
this.last = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
def arena::run::regrow(&mut this, addr: *mut u8, old: u64, size: u64, alignment: u64, least: u64): *mut u8 {
|
||||
if addr == null {
|
||||
return this.take(size, alignment, least);
|
||||
}
|
||||
if addr == this.last && addr + old == this.at && size <= this.end as u64 - addr as u64 {
|
||||
this.at = addr + size;
|
||||
return addr;
|
||||
}
|
||||
|
||||
val made := this.take(size, alignment, least);
|
||||
memcpy(made, addr, old < size ? old : size);
|
||||
return made;
|
||||
}
|
||||
|
||||
# Hands out from the first chunk again.
|
||||
def arena::run::restart(&mut this): void {
|
||||
if this.first == null {
|
||||
return;
|
||||
}
|
||||
this.enter(this.first);
|
||||
}
|
||||
|
||||
# Hands out from a spot again.
|
||||
def arena::run::back_to(&mut this, to: arena::spot): void {
|
||||
if to.chunk == null {
|
||||
this.restart();
|
||||
return;
|
||||
}
|
||||
this.chunk = to.chunk;
|
||||
this.at = to.at;
|
||||
this.end = arena::data_of(to.chunk) + (*to.chunk).size;
|
||||
this.last = null;
|
||||
}
|
||||
|
||||
# Gives back every chunk the run took, leaving it holding none.
|
||||
def arena::run::free(&mut this): void {
|
||||
var each := this.first;
|
||||
while each != null {
|
||||
val next := (*each).next;
|
||||
if (*each).owned {
|
||||
raw_release(each as *mut u8);
|
||||
}
|
||||
each = next;
|
||||
}
|
||||
this.first = null;
|
||||
this.chunk = null;
|
||||
this.at = null;
|
||||
this.end = null;
|
||||
this.last = null;
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
#! @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>());
|
||||
}
|
||||
Reference in new issue
Block a user