# Arenas

> Every output whose length only the call knows, the embeddings, the states and their offsets, lands in an arena_t you pass, and the verb answers views over it without copying.

`#include <uform/types.hpp>` 4.0.0-rc.1 · c58749fe

Every output whose length only the call knows, the embeddings, the states and their offsets, lands in an `arena_t` you pass, and the verb answers views over it without copying. `arena_t::make({.executor = executor.handle()})` places it where that executor computes, decided once, and `arena_t::make()` makes host pages for the calling thread. Each call rewinds its arena, so the previous call’s views die and a steady loop allocates once. To keep a result past the next call, pass that call another arena; an arena serves one call at a time, so each deferred call needs its own. An arena from a VRAM executor holds device memory, which `describe()` reports as `uform_residency_vram_k` and host code must not read, while `view.data` still hands the rows to another library, such as a USearch VRAM index. Views are plain spans that nothing invalidates, so check `describe().host_access` before host code reads one, and copy what you keep instead of holding a span across calls.

```cpp
auto [arena, arena_status] = uf::arena_t::make({.executor = executor.handle()});
for (uf::tape_view<char> const batch : batches) {
    auto [encoded, status] = model.on(executor).text_encode(batch, uf::encode_outputs_t::embeddings_k, uform_scalar_f32_k, arena);
    consume(encoded.embeddings); // rewinds next iteration
}
```

Destroying an arena waits for the calls writing into it, and a call naming an arena that serves another call finishes with `status_code_t::contended_k`. An arena never outlives the executor that made it, so declare the executor before the arena and scope exit destroys the arena first, and an arena from `arena_t::make()` lives until it is destroyed.

## API

### Types

#### `owner`

Owns one handle of `handle_type_`, which `free_` releases, moving it and never copying it.

[types.hpp:242 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L242)

```cpp
template <typename handle_type_,
          void(*)(handle_type_) free_>
class owner
```

#### `owning`

A handle owned, moved and never copied, whose verbs block on the calling thread.

[types.hpp:285 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L285)

```cpp
template <typename handle_type_,
          void(*)(handle_type_) free_>
struct owning
```

#### Fields

| `completion` | `uform_completion_t` | |
| - | - | - |

#### `caller_owned`

A handle the caller owns and the executor its verbs run on, completing as `completion_` says.

[types.hpp:296 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L296)

```cpp
template <typename handle_type_,
          uform_completion_t completion_>
struct caller_owned
```

#### Fields

| `completion` | `uform_completion_t` | |
| - | - | - |

#### Class `arena_t`

Owns one arena; every call writing outputs into it rewinds it first, ending the previous call's views.3

[types.hpp:578 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L578)

```cpp
class arena_t
```

It must not outlive the executor that made it, so declare that executor first; one made with no executor lives until it is destroyed. It must outlive every export opened on its outputs: a free those refuse leaves the arena allocated with no owner left to free it.

##### `handle`

The C handle, for [`uform_arena_export_open`](https://www.unum.cloud/docs/uform/c/arenas.md#uform_arena_export_open) among others; close every export opened through it before this owner dies.

[types.hpp:584 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L584)

```cpp
uform_arena_t handle() const noexcept;
```

##### `describe`

Reports where the arena's outputs live, which decides whether host code may read them.

[types.hpp:593 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L593)

```cpp
expected<uform_arena_description_t> describe() const noexcept;
```

##### `make`

Creates an empty arena where the executor `arguments` names computes.

[types.hpp:588 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L588)

```cpp
expected<arena_t> make(uform_arena_init_t arguments = {}) noexcept;
```

### Functions

#### `free_arena`

Frees `arena` as its owner dies, which an open export of its outputs refuses, leaving it to the export's holder, who closes every export before the owner dies.

[types.hpp:570 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L570)

```cpp
void free_arena(uform_arena_t arena) noexcept;
```
