---
title: "Ownership"
description: "One count per block, releases sited on the path rather than at a scope, Perceus reuse, and cycles reclaimed by trial deletion rather than tracing."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.atoll.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Ownership

Atoll reclaims memory with a per-block reference count that the compiler
maintains, plus a scan that reclaims what counting alone cannot. No source
program contains a retain, a release or a free.

This page is the runtime side. For what a programmer writes and observes, see
[Memory](https://docs.atoll.workreef.dev/language/memory/) — [Destruction](https://docs.atoll.workreef.dev/language/memory/destruction/) for
where a release lands, [References](https://docs.atoll.workreef.dev/language/memory/references/) for where a
`^T` may travel, and [Cycles](https://docs.atoll.workreef.dev/language/memory/cycles/) for what collection
costs.

Counts are ordinary `u32` words, not atomics: the Store reactor is the sole
mutator of one instance's arena. Moving a value to another instance is an
explicit deep transfer — sharing a bare handle between reactors is invalid.

## One count per block

A block's count lives at a fixed offset behind its payload, so emitted code
reaches it with a constant. A fresh block starts at one. `u32::MAX` marks
immortal storage, such as a pooled literal, which retain and release leave
alone.

**The granularity is the block, and that is a language-visible fact.** One count
covers a whole backing buffer, so a pointer to a single element pins all of it.
There is no sub-block count and no per-view count.

It follows that **views diverge after a grow**. A live interior pointer holds a
count of its own, so growth moves to a fresh block and leaves the old one
pinned. The pointer stays valid and keeps observing the *old* contents —
the same divergence Go has between a slice and the array behind it, and safe for
the same reason.

## Releases go on the path, not at a scope

The compiler sites a release **on each path**, and *where* on the path is two
answers rather than one:

- **Forwarded or consumed** — returned, stored into an aggregate, or passed to
  an owning parameter. That *is* the consumption, and no release is emitted.
- **Never named again** — released on the spot, right after the op that defined
  it. `rc.rs` says why it is not held: "a live range the optimizer would have to
  shorten and a reader would have to explain".
- **Named later** — held, and sited at the **terminator**: before it when the
  block has no successors or when every edge owes the release, on an interposed
  edge block when only some do, and not in this block at all when a successor
  still names it.
- **Live on one arm of a branch and dead on the other** — the release goes on
  the dead arm.

That last case is where a naive scheme leaks. Here a value consumed on some
incoming edges and not others is a verifier error, not a silent gap.

**This page said "immediately after the last use" for the read-and-borrow case,
and that is the case it got wrong.** A value whose last use is a read or a
borrow reaches the end of its block, not the line after the read. The
difference is user-visible whenever the type owes a `Drop` body, and it is the
whole of what `ATOLL4101` reports —
`a_drop_body_runs_once_on_every_path`'s ordering rows measure it by having the
body write through a cell the program then reads.
[Destruction](https://docs.atoll.workreef.dev/language/memory/destruction/) is the reader-facing half.

## What a release does

Freeing a block may require walking it, and what the walk looks like is decided
by the type:

| Content | Release |
|---|---|
| nothing reference-carrying | free the block wholesale |
| a fixed set of fields | release each offset — no function needed |
| a tagged value | which offsets are live depends on the discriminant |
| a type with a `Drop` impl | run the body, then walk what remains |

The last row is the general mechanism rather than a special case, and the
classifier keys on exactly one thing: whether the type declares `Drop`. A
collection with reference-carrying elements lands there too — its walk is `len`
elements and `len` is a field, so no static offset list can describe it.

## Reuse

Perceus-style reuse fuses a destruction with a following allocation of
compatible storage. If the source count is one, generated code reuses the block
and initializes the new value in place; if it is shared, the code releases and
takes a fresh block.

Reuse is observable only as performance. Atoll does not expose stable object
addresses, and required field releases still run before anything is overwritten.

## Cycles are collected, not declared

Reference counting cannot reclaim a cycle, and Atoll can build one: `^T` is an
address, the fields behind it are mutable, and

```atoll nocheck the same declaration, the same assignment
a.next = b     // a list
a.next = a     // a cycle
```

are indistinguishable at the type level. Separating them at each assignment
would mean proving the target is not already reachable from the source — alias
analysis, which over-refuses in practice.

**So the compiler refuses nothing and asks for nothing.** There is no `@weak`,
no capture annotation and no cycle diagnostic.

### Trial deletion

A decrement that does **not** reach zero makes the block a candidate. A
collection walks a candidate's subgraph subtracting the edges inside it from a
*shadow* count. If a shadow reaches zero, every reference to that block came
from inside the subgraph, and the subgraph is garbage.

Three properties follow, and they are why this and not tracing:

- Work is proportional to **garbage**, not to the live heap.
- **No root enumeration.** A reference held by a parked frame is simply never
  subtracted, so the count stays positive and the block is kept. No stack maps,
  no safepoints, no marking phase.
- The edge enumeration it needs **is the release walk**. One traversal
  description, three leaf actions.

The real count is never touched. Classic trial deletion deflates it and restores
it afterwards; Atoll cannot, because a table's writes read it — guest code
observing a deflated count would write in place on a block another task is
reading. That is a miscompile rather than a leak, and it is the one failure the
shadow count exists to make unspellable.

Here is a cycle, built and collected:

```atoll run=42 collected=2
import intrinsics

struct Node {
    v    int
    next ^Node?
}

fn main() int {
    mut a: ^Node = ^Node { v: 21, next: None }
    b: ^Node = ^Node { v: 21, next: Some(a) }
    a.next = Some(b)
    intrinsics.gc_now()
    return a.v + b.v
}
```

`collected=2` is the part worth reading. The heap comes back to empty either
way — a program that never built a cycle also ends at zero — so the block count
the scan gave back is the only number that separates *collected* from *never
allocated in the first place*. Delete the `a.next = Some(b)` line and the
example still answers 42 with a clean heap; only `collected` notices.

**The `gc_now()` is what puts a collection inside this program's run**, and it
is there for a three-line program rather than for production. The gate that runs
this page honours the request the way the runtime does and asks for nothing
itself, so `collected=2` is the request's doing. A collection runs where the
guest can be re-entered, and what normally puts one there is the allocator
pacing growth against garbage. Take the line out and the example still answers
42, and the run ends with both blocks still live, because nothing had asked for
a scan. `atoll run` passes it all the same: a command that checks a run for
leaks asks for one last collection when the run ends holding blocks, and
reports only what survives it. See
[Cycles](https://docs.atoll.workreef.dev/language/memory/cycles/#when-a-collection-runs).

### Being on a cycle path is a size class

The allocator is class-segregated and a segment belongs to one class, so blocks
a collection can reach get a parallel ladder of classes. Whether a block is on
one is then a comparison against the class count, read **from the pointer tag
with no memory access**.

A cyclic block's header grows at the *front* — ahead of the plain one, carrying
the candidate link, the scan link, the shadow count and the walker id. The last
two words are unchanged, so the count stays at its constant offset; a header
that grew at the back would move a number that appears in emitted code.

**Reachable and capable are different sets.** A `List[^Node]` buffer inside a
cyclic `Node` cannot name itself and can never be a candidate, but a cycle runs
through it. So every block a scan can reach carries a shadow of its own. Walking
*through* such a block instead — attributing its edges to whoever reached it —
is unsound the moment two nodes share one list: its elements get subtracted once
per path, a live block's shadow reaches zero, and it is freed.

The two links are not one link used twice. The **candidate buffer** survives
between collections and holds what a decrement suspected; the **scan list**
holds what one collection touched, which is every block those candidates own,
transitively. Most of the garbage a collection finds is on the second list and
was never on the first.

### The static filter gates buffer entry, and nothing else

Which types can take part in a cycle is decided statically over **pointer edges
only** — `^T`, `^T?`, and pointers reached through a collection's type
arguments. By-value containment is not an edge, and the reason is one owner: a
value moved into itself is a value used after it moved, so a block reached only
by value cannot come to name itself.

An owning pointer field has no such gate — `a.next = a` is a plain field store —
which is why the edge relation is exactly the owning-pointer one. Every case the
analysis cannot decide answers *yes*.

The two ways it can be wrong are not symmetric:

| The filter says | Costs |
|---|---|
| *not cyclic* when it is | an unbounded leak, and a `Drop` body that never runs |
| *cyclic* when it is not | a wasted buffer slot |

So it is conservative by construction. The bit gates **buffer entry only** —
using it to skip a child during edge enumeration would leave the subtraction
incomplete, and an incomplete subtraction frees live memory.

## Destruction paths

| Event | Behavior |
|---|---|
| ordinary release | decrement, typed walk at zero, definite free |
| cycle collection | shadow subtraction, typed cleanup, deferred free |
| poisoned eviction | release the whole arena backing allocation |

The third is fault containment and must not be described as equivalent to
running every destructor. If execution was interrupted while ownership state may
be inconsistent, the runtime evicts the arena rather than promising a clean
unwind.

A collection's free is **deferred** because the candidate link lives in the
block's own bytes: handing them to a free list would let the next allocation
overwrite an address a later sweep still follows. The block is marked dead and
stays carved until the sweep finishes it, which keeps its segment from being
retired underneath the buffer.

## What a finished run can still hold

A run can end holding something in four ways, and they are not substitutes for
one another:

| Axis | Counts | Means |
| --- | --- | --- |
| `live_blocks` | blocks the heap still holds | a value was not released — read it as `stranded_blocks()`, since a collected cycle's blocks are *deferred*, not leaked |
| `held_host_calls` | host futures the reactor still owns | a `@host` call was never answered, and its slot is unusable |
| `parked_frames` | tasks suspended and never resumed | a hang the run got past by answering anyway |
| `timers_armed` | deadlines armed and never fired | a `select` arm that outlived its own wait |

`atoll run` gates on the first two, and on `double_frees` — which is not a leak
axis but the other half of the first, because a run can balance to zero live
blocks *because* it released one twice. The last two surface only in the
quiescent-fault report, which fires on a **wedged** run rather than a finished
one, so a program that answers correctly while stranding a parked frame passes
the CLI's gate today.

Reading `live_blocks` raw is the mistake this list exists to stop: a block on
the candidate buffer whose count already reached zero is carved, counted and
garbage, and it goes back at the next collection rather than at the release
that proved it dead. Nine gates have restated that rule and four of them read
the raw number, reporting every program that abandoned a cycle as a leak.

## Invariants

- a live block has a positive or immortal count;
- only the owning reactor mutates counts or allocator state;
- every shadow is zero outside a collection — a surviving one is a subtraction
  applied twice, and twice reaches zero on a live subgraph;
- three counts of the candidate buffer agree: the list length, the number of
  blocks whose status says buffered, and the maintained counter;
- a second release of a deferred block is the same fault as a second release of
  a freed one.

Source: https://docs.atoll.workreef.dev/runtime/memory/ownership/index.mdx
