rfc: 0008 revision 2 per owner review
- own.take added (T -> own<T> -> T); no take for own_array - no region-specific limit: one quota mechanism, cap.narrow.budget - no hidden destruction: own/own_array/region contents must not contain linear values until linear_aggregates - live allocation vs live reference: borrow.end never returns quota; reset/destroy return all bytes; E2104 means a live reference would be invalidated - narrowing reserves nothing; children compete for the parent budget
This commit is contained in:
1 parent
75dfaac1a6
commit
d4d641ad84
1 file changed
+34
-8
@@ -1,6 +1,6 @@
|
||||
# RFC 0008: Heap Allocation, Regions and Allocation Budgets
|
||||
|
||||
Status: Proposed
|
||||
Status: Proposed (revision 2: owner review applied)
|
||||
Scope: AIR format 0, features `heap` and `regions`; `mem.alloc` narrowing
|
||||
Language version: 0.1
|
||||
Supersedes: —
|
||||
@@ -26,6 +26,7 @@ cap<mem.alloc> authority + memory quota (feature heap, registry of ca
|
||||
3. **No dangling access.** Heap and region memory is reached only through second-class borrows, which cannot outlive the owner (heap) or the region generation (regions). There are no runtime generation checks.
|
||||
4. **Bounded memory.** Every allocation is charged against a budget held by the authority object of the capability. The budget limits the bytes that are **live at the same time**.
|
||||
5. **Shared quota.** Copies of a capability refer to one authority object and share one budget; narrowing creates a child budget whose charges also count against every ancestor.
|
||||
6. **No hidden destruction of linear values.** `own.free`, `own.take`, `region.reset` and `region.destroy` can never make a linear value disappear: until `linear_aggregates` is specified, values stored in `own<T>`, `own_array<T>` and region allocations MUST NOT transitively contain linear values (resources, owners, regions).
|
||||
|
||||
## 1. Allocation authority (feature `heap`)
|
||||
|
||||
@@ -38,7 +39,9 @@ alloc 10 MiB → used = 10 MiB
|
||||
free 10 MiB → used = 0
|
||||
```
|
||||
|
||||
The budget is a limit on simultaneously live memory, not a cumulative execution quota. Implementations MAY additionally report `total_allocated_bytes`, `peak_live_bytes` and `allocation_count` in audit reports; these never affect semantics.
|
||||
The budget is a limit on simultaneously live memory, not a cumulative execution quota.
|
||||
|
||||
*Live allocation* and *live reference* are different things. A region allocation stays charged until `region.reset` or `region.destroy`, even after every borrow of it has ended: `borrow.end` never returns quota, because the region still holds the memory. Implementations MAY additionally report `total_allocated_bytes`, `peak_live_bytes` and `allocation_count` in audit reports; these never affect semantics.
|
||||
|
||||
Accounted bytes are those of spec 08 §7.1: `own<T>` charges `accounted_size(T)`, `own_array<T>` charges `n × accounted_size(T)` with checked arithmetic (overflow fails the allocation with `budget_exceeded`). Allocator overhead is not charged.
|
||||
|
||||
@@ -50,6 +53,13 @@ The scope of a `mem.alloc` grant is the byte limit (`--grant heap=mem.alloc:6710
|
||||
|
||||
`cap.narrow.budget %c, %n, ok -> @ok, err -> @err` (`%n: usize`) creates a child authority object with `limit = n` and parent `%c`. The `err` edge receives `not_subset` if `n` exceeds the parent's limit. Two copies of either capability share their object's budget.
|
||||
|
||||
Narrowing reserves nothing. Children of one parent may have limits that add up to more than the parent's (a 64 MiB parent with two 48 MiB children); they then compete for the parent's real 64 MiB, because every charge to a child is also a charge to the parent:
|
||||
|
||||
```text
|
||||
allocate 10 MiB through child → child.used += 10, parent.used += 10
|
||||
free it → child.used -= 10, parent.used -= 10
|
||||
```
|
||||
|
||||
### 1.4 Errors
|
||||
|
||||
`alloc_error` is a built-in enum with the variants `budget_exceeded` (the budget of the capability or an ancestor would be exceeded) and `out_of_memory` (the host could not provide memory; never produced by the `deterministic` profile). Its variant set is frozen on publication.
|
||||
@@ -73,10 +83,13 @@ Both are linear and not copyable; as members of aggregates they need `linear_agg
|
||||
|---|---|---|---|
|
||||
| `own.new %v, %c, ok -> @ok, err -> @err` | `T`, `cap<mem.alloc>` → `@ok(%o: own<T>)`, `@err(%e: alloc_error)` | `mem.alloc` | object initialized with `%v` |
|
||||
| `own.array %n, %v, %c, ok -> @ok, err -> @err` | `usize`, `T`, `cap<mem.alloc>` → `@ok(%a: own_array<T>)` | `mem.alloc` | every element initialized with `%v` |
|
||||
| `own.take %o` | `own<T>` (consumed) → `T` | — | moves the value out, frees the allocation, returns the bytes |
|
||||
| `own.free %o` | `own<T>` or `own_array<T>` (consumed) | — | returns the bytes to the budget |
|
||||
| `borrow.ref` / `borrow.mut` | `own<T>` → `ref<T>` / `mut<T>`; `own_array<T>` → `slice<T>` / `slice_mut<T>` | — | the owner is the borrow root |
|
||||
|
||||
Heap memory is always initialized, so no initialization tracking is needed. `own.free` while a borrow of the owner is live is `E2101`. An owner is passed to a function by value (moving it) or by borrow (lending it), exactly like a resource.
|
||||
Heap memory is always initialized, so no initialization tracking is needed. `own.free` or `own.take` while a borrow of the owner is live is `E2101`. `own.take` is the inverse of `own.new` (`T → own<T> → T`); an `own_array<T>` has no `take`, since a runtime-sized array is not an SSA value.
|
||||
|
||||
`T` in `own<T>` and `own_array<T>` must be a value type; a linear `T` (for example `own<file<read>>` or `own<own<u64>>`) needs `linear_aggregates` (`E0006` reason `feature_not_declared`), which will define extraction and destruction of nested linear values. An owner is passed to a function by value (moving it) or by borrow (lending it), exactly like a resource.
|
||||
|
||||
## 3. Regions (feature `regions`)
|
||||
|
||||
@@ -91,13 +104,24 @@ Depends on `capabilities`, `effects`, `heap`, `ownership`, `resources`.
|
||||
| `region.create %c, ok -> @ok, err -> @err` | `cap<mem.alloc>` → `@ok(%r: region)`, `@err(%e: alloc_error)` | `mem.alloc` | allocations of the region are charged to `%c` |
|
||||
| `region.new %rb, %v, ok -> @ok, err -> @err` | `ref<region>`, `T` → `@ok(%p: mut<T>)` | `mem.alloc` | fresh object initialized with `%v` |
|
||||
| `region.array %rb, %n, %v, ok -> @ok, err -> @err` | `ref<region>`, `usize`, `T` → `@ok(%s: slice_mut<T>)` | `mem.alloc` | fresh array |
|
||||
| `region.reset %r` | `region` (not consumed) | — | frees every allocation, returns the bytes, starts a new generation |
|
||||
| `region.destroy %r` | `region` (consumed) | — | frees every allocation and the region |
|
||||
| `region.reset %r` | `region` (not consumed) | — | frees every allocation of the generation, returns all their bytes, starts a new generation |
|
||||
| `region.destroy %r` | `region` (consumed) | — | frees every allocation and the region, returns all their bytes |
|
||||
|
||||
Values stored by `region.new`/`region.array` must be value types (no linear values), for the same reason as for `own<T>`.
|
||||
|
||||
Budget accounting of a region, normatively:
|
||||
|
||||
```text
|
||||
region.new / region.array → used += allocation charge
|
||||
borrow.end of an allocation → used unchanged
|
||||
region.reset → used -= every allocation of the generation
|
||||
region.destroy → used -= every allocation of the region
|
||||
```
|
||||
|
||||
### 3.2 Verifier rules (static generations, Q-010)
|
||||
|
||||
- A region allocation `%p` is an owned borrow whose parent is the shared region borrow `%rb` and whose path is a fresh element unique to that allocation. Allocations are therefore disjoint from each other, and the exclusive borrow `mut<T>` of a fresh object does not conflict with the shared parent (this is the one place where an exclusive child of a shared borrow is permitted, because the storage is new).
|
||||
- `region.reset %r` and `region.destroy %r` are exclusive accesses to the region value. If any borrow derived from `%r` is live — in particular any allocation of the current generation — the instruction is `E2104` (`memory.region_generation`): those references would belong to an invalidated generation.
|
||||
- `region.reset %r` and `region.destroy %r` are exclusive accesses to the region value. If a **live reference** into the region exists — any live borrow derived from `%r`, in particular a live borrow of an allocation — the instruction is `E2104` (`memory.region_generation`): reset or destroy would invalidate a live reference. Allocations whose borrows have all ended are not an obstacle; destroying them is what `reset` and `destroy` are for.
|
||||
- Ending `%rb` while one of its allocations is live is `E2102`, so no allocation outlives the borrow it came from.
|
||||
- A function may return a region allocation with `from %rb` when `%rb` is a `ref<region>` parameter (RFC 0005 §5.3 applies unchanged: the root is the parameter). Frame-derived borrows still never escape (`E2108`).
|
||||
|
||||
@@ -175,8 +199,10 @@ On acceptance: spec 03 §7–§8 (heap and regions realized), spec 05 §10 (budg
|
||||
- **Region allocation through `mut<region>`**: an exclusive borrow per allocation would freeze the region while any allocation is live, so only one allocation could exist at a time. A shared borrow with fresh, disjoint allocation paths keeps allocations independent and still makes `reset`/`destroy` conflict with every live allocation.
|
||||
- **Uninitialized heap allocation** with initialization tracking per object: more verifier state for little gain at this stage; `own.new`/`own.array` initialize from a value.
|
||||
- **Implicit default budget in the deterministic profile**: a hidden policy; an explicit limit in the grant is required instead.
|
||||
- **Region-specific limits** (`region.create … limit`): would duplicate the capability model. Rejected.
|
||||
- **Reserving quota at narrowing time**: makes hierarchical budgets heavy; children compete for the parent's budget instead.
|
||||
- **Returning region quota on `borrow.end`**: would let a program keep re-allocating in a region that still holds all earlier allocations, bypassing the budget. Rejected.
|
||||
|
||||
## Unresolved questions
|
||||
|
||||
- Whether `own<T>` should support moving the value out (`own.take`, freeing the cell) in format 0, or only borrowing and freeing.
|
||||
- Whether a region should accept its own byte limit at creation in addition to the capability's budget (proposed: no — narrow the capability with `budget` first).
|
||||
None. Decided: `own.take` is part of format 0 (no `take` for `own_array`); regions have no limit of their own — the one quota mechanism is `cap.narrow.budget`.
|
||||
Reference in new issue
Block a user