Go to the first, previous, next, last section, table of contents.


4.8 Allocators

  1. The evaluation of an allocator creates an object and yields an access value that designates the object.

    Syntax

  2. allocator ::=
       new subtype_indication | new qualified_expression
    

    Name Resolution Rules

  3. The expected type for an allocator shall be a single access-to-object type whose designated type covers the type determined by the subtype_mark of the subtype_indication or qualified_expression.

    Legality Rules

  4. An initialized allocator is an allocator with a qualified_expression. An uninitialized allocator is one with a subtype_indication. In the subtype_indication of an uninitialized allocator, a constraint is permitted only if the subtype_mark denotes an unconstrained composite subtype; if there is no constraint, then the subtype_mark shall denote a definite subtype.
  5. If the type of the allocator is an access-to-constant type, the allocator shall be an initialized allocator. If the designated type is limited, the allocator shall be an uninitialized allocator.

    Static Semantics

  6. If the designated type of the type of the allocator is elementary, then the subtype of the created object is the designated subtype. If the designated type is composite, then the created object is always constrained; if the designated subtype is constrained, then it provides the constraint of the created object; otherwise, the object is constrained by its initial value (even if the designated subtype is unconstrained with defaults).

    Dynamic Semantics

  7. For the evaluation of an allocator, the elaboration of the subtype_indication or the evaluation of the qualified_expression is performed first. For the evaluation of an initialized allocator, an object of the designated type is created and the value of the qualified_expression is converted to the designated subtype and assigned to the object.
  8. For the evaluation of an uninitialized allocator:
    1. If the designated type is elementary, an object of the designated subtype is created and any implicit initial value is assigned;
    2. If the designated type is composite, an object of the designated type is created with tag, if any, determined by the subtype_mark of the subtype_indication; any per-object constraints on subcomponents are elaborated and any implicit initial values for the subcomponents of the object are obtained as determined by the subtype_indication and assigned to the corresponding subcomponents. A check is made that the value of the object belongs to the designated subtype. Constraint_Error is raised if this check fails. This check and the initialization of the object are performed in an arbitrary order.

  1. If the created object contains any tasks, they are activated, See section 9.2 Task Execution - Task Activation. Finally, an access value that designates the created object is returned.

    NOTES

  2. (24) Allocators cannot create objects of an abstract type. See section 3.9.3 Abstract Types and Subprograms.
  3. (25) If any part of the created object is controlled, the initialization includes calls on corresponding Initialize or Adjust procedures. See section 7.6 User-Defined Assignment and Finalization.
  4. (26) As explained in See section 13.11 Storage Management, the storage for an object allocated by an allocator comes from a storage pool (possibly user defined). The exception Storage_Error is raised by an allocator if there is not enough storage. Instances of Unchecked_Deallocation may be used to explicitly reclaim storage.
  5. (27) Implementations are permitted, but not required, to provide garbage collection, See section 13.11.3 Pragma Controlled.

    Examples

  6. Examples of allocators:
  7. new Cell'(0, null, null)
    -- initialized explicitly, See section 3.10.1 Incomplete Type Declarations
    
    new Cell'(Value => 0, Succ => null, Pred => null)
    -- initialized explicitly
    
    new Cell
    -- not initialized
    
  8. new Matrix(1 .. 10, 1 .. 20)
    -- the bounds only are given
    
    new Matrix'(1 .. 10 => (1 .. 20 => 0.0))
    -- initialized explicitly
    
  9. new Buffer(100)
    -- the discriminant only is given
    
    new Buffer'(Size => 80, Pos => 0, Value => (1 .. 80 => 'A'))
    -- initialized explicitly
    
  10. Expr_Ptr'(new Literal)
    -- allocator for access-to-class-wide type, See section 3.9.1 Type Extensions
    
    Expr_Ptr'(new Literal'(Expression with 3.5))
    -- initialized explicitly
    


Go to the first, previous, next, last section, table of contents.