#! @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(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 { 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>` 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 : owning, copyable, moveable { var _at: &mut rc_cell; } #! 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 : owning, copyable, moveable { var _at: *mut rc_cell; } #! Moves a value to the heap, with one owner. #! @param value The value. #! @returns Its first owner. pub def rc::new(value: T): rc { val at := allocate_for::>(size_of::>()) as *mut rc_cell; at[0] = rc_cell:: { owners = 1, weak = 1, value = move(&mut value) }; return rc:: { _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::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::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::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::weak(&this): rc_weak { this._at.weak++; return rc_weak:: { _at = this._at as *mut rc_cell }; } #! Makes another owner of the same value. #! @param other An owner. #! @returns The new owner. pub def rc::copy_of(other: &rc): rc { other._at.owners++; return rc:: { _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::move_of(other: &mut rc): rc { val made := rc:: { _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::close(&mut this): void { val at := this._at as *mut rc_cell; 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::(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::lock(&this): maybe> { return nothing {} if this._at == null || this._at[0].owners == 0; this._at[0].owners++; return rc:: { _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::copy_of(other: &rc_weak): rc_weak { if other._at != null { other._at[0].weak++; } return rc_weak:: { _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::move_of(other: &mut rc_weak): rc_weak { val made := rc_weak:: { _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::close(&mut this): void { val at := this._at; return if at == null; this._at = null; rc_cell_let_go::(at); } # Lets go of the weak count's one, giving back the memory where it was the last. def rc_cell_let_go(at: *mut rc_cell): void { at[0].weak--; if at[0].weak == 0 { raw_release(at as *mut u8); } }