# VUMM Memory Model

VUMM is Vex's compiler-managed ownership and allocation machinery. Application
code expresses values, borrows, moves, and managed sharing; the compiler
selects cleanup and storage strategies from their lifetimes and escape paths.
The public ownership rules remain the starting point. A representation chosen
by an optimizer is not an additional permission to access a value.

## One owning surface, several implementation strategies

`Box<T>` is the public heap-value handle. Applications do not select a separate
`Rc` or `Arc` wrapper to request local or cross-task ownership bookkeeping.

```vex
fn main(): i32 {
    let answer = Box.new<i32>(42);
    $println(*answer.getRef());
    return 0;
}
```

Expected stdout is `42`, exit status 0. This simple program does not establish
which allocation or reference-count operations survive optimization.

The compiler distinguishes unique, locally shared, and concurrently shared
ownership. These are internal strategies, not user-defined variants of the
`Box<T>` type:

| Situation | User-visible ownership contract | Cost to assess |
| --- | --- | --- |
| One live owning handle | Moves transfer the handle; exclusive access still obeys borrow rules. | Allocation, cleanup, and whether an allocation can be eliminated or placed in an arena. |
| Multiple local handles | A box clone shares the payload. Shared handles keep it alive; ordinary mutable payload access is refused while sharing remains. | Retain/release operations and any compiler-proven elimination of them. |
| Ownership crossing a task boundary | The compiler arranges the managed ownership needed by supported captures. Referenced external data must also satisfy capture/lifetime rules. | Concurrent ownership bookkeeping, task transport, and application synchronization. |

Thread-safe ownership bookkeeping does not make arbitrary mutation of `T`
thread-safe. A `go` capture is not a blanket escape from lifetime checking and
is not evidence that all nested types or foreign resources can cross tasks.
Read the [concurrency guide](../concurrency/overview.md) for the capture model.

## Moves, copies, and clones

- **Move:** transfers an owning value. The previous binding cannot continue to
  use a value that has been moved away.
- **Copy:** duplicates a value admitted by the type's copy rules. Declaring a
  marker cannot make an owning payload safely bitwise-copyable.
- **Clone:** follows the specific type's API. `Box.clone()` shares its payload;
  other containers may clone their elements or construct independent storage.
- **Drop:** releases ownership and performs the type's cleanup. For a shared
  box, releasing one handle preserves the payload for the remaining owners.

Do not infer that every `clone()` in Vex is a reference-count increment or a
deep copy. The receiver type determines the operation. The [Box reference](box.md)
describes shared mutation checks, taking a payload, and empty handles.

## Borrowing is separate from ownership bookkeeping

A reference or `Span<T>` view does not add an owning handle. Its owner must
remain alive, and operations that invalidate its storage are forbidden while
the view is live. Allocating an object on the heap does not automatically
extend every reference derived from it.

For example, growing a vector may relocate its elements even when the vector
itself is stored in a box. Keeping the box alive alone is not a proof that an
earlier pointer or span into that vector is still valid. Prefer APIs whose
borrows express the relationship and keep raw-address operations inside a
small unsafe boundary.

## Cleanup and generic containers

The compiler specializes cleanup for concrete payload types. A container must
release its own allocation and arrange cleanup of the initialized elements it
owns; borrowed views must not destroy their source. Moving a field or taking a
box payload transfers that cleanup responsibility to the destination.

Normal scope cleanup, panic/unwind behavior, and process termination are
different execution paths. Use the resource API's explicit close or commit
operation when the program needs to observe a cleanup error. Automatic
destruction handles owned storage; it is not a transaction or a promise of
recovery after process termination.

## Allocation and accelerator boundaries

Allocation strategy can depend on escape analysis, payload properties,
optimization settings, runtime configuration, and the target. Arena, heap,
local-sharing, and concurrent-sharing mechanisms should be understood as
implementation choices. There is no general zero-allocation or zero-overhead
promise for an owning type.

Host/GPU buffer transport has additional alignment, lifetime, synchronization,
and backend requirements. Shared physical memory does not remove those
requirements or guarantee that every transfer avoids a copy. Use the
[SIMD/SIMT contract](../simd/simd-simt-contract.md) for that boundary rather
than deriving device behavior from `Box<T>`.

## Inspecting an ownership decision

The CLI exposes the boxing explanation on the analysis command:

```sh
vex lint example.vx --explain-boxing
```

Use this report to understand the compiler's analysis of that input. `lint`
does not execute the program or measure allocation cost. For a performance
claim, retain the compiler identity, target, build options, generated artifact,
and measurements of the actual workload.

## Next steps

- [Ownership](ownership.md)
- [Borrowing](borrowing.md)
- [Box method contracts](box.md)
- [Lifetimes](lifetimes.md)
- [Verification evidence](../verification-evidence.md)
