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, acquire, and clear 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<ValueSchemaName>.- 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
Fields
SCHEMA_NAME: staticTextschema-name field,Owner<ValueSchemaName>.OWNER: static empty-Tuplemarker.destructor: static destructor routine used when the owned object is destroyed.elementType: staticRefto the owned element schema.memory: storedRefto the owned object; empty owners storeNIL.
Methods
valid?
(-- valid) Tests whether the owner currently holds an object.acquire
(element --) Replaces the current owned object with element, a mutable Ref to storage of the element schema allocated with mplMalloc using the element's objectSize. Ownership of the storage transfers to the owner; acquire allocates nothing and copies nothing.Postconditions
clear
(--) Destroys the owned object and leaves the owner empty.Postconditions
equal
(other -- cond) Reports whether other, an owner of the same element schema, holds the same object; two empty owners are equal.lower
(-- owner) Returns an owner lowered to the base element type.Preconditions
valid?isTRUE.- The owned element's schema has a field named
base. - The reference obtained from
basepoints to the start of the original allocation; the base schema'sobjectSizeis the size of that allocation, and destroyingbasedestroys the whole element.
Postconditions
- The original owner becomes empty.
- The result is a new
Ownerof thebasefield and takes over the same storage; it does not preserve the original destructor.
Preconditions
initDerived
(object --) Allocates storage from object and stores the base view in the owner. The base schema views the stored element at offset 0.Preconditions
owner (element -- owner)
Constructs Owner from one element and transfers ownership of newly allocated storage to the result.
Example: owner and release types
During compilation, valid? is a Cond, get is an Int32 Cref, and release returns an Int32 Ref, after which valid? is FALSE.
"Owner" use
"control" use
"memory" use
{} 0 {} [
holder: 5 owner;
holder.valid? printStack _:;
holder.get printStack _:;
released: @holder.release;
@released printStack _:;
4nx released storageAddress mplFree
holder.valid? printStack _:;
0
] "main" exportFunction
Expected Output During Compilation
Cond
Int32 Cref
Int32 Ref
FALSE
Example: get and release
get reads the owned 5, and release hands the same object to the caller, which frees it with mplFree.
"Owner" use
"String" use
"control" use
"memory" use
{} Int32 {} [
holder: 5 owner;
("value=" @holder.get new LF) printList
released: @holder.release;
("released=" @released new LF) printList
4nx released storageAddress mplFree
0
] "main" exportFunction
Expected Output
value=5
released=5
Example: clear resets validity
clear destroys the owned object, so valid? changes from TRUE to FALSE.
"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. The base schema views the stored element at offset 0.
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.
Known issue: with memory's getHeapUsedSize imported after Owner, a call selects it instead; it returns 0nx for an owner.
- If the owner is empty, the result is
0nx. - Otherwise the result is the
objectSizeof the object thatgetreturns plus that object's own heap usage. Known issue: if that object is not an Owner, its heap usage counts as0nxunless measuring it uses agetHeapUsedSizemarked withoverload; the Standard Library has none.