---
title: "Arenas"
description: "Unit-instance arena growth, relocation, and generations."
---

> 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.

# Arenas

> **Written against the retired host**
>
> This page was written against `atoll-runtime-wasm`, and that host is **gone** —
> it is on the workspace `exclude` list and no longer builds. What runs is
> `crates/atoll-runtime`: one wasmtime `Store` per reactor, with host bodies as
> ordinary `async fn`s reached through the generated imports in `atoll-hosts`.
>
> The **surface** below is current: it is what `stdlib/` declares and what a
> program can name. Statements about *how the host answers* have not all been
> re-measured against the shipping reactor, so read those as the contract a host
> owes rather than as a description of what it does today.

Every live [Unit instance](https://docs.atoll.workreef.dev/runtime/units/) owns one persistent arena in its
link group's WebAssembly linear memory.

**Read the growth and relocation protocol below against that caution.** The
allocator a program's values come from is `segalloc`, for which an arena is a
*set* of 16 KiB segments rather than one range — so growth takes another
segment, moves nothing, and invalidates no live pointer. See
[Memory](https://docs.atoll.workreef.dev/runtime/memory/). The arena holds that instance's values,
task tree, continuation frames, and scheduler nodes. It survives ordinary
invocation completion and disappears when the instance is evicted.

An arena is therefore an isolation and reclamation boundary:

- values from different instances do not share arena offsets;
- an instance can be migrated by copying its arena at a safepoint;
- eviction releases all of the instance's in-arena storage together;
- corruption or forced termination can poison one instance without requiring
  reconstruction of individual allocations.

## Initial size

The default initial runtime arena body capacity is 256 KiB. It can be set
explicitly, or selected adaptively. When an instance is evicted, the Store
remembers that Unit's observed peak. A later instance starts with at least the
configured floor and may reserve roughly 125 percent of the remembered peak.

These values are tuning policy, not portable language guarantees. Code must
handle allocation failure regardless of the initial capacity.

## Growth

Growth can occur proactively or in an allocation slow path.

The trampoline periodically checks pressure. At its current cadence of every
64 ticks, usage above seven eighths of capacity first offers cycle collection a
chance to reclaim storage and then attempts to double the body capacity.

An allocation failure requests enough space to satisfy the pending block plus
headroom. The current target is approximately:

```text
max(2 × current capacity, current capacity + requested bytes + 4 KiB)
```

All arithmetic is checked against the `u32` arena representation and configured
limits. After a successful relocation the runtime:

1. allocates a larger raw range;
2. copies the complete builder header and body;
3. increments `gen_id`;
4. publishes the new body base and capacity to the active link group;
5. frees or retires the old generation;
6. retries the interrupted allocation.

Because every stored relationship uses relative offsets, the copied object
graph does not need pointer rewriting.

## Generations

A generation identifies one physical placement of an arena. Relocation changes
the generation even though all arena-relative offsets retain their meanings.

Asynchronous host operations use two strategies:

- direct operations retain offsets and derive an absolute address from the
  current base on each poll;
- operations that must capture a raw address pin the exact arena generation
  before suspension.

If a pinned arena relocates, the runtime puts the old backing allocation on a
retired list instead of freeing it. Completion unpins the exact captured base,
after which unpinned retired generations can be reaped. A pin does not prevent
the active arena from moving; it prevents the pinned old storage from becoming
invalid prematurely.

Runtimes that lack the pin, unpin, and retired-generation ABI gate growth until
no I/O operation is active. The current ABI supports mid-I/O growth through
generation pins.

## Safepoints

Snapshot and migration require a coherent arena. The runtime reaches a
safepoint where generated code is not mutating the arena, then requires no
unresolved pins on the captured generation. Host resources are handled
separately because an arena copy cannot serialize an operating-system socket,
file descriptor, or database connection.

Restore copies the bytes into a new backing allocation, publishes a new base,
and advances the generation. Supported host resources are detached and
reattached through the migration protocol described in
[Deployment](https://docs.atoll.workreef.dev/runtime/deployment/).

## Failure rules

Allocation failure is recoverable only while the runtime can still trust the
arena and scheduler state. A forcefully terminated invocation can interrupt
generated ownership operations, so its Unit instance is poisoned. The runtime
then evicts the complete arena; it does not walk possibly inconsistent
reference counts or promise to run every user destructor.

Optional arena verification checks the physical block walk, free lists, size
classes, and counters. It is enabled by the `arena-verify` build feature or
`ATOLL_ARENA_VERIFY` diagnostics, and is intentionally disabled on normal hot
paths.

## Invariants

- `total_size` never exceeds body capacity.
- All nonzero live offsets resolve within the current body.
- Relocation preserves the complete header and body byte sequence.
- `gen_id` changes whenever the physical placement changes.
- A retired generation is not freed while pinned.
- Only the owning Unit instance mutates its arena on its Store reactor.
- Arena teardown is not a substitute for ordinary `Drop` semantics during
  successful execution.

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