Ownership & Allocation

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 once

Closing 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 exampleLevelRequirement
Passing one owned file must not produce two independent owners.MUST NOTIntroduce implicit Sun copies across a compiled boundary.
After moving a file to the library, only the library closes it.MUSTLeave exactly one owner responsible for cleanup after a successful transfer.
A second argument fails to initialize after the first was moved.MUSTDefine the transfer point and cleanup responsibility if argument evaluation, construction, or the call fails.
Constructing field B fails after field A succeeded.MUSTDestroy only the parts that were initialized.
A library returns a reference tied to an argument lifetime.MUSTPreserve 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 NOTGive the producer and consumer conflicting optimizer assumptions about the same borrowed storage.
Memory from allocator A is incorrectly returned to allocator B.MUSTUse a compatible deallocation operation with the required alignment and allocator state.
A value still needs its destructor after its creating component shuts down.MUSTKeep 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.