Owning wrapper over dynamically allocated storage with automatic destruction of the owned object while ownership remains in the wrapper.
Ownership boundary
getborrows access while ownership stays in the wrapper.cleardestroys the owned object and leaves the wrapper empty.releasereturns one Ref and leaves the wrapper empty. Automatic destruction no longer applies to that object after release.lowertransfers the same owned allocation to one base-typed owner and empties the original owner.valid?reports whether the wrapper currently owns one object.
OwnerWithDestructor (Value destructor -- ownerSchema)
Constructs an owner schema from an element schema and an explicit destructor callable.
Known issue: an OwnerWithDestructor fails to compile when its explicit destructor omits the allocation size: its DIE and acquire paths then call mplFree with the address only while mplFree expects the size and the address (Argument (target) is not on the stack reported from memory.mpl). A destructor with stack effect (element -- allocationSize) can compile when it returns the size of the original allocation. Owner and owner are unaffected.
Valueis the owned element schema,destructoris called with the allocated element before its storage is freed, andownerSchemais namedOwner<Value>.- The resulting schema exposes the same ownership operations as
Owner, including validity, acquisition, clearing, borrowing, lowering, initialization, and release. ownerconstructsOwner, which isOwnerWithDestructorwith a generated destructor (manuallyDestroyVariable, thenobjectSize, whose result is the sizemplFreeconsumes).ownerDerivedpasses an explicit base schema and destructor toOwnerWithDestructorand is subject to the known issue above.
objectSize (object -- size)
Returns the storage size for a value or the size supplied by its SIZE field.
- The default overload calls
storageSizeand returns oneNatx. - For an object exposing
SIZE, the second overload returns.SIZEinstead.
Owner
Methods
valid?
(-- valid) Tests whether the owner currently holds an object.Postconditions
clear
(--) Destroys the owned object and leaves the owner empty.Postconditions
lower
(-- owner) Returns an owner lowered to the base element type.Preconditions
Postconditions
- The original owner becomes empty.
- The result owner takes over the same storage.
Preconditions
Preconditions
Preconditions
Postconditions
Fields
owner (element -- owner)
Constructs Owner from one element and transfers ownership of newly allocated storage to the result.
Compile-time owner and release types
"Owner" use
"control" use
{} 0 {} [
holder: 5 owner;
holder.valid? printStack _:;
holder.get printStack _:;
@holder.release printStack _:;
holder.valid? printStack _:;
0
] "main" exportFunction
Expected Output During Compilation
Cond
Int32 Cref
Int32 Ref
FALSE
Runtime example
"Owner" use
"String" use
"control" use
{} Int32 {} [
holder: 5 owner;
("value=" @holder.get new LF) printList
released: @holder.release;
("released=" @released new LF) printList
0
] "main" exportFunction
Expected Output
value=5
released=5
Runtime example: clear resets validity
"Owner" use
"String" use
"control" use
{} Int32 {} [
holder: 5 owner;
("valid0=" holder.valid? LF) printList
@holder.clear
("valid1=" holder.valid? LF) printList
0
] "main" exportFunction
Expected Output
valid0=TRUE
valid1=FALSE
ownerDerived (element base destructor -- owner)
Constructs an owner with explicit base type and destructor handling.
Known issue: the result is an OwnerWithDestructor; it fails when the supplied destructor omits the required allocation size.
getHeapUsedSize (owner -- size)
Returns total heap usage of the owned object as Natx.
- If the owner is empty, the result is
0nx. - If the owned element participates in
getHeapUsedSize, that result is included recursively.