Files
std/containers/rc.sakura
T
2026-11-13 21:54:22 -06:00

168 lines
5.3 KiB
Plaintext

#! @file rc.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `rc`, one value on the heap that every copy owns, counted plainly,
#! for values one thread shares, as Rust's `Rc`; and `rc_weak`, which refers to
#! it without keeping it. `arc` counts atomically, for values threads share.
#!
#! Each copy counts one more owner, and the last to close closes the value.
#! Counting plainly costs an add, where counting atomically costs a locked add:
#! fourteen times as long for a copy alone, and half again as long for a list
#! walked with a counted cursor, measured as `docs/performance.md` says.
module std::containers;
# Allocates a block a counted value is written into: zeroed where the value
# closes, since writing it closes what the block held, as `box` allocates.
def allocate_for<T>(size: u64): *mut u8 {
if const T is closeable {
return raw_allocate_zeroed(size);
}
return raw_allocate(size);
}
# What an `rc` points at: the counts, then the value. The weak count holds one
# for all the owners together, so the last owner to close and the last weak
# reference to close agree on which gives the memory back.
struct rc_cell<T> {
var owners: u64;
var weak: u64;
var value: T;
}
#! One value on the heap, owned by every copy, which `copy` makes, and closed
#! where the last copy closes. Copies count plainly, so threads never share
#! one; `arc` is for that.
#!
#! An `rc` is one word, the counted value's address, which is never null, so
#! `maybe<rc<T>>` is one word too. Moving one moves the address and leaves the
#! one moved from empty, closing nothing.
#! @tparam T What it holds.
pub struct rc<T> : owning, copyable, moveable {
var _at: &mut rc_cell<T>;
}
#! A reference to a value an `rc` owns that does not keep it: `lock` gives an
#! owner while any other is left.
#! @tparam T What it refers to.
pub struct rc_weak<T> : owning, copyable, moveable {
var _at: *mut rc_cell<T>;
}
#! Moves a value to the heap, with one owner.
#! @param value The value.
#! @returns Its first owner.
pub def rc<T>::new(value: T): rc<T> {
val at := allocate_for::<rc_cell<T>>(size_of::<rc_cell<T>>()) as *mut rc_cell<T>;
at[0] = rc_cell::<T> { owners = 1, weak = 1, value = move(&mut value) };
return rc::<T> { _at = through(at) };
}
#! Lends the value an `rc` owns.
#! @param this The owner.
#! @returns The value, good while any owner is left.
pub def rc<T>::get(&this): &T {
return &this._at.value;
}
#! Lends the value an `rc` owns, to be changed, which every copy sees.
#! @param this The owner.
#! @returns The value, good while any owner is left.
pub def rc<T>::get(&mut this): &mut T {
return &mut this._at.value;
}
#! How many owners share the value.
#! @param this An owner.
#! @returns How many, this one among them.
pub def rc<T>::count(&this): u64 {
return this._at.owners;
}
#! A reference to the value that does not keep it.
#! @param this An owner.
#! @returns The reference.
pub def rc<T>::weak(&this): rc_weak<T> {
this._at.weak++;
return rc_weak::<T> { _at = this._at as *mut rc_cell<T> };
}
#! Makes another owner of the same value.
#! @param other An owner.
#! @returns The new owner.
pub def rc<T>::copy_of(other: &rc<T>): rc<T> {
other._at.owners++;
return rc::<T> { _at = other._at };
}
#! Moves an owner, leaving the one moved from empty.
#! @param other The owner to move from.
#! @returns The owner it was.
pub def rc<T>::move_of(other: &mut rc<T>): rc<T> {
val made := rc::<T> { _at = other._at };
(other as *mut u64)[0] = 0;
return made;
}
#! Closes an owner: the value too where it was the last, and the memory where no
#! weak reference is left either.
#! @param this The owner, which is empty after.
pub def rc<T>::close(&mut this): void {
val at := this._at as *mut rc_cell<T>;
return if at == null;
(this as *mut u64)[0] = 0;
at[0].owners--;
return if at[0].owners != 0;
close(&mut at[0].value);
rc_cell_let_go::<T>(at);
}
#! An owner of the value a weak reference refers to, where any is left.
#! @param this The weak reference.
#! @returns The new owner, or nothing where the last closed.
pub def rc_weak<T>::lock(&this): maybe<rc<T>> {
return nothing {} if this._at == null || this._at[0].owners == 0;
this._at[0].owners++;
return rc::<T> { _at = through(this._at) };
}
#! Makes another weak reference to the same value.
#! @param other A weak reference.
#! @returns The new one.
pub def rc_weak<T>::copy_of(other: &rc_weak<T>): rc_weak<T> {
if other._at != null {
other._at[0].weak++;
}
return rc_weak::<T> { _at = other._at };
}
#! Moves a weak reference, leaving the one moved from empty.
#! @param other The weak reference to move from.
#! @returns The weak reference it was.
pub def rc_weak<T>::move_of(other: &mut rc_weak<T>): rc_weak<T> {
val made := rc_weak::<T> { _at = other._at };
other._at = null;
return made;
}
#! Closes a weak reference, giving back the memory where it was the last
#! reference of any kind.
#! @param this The weak reference, which is empty after.
pub def rc_weak<T>::close(&mut this): void {
val at := this._at;
return if at == null;
this._at = null;
rc_cell_let_go::<T>(at);
}
# Lets go of the weak count's one, giving back the memory where it was the last.
def rc_cell_let_go<T>(at: *mut rc_cell<T>): void {
at[0].weak--;
if at[0].weak == 0 {
raw_release(at as *mut u8);
}
}