Owning wrapper over dynamically allocated storage with automatic destruction of the owned object while ownership remains in the wrapper.


Ownership boundary


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.


objectSize (object -- size)

Returns the storage size for a value or the size supplied by its SIZE field.


Owner

Fields

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

  • The previous owned object is destroyed if one was present.
  • valid? becomes TRUE.

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.

get

(-- ref) Returns Ref to the owned object.

Preconditions

getHash

(-- hash) Returns the address of the owned object cast to Nat32 and multiplied by 0x9E3779B9n32 with wrapping arithmetic; an empty owner returns 0n32.

getUnsafe

(-- ref) Returns Ref to the owned object; an empty owner returns NIL.

lower

(-- owner) Returns an owner lowered to the base element type.

Preconditions

  • valid? is TRUE.
  • The owned element's schema has a field named base.
  • The reference obtained from base points to the start of the original allocation; the base schema's objectSize is the size of that allocation, and destroying base destroys the whole element.

Postconditions

  • The original owner becomes empty.
  • The result is a new Owner of the base field and takes over the same storage; it does not preserve the original destructor.

init

(object --) Allocates storage and initializes the owner from object.

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

release

(-- ref) Releases the owned object as Ref and leaves the owner empty.

Preconditions

Postconditions

  • valid? becomes FALSE.
  • Ownership of the storage passes to the caller: destroy the object and free its storage with mplFree using the size of the original allocation.

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.


See also