#! @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): 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::(), 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; }