Ownership & Allocation
Part of the Sun ABI draft. These are proposed requirements, not guarantees of current releases. Open decisions must be resolved before stabilization.
Ownership, borrowing, and allocation
Suppose app moves an owned file into a library operation:
before transfer: app is responsible for closing the file
successful move: library is responsible; app no longer owns it
destruction: the file is closed onceClosing it on both sides is a bug; closing it on neither side leaks the resource. The calling contract must also say who cleans up if the call fails during transfer. This example describes ownership, not an implicit copy operation.
Moving bits does not grant permission to copy ownership. Destroying a value and freeing its storage are separate operations.
| Minimal example | Level | Requirement |
|---|---|---|
| Passing one owned file must not produce two independent owners. | MUST NOT | Introduce implicit Sun copies across a compiled boundary. |
| After moving a file to the library, only the library closes it. | MUST | Leave exactly one owner responsible for cleanup after a successful transfer. |
| A second argument fails to initialize after the first was moved. | MUST | Define the transfer point and cleanup responsibility if argument evaluation, construction, or the call fails. |
| Constructing field B fails after field A succeeded. | MUST | Destroy only the parts that were initialized. |
| A library returns a reference tied to an argument lifetime. | MUST | Preserve mutability, lifetime relationships, and aliasing constraints needed for safety checks. A machine pointer alone is insufficient. |
| One side assumes a reference is exclusive while the other aliases it. | MUST NOT | Give the producer and consumer conflicting optimizer assumptions about the same borrowed storage. |
| Memory from allocator A is incorrectly returned to allocator B. | MUST | Use a compatible deallocation operation with the required alignment and allocator state. |
| A value still needs its destructor after its creating component shuts down. | MUST | Keep destruction and deallocation services available for the allocation's lifetime. |
For example, Unique<T, A> retains its allocator binding, destroys T, and
returns storage through that allocator. An owned interface created by
std.mem.create_interface holds a pointer to a box containing the allocator
reference and the concrete value. Across a library boundary, the receiver
pointer serves dispatch and the cleanup function receives the original box
pointer. The owner retains the lifetimes of its allocator and the references
held by the concrete value.
Open decisions
Specify transfer and failure rules for each parameter mode, construction, return, and cleanup path. Define the system allocator contract and runtime linkage rules before promising ownership compatibility across compiler versions.